Go Down

Topic: Read values from webpage to control relay (Ethernet Shield) (Read 5 times) previous topic - next topic

George-Xrik

Hi,

What I am trying to do is with arduino get values from a web site, and from the values close or open a relay (turn a digital pin high or low). While at the same time host a simple HTML page that displays the current position of the relay and the values I am reading from the website.

I am using an arduino mega (ATmega1280) board and an Ethernet shield.

I have managed to set up a simple HTML page that can be viewed on the local network and externally over internet, using the web server example. In other words I have set up the port forwarding for the arduino.

I need help or suggestions (even feasibility) on how to get values from a web site and displaying them on the html page hosted by the arduino.

Thanks

PaulS

Quote
I need help or suggestions (even feasibility) on how to get values from a web site and displaying them on the html page hosted by the arduino.

What web site do you want to get data from? Is the Arduino supposed to to get data mining in addition to serving up a page?

George-Xrik

The web site is http://80.85.104.41/ its a sunny webbox, say I would like to get the total yield value.

Quote
Is the Arduino supposed to to get data mining in addition to serving up a page?


I have no idea, that`s why I am posting here to see if anyone has done it before or if it is even possible. If its not possible, I would have to settle for getting the values and activating the relay only (would still need help to do this).

PaulS

That site requires a password to access. What happens when you enter a password?

Making the Arduino-as-server serve up a web page that contains a form, and making the Arduino process the form when the submit button is pressed is a piece of cake, it you understand client/server/web programming.

If not, zoomkat will come along shortly and post some code. Mine's on my other computer.

zoomkat

Quote
What I am trying to do is with arduino get values from a web site, and from the values close or open a relay (turn a digital pin high or low). While at the same time host a simple HTML page that displays the current position of the relay and the values I am reading from the website.


Below is some combined server/client code you can experiment with. You will need to develop the code required to perform your specific needs.

Code: [Select]

//zoomkat 12-08-11, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 4 high/low
//use the \ slash to escape the " in the html
//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>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
IPAddress ip(192,168,1,102); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(84); //server port
EthernetClient client;
String readString;

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

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  Ethernet.begin(mac, ip, subnet, gateway);
  server.begin();
  Serial.begin(9600);
  Serial.println("server/client 1.0 test 12/08/11"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0)
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call sendGET function
    }
  } 

  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

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println(); 
          }
          else {
            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 1.0 button</H1>");

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

            //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
            client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

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

        }
      }
    }
  }
}

//////////////////////////
void sendGET() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}


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

Go Up