pin reading and writing via web server

I am using a I2C Sensor on an Arduino UNO with wiznet ethenet shield. How can i implement pin reading and pin writing in to this excisting code?

#include <Wire.h>
#include <SSC.h>
#include <SPI.h>
#include <Ethernet.h>
boolean reading = false;

// create an SSC sensor with I2C address 0x78 and power pin 8.
SSC ssc(0x28, 8);

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac = {
0x12, 0x34, 0x45, 0x78, 0x9A, 0xB0 };
IPAddress ip(192,168,1,150);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 85 is default for HTTP):
EthernetServer server(85);

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
Wire.begin();

// set min / max reading and pressure, see datasheet for the values for your sensor
ssc.setMinRaw(1638);
ssc.setMaxRaw(14742);
ssc.setMinPressure(0);
ssc.setMaxPressure(100);

// start the sensor
Serial.print(“start()\t\t”);
Serial.println(ssc.start());

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
Serial.println(ssc.update());
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(“new client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println(“Refresh: 1”); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
// output the value of HSC
{
client.print(“value at HSC “);
client.print(” is “);
client.print(ssc.pressure());
client.println(”
“);
}
client.println(””);
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println(“client disconnected”);
}
}

How can i implement pin reading and pin writing in to this excisting code?

I’m sure that you know that pin reading is done with digitalRead() and pin writing is done with digitalWrite().

Your question is really one of “how do I design a web page to facilitate doing this?”. We have no idea what you want your web page to look like. It will involve a form, with one or more submit buttons. Each submit button will trigger the same action, causing a new GET request. That GET request will include information from the submit button that was involved. You need to parse the GET request to determine what to do.

The first step, though, is to design the form. The second step is to get the Arduino to serve up the form. The third step is to simply look at the GET request that the Arduino gets. The fourth step is to parse the GET request, to determine what to do. The final step is to “Just Do It!”.

Hi,

thanks for fast reply.

There is no need for a graphical web interface.

Now the web server provides a blank page with the following output:

"value at HSC is (value)"

this comes from this part in the code

{ client.print("value at HSC "); client.print(" is "); client.print(ssc.pressure()); client.println(" "); }

additionaly i want to print the state of each pin to the same site like the following (example):

"value at HSC is (value)" pin 1 high pin 2 high pin 3 low pin 4low ect.

and i want to change the state at a pin by entering a request such as:

Http://192.168.1.150:85/cs2 (to change pin 2 from high to low as example)

cheers

Now the web server provides a blank page with the following output:

Perhaps you should right click and choose the “View page source” option. I doubt that the page is actually blank. It’s just missing a lot of useful stuff, like the and tags.

and i want to change the state at a pin by entering a request such as:

What does the Arduino print, if anything, when you enter that malformed URL in the browser’s address field?

If you just want to type the URL into the browser's address field and aren't bothered about using a web page to do that for you, you can just use the standard web server examples that show you how to retrieve the path and URL-encoded parameters from the request, and use conventional string processing techniques to identify the parameters in the URL. Just printing out the URL for each request would get you 80% of the way there and I'm sure you can find web server examples that do that.

Hi,

sorry for beeing such a newbie.

I have this “malformed URL” thing from the following example:

http://bildr.org/2011/06/arduino-ethernet-pin-control/

actually the arduino provides the following page source:

value at HSC is 2.42

I am very new to this and it took me days to combine this code just to see the value of the I2C sensor on my web page. I need to switch outputs and read inputs in a very simple way, thats why i am interessted in this “malformed” URL.

cheers

I have this "malformed URL" thing from the following example:

That URL is not malformed. Yours had two http:s (one with a capital H). THAT was a malformed URL. Before you can use the Arduino as a server, you really need to understand client/server communications.

Hi,

i still need some help in for the given code, i simply want to add switching pin 1 and 2 with a URL line.

can somebody add the necessary code ?

cheers

can somebody add the necessary code ?

Lots of people can. How much are you willing to pay?

@PaulS Go away!!! :0

others, need still some help.

The URL that was sent is included within the HTTP request. Since you print out the whole request to the Serial port, you should already be able to see it there.

