//#include /* * http://stefanfrings.de/esp8266/index.html * modifiziert by R.P. * https://arduino.esp8266.com/stable/package_esp8266com_index.json ESP8266 Boards (2.6.0-dev) Board: "WIFI Kit 8" */ #include #include "oled.h" //#define SSID "Muschikatze" //#define PASSWORD "Geheim" #define SERVER "ptb.de" // Stationsmode (STA mode) #ifndef STASSID #define STASSID "Router3" #define STAPSK "1234123487658765" #endif const char* ssid = STASSID; const char* password = STAPSK; // There are 2 different versions of the board // static OLED display=OLED(2,14,4); static OLED display=OLED(4,5,16); static WiFiClient client; static char buffer[200]; /** * Sammle eine Zeile Text ein. * Rufe diese Funktion wiederholt auf, bis sie true zurueck gibt. Dann hast * du eine Zeile im Puffer. Wenn die Zeile nicht in den Puffer passt, wird * sie abgeschnitten. * * @param source Der Quell-Stream. * @param buffer Der Ziel-Puffer, muss bereits mit '\0' terminiert sein. * @param bufSize Groesze des Ziel-Puffers. * @param terminator Das letzte Zeichen, das gelesen werden soll, normalerweise '\n'. * @return True wennn das terminierende Zeichen erreicht wurde. */ bool append_until(Stream& source, char* buffer, int bufSize, char terminator) { int data=source.read(); if (data>=0) { int len=static_cast(strlen(buffer)); do { if (len(data); } if (data==terminator) { buffer[len]=0; return true; } data=source.read(); } while (data>=0); buffer[len]=0; } return false; } void setup() { Serial.begin(9600); display.init(); display.set_contrast(16); display.draw_string_P(0,0,PSTR("Connecting to the AP")); display.display(); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); // Configure WIFI WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); // Wait for connection to the AP while (WiFi.status() != WL_CONNECTED) { delay(50); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Show the own IP address display.draw_string(0,8,"IP="); display.draw_string(18,8,WiFi.localIP().toString().c_str()); display.display(); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("https://192.168.0.178/"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { delay(1000); display.scroll_up(8,20); display.draw_string_P(0,24,PSTR("Connecting to")); display.display(); display.scroll_up(8,20); display.draw_string(12,24,SERVER); display.display(); // Connect to the server if (client.connect(SERVER, 80)) { display.scroll_up(8,20); display.draw_string_P(0,24,PSTR("Connected")); display.display(); // Sent HTTP request client.print(F("GET / HTTP/1.1\r\nHost: ")); client.print(SERVER); client.print(F("\r\nConnection: close\r\n\r\n")); // receive the HTTP response bool foundDate=false; buffer[0]=0; while (client.connected()) { bool receivedLine=append_until(client,buffer,sizeof(buffer),'\n'); if (receivedLine) { // Find the Date header and display its value, example: // Date: Tue, 18 Dec 2018 09:35:00 GMT if (strstr(buffer, "Date: ")) { foundDate=true; // Skip the header name (Date:) char* date=buffer+6; // Split the string into date and time parts at the 4th space char* time=date; int count=0; while (*time) { if (*time==' ') { count++; if (count==4) { *time=0; time++; break; } } time++; } // Display date and time display.scroll_up(8,20); display.draw_string(0,24,date); // Tue, 18 Dec 2018 display.scroll_up(8,20); display.draw_string(0,24,time); // 09:35:00 GMT display.display(); break; } buffer[0]=0; } } if (!foundDate) { display.scroll_up(8,20); display.draw_string_P(0,24,PSTR("No date received")); display.display(); } } else { display.scroll_up(8,20); display.draw_string_P(0,24,PSTR("Conn. Failed")); display.display(); } // Disconnect client.stop(); }