`
前言:
开发板已经收到好多天了,板子很漂亮小巧精致。
正文:
最近个别地区的疫情让我很焦虑(开学遥遥无期),我用这个esp32开发板做了一个大连疫情实时显示器,希望疫情快点过去。
本次编程使用的是arduino,
实验材料: esp32开发板,OLED屏,杜邦线,数据线。
全国新型肺炎疫情的实时数据接口:https://lab.isaaclin.cn/nCoV/
获取辽宁省疫情情况:
接口网址 :https://lab.isaaclin.cn/nCoV/api/area?latest=1&province=辽宁省
废话不多说直接上
代码:
- #include
- #include
- #include
- #include
- #include
- #include <ticker.h>
- // 定时器
- Ticker timer;
- boolean flag = true;
- // 0.96寸OLED12864显示屏
- U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
- const char* ssid = "wifi名";
- const char* password = "密码";
- String apiUrl = "https://lab.isaaclin.cn/nCoV/api/area?latest=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81";
- const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(11) + JSON_OBJECT_SIZE(2) + 11 * JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(16) + 2070;
- // 辽宁疫情数据变量
- int ln_confirmedCount;
- int ln_suspectedCount;
- int ln_curedCount;
- int ln_deadCount;
- //大连疫情数据变量
- int dalian_confirmedCount;
- int dalian_suspectedCount;
- int dalian_curedCount;
- int dalian_deadCount;
- // 铁岭疫情数据变量
- int tieling_confirmedCount;
- int tieling_suspectedCount;
- int tieling_curedCount;
- int tieling_deadCount;
- void setup()
- {
- // 初始化OLED显示屏
- u8g2.begin();
- u8g2.enableUTF8Print();
- u8g2.setFont(u8g2_font_wqy12_t_gb2312a);
- u8g2.setFontPosTop();
- u8g2.clearDisplay();
- Serial.begin(115200);
- // 连接网络
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.print("Local IP: ");
- Serial.println(WiFi.localIP());
- // 设置定时器
- timer.attach(60, timerCallback); // 每隔1min
- getnCovData();
- }
- void loop()
- {
- while (flag)
- {
- getnCovData();
- flag = false;
- serialPrintData();
- u8g2.firstPage();
- do
- {
- displayData();
- } while (u8g2.nextPage());
- }
- }
- // 定时器回调函数
- void timerCallback()
- {
- flag = true;
- }
- // 获取 nCoV 数据
- void getnCovData()
- {
- HTTPClient http;
- http.begin(apiUrl);
- int httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK)
- {
- String results = http.getString();
- // Serial.println(results); // 网页内容
- DynamicJsonBuffer jsonBuffer(capacity);
- JsonObject& root = jsonBuffer.parseObject(results);
- if (!root.success())
- {
- Serial.println("parseObject() failed");
- return;
- }
- // --------辽宁省数据 -----------
- JsonObject& provinces = root["results"][0];
- const char* countryName = provinces["countryName"]; // "中国"
- const char* provinceName = provinces["provinceName"]; // "辽宁省"
- const char* provinceEnglishName = provinces["provinceEnglishName"]; // "liaoning"
- ln_confirmedCount = provinces["confirmedCount"];
- ln_suspectedCount = provinces["suspectedCount"];
- ln_curedCount = provinces["curedCount"];
- ln_deadCount = provinces["deadCount"];
- // -------- cities -----------
- JsonArray& cities = provinces["cities"];
- // -------- 大连数据 -----------
- JsonObject& dalian = cities[0];
- const char* dalian_cityEnglishName = dalian["cityEnglishName"]; // "Hangzhou"
- dalian_confirmedCount = dalian["confirmedCount"];
- dalian_suspectedCount = dalian["suspectedCount"];
- dalian_curedCount = dalian["curedCount"];
- dalian_deadCount = dalian["deadCount"];
- // -------- 铁岭数据 -----------
- JsonObject& tieling = cities[2];
- const char* tieling_cityEnglishName = tieling["cityEnglishName"]; // "Ningbo"
- tieling_confirmedCount = tieling["confirmedCount"];
- tieling_suspectedCount = tieling["suspectedCount"];
- tieling_curedCount = tieling["curedCount"];
- tieling_deadCount = tieling["deadCount"];
- }
- else
- {
- Serial.println("GET Error.");
- }
- http.end();
- }
- // -------- 串口打印实时疫情信息 -----------
- void serialPrintData()
- {
- Serial.println("辽宁省新型肺炎疫情实时数据");
- Serial.println("-----------------------------------------");
- Serial.print("辽宁: 确诊:");
- Serial.print(ln_confirmedCount);
- Serial.print(" 治愈:");
- Serial.print(ln_curedCount);
- Serial.print(" 死亡:");
- Serial.println(ln_deadCount);
- Serial.print("大连: 确诊:");
- Serial.print(dalian_confirmedCount);
- Serial.print(" 治愈:");
- Serial.print(dalian_curedCount);
- Serial.print(" 死亡:");
- Serial.println(dalian_deadCount);
- Serial.print("铁岭: 确诊:");
- Serial.print(tieling_confirmedCount);
- Serial.print(" 治愈:");
- Serial.print(tieling_curedCount);
- Serial.print(" 死亡:");
- Serial.println(tieling_deadCount);
- Serial.println();
- }
- // 在OLED上显示实时疫情数据
- void displayData()
- {
- u8g2.clearDisplay();
- // 绘制表格
- u8g2.drawFrame(0, 16, 128, 48);
- u8g2.drawLine(0, 32, 127, 32);
- u8g2.drawLine(0, 48, 127, 48);
- u8g2.drawLine(32, 16, 32, 63);
- u8g2.drawLine(64, 16, 64, 63);
- u8g2.drawLine(96, 16, 96, 63);
- // 标题
- u8g2.setCursor(18, 2);
- u8g2.print("辽宁疫情实时信息");
- // 表格类别
- u8g2.setCursor(36, 20);
- u8g2.print("确诊");
- u8g2.setCursor(68, 20);
- u8g2.print("治愈");
- u8g2.setCursor(100, 20);
- u8g2.print("死亡");
- // 辽宁情况
- u8g2.setCursor(4, 36);
- u8g2.print("辽宁");
- u8g2.setCursor(38, 36);
- u8g2.print(ln_confirmedCount);
- u8g2.setCursor(72, 36);
- u8g2.print(ln_curedCount);
- u8g2.setCursor(104, 36);
- u8g2.print(ln_deadCount);
- // 大连情况
- u8g2.setCursor(4, 52);
- u8g2.print("大连");
- u8g2.setCursor(40, 52);
- u8g2.print(dalian_confirmedCount);
- u8g2.setCursor(72, 52);
- u8g2.print(dalian_curedCount);
- u8g2.setCursor(104, 52);
- u8g2.print(dalian_deadCount);
- }
实验现象:
希望疫情赶紧消失!
`