stage gitlab1

This commit is contained in:
Laurent Deleers
2020-04-10 10:40:11 +02:00
parent cdce5cbf3c
commit 7b6dfd11ec
3 changed files with 170 additions and 0 deletions

49
arduino-covid.ino Normal file
View File

@@ -0,0 +1,49 @@
/* For Wemos D1 and OLED 128*64 display */
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#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);
}

101
covid19.cpp Normal file
View File

@@ -0,0 +1,101 @@
#include "covid19.h"
Covid19::Covid19() {
lastWorld = 0;
}
String Covid19::getDataFor(String myuri) {
String payload;
std::unique_ptr<BearSSL::WiFiClientSecure>client(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;
}

20
covid19.h Normal file
View File

@@ -0,0 +1,20 @@
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
class Covid19 {
private:
int lastWorld;
String getDataFor(String myuri);
public:
Covid19();
void displayCountry(String country, Adafruit_SSD1306 &OLED);
void displayWorld(Adafruit_SSD1306 &OLED);
};