I am trying to build a home automation system with an arduino mega 2560 and a wifi shield r3.
This system takes temperature measurements from some ds18b20 sensors and depending on the seting temperature turns ON or OFF some leds.
My problem is with the html side of the program. I can't take some data from a form so as to compare them to my actual temperature.
The second problem i have is that the inner html function is not working properly. I can't figure out why.
Web server development on Arduino is not a trivial task. I have been building an application for nearly two years and I am still working through some deep web server development issues - particularly around defending the website from hackers.
If you know the structure of an html request you will know that POST form data is in the second part of the request after the blank line that delimits the end of the first section of the html request. Beyond that you need to be good at string (or String) parsing in order to extract the form data that you want.
You can download my source code on my website at http://www.2wg.co.nz/ but the code is substantial. Start off by reviewing the WebServerProcess() procedure and see if you can work out how it is working. The code is well structured and well commented so you may be able to solve a few problems if you make an effort.
My problem is with the html side of the program. I can't take some data from a form so as to compare them to my actual temperature.
Below is some server servo test code that uses a text box to send servo commands to an arduino web server. You might use something similar to capture a value for comparison.
The second problem i have is that the inner html function is not working properly. I can't figure out why.
I can't figure out what you are talking about here.
//zoomkat 6-13-15
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually *DOES NOT WORK*.
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//int n; //value to write to servo
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, newString;
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7, 500, 2500); //the pin for the servo control, and range if desired
Serial.println("server text box servo test"); // 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.print(readString); //see what was captured
//now output HTML data header
client.println("HTTP/1.1 200 OK");
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>HTML form GET example</H1>");
client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page
client.println("Set servo position: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");
client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Send servo position'>");
client.println("</FORM>");
client.println("
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
/////////////////////
if (readString.length() >0) {
//Serial.println(readString); //prints string to serial port out
int pos1 = readString.indexOf('=');
int pos2 = readString.indexOf('&');
newString = readString.substring(pos1+1, pos2);
Serial.print("newString is: ");
Serial.println(newString);
int n = newString.toInt();
Serial.print("The value sent is: ");
Serial.println(n);
readString=""; //clears variable for new input
newString=""; //clears variable for new input
// auto select appropriate value
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
Serial.println();
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
Serial.println();
}
}
}
}
}
}
}
Thanks Zoomkat. That was very useful. can you tell me if i had two different text fields, what change i have to make to the "if (readString.length() >0)" function?
The below shows how to put more text boxes in an html form. Use the serial monitor to view what is received by the arduino. It should be a longer String with both text box contents in it. You can use the String functions to parse out the values in a similar fashon.