Pages: [1]   Go Down
Author Topic: Controlling arduino from internet, preferably facebook.  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 26, 2013, 12:36:16 pm by JimboZA » Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very basic web page control code

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="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very basic web page control code

Excellent, thanks.
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-roll . 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.

Quote
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:

Code:
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.

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

//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="";

        }
      }
    }
  }
}
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 smiley
Thanks for your interest though, and I'm glad this thread was useful to you.
Merry Christmas smiley
Logged

Pages: [1]   Go Up
Jump to: