#include #include #include #include /* OLED DETAILS */ #define PIN_RST 16 #define PIN_SDA 4 #define PIN_SCL 5 U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, PIN_RST, PIN_SCL, PIN_SDA); /* WIFI DETAILS */ const char* ssid = "SSID"; const char* password = "password"; const char* host_name = "heltecwifi8"; const char* api_url = "http://yourdomain.com/weather.php"; /* VARIABLES */ bool booted = false; int second_count = 0; int page = 0; /* WEATHER VARIABLES */ int temp; int temp_min; int temp_max; byte humidity; int pressure; String conditions; String weekday; byte day; byte month; int year; byte hours; byte minutes; byte seconds; static const char *monthlong[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; void setup() { Serial.begin(115200); Serial.println(); WiFi.hostname(host_name); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.printf("Connecting to %s\n", ssid); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Succesfully connected!"); Serial.print("Host Name: "); Serial.println(WiFi.hostname()); Serial.print("IP address: "); Serial.println(WiFi.localIP()); u8g2.begin(); // OLED: WIFI IP ADDRESS u8g2.firstPage(); do { u8g2.setFont(u8g2_font_open_iconic_embedded_1x_t); u8g2.drawGlyph(60, 12, 0x0050); u8g2.setFont(u8g2_font_mozart_nbp_tf); u8g2.drawStr(26, 30, WiFi.localIP().toString().c_str()); } while (u8g2.nextPage()); delay(1000); } void loop() { String string; // Fetch if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.useHTTP10(true); http.begin(api_url); int httpCode = http.GET(); if (httpCode > 0) { //const size_t bufferSize = JSON_OBJECT_SIZE(14); //DynamicJsonBuffer jsonBuffer(bufferSize); //JsonObject& doc = jsonBuffer.parseObject(http.getString()); DynamicJsonDocument doc(2048); deserializeJson(doc, http.getStream()); temp = doc["temp"]; temp_min = doc["temp_min"]; temp_max = doc["temp_max"]; humidity = doc["humidity"]; pressure = doc["pressure"]; const char *a1 = doc["conditions"]; conditions = String(a1); const char *a2 = doc["weekday"]; weekday = String(a2); day = doc["day"]; month = doc["month"]; year = doc["year"]; hours = doc["hours"]; minutes = doc["minutes"]; seconds = doc["seconds"]; } http.end(); } u8g2.clear(); // OLED: TEMPERATURE u8g2.firstPage(); do { u8g2.setFont(u8g2_font_open_iconic_weather_4x_t); if (conditions.equals("Clear")) u8g2.drawGlyph(0, 32, 0x0045); if (conditions.equals("Clouds")) u8g2.drawGlyph(0, 32, 0x0041); if (conditions.equals("Drizzle")) u8g2.drawGlyph(0, 32, 0x0040); if (conditions.equals("Thunderstorm") || conditions.equals("Rain") || conditions.equals("Snow")) u8g2.drawGlyph(0, 32, 0x0043); //u8g2.setFont(u8g2_font_crox5h_tf); u8g2.setFont(u8g2_font_inr24_mf); string = String(temp) + "°C"; u8g2.drawUTF8(40, 32, string.c_str()); } while (u8g2.nextPage()); delay(2500); u8g2.clear(); // OLED: HUMIDITY u8g2.firstPage(); do { //u8g2.setFont(u8g2_font_crox5h_tf); u8g2.setFont(u8g2_font_inr24_mf); string = String(humidity) + "%"; u8g2.drawStr(40, 32, string.c_str()); } while (u8g2.nextPage()); delay(2500); u8g2.clear(); // OLED: PRESSURE u8g2.firstPage(); do { //u8g2.setFont(u8g2_font_crox5h_tf); u8g2.setFont(u8g2_font_inr19_mf); string = String(pressure) + "kPa"; u8g2.drawStr(0, 32, string.c_str()); } while (u8g2.nextPage()); delay(2500); u8g2.clear(); // OLED: DATE u8g2.firstPage(); do { u8g2.setFont(u8g2_font_crox5h_tf); string = String(weekday) + " " + String(day) + " " + String(monthlong[month]); // + " " + String(year); u8g2.drawStr(0, 32, string.c_str()); } while (u8g2.nextPage()); delay(2500); u8g2.clear(); // OLED: TIME u8g2.firstPage(); do { //u8g2.setFont(u8g2_font_inr19_mf); u8g2.setFont(u8g2_font_inr24_mf); //string = String(hours) + " : " + String(minutes); string = hours; string.concat (":"); if (minutes <10) string.concat("0"); string.concat (minutes); u8g2.drawStr(0, 32, string.c_str()); } while (u8g2.nextPage()); delay(5*2500); }