#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo servo1; // with servo I mean brushless motor
Servo servo2;
Servo servo3;
Servo servo4;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 2, 5 }; // ip in lan (that’s what you need to use in your browser. (“192.168.2.5”)
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
void setup() {
servo1.attach(9); // My motors are attached to outputs 6, 7, 8 and 9
servo2.attach(8);
servo3.attach(7);
servo4.attach(6);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led, OUTPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print(“server is at “);
Serial.println(Ethernet.localIP());
}
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == ‘\n’) {
Serial.println(readString); //print to serial monitor for debuging
client.println(“HTTP/1.1 200 OK”); //send new page //This is the HTML page i’m using for controlling the arduino
client.println(“Content-Type: text/html”);
client.println();
client.println(“<HTML>”);
client.println(“<HEAD>”);
client.println(“<TITLE></TITLE>”);
client.println(“</HEAD>”);
client.println(“<BODY>”);
client.println(“<H1></H1>”);
client.println(“<hr />”);
client.println(“<br />”);
client.println(“<H2>Arduino with Ethernet Shield</H2>”);
client.println(“<br />”);
client.println(“<a href=\”/?1500\”\”>1500</a><br />”);
client.println(“<a href=\”/?1200\”\”>1200</a><br />”);
client.println(“<a href=\”/?1000\”\”>1000</a><br /><br />”);
client.println(“<a href=\”/?vooruit\”\”>kwart vooruit</a><br />”);
client.println(“<a href=\”/?links\”\”>Links</a>”);
client.println(“<a href=\”/?Rechts\”\”>Rechts</a>”);
client.println(“<br />”);
client.println(“</BODY>”);
client.println(“</HTML>”);
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf(“?1500”) >0){
{ servo4.write(1500); // tell servo to go to position
delay(15); }} // waits 15ms for the servo to reach the position
if (readString.indexOf(“?1200”) >0){
{ servo4.write(1200); delay(15); }}
if (readString.indexOf(“?1000”) >0){
{servo4.write(1000); delay(15); }}
if (readString.indexOf(“?1500”) >0){
{ servo3.write(1500); // tell servo to go to position
delay(15); }} // waits 15ms for the servo to reach the position
if (readString.indexOf(“?1200”) >0){
{ servo3.write(1200); delay(15); }}
if (readString.indexOf(“?1000”) >0){
{servo3.write(1000); delay(15); }}
if (readString.indexOf(“?1500”) >0){
{ servo2.write(1500); // tell servo to go to position
delay(15); }} // waits 15ms for the servo to reach the position
if (readString.indexOf(“?1200”) >0){
{ servo2.write(1200); delay(15); }}
if (readString.indexOf(“?1000”) >0){
{servo2.write(1000); delay(15); }}
if (readString.indexOf(“?1500”) >0){
{ servo1.write(1500); // tell servo to go to position
delay(15); }} // waits 15ms for the servo to reach the position
if (readString.indexOf(“?1200”) >0){
{ servo1.write(1200); delay(15); }}
if (readString.indexOf(“?1000”) >0){
{servo1.write(1000); delay(15); }}
//besturen, vooruit, links en rechts
//vooruit
if (readString.indexOf(“?vooruit”) >0){ //Vooruit=forwards, I “connect the a href ‘vooruit’ to 2 actions, making motor 1 and 2 spin at 1200ms, 20% of their power
{ servo1.write(1200); delay(15); }}
if (readString.indexOf(“?vooruit”) >0){
{servo2.write(1200); delay(15); }}
//Links
if (readString.indexOf(“?links”) >0){ //Links=Left, making motor 1 spin at 2000ms/100% and motor 2 1100ms/10%
{ servo1.write(2000); delay(15); }}
if (readString.indexOf(“?links”) >0){
{servo2.write(1100); delay(15); }}
//Rechts
if (readString.indexOf(“?Rechts”) >0){ //Rechts=Right, same as left but vica versa
{ servo1.write(1100); delay(15); }}
if (readString.indexOf(“?Rechts”) >0){
{servo2.write(2000); delay(15); }}
//clearing string for next read
readString=””;
}
}
}
}
}
The HTML code in the Arduino make me see this page when I browse the IP-adres.
- 1500 makes all my motors spin at 50%
- 1200 spin at 20%
- 100 stops them all
- kwart vooruit, makes 2 of my motors sping at 20% (for forward thrust)
- Links makes motor 1 spin at 100% and motor 2 at 20% (for turning)
- Rechts the same but vica versa
Hope you can use this code, or if you have sugestions for me making it better, let me know!
greetz!