Controlling arduino from internet, preferably facebook.

Hi there.
I'm a bit of a beginner in terms of arduino in conjunction with the internet, so am just wondering whether this is possible and if so how easy it would be to do.
Say I want to turn on a pin from the internet. Would the easiest way to do this be to go through an ethernet shield or through serial from my laptop?
Also, what's the best way in terms of the actual site used to control the board, would it be in any way possible to do this through facebook messenging?
Any help much appreciated.

You can use an arduino with Ethernet shield to act as a server and control arduino pins via a web page.

Ok thanks, is it possible to do it via facebook messaging as previously mentioned?

zoomkat:
You can use an arduino with Ethernet shield to act as a server and control arduino pins via a web page.

Hm, I've been dabbling in using the e'net shield as a server, but didn't yet think of using the webpage to send back to the server... so far only got the server to tell me the temp from a sensor.

@zk, where's a good place to look at how to code the page to submit stuff back? I guess raw html is easiest to start with?

EDIT.... I found ArduServer in the Playground, I'll start there.

Very basic web page control code

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

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
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

zoomkat:
Very basic web page control code

Excellent, thanks.

Hi zoomkat, when you have a chance I'd appreciate a bit of help with that above code please? But no rush at all, when you can.

I used it almost exactly as-is, just changed the ip address and using pin 8 for the led not 5. Also zapped the servo stuff which I didn't need. My code is below, although it's basically yours.

Here's the thing: when I hit the "off" button, the led blinks off and immediately comes back on. The serial output is as below, and it looks to me that it's immediately looping back round (as it would, with loop() ) but being detected as "on" by default. I have absolutely no clue what that code's actually doing :roll_eyes: . This serial output is the result of one push of the on, since I start with it off in setup(), and then a single click of the off, and you can see it goes right back on.

server pin 8 test 1.0
GET /?on HTTP/1.1

Led On
GET /favicon.ico HTTP/1.1

Led On
GET /?off HTTP/1.1

Led Off
GET /favicon.ico HTTP/1.1

Led On

I'd appreciate a) a fix for it staying off and b) a longer term thing, maybe an explanation of what that button-related code is doing. But only if / when you can...

That basically means I don't understand these lines:

client.println("<a href=\"/?on\">ON</a>"); 

// and

if(readString.indexOf("on") >0)

But I also can't see where the GET is kicked off.

Here's my code, just in case I accidentally dislodged something important while editing.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

//jim 7 dec with my details- ip address etc

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 10, 0, 0, 200}; // ip in lan
byte gateway[] = { 10, 0, 0, 2 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(8085); //server port

String readString; 
int ledPin = 8;  //<<<<<<<<<<<<<< new

//////////////////////

void setup(){

  pinMode(ledPin, OUTPUT); //pin selected to control
  digitalWrite(ledPin, LOW);  //<<<<<<<<<<<< new
  
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();


  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server pin 8 test 1.0"); // so I can keep track of what is loaded
}

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
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button ala Jim</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(ledPin, HIGH);    // set pin  high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(ledPin, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

The problem is probably that the arduino is only looking for the presence of the string "on" in the received GET request. The browser you are using is also sending a GET request for "favicon.ico" which also contains the string "on". You can just change the arduino code to "on8" and then look for that string (which will not be in "favicon.ico"). Try the modified lines below in your code and see if that helps.

client.println("<a href=\"/?on8\">ON</a>");

if(readString.indexOf("on8") >0)//checks for on

That was exactly the thing zk, thanks.

If anyone ever doubted the power of Serial.print for debugging, they should see this as a case study.

Update... I signed for a trial of ddns with dyn.com, and turns out that's one that my modem supports. So, unless I have unplugged stuff due to our huge Johannesburg electrical storms, my dining room temp and the local time, as well as the ability to turn an led off and on, is now available to all at:

http://jimboza.gotdns.com:8085.

Trial ends 30 Dec, at which time I might spring the 25USD.

Thanks a heap to Tim who surfs and the Kat who zooms for their input.

@Musicboy- any luck with the fb thing?

@JimboZA No unfortunately not, I've come to the conclusion that it's not realistically possible.
I think I might just settle for twitter isntead, as it's a lot easier :slight_smile:
Thanks for your interest though, and I'm glad this thread was useful to you.
Merry Christmas :slight_smile: