Read values from webpage to control relay (Ethernet Shield)

@ PaulS

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

It directs you to a new page that gives a more detailed account of the Kwh production form a PV plant, i.e Current and Voltage measurement values are stored in this data base.

I am trying set up the ardunio to get this values and show them on the web page and also use them to actuate the relay.

Does the target page have a RSS feed or does the website provide an API? Otherwise you need to look at scraping the value from the site/page, here's a Wikipedia article on web scraping. Web scraping - Wikipedia

Should the led (pin4) remain off(low) once the off button in the web page has been clicked?
....as it is only going off for a moment and back on, i.e one blink.

Pin 4 should stay low until another request tells it to go high. There should be no blink (no pin state changeunless commanded). Do note that using your browser's back button will resend the previous command.

what is this part of the code doing?

This tells the browser to not expect a new page to be sent. I use this with a javascript video feed to prevent disruption of the feed. You can remove it if you don't need it.

Also this part of the code is confusing me,

This is the client part of the code. It sends a request to a server for the arduino.txt file.

This is the client part of the code. It sends a request to a server for the arduino.txt file.

can you explain further the arduino.txt !

Does the target page have a RSS feed or does the website provide an API? Otherwise you need to look at scraping the value from the site/page

no neither an RSS feed nor an API, so will look into scrapping the values.

I finally got some time to continue this project.

Picking up where I left off, the following code is working fine. With one exception when I click the on or off button using google chrome as the browser it only blinks off the led and the turns the led back on. So if anyone knows why this is happening I would be happy to learn about it, but for now I will use explorer and safari as browser when communicating with arduino.

Also if anyone know about a good tutorial on ethernet syntax would be much appreciated.

