|
本帖最后由 topdog 于 2021-2-28 20:36 编辑
新冠病毒(COVID-19)2020年在全球肆虐,创客们也行动起来,把全球的医疗数据汇总观看,更能够使大家警惕病毒的危害,戴好口罩做好个人防护,保持安全社交距离,注意个人卫生勤洗手。著名的网站postman汇总了数据公布了API。非常感谢他们无私地为抗疫作出的贡献。
硬件使用乐鑫出品的ESP8266,OLED 屏幕,四根杜邦导线等,软件使用ARDUINO IDE制作了新冠病毒各国数据的显示项目。作一些说明:
1,本实例使用的API是https加密链接,需要网站的指纹,才能返回数据。譬如:Chrome浏览器,将API复制到地址栏,点击前方黑体锁型符号,证书-->详细信息-->指纹,双击。这就是fingerprint 。
2,用了Arduino官方推荐的Arduino_Json来解析数据,这是CJSON的方法,简洁明了。3,update后面13位数字是unix date,本例使用了字符串直接截取的方法,line第11位到21位就能够满足需求,后面三位舍去。
4,滚动播出中国、美国、意大利、印度等国家的新冠疫情数据供大家参考。
5,U8x8是u8g2库中对存储体消耗较小的,值得推荐。
6,0.96和0.66两种规格OLED 屏幕都可以使用。程序如下:
/*
库文件:
https://github.com/arduino-libraries/Arduino_JSON
https://github.com/olikraus/U8g2_Arduino
https://github.com/PaulStoffregen/Time
硬体接线:
OLED SDA 接 ESP8266 GPIO5
OLED SCL 接 ESP8266 GPIO4
*/
#include <Arduino.h>
#include <U8x8lib.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Arduino_JSON.h>
#include <TimeLib.h>
#ifndef STASSID
#define STASSID "WIFI名称"
#define STAPSK "WIFI密码"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "corona.lmao.ninja";
const int httpsPort = 443;
const int DisplayTime = 3000;
String line = "";
String Country;
unsigned int unix_data;
int Cases,TodayCases,Deaths,TodayDeaths,Recovered,Active,Critical;
const char fingerprint[] PROGMEM = "**********指纹**********";
//U8X8_SSD1306_64X48_ER_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
u8x8.begin();
u8x8.setPowerSave(0);
u8x8.setFlipMode(0);
u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
u8x8.clear();
u8x8.drawString(0,0,"connecting to");
u8x8.setCursor(0, 1);
u8x8.print(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
u8x8.setCursor(i, 2);
u8x8.print(".");
i++;
}
u8x8.drawString(0,3,"WiFi connected");
u8x8.drawString(0,4,"IP address:");
u8x8.setCursor(0,5);
u8x8.print(WiFi.localIP());
}
void loop() {
Display("USA");
delay(1000);
Display("china");
delay(1000);
Display("Italy");
delay(1000);
Display("India");
delay(1000);
}
void Display(String Sate){
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
u8x8.clear();
return;
}
String url = "/v2/countries/" + Sate;
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
line = client.readStringUntil('\n');
}
unix_data = line.substring(11, 21).toInt();
demoParse();
delay(DisplayTime);
u8x8.clear();
u8x8.drawString(0,0,"Covid-19");
u8x8.setCursor(2, 1);
u8x8.print(Country);
u8x8.drawString(0,2,"Report:");
u8x8.drawString(0,3,"DateTime");
u8x8.setCursor(0, 4);
u8x8.print(year(unix_data));
printDigits(month(unix_data));
printDigits(day(unix_data));
u8x8.setCursor(0, 5);
u8x8.print(String(hour(unix_data)) + ":" + String(minute(unix_data)) + ":" + String(second(unix_data)));
delay(DisplayTime);
u8x8.clear();
u8x8.drawString(0,0,"Cases:");
u8x8.setCursor(0, 1);
u8x8.print(Cases);
u8x8.drawString(0,2,"TodayCases:");
u8x8.setCursor(0, 3);
u8x8.print(TodayCases);
u8x8.drawString(0,4,"Deaths:");
u8x8.setCursor(0, 5);
u8x8.print(Deaths);
delay(DisplayTime);
u8x8.clear();
u8x8.drawString(0,0,"TodayDeaths:");
u8x8.setCursor(0, 1);
u8x8.print(TodayDeaths);
u8x8.drawString(0,2,"Recovered:");
u8x8.setCursor(0, 3);
u8x8.print(Recovered);
u8x8.drawString(0,4,"Active:");
u8x8.setCursor(0, 5);
u8x8.print(Active);
delay(DisplayTime);
u8x8.clear();
u8x8.drawString(0,0,"Critical:");
u8x8.setCursor(0, 1);
u8x8.print(Critical);
delay(DisplayTime);
}
void printDigits(int digits)
{
if (digits < 10)
u8x8.print('0');
u8x8.print(digits);
}
void demoParse() {
JSONVar myObject = JSON.parse(line);
if (myObject.hasOwnProperty("country")) {
Country = (const char*) myObject["country"];
}
if (myObject.hasOwnProperty("cases")) {
Cases = myObject["cases"];
}
if (myObject.hasOwnProperty("todayCases")) {
TodayCases = myObject["todayCases"];
}
if (myObject.hasOwnProperty("deaths")) {
Deaths = myObject["deaths"];
}
if (myObject.hasOwnProperty("todayDeaths")) {
TodayDeaths = myObject["todayDeaths"];
}
if (myObject.hasOwnProperty("recovered")) {
Recovered = myObject["recovered"];
}
if (myObject.hasOwnProperty("active")) {
Active = myObject["active"];
}
if (myObject.hasOwnProperty("critical")) {
Critical = myObject["critical"];
}
}
(完)
|
|