From 7b6dfd11ec697f4a87d860e91b621f4ac8100261 Mon Sep 17 00:00:00 2001 From: Laurent Deleers Date: Fri, 10 Apr 2020 10:40:11 +0200 Subject: [PATCH] stage gitlab1 --- arduino-covid.ino | 49 ++++++++++++++++++++++ covid19.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++ covid19.h | 20 +++++++++ 3 files changed, 170 insertions(+) create mode 100644 arduino-covid.ino create mode 100644 covid19.cpp create mode 100644 covid19.h diff --git a/arduino-covid.ino b/arduino-covid.ino new file mode 100644 index 0000000..f75cfd8 --- /dev/null +++ b/arduino-covid.ino @@ -0,0 +1,49 @@ +/* For Wemos D1 and OLED 128*64 display */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "covid19.h" + + +#define OLED_RESET 0 // GPIO0 +Adafruit_SSD1306 OLED(OLED_RESET); +ESP8266WiFiMulti WiFiMulti; + + +void setup() { + Serial.begin(115200); + Serial.println(); + delay(1000); + Serial.flush(); + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("lauIOT", "superiot1"); + + OLED.begin(); + OLED.clearDisplay(); + OLED.setTextWrap(false); + OLED.setTextSize(2); + OLED.setTextColor(WHITE); + OLED.setCursor(0,0); + OLED.println("= Home =\n= Dash ="); + OLED.display(); + delay(1000); +} + + +void loop() { + Covid19 COV; + + if ((WiFiMulti.run() == WL_CONNECTED)) { + COV.displayCountry("belgium",OLED); + delay(60000); + COV.displayWorld(OLED); + } + delay(60000); +} diff --git a/covid19.cpp b/covid19.cpp new file mode 100644 index 0000000..5b96275 --- /dev/null +++ b/covid19.cpp @@ -0,0 +1,101 @@ +#include "covid19.h" + +Covid19::Covid19() { + lastWorld = 0; + } + +String Covid19::getDataFor(String myuri) { + String payload; + std::unique_ptrclient(new BearSSL::WiFiClientSecure); + client->setInsecure(); + HTTPClient https; + https.setTimeout(5000); + Serial.print("[HTTPS] begin...\n"); + if (https.begin(*client, "https://corona.lmao.ninja/"+myuri)) { + int httpCode = https.GET(); + if (httpCode > 0) { + Serial.printf("[HTTPS] GET... code: %d\n", httpCode); + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + payload = https.getString(); + } + } else { + payload = "{'error " + (String)https.errorToString(httpCode).c_str() + "'}"; + //Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); + } + https.end(); + } else { + Serial.printf("[HTTPS] Unable to connect\n"); + payload = "{'error'}"; + } + return payload; + } + + +void Covid19::displayCountry(String country, Adafruit_SSD1306 &OLED) { + String json = getDataFor("countries/"+country); + StaticJsonDocument<1024> doc; + DeserializationError error = deserializeJson(doc, json); + if (error) { + Serial.print(F("deserializeJson() failed: ")); + Serial.println(error.c_str()); + return; + } + int cases = doc["cases"]; + int todayCases = doc["todayCases"]; + int deaths = doc["deaths"]; + int todayDeaths = doc["todayDeaths"]; + int recovered = doc["recovered"]; + Serial.print("|=============================\n"); + Serial.print("| STATS FOR : " + country + "\n"); + Serial.print("|=============================\n"); + Serial.print("| Cases : " + String(cases) + " \n"); + Serial.print("| todayCases : " + String(todayCases) + " \n"); + Serial.print("| deaths : " + String(deaths) + " \n"); + Serial.print("| todayDeaths : " + String(todayDeaths) + " \n"); + Serial.print("| recovered : " + String(recovered) + " \n"); + OLED.clearDisplay(); + OLED.setTextWrap(false); + OLED.setTextSize(1); + OLED.setTextColor(WHITE); + OLED.setCursor(0,0); + OLED.println(" === " + country + " === " ); + OLED.println("Cases : " + String(cases) + " (" + todayCases + ")" ); + OLED.println("Death : " + String(deaths) ); + OLED.println("Recov : " + String(recovered) ); + OLED.display(); + } + +void Covid19::displayWorld(Adafruit_SSD1306 &OLED) { + String json = getDataFor("all"); + StaticJsonDocument<512> doc; + DeserializationError error = deserializeJson(doc, json); + if (error) { + Serial.print(F("deserializeJson() failed: ")); + Serial.println(error.c_str()); + return; + } + int cases = doc["cases"]; + int deaths = doc["deaths"]; + int recovered = doc["recovered"]; + if(lastWorld==0) lastWorld = cases; + int delta = cases - lastWorld; + + Serial.print("|=============================\n"); + Serial.print("| STATS FOR : WORLD \n"); + Serial.print("|=============================\n"); + Serial.print("| Cases : " + String(cases) + " ("+delta+")\n"); + Serial.print("| deaths : " + String(deaths) + " \n"); + Serial.print("| recovered : " + String(recovered) + " \n"); + OLED.clearDisplay(); + OLED.setTextWrap(false); + OLED.setTextSize(1); + OLED.setTextColor(WHITE); + OLED.setCursor(0,0); + OLED.println(" === WORLDWIDE === " ); + OLED.println("Cases : " + String(cases) + "("+delta+")" ); + OLED.println("Death : " + String(deaths) ); + OLED.println("Recov : " + String(recovered) ); + OLED.display(); + + lastWorld = cases; + } diff --git a/covid19.h b/covid19.h new file mode 100644 index 0000000..e71fd4c --- /dev/null +++ b/covid19.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include + + +class Covid19 { + + private: + int lastWorld; + String getDataFor(String myuri); + + public: + Covid19(); + void displayCountry(String country, Adafruit_SSD1306 &OLED); + void displayWorld(Adafruit_SSD1306 &OLED); + + };