//George-Xrik 31-03-12, updated
//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.177 or http://88.203.15.76 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,2,130); // ip in lan
IPAddress gateway(192,168,2,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(80); //server port
EthernetClient client;
String readString; 

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

void setup(){

  pinMode(8, OUTPUT); //pin selected to control
  Ethernet.begin(mac, ip, subnet, gateway); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 31/03/12"); // keep track of what is loaded
  Serial.println("Send 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("<H1>George'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(8, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(8, 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();

}

the following is the serial communication

server/client 1.0 test 31/03/12
Send g in serial monitor to test client
connected
HTTP/1.1 200 OK
Date: Sat, 28 Apr 2012 15:03:54 GMT
Server: Apache
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================

GET /?on HTTP/1.1 >>> from Safari (iphone)

Led On
GET /?off HTTP/1.1

Led Off
GET /?on HTTP/1.1

Led On
GET /?off HTTP/1.1

Led Off
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 finally got some time to continue this project.

Picking up where I left off, the following code is working fine. With one exception when I click the on or off button using google chrome as the browser it only blinks off the led and the turns the led back on. So if anyone knows why this is happening I would be happy to learn about it, but for now I will use explorer and safari as browser when communicating with arduino.

Also if anyone know about a good tutorial on ethernet syntax would be much appreciated.

//George-Xrik 31-03-12, updated
//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.177 or http://88.203.15.76 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,2,130); // ip in lan
IPAddress gateway(192,168,2,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(80); //server port
EthernetClient client;
String readString; 

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

void setup(){

  pinMode(8, OUTPUT); //pin selected to control
  Ethernet.begin(mac, ip, subnet, gateway); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 31/03/12"); // keep track of what is loaded
  Serial.println("Send 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("<H1>George'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(8, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(8, 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();

}

the following is the serial communication

server/client 1.0 test 31/03/12
Send g in serial monitor to test client
connected
HTTP/1.1 200 OK
Date: Sat, 28 Apr 2012 15:03:54 GMT
Server: Apache
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================

GET /?on HTTP/1.1                 >>> from Safari (iphone)

Led On                                  >>> from Safari (iphone)
GET /?off HTTP/1.1                >>> from Safari (iphone)

Led Off                                 >>> from Safari (iphone)
GET /?on HTTP/1.1                 >>> from explorer 

Led On                                  >>> from explorer 
GET /?off HTTP/1.1                >>> from explorer

Led Off                                 >>> from explorer
GET /?on HTTP/1.1                 >>> from chrome

Led On                                 >>> from chrome
GET /favicon.ico HTTP/1.1     >>> from chrome

Led On                                >>> from chrome
GET /?off HTTP/1.1              >>> from chrome

Led Off                               >>> from chrome
GET /favicon.ico HTTP/1.1     >>> from chrome

Led On                                >>> from chrome

What should be noted is that i only click on and than off once on each browser, chrome acts strangely and turns the led back on.

With one exception when I click the on or off button using google chrome as the browser it only blinks off the led and the turns the led back on. So if anyone knows why this is happening I would be happy to learn about it, but for now I will use explorer and safari as browser when communicating with arduino.

Sounds like chrome is being non compliant with the returned 204 status code. 204 tells the browser to not refresh the currrent page. Looks like chrome is refreshing orreloading a prevous page instead of doing nothing. The below code refreshes the current page and doesn't have te 204 code included.

//zoomkat 2-13-12
//DNS and DHCP-based web client test code
//for use with IDE 1.0
//open serial monitor and send an e to test
//and to see test result
//for use with W5100 based ethernet shields
//browser equivelant URL: 
// http://web.comporium.net/~shb/arduino.txt
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "www.ahaw.allalla.com"; // zoomkat's test web page server
EthernetClient client;

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

void setup(){
  Serial.begin(9600); 
  Serial.println("DNS and DHCP-based web client test 2/13/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    //test GET request
    client.println("GET /dataterima.xml HTTP/1.0"); //download text
    //client.println("GET /terimaphp.php?trua=0ff1001 HTTP/1.0"); //download text
    client.println("Host: www.ahaw.allalla.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    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(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

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

}

thanks for the reply

The below code refreshes the current page and doesn't have te 204 code included.

which part would be the 204 code, that I have to eliminate ?

George-Xrik:
thanks for the reply

The below code refreshes the current page and doesn't have te 204 code included.

which part would be the 204 code, that I have to eliminate ?

Perhaps the part that is not included. 8)

I am wondering how to do this myself. I have a website that is hosted with "Hostgator" and I would like to use it to switch on or off a light, nothing to tricky. I even have the Arduino live at http://70.68.49.190:89 so it's port forwarded, and I have the example Sketch working.
How do I write a simple sketch that works with my website rather than Twitter to interface with my Arduino? Is it RSS?
I have a Micro SD card plugged in to the Ethernet shield, is there a way to store HTML, XML and PHP documents on it and just call them when they are needed?

Thanks for your help and time.

Hi imrobc

As far as i know you can call data in the SD card at any time however I have never done this. But you will surely find something similar in the forum you can alter to suit your needs. What do mean by interface your website with your arduino ? what do u plan to do?

What if you are using a wifi module instead of ethernet shield?

Do I just change the mac and specify an ip?

The wifi and ethernet hardware are just transports, not protocols, so the code is interchangeable. Start the wifi shield in the setup function instead of the ethernet shield, then...

// change this
EthernetClient client;
// to this
WiFiClient client;

edit: Take a look at zoomkat's code earlier in this thread for the rest of the code. You can take a look at mine in the playground.
http://playground.arduino.cc/Code/WebClient

Thank you for the reply! :slight_smile:

Its returns "WiFiClient does not name a type".

I attempted to change everything to 'WiFi' but it just says not connected.

EDIT
My wifi module can be reached at its ip address on the network... That means it's already connected?
Also, should the module be in server mode or client? I would think client as it is getting data from a server...

Then you didn't change the includes to the wifi shield.

#include <SPI.h>
#include <WiFi.h>

edit: You keep referring to this device as a module instead of a shield. You should post a link to the wifi hardware you are using.

I posted the English documentation on this module in another persons thread.

http://forum.arduino.cc/index.php?topic=241146.msg1852857#msg1852857

So I presume I need to change all references to "Ethernet" to "WiFi"?

This part is easy. That is not an Arduino WiFi shield. It does not use the same library as the official Wifi shield, so I can't help you with it. Maybe you can get some help from the users in the other thread you posted a link to.

Okay. Thank you for your time. :slight_smile:

Hello, I need a help, I am doing my project with arduino, I have 2 temperature sensors DS18B20 and I can print them on webserver. But the third "temperature" will be just a number that I want to set in input text field with a submit button and somehow send it from webserver to Arduino to compare with real values of temperature and according to comparison opening/closeing relays. I tried to do it from string but I need an integer or float. Can you help me please ? i´m sending the code.

#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Ethernet.h>
#define ONE_WIRE_BUS_PIN 2
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress Probe01 = { 0x28, 0x55, 0x76, 0xA2, 0x09, 0x00, 0x00, 0x0A };
DeviceAddress Probe02 = { 0x28, 0xFD, 0x3F, 0xA2, 0x09, 0x00, 0x00, 0xED };
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x9C, 0xB7};
IPAddress ip(192, 168, 0, 177);
EthernetServer server(80);
String readString;
int rele1=4;

void setup()
{
pinMode(rele1, OUTPUT);
digitalWrite(rele1, HIGH);
Serial.begin(9600);
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);

// Initialize the Temperature measurement library
sensors.begin();

sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);

Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(rele1, OUTPUT);
digitalWrite(rele1, HIGH);
}

void loop()
{

delay(1000);
Serial.println();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());
Serial.print("Getting temperatures... ");
Serial.println();

sensors.requestTemperatures();

Serial.print("Probe 01 temperature is: ");
printTemperature(Probe01);
Serial.println();

Serial.print("Probe 02 temperature is: ");
printTemperature(Probe02);
Serial.println();

EthernetClient client = server.available();

if (client) {
Serial.println("new client");

boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {

//store characters to string
readString += c;
//Serial.print(c);
Serial.write(c);
}
if (c == '\n' && currentLineIsBlank) {

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 5");
client.println();
client.println("");
client.println("");
client.println("");
client.print("

 

");
client.print("

Actual temperatures

");
client.print("

temp 1 = ");
client.print(sensors.getTempC(Probe01));
client.print("

temp 2 = ");
client.print(sensors.getTempC(Probe02));
client.print("
");

client.println(""); //uses IP/port of web page

client.println("Požadovaná teplota:
");

client.println("");

client.println("");

client.println("");
break;
}
if (c == '\n') {

currentLineIsBlank = true;
} else if (c != '\r') {

currentLineIsBlank = false;
}
}
}

delay(1);

client.stop();

Serial.println("client disconnected");
}

if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number

Serial.print(n);
readString="";
}
if(readString.toInt()>sensors.getTempC(Probe01)){

digitalWrite(rele1, LOW);
}
else{
digitalWrite(rele1, HIGH);
}
}

void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
Serial.print("C: ");
Serial.print(tempC);

}
}