Monday, May 11, 2015

Arduino web server - analog

If you have ever loaded the ethernet software from the Arduino IDE, you will recognize this web page.




This article is more about window dressing more than anything else. The Arduino comes with a nice bit of web server code that reports the results of some temperature sensors. Thought the code could use at least some minimal improvements. Also put the the Arduino web address on the dns so that it could be called by the hostname lookup instead of the ipaddress. The important part of the changed code is included. Normally you can not store images on the Arduino, but you can reference them from another site.




[code]

void loop() {
  // 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("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // output the value of each analog input pin
        
          client.println("<hr>");
          client.println("<center>");
          client.println("<h2>Analog sensor information page</h2>");
          client.println("</center>");
          client.println("<hr>");
          client.println("<br><br>");
          client.println("<img src='http://cdn.instructables.com/FFO/DOLC/FVW22FQV/FFODOLCFVW22FQV.LARGE.gif' width='200' height='200'>");
          client.println("<table border='1'>");
          client.println("<tr>");
          client.println("<td>");
          client.println("Sensor number");
          client.println("</td>");
          client.println("<td>");
          client.println("Sensor value");
          client.println("</td>");
          client.println("</tr>");
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
           client.println("<tr>");
            client.println("<td>");
            int sensorReading = analogRead(analogChannel);
                
            client.print(analogChannel);
         
            client.println("</td>");
            client.println("<td>");
            client.print(sensorReading);
            client.println("<br />");     
             client.println("</td>");
            client.println("</tr>");
                   
      }
        client.println("</table>");
          client.println("</html>");
          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 disonnected");
  }
[/code]




Example of how to the the temperature with an arduino.  From Ubuntu and the Arduino on www.instructables.com



Temp sensor. Is it too hot for you or your equipment, so now you can tell. Using an ice cube  will give you a good test of the unit.  Could also be the start of a sous vide machine.

<code.>

//declare variables
float tempC;
int tempPin = 0;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
{
tempC = analogRead(tempPin);           //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
Serial.print((byte)tempC);             //send the data to the computer
delay(1000);                           //wait one second before sending new data
}

</code>

No comments:

Post a Comment