Ethernet Sensor Data with PHP/HTML

Hello there,

Im working on manipulating an example of code I found on the Arduino website. I specifically need to insert some sensor value read in the loop into the html/php code where the sensor data is going to be displayed. I tried using a textbox and setting the name of it to the name of the variable, hoping that the data would input into that box, but it didnt work. So, could someone show me how to display that sensor value into the html text or the textbox? I know what Im asking is probably trivial, but its just not coming to me. =( I would appreciate the help!

// Page 1
PROGMEM prog_char http_uri1[] = "/";
PROGMEM prog_char content_title1[] = "<h2>Sensor Data</h2>";
PROGMEM prog_char content_page1[] = "<hr /><h3>Sensor 1</h3><p><font color=green><b>Gas Sensor 1 Data goes here: </b><input type=text name=sensor1 value=sensor1></font></p>

 <h3>Sensor 2</h3><p><font color=green><b>Gas Sensor 2 Data goes here: </b><input type=text name=sensor2 value=sensor2></font></p>
";
void loop() {
    
    int analogChannel=0;
    int sensor1 = analogRead(analogChannel);
   
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  
}

Thanks in advance! :slight_smile:

Suppose you are using the Ethernet Shield and library here (http://arduino.cc/en/Guide/ArduinoEthernetShield). Even though you use a different library, this mean will still work.

You can use the below code to insert the dynamic data.

// client is an instance of Client
int sensor1 = analogRead(analogChannel);
client.print( " <span id='sensor1'>");
client.print(sensor1);
client.print( "</span>\n");

Im working on manipulating an example of code I found on the Arduino website. I specifically need to insert some sensor value read in the loop into the html/php code where the sensor data is going to be displayed.

Are you trying to display the data in a web page supplied from the arduino, or are you trying to upload the data to some other server?

zoomkat:
Are you trying to display the data in a web page supplied from the Arduino, or are you trying to upload the data to some other server?

Actually both. I’d like a web page to be available for whoever wants to check the Arduino on site, and also to get the data sent to a local server.

I tested the code, and thats not what Im trying to do. If you look at the image, I want the data to display where it says sensor1.

I tested the code, and thats not what Im trying to do. If you look at the image, I want the data to display where it says sensor1.

I guess you misunderstood my meaning. You can’t add my code at end of your program. The string "


Sensor 1

Gas Sensor 1 Data goes here:

Sensor 2

Gas Sensor 2 Data goes here:

" can't be sent in a sentence. I add more details in the code below. It demoes how to **INSERT** the sensor data into the html body.

BTW, sending the html code with multi print sentences in the program will give you a good code style. Appending “\n” at the end of some sentences you sent will give you a good code appearance when you check the html source code at the browser side.

int sensor1 = analogRead(analogChannel1);
int sensor2 = analogRead(analogChannel2);

client.print( "<hr /><h3>Sensor 1</h3>\n");
client.print( "<p><font color=green><b>Gas Sensor 1 Data goes here: </b>");
client.print( " <span id='sensor1'>");
client.print(sensor1);
client.print( "</span>\n");
client.print( "</font></p>
\n");

client.print( "
<h3>Sensor 2</h3>\n");
client.print( "<p><font color=green><b>Gas Sensor 2 Data goes here: </b>");
client.print( " <span id='sensor2'>");
client.print(sensor2);
client.print( "</span>\n");
client.print( "</font></p>
\n");

Actually both. I’d like a web page to be available for whoever wants to check the Arduino on site, and also to get the data sent to a local server.

To send data to a web server, you can use the GET and POST http request. The code below is from the sketch of the Ethernet library. It demoes how to send data to web server with get request.

byte server[] = { 173,194,33,104 }; // Google
Client client(server, 80);
if (client.connect()) {
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.0");
  }

The below has the arduino server code to return a fairly complex web page. You could make something simpler to reurn requested data to a data box in a web page. I haven't seen code to run both client and server in the same code, but it probably can be done. The server would need to run all the time in a loop, and the client would be embeded in the server loop and be run when a some condition is met.

http://www.scienceprog.com/getting-hands-on-arduino-ethernet-shield/

I tried to insert it into the loop. What Im trying to do is insert it into the

PROGMEM prog_char content_page1[] = "<hr /><h3>Sensor 1</h3><p><font color=green><b>Gas Sensor 1 Data goes here: </b><input type=text name=sensor1 value=sensor1></font></p>

 <h3>Sensor 2</h3><p><font color=green><b>Gas Sensor 2 Data goes here: </b><input type=text name=sensor2 value=sensor2></font></p>
";

section of code.

I’d like very much to keep the same formatting features, and the loading from memory part of things, and the easier multi-page setup. But I need to insert sensor data into that array also. Will the example code you give allow me to do it so that I dont have to use client.print(). Im trying to avoid that if possible.

The below has the arduino server code to return a fairly complex web page. You could make something simpler to reurn requested data to a data box in a web page. I haven’t seen code to run both client and server in the same code, but it probably can be done. The server would need to run all the time in a loop, and the client would be embeded in the server loop and be run when a some condition is met.

http://www.scienceprog.com/getting-hands-on-arduino-ethernet-shield/

I looked at the link, but its not doing what I need. Im not trying to do a complex web page, Im trying to do a simple one that loads from the memory, and inserts live data into that code. Something like setting a variable to a php variable, or javascript variable… :~ Thanks!

Im not trying to do a complex web page, Im trying to do a simple one that loads from the memory, and inserts live data into that code. Something like setting a variable to a php variable, or javascript variable... Thanks!

If you want to transfer data barebones, then you don't need to load anything from memory, just send the data. You can open the below link in your browser, then look at the page source. No PHP, no javascript, no nothing, just the data.

http://208.104.2.86/~shb/arduino.txt

I think perhaps I've been trying to do things the hard way, and host a site and show data at the same time off an arduino. I'm gonna change it to only post data to some other server and that server will host a website to show off the data.

Do you happen to know how I can setup the other server so that the data stream can be logged into a database? Or is it application specific to all the software I'll be running?

I appreciate the help!

Please have a lool at this site (http://www.pachube.com/). If if meets your requirement, you even don't need to create a web server.