To use it in your sketch you would need to read the URL into a char array buffer, locate the part of the URL which contains your command and then extract the command and parse them. Locating and buffering the URL is something that web servers normally do and you should have no trouble find examples that do this. Then all that's left is a bit of simple string parsing using strchr() and strtok().

jakobjua:
Hi, i still need some help in for the given code, i simply want to add switching pin 1 and 2 with a URL line.
can somebody add the necessary code ? cheers

Below is some test code for setting the arduino pins hi/lo via a web page (does not provide current pin state).

//zoomkat 8-04-12
//simple button GET server code to control arduino pins
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use ' instead of " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
///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
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
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server multi pin button 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>");
          
          // For simple testing, pin 5, 6, 7, and 8 are used in buttons
          // DIY buttons
          client.println("<a href=/?on2 >ON</a>"); 
          client.println("<a href=/?off3 >OFF</a>"); 
          client.println("&nbsp;<a href=/?off357 >ALL OFF</a>

"); 

          // mousedown buttons
          client.println("<input type=button value=ON onmousedown=location.href='/?on4;'>"); 
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5;'>");        
          client.println("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579;'>

");        
                   
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6;'>ON</>"); 
          client.println("<input type=radio onmousedown=location.href='/?off7;'>OFF</>"); 
          client.println("&nbsp;<input type=radio onmousedown=location.href='/?off3579;'>ALL OFF</>

");    
   
          
          // custom buttons
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on8;'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off9;'>");
          client.print("&nbsp;<input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579;'>");

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

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }
          
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          
           if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
          }     
          
            if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
          }         
             
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

many thx to Zoomkat,

i could adopt the given code to my needs.

is there a way to automatically refresh the value “client.print(ssc.pressure());” every 5 seconds?

cheers

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

int pin_2 = 2;
int pin_5 = 5;

// create an SSC sensor with I2C address 0x78 and power pin 8.
SSC ssc(0x28, 8);

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac = {
0x12, 0x34, 0x45, 0x78, 0x9A, 0xB0 };
IPAddress ip(192,168,1,150);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 85 is default for HTTP):
EthernetServer server(85);

String readString;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
Wire.begin();
pinMode(pin_2, INPUT);
pinMode(pin_5, OUTPUT); //pin selected to control

// set min / max reading and pressure, see datasheet for the values for your sensor
ssc.setMinRaw(1638);
ssc.setMaxRaw(14742);
ssc.setMinPressure(0);
ssc.setMaxPressure(100);

// start the sensor
Serial.print(“start()\t\t”);
Serial.println(ssc.start());

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
Serial.println(ssc.update());
int pinstate_2 = digitalRead(pin_2);
// listen for incoming clients
// 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("");
client.println("");
client.println(“with thanks to Zoomkat”);
client.println("");
client.println("");

client.println(“

CasaControl

”);
client.println("
“);
client.println(“Level is at”);
client.print(ssc.pressure());
client.println(”
“);
client.println(“PinState2:”);
client.print(pinstate_2);
client.println(”
");

// mousedown buttons pin5
client.println(“SetPin5”);
client.println("<input type=button value=ON onmousedown=location.href=’/?on2;’>");
client.println("<input type=button value=OFF onmousedown=location.href=’/?off3;’>");

client.println("");
client.println("");

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

///////////////////// control arduino pin
if(readString.indexOf(‘2’) >0)//checks for 2
{
digitalWrite(pin_5, HIGH); // set pin 5 high
Serial.println(“Led 5 On”);
}
if(readString.indexOf(‘3’) >0)//checks for 3
{
digitalWrite(pin_5, LOW); // set pin 5 low
Serial.println(“Led 5 Off”);
}

//clearing string for next read
readString="";

}
}
}
}
}

Some server test code that reads the current arduino analog input values, and supplys this info to a requesting client. The supplied web page incorporates some meta refresh parts.

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino 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
unsigned long int x=0; //set refresh counter to 0
String readString; 

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

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("

");
            
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
            
            client.print("
analog input1 is: ");
            client.print(analogRead(analogInPin1));
                        
            client.print("
analog input2 is: ");
            client.print(analogRead(analogInPin2));
            
            client.print("
analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                    
            client.print("
analog input4 is: ");
            client.print(analogRead(analogInPin4));
            
            client.print("
analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("
</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}