#include #include // Time to sleep (in seconds): const int sleepTimeS = 10; // WiFi information const char WIFI_SSID[] = "Livebox-37cc"; const char WIFI_PSK[] = "8A6060920A8A86896F770F2C47"; // Remote site information const char http_site[] = "192.168.0.10"; const int http_port = 8080; // Pin definitions const int LED_PIN = 0; #define DEBUG true // Global variables WiFiClient client; void setup() { // Set up serial console to read web page Serial.begin(9600); Serial.print("Thing GET Example"); // Set up LED for debugging pinMode(LED_PIN, OUTPUT); } void loop() { int lum = analogRead(2); // If the server has disconnected, stop the client and WiFi if ( !client.connected() ) { // Connect to WiFi connectWiFi(); // Attempt to connect to website if ( !getPage("/json.htm?type=command¶m=udevice&idx=294&svalue=" + String(lum)) ) { Serial.println("GET request failed"); } while(!client.available()){ delay(10); } // // If there are incoming bytes, print them // while ( client.available() ) { // char c = client.read(); // Serial.print(c); // } String ret = recoitDuESP8266WithJson(10000, "status"); debugPrintln("** ret=" + ret); Serial.println(); // Close socket and wait for disconnect from WiFi client.stop(); if ( WiFi.status() != WL_DISCONNECTED ) { WiFi.disconnect(); } // Turn off LED digitalWrite(LED_PIN, LOW); // Do nothing Serial.println("Finished Thing GET test"); // while(true){ // delay(30000); //} } WiFi.forceSleepBegin(); delay(10000); // WiFi.deepSleep(sleepTimeS * 1000000); WiFi.forceSleepWake(); } // Attempt to connect to WiFi void connectWiFi() { byte led_status = 0; // Set WiFi mode to station (client) WiFi.mode(WIFI_STA); // Initiate connection with SSID and PSK WiFi.begin(WIFI_SSID, WIFI_PSK); // Blink LED while we wait for WiFi connection while ( WiFi.status() != WL_CONNECTED ) { digitalWrite(LED_PIN, led_status); led_status ^= 0x01; delay(100); } // Turn LED on when we are connected digitalWrite(LED_PIN, HIGH); } /****************************************************************/ /*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/ /****************************************************************/ String recoitDuESP8266WithJson(const int timeout, String jsonId) { String ret = ""; String reponse = ""; boolean found = false; long int time = millis(); while ( (time + timeout) > millis()) { while (client.available()) { //reponse = ""; char c = client.read(); reponse += c; //reponse.trim(); if (reponse.equalsIgnoreCase(jsonId)) { debugPrint("Trouve" + reponse + " " + jsonId); } if (c == 10 || c == 13 || reponse.length() > 80) { reponse.trim(); // debugPrint(reponse); int p = reponse.indexOf(':'); String id = reponse.substring(0, p); String value = reponse.substring(p + 1, reponse.length() - 1); id.trim(); value.trim(); id.replace("\"", ""); value.replace("\"", ""); if (id.equalsIgnoreCase(jsonId) && !found) { debugPrintln("========> Trouve " + jsonId + " == " + value); ret = value; found = true; } else { //debugPrintln(id + " " + value); } reponse = ""; } } } //debugPrintln(reponse); return ret; } /****************************************************************/ /*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/ /****************************************************************/ void recoitDuESP8266(const int timeout) { String reponse = ""; long int time = millis(); while ( (time + timeout) > millis()) { while (client.available()) { char c = client.read(); reponse += c; } } debugPrintln(reponse); } // Perform an HTTP GET request to a remote page bool getPage(String page) { // Attempt to make a connection to the remote server if ( !client.connect(http_site, http_port) ) { return false; } // Make an HTTP GET request client.println("GET " + page + " HTTP/1.1"); client.print("Host: "); client.println(http_site); client.println("Connection: close"); client.println(); return true; } void debugPrint(String m) { #ifdef DEBUG Serial.print(m); #endif } void debugPrintln(String m) { #ifdef DEBUG Serial.println(m); #endif }