Hi everyone,
First of all, I want to apologize if this is not the correct place to post, and secondly as you notice my english it is not so good.
I hope you could help me. I want to show in a web page the values of my sensors. By now I am playing with a little example.
The system is composed by an arduino UNO and an Ethernet shield. Both work as a server. I made a "web page" in html:
("SERVER.txt"):
Servidor Web Arduino(F1.txt):
name:
temperature:
humidity:
address:
sleepCycle:
asynchronus:
I want to explain this, I create as files as sensors. If I have 4 sensors there are 4 files F1.txt,F2.txt,F3.txt,F4.txt. Itl would be sense with the sketch.
It is supposed that if a push the button, a http get request is sent to my board. And then it replies with a json object:
(this structure is saved as a file )
{
"name": "fio1"
"variables":{
"address": "0x01",
"temperature": "24",
"humidity": "85",
"asynchronus": "0",
"sleepCycle": "20"
}
}
And I think when it is send to the browser it will work... I show you my super-sketch:
// LIBRERIAS
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h> // ocupa 5KB
char fio[3];
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x49, 0xFD };
byte ip[] = { 192, 168, 1, 2 }; //aula de informática {172,31,55,44} // laboratorio { 192, 168, 112, 31 }
byte gateway[] = { 192, 168, 1, 1};
Server server(80); // Puerto 80, HTTP
// VARIABLES GLOBALES
//Ethernet Shield uses pins 10,11,12,13.
#define bufferMax 128
int bufferSize;
char buffer[bufferMax];
// SD
File myFile;
#define ssPin 10
#define chipSelect 4
void setup() {
Ethernet.begin(mac, ip, gateway);
server.begin();
Serial.begin(9600); // inicia comunicación serie
pinMode(ssPin,OUTPUT);
if (!SD.begin(chipSelect)) { // inicia la tarjeta SD
Serial.println("initialization failed!");
return;
}
}
void loop() {
listenForClient();
}
void listenForClient() {
Client client = server.available(); // espera a que haya datos disponilbles
if ( client ) {
waitForRequest(client);
int fioNumber = parseReceivedRequest(client);
if (fioNumber != 0)
sendGetResponse(fioNumber, client);
}
client.stop();
}
void waitForRequest(Client client) {
bufferSize = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n')
break;
else
if (bufferSize < bufferMax)
buffer[bufferSize++] = c;
else
break;
}
}
Serial.print(buffer);
}
int parseReceivedRequest(Client client) { // Petición recibida del tipo: "GET &fio1 HTTP/1.1"
char* qm, * space;
fio[0]=0;
// strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
if ( qm = strstr(buffer, "&fio") ) { //apuntará a &
qm += 4; // sumando 5 apunta al valor deseado ( 4 letras --> "&", "f", "i", "o")
space = strstr(qm, " ") + 1;
strncat(fio, qm, space-qm-5);
return atoi(fio);
} else if ( qm = strstr(buffer, " ") ) {
webPrint(client);
return 0;
} else
return 0;
}
void sendGetResponse(int fioNumber, Client client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
char filename[8];
sprintf (filename, "F%d.jsn", fioNumber);
filePrint(filename, client);
client.println(" ");
}
void webPrint (Client client) {
filePrint("SERVER.TXT", client);
char filename[6];
int fiolength = 1, i = 1;
for ( i = 1; i <= fiolength; i++ ) {
sprintf (filename, "F%d.txt", i);
filePrint (filename, client);
}
client.println("");
client.println("");
}
// fuente: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1295739118
void filePrint (char filename[10], Client client){
char a;
if (myFile = SD.open(filename, FILE_READ)) {
while ( myFile.available() ) {
a = (char)myFile.read();
client.print(a);
Serial.print(a);
}
myFile.close(); // CERRAR FICHERO
}
}
basically I wait for http request, I write http:\192.168.1.2 in the browser... so I received "GET /HTTP1.1" as there is not information I print the web page. Then I wait for clicking the button, so it will appear an GET fio1 /HTTP1.1. I parse this last one, and response to it. I search on the micro SD card for a file called fio1 and then print this information that is the json structure and nothing happens. The goal was when the structure arrives to the client the script execute itself and fill the gap I wrote as <div ahref ... .... but... Maybe I make a mistake writing the html or the script or the http json response is not the correct one...
I am sorry it is sort of big... xD. Well, my questions are:
I do not have idea of html or http, just the few things I´ve read. And it is a mess. So I am not know if the html is well proposed. Or if I can response the http get request as I did just printing on the client, ...
I hope you could help me! Just read this is remarkable.
thank you !