#include #include #include #include #include #include #include #include #include Adafruit_SSD1306 display = Adafruit_SSD1306(); #define BUTTON_A 0 #define BUTTON_B 16 #define BUTTON_C 2 #define LED 0 const char* ssid = "ESP8266 - TEST"; const char* password = "Coconuts08"; /*const char* ssid = "ESP8266"; const char* password = "password";*/ const char* host = "192.168.101.18"; ESP8266WebServer server(80); const int Led = 14; #if (SSD1306_LCDHEIGHT != 32) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif void setup() { pinMode(Led, OUTPUT); Serial.begin(115200); WiFi.begin(ssid, password); Serial.println(""); digitalWrite(Led, LOW); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); String IP = WiFi.localIP()+""; digitalWrite(Led, HIGH); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) // init done // Show image buffer on the display hardware. // Since the buffer is intialized with an Adafruit splashscreen // internally, this will display the splashscreen. display.display(); delay(1000); // Clear the buffer. display.clearDisplay(); display.display(); Serial.println("IO test"); pinMode(BUTTON_A, INPUT_PULLUP); pinMode(BUTTON_B, INPUT_PULLUP); pinMode(BUTTON_C, INPUT_PULLUP); // text display tests display.setTextSize(1); display.setTextColor(WHITE); } void loop() { /* if (! digitalRead(BUTTON_A)) display.print("A"); if (! digitalRead(BUTTON_B)) display.print("B"); if (! digitalRead(BUTTON_C)) display.print("C"); delay(10); yield(); display.display();*/ if(WiFi.status() != WL_CONNECTED){ digitalWrite(Led, LOW); Serial.println("Trying to reconnect !"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("Connected !"); }else{ if(digitalRead(Led)==LOW){ digitalWrite(Led, HIGH); } if(!digitalRead(BUTTON_A)){ display.clearDisplay(); display.setCursor(0,0); display.println("Connect to hum sensor"); display.println(""); WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); digitalWrite(Led, LOW); return; } String urlTemp = "/temp"; Serial.print("Requesting URL: "); client.print(String("GET ") + urlTemp + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); String json = ""; boolean httpBody = false; // while (client.available()) { String line = client.readStringUntil('\r'); Serial.println(line); if (!httpBody && line.charAt(1) == '{') { httpBody = true; } if (httpBody) { json += line; } //} StaticJsonBuffer<200> jsonBuffer; Serial.println("Got data:"); Serial.println(json); JsonObject& root = jsonBuffer.parseObject(json); String temp = root["temperature"]; String hum = root["humidity"]; Serial.println("Temperature :"+temp); Serial.println("Humidity :"+hum); display.println("Temp: "+temp+"C - Hum: "+hum+"%"); display.display(); } } }