void listenForClient() {
Client cliente = server.available(); // espera a que haya datos disponilbles
boolean currentLineIsBlank = true, flag = true;
bufferSize = 0;
if (cliente) {
while (cliente.connected()) {
while (cliente.available()) {
char c = cliente.read(); //mySerial.write(c);
if (c == '\n' && currentLineIsBlank) {
// Here is where the POST data is.
while(cliente.available()) { // Datos si es un POST
c = cliente.read();
bufferData[bufferSize++] = c;
}
bufferData[bufferSize] = 0;
cliente.println("HTTP/1.1 200 OK");
cliente.println();
createRequest(bufferData);
cliente.flush();
cliente.stop();
}
else if (c == '\n') { // NEW LINE
currentLineIsBlank = true;
if (flag) {
flag = false;
buffer[bufferSize] = 0; // no me pinta salto de linea si bufferSize+1
bufferSize = 0; //Serial.print(buffer);
if ( strstr(buffer, "GET / HTTP/1.1") ) {
filePrint("SRV.txt", cliente);
cliente.flush();
cliente.stop();
}
else
if ( strstr(buffer, "GET /fioList HTTP/1.1") ){
sendGetResponse(cliente);
cliente.flush();
cliente.stop();
}
else
if ( !strstr(buffer, "POST /changes HTTP/1.1") ) {
cliente.flush();
cliente.stop();
}
}
}
else if (c != '\r') { // you've gotten a character on the current line
currentLineIsBlank = false;
if ( bufferSize < bufferMax && flag )
buffer[bufferSize++] = c;
}
} // client.available
} // client.connected
} // end if
} // end listenForClient
void sendGetResponse(Client cliente) {
char filename[9];
cliente.println("HTTP/1.1 200 OK\nContent-Type: application/json\nConnection: close");
cliente.println();
cliente.print("{\"fioList\":[{\"asynchronus\":\"");
cliente.print(asynchronusCoord);
cliente.print("\",\"sleepCycle\":\"");
cliente.print(sleepCycleCoord);
cliente.print("\"},");
for (int k = 0; k < fioNumber; k++) {
sprintf (filename, "F%d.JS", fioList[k]); // si Fx.js guardado en la microSd como Fx.JS
filePrint(filename, cliente);
////////////////////////////////////////
filename[0] = 0;
sprintf (filename, "G%d.JS", fioList[k]);
if (SD.exists(filename)){
cliente.print(",");
filePrint(filename, cliente);
filename[0] = 0;
sprintf (filename, "H%d.JS", fioList[k]);
if (SD.exists(filename)){
cliente.print(",");
filePrint(filename, cliente);
}
else {
cliente.print("}");
}
}
else {
cliente.print("}");
}
///////////////////////////////////////
if ( (k+1) != fioNumber)
cliente.println(",");
}
cliente.println("]}");
}
void filePrint (char filename[9], Client cliente){
if (myFile = SD.open(filename, FILE_READ)) {
while ( myFile.available() ) {
cliente.print( (char) myFile.read() );
}
myFile.close(); // CERRAR FICHERO
}
}
void generateJson (uint8_t address, uint8_t temperature, uint8_t humidity, uint8_t sleepCycle, uint8_t asynchronus, uint8_t uid[]) {
char filename[9], string[9];
int hora, minuto;
sprintf (filename, "F%d.JS", address);// String jsonString; Usando string no lo escribe correctamente
if ( SD.exists(filename) ) {
SD.remove(filename);
}
// CREACION FICHERO
if ((myFile = SD.open(filename, FILE_WRITE))) { // si esta abierto ...
myFile.print("{\"address\":\"");
myFile.print(address, DEC);
myFile.print("\",\"temperature\":\"");
myFile.print(temperature, DEC);
myFile.print("\",\"humidity\":\"");
myFile.print(humidity, DEC);
myFile.print("\",\"asynchronus\":\"");
myFile.print(asynchronus, DEC);
myFile.print("\",\"sleepCycle\":\"");
myFile.print(sleepCycle, DEC);
myFile.print("\",\"uid\":\"");
for ( int k = 0; k < 4; k++ ) {
myFile.print(uid[k], HEX);
}
myFile.print("\"");
myFile.close();// CERRAR FICHERO
}
if ( uid[4] != 0) {
filename[0] = 0;
string[0] = 0;
if ( uid[4] == 1 ){
sprintf (filename, "G%d.JS", address);
sprintf (string, "%c", "\"init\":\"");
}
else{
if ( uid[4] == 2 ) {
sprintf (filename, "H%d.JS", address);
sprintf (string, "%c", "\"wait\":\"");
}
}
// CREACION FICHERO
if ((myFile = SD.open(filename, FILE_WRITE))) { // si esta abierto ...
myFile.print(string);
dateTime(hora, minuto);
myFile.print(hora,DEC);
myFile.print(":");
myFile.print(minuto,DEC);
myFile.print("\"");
myFile.close();// CERRAR FICHERO
}
}
}
void createRequest(char buffer[]) {
uint8_t frameId = millis()*0x00FF;
char *param, *equal, sleepCycle[3], asynchronus[2];
sleepCycle[0] = 0;
asynchronus[0] = 0;
//sleepCycle=20&asynchronus=0
equal = strstr(buffer, "=") + 1;
param = strstr(equal, "&");
strncat(sleepCycle, equal, param-equal); // sleepCycle
equal = strstr(param, "=") + 1;
strncat(asynchronus, equal, 1); // asynchronus
uint8_t data[] = {CONFIGURATION, 2, atoi(sleepCycle), atoi(asynchronus)}; // Configuration request: [0,2,SLEEP_CYCLE,ASYNCHRONUS]
uint8_t length = sizeof(data);
if ( sendRequest( data, length, frameId) == false ) {
saveRequest(data, length, frameId);
flag2 = true; // al no ser enviada se levanta la bandera de pendientes
}
else { // se consiguió enviar
sleepCycleCoord = atoi(sleepCycle);
asynchronusCoord = atoi(asynchronus);
if (flag1 == true){ // solo vale true la primera vez que se envía el cambio de ciclo.
flag1 == false;
time1 = millis(); // time 1 toma su primer valor
}
}
}