Arduino ethernet library

now browsing by tag

 
 

Arduino ethernet library

One feature that many of us want to use the Arduino is connect it to the internet so that we can think of the most ambitious projects, such as a simple project to flash an LED to trigger a button, or a more complex like and Arduino home security system, gives joy to all of us do it remotely!

There is a Arduino ethernet library (you can import it from Arduino IDE), through it and a ethernet shield (there are wi-fi, 3g, but I will speak of this) can communicate with the Arduino remotely, but there are many questions on how to do this. The approach I use is to use GET and POST to send parameters to the arduino, it receives and analyzes the command that must run according to the received parameter.

arduino-ethernet-shield-poe-large[1]

Arduino ethernet shield

First we have to call a function inside “Loop” that will always be waiting for customers on the server.

void loop(){
 listenForEthernetClients();  //check for web client
}


I have in the functionlistenForEthernetClients():

void listenForEthernetClients() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
 boolean currentLineIsBlank = true;
 String readString;
 char c;
 
    Serial.println("[listenForEthernetClients]!Got a client !");
    while (client.connected()) {
 if (client.available()) {
 while(readString.length()<=10){
 c = client.read();
 readString.concat(c);
 }
 if(readString.indexOf("A=")>0) OpenDoor(readString, client); //if GET for open door 
 if(readString.indexOf("B=")>0) OpenWindow(client); //if GET for open window
 if(readString.indexOf("C=")>0) LightsON(client); //if GET for lights on
 if(readString.indexOf("D=")>0) LightsOFF(client); //if GET for lights off
 else break;
 }
 // give the web browser time to receive the data
 delay(500);
 client.stop();    // close the connection:
 }
 client.stop();
  }
}

This function will receive parameters and execute a function depending on the parameter received. You can replace the function names, and put many as you want :)

If the parameter is “A” the Arduino will execute the function Opendoor , if your content is :

if (doorPin == LOW) {     
    // turn on:    
    digitalWrite(doorPin, HIGH);  
  } 

will switch the status of the pin configured to control the door lock!

And now let’s see how sending states Arduino pins to the internet, to use this information eg on a website. Imagine you want to send the information to a website that has the internet, then call the function sendStatus whenever you need it.

void sendStatus(EthernetClient client){
  String url= "<meta HTTP-EQUIV='REFRESH' content='0; url=http://example.com/testpage.php?";
  url += "?A="; url += digitalRead(doorPin);
  url += "&B="; url += digitalRead(windowPin);
  url += "&C="; url += digitalRead(lightsPins);
 
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connnection: close");
  client.println();
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.print(url);
  client.println ("'>");
  client.println("</html>");
 
}

In the url will the information sent by the Arduino, just do whatever you want with them :)

Imagination is the limit, there are many types of sensors that we can use, as you can see, with just a little planning can do things like add home security systems to our home.