I made a simple webpage (served by arduino) where you can press a submit button:
When you press the submit button in the URL shows up 192.168.1.1?SL=2&on=1
I want to save the data behind SL= and on= to EEPROM so I would use EEPROM.write(addr, val)
where the first time val = 2 (the data after SL=) and the second time val = 1 (the data after on=)
but how can I read the values and feed them to EERPOM.write
while (client.connected())
{
while (client.available()) // Not if - you want to read all the data
{
char c = client.read();
if (c == '\n' && currentLineIsBlank)
{
// Snipped the code to send the page with form in the first place
}
if (c == '\n')
{
currentLineIsBlank = true;
}
else if (c != '\r')
{
currentLineIsBlank = false;
// Accumulate the data in an array here...
}
You should start by simply echoing to the serial port what the client sends. You need to distinguish between the client sending a request for the form (no extra data) and a post back with data. Echo what the client sends, so that you can see the difference.
in the meantime I have been reading on Text Finder library but I still can manage to get the data
First I'll simple try to get the data and show it in the Serial (or on the webpage itself)
it should be something like this:
if(finder.find("<SL=>") )
{
int Slave = finder.getValue();
Serial.print("I've found an ID: ");
Serial.println(Slave);
if(finder.find("<on>") )
{
int active = finder.getValue();
Serial.print("I've found active: ");
Serial.println(active);
}
}
I DID IT 8) , well the first part: I'm able to GET the data out of the URL and show it in Serial Monitor. Now I have to find out how to write it to EEPROM (but I have a clue, because i've done it before)
This sketch gives you a webpage where you can fill out a form and when you press submit you will see it in the serial monitor. It has a home button to
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,14,85 };
Server server(80);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("ready");
}
void loop()
{
Client client = server.available();
if (client) {
TextFinder finder(client );
while (client.connected()) {
if (client.available()) {
if( finder.find("GET /") ) {
while(finder.findUntil("SL", "\n\r") ) {
int Slave = finder.getValue();
Serial.print("I've found a Slave");
Serial.print(Slave);
if(finder.findUntil("on", "\n\r") ) {
int active = finder.getValue();
if ( int active = 1) {
Serial.print(" and it is active ");
}
else {
Serial.print("and he is NOT active ");
}
}
if(finder.findUntil("ip", "\n\r") ) {
int slip = finder.getValue();
Serial.print("on ip: ");
Serial.println(slip);
}
}
}
Serial.println();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><body><table><form><tr><td>slave:<select name=\"SL\">");
client.print("<option>1</option><option>2</option></select></td><td>active:");
client.print("<input type=\"checkbox\" name=\"on\" value=\"1\"></td><td>");
for (int i= 0; i < 3; i++) //we take the same first ip values you set in byte ip
{
client.print(ip[i],DEC);
client.print(".");
}
client.print("<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"ip\"></td>");
client.print("<td><input type=\"submit\" value=\"SUBMIT\"></form></td></tr></table>
");
client.print("<FORM><INPUT TYPE=\"BUTTON\" VALUE=\"HOME\" ONCLICK=\"window.location.href='http://");
client.print(ip[0],DEC);
for (int i= 1; i < 4; i++)
{
client.print(".");
client.print(ip[i],DEC);
}
client.print("'\"></FORM>");
client.print("</body></html>");
break;
}
}
delay(1);
client.stop();
}
}