本帖最后由 526598 于 2020-7-6 09:33 编辑
本贴针对 blinker新版本库(v0.3.4)天气接口重新编写程序
/\ \ /\ \ __ /\ \ v0.3.4
\ \ \___ \ \ \ /\_\ ___\ \ \/'\ __ _ __
.\ \ '__`\\ \ \ \/\ \ /' _ `\ \ , < /'__`\/\`'__\
\ \ \L\ \\ \ \_\ \ \/\ \/\ \ \ \\`\ /\ __/\ \ \./
\ \_,__/ \ \__\\ \_\ \_\ \_\ \_\ \_\ \____\\ \_\
\/___/ \/__/ \/_/\/_/\/_/\/_/\/_/\/____/ \/_/
低于此版本请参考https://www.arduino.cn/thread-98403-1-1.html
材料清单请参考我之前的帖子https://www.arduino.cn/thread-98403-1-1.html
此版程序自定义天气图标(上一版的样式太少)
增加屏幕刷新功能,因为复工之前工作积攒的比较多时间有限,有些地方不是很满意,希望大家多包涵
天气信息于Blinker点灯平台获取 Lcd12864 驱动为u8g2 时间字体:u8g2_font_logisoso28_tf 日期字体:u8g2_font_wqy12_t_gb2312a 温度字体:u8g2_font_wqy16_t_gb2312b 字体请参考u8g2库文件 (win10)C:\Users\Administrator\Documents\Arduino\libraries\u8g2\src\clib\u8g2.h U8g2lib库在Arduino中 项目-加载库-管理库-搜索u8g2-安装即可或参考https://github.com/olikraus/u8g2 手动下载地址(未在其他机器测试):http://downloads.arduino.cc/libraries/github.com/olikraus/U8g2-2.27.6.zip其他型号LCD参考Arduino开发软件的文件-示例-u8g2,任选里面有好多注释掉的连接方式仔细看备注 其他问题后面补充,有疑问请留言,所学有限尽量回答,见谅!
视频地址:https://www.bilibili.com/video/BV12k4y1679Q/
连接图8266 ESP -01
连接图8266 ESP -12F注意U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 0, /* data=*/ 2, /* CS=*/ 1, /* reset=*/ 16);// Feather HUZZAH ESP8266, E=clock=14, RW=data=13, RS=CS
因为esp-12f针脚比较充裕 修改 /*CS=*/ 1 为 /*CS=*/ 4 ,方便布线。下图为3v12864接法,5v12864请将红黑线分别接到5v正负上
esp-12f针脚定义参考
- #define BLINKER_WIFI
- #include <Blinker.h>
- #include <SPI.h>
- #include <U8g2lib.h>
- char auth[] = "Your Device Secret Key";
- char ssid[] = "Your WiFi network SSID or name";
- char pswd[] = "Your WiFi network WPA password or WEP key";
- char w_addr[]="beijingshi"; /*城市关键字.注意不要有空格
- 城市关键字,参考 https://diandeng.tech/doc/weather-and-air
- */
- int num_second=0;
- int num_minute=0;
- int num_hour=0;
- int num_month=0;
- int num_mday=0;
- int num_year=0;
- char aqi_level[12]="";
- char aqi_pm25[8]="";
- char aqi_pm10[8]="";
- char aqi_SO2[8]="";
- char aqi_NO2[8]="";
- char aqi_CO[8]="";
- char aqi_O3[8]="";
- char aqi_4[26]="";
- int weather_code=0;
- char w_weather[10]="";
- char w_temp[7]="18 °C";
- char* ch;//用于查找数据是否包含天气关键字时接受返回值
- bool first_label=false; //开机标签,开机第一时间更新天气信息
- U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 0, /* data=*/ 2, /* CS=*/ 1, /* reset=*/ 16); // Feather HUZZAH ESP8266, E=clock=14, RW=data=13, RS=CS
- //天气图标列表[阴,雪,雨,多云,晴,雾霾,夜晚晴,夜晚多云] 取模教程 https://www.arduino.cn/thread-42174-1-1.html
- static unsigned char weather_icon[8][180]={/*阴*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,
- 0xE0,0x3F,0x00,0xF0,0x00,0xF0,0xFF,0x00,0xF0,0x00,0x38,0xC0,0x01,0xF0,0x00,0x0C,0x00,0x03,
- 0xF0,0x00,0x06,0x00,0x06,0xF0,0x00,0x07,0x00,0x0E,0xF0,0xE0,0x03,0x00,0x0C,0xF0,0xF0,0x03,
- 0x00,0x0C,0xF0,0x38,0x00,0x00,0x18,0xF0,0x0C,0x00,0x00,0xF8,0xF0,0x0E,0x00,0x00,0xF8,0xF1,
- 0x06,0x00,0x00,0x80,0xF3,0x06,0x00,0x00,0x00,0xF7,0x06,0x00,0x00,0x00,0xF6,0x06,0x00,0x00,
- 0x00,0xF6,0x06,0x00,0x00,0x00,0xF6,0x06,0x00,0x00,0x00,0xF6,0x0E,0x00,0x00,0x00,0xF6,0x0C,
- 0x00,0x00,0x00,0xF7,0x38,0x00,0x00,0x80,0xF3,0xF0,0xFF,0xFF,0xFF,0xF1,0xE0,0xFF,0xFF,0xFF,
- 0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*雪*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x80,0x19,0x00,0xF0,0x00,0x80,0x1F,0x00,0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,
- 0x00,0x06,0x00,0xF0,0x00,0x00,0x06,0x00,0xF0,0x80,0x01,0x06,0x18,0xF0,0x80,0x01,0x06,0x18,
- 0xF0,0x80,0x01,0x06,0x18,0xF0,0xE0,0x83,0x1F,0x7E,0xF0,0xE0,0xFF,0xF9,0x7F,0xF0,0x00,0xFC,
- 0xF0,0x03,0xF0,0x00,0x70,0xE0,0x00,0xF0,0x00,0x30,0xC0,0x00,0xF0,0x00,0x30,0xC0,0x00,0xF0,
- 0x00,0x30,0xC0,0x00,0xF0,0x00,0x30,0xC0,0x00,0xF0,0x00,0x70,0xE0,0x00,0xF0,0x00,0xFC,0xF0,
- 0x03,0xF0,0xE0,0xFF,0xF9,0x7F,0xF0,0xE0,0x87,0x1F,0x7C,0xF0,0x80,0x01,0x06,0x18,0xF0,0x80,
- 0x01,0x06,0x18,0xF0,0x80,0x01,0x06,0x18,0xF0,0x00,0x00,0x06,0x00,0xF0,0x00,0x00,0x06,0x00,
- 0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,0x80,0x1F,0x00,0xF0,0x00,0x80,0x19,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*雨*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0xC0,0x3F,0x00,0xF0,0x00,0xF0,0xFF,0x00,0xF0,0x00,0x38,0xC0,0x01,0xF0,0x00,
- 0x1C,0x80,0x03,0xF0,0x00,0x0C,0x00,0x03,0xF0,0x00,0x06,0x00,0x06,0xF0,0xE0,0x07,0x00,0x06,
- 0xF0,0xF0,0x03,0x00,0x04,0xF0,0x38,0x00,0x00,0x1C,0xF0,0x18,0x00,0x00,0xFC,0xF0,0x0C,0x00,
- 0x00,0xF8,0xF1,0x0C,0x00,0x00,0x80,0xF1,0x0C,0x00,0x00,0x00,0xF3,0x0C,0x00,0x00,0x00,0xF3,
- 0x0C,0x00,0x00,0x00,0xF3,0x18,0x30,0x0C,0x03,0xF3,0x38,0x30,0x0C,0x83,0xF1,0x70,0x30,0x0C,
- 0xC3,0xF1,0x60,0x18,0x86,0xC1,0xF0,0x00,0x18,0x86,0x01,0xF0,0x00,0x18,0x86,0x01,0xF0,0x00,
- 0x1C,0xC7,0x01,0xF0,0x00,0x0C,0xC3,0x00,0xF0,0x00,0x0C,0xC3,0x00,0xF0,0x00,0x00,0x03,0x00,
- 0xF0,0x00,0x80,0x01,0x00,0xF0,0x00,0x80,0x01,0x00,0xF0,0x00,0x80,0x01,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*多云*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x38,0x00,0xF0,0x80,0x03,0x30,0x00,0xF0,0x80,0x01,0x20,0x00,0xF0,0x80,
- 0x00,0x00,0x00,0xF0,0x00,0xE0,0x07,0x00,0xF0,0x00,0xF8,0x0F,0x00,0xF0,0x00,0x1C,0x1C,0x00,
- 0xF0,0x00,0x0E,0x30,0x00,0xF0,0x00,0x06,0x70,0x00,0xF0,0x00,0x07,0x60,0x08,0xF0,0x00,0x03,
- 0xC0,0x18,0xF0,0x08,0x03,0xC0,0x08,0xF0,0x0C,0xC3,0x7F,0x00,0xF0,0x08,0xE3,0xFF,0x00,0xF0,
- 0x00,0x7E,0xE0,0x00,0xF0,0x00,0x1E,0x80,0x01,0xF0,0x00,0x1C,0x80,0x03,0xF0,0x00,0x0E,0x00,
- 0x03,0xF0,0xC0,0x0F,0x00,0x03,0xF0,0xE0,0x07,0x00,0x03,0xF0,0x70,0x00,0x00,0x1F,0xF0,0x38,
- 0x00,0x00,0x7F,0xF0,0x38,0x00,0x00,0xFE,0xF0,0x38,0x00,0x00,0xE0,0xF1,0x38,0x00,0x00,0xC0,
- 0xF1,0x70,0x00,0x00,0xC0,0xF1,0xE0,0xFF,0xFF,0xFF,0xF0,0xC0,0xFF,0xFF,0x7F,0xF0,0x00,0xFE,
- 0xFF,0x1F,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*晴*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x06,0x00,0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,
- 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
- 0xF0,0xC0,0x81,0x1F,0x38,0xF0,0xC0,0xC0,0x3F,0x30,0xF0,0x40,0xE0,0x70,0x20,0xF0,0x00,0x30,
- 0xC0,0x00,0xF0,0x00,0x38,0xC0,0x01,0xF0,0x00,0x18,0x80,0x01,0xF0,0x00,0x18,0x80,0x01,0xF0,
- 0x00,0x18,0x80,0x01,0xF0,0x00,0x18,0x80,0x01,0xF0,0x00,0x38,0xC0,0x01,0xF0,0x00,0x30,0xC0,
- 0x00,0xF0,0x40,0xE0,0x70,0x20,0xF0,0xC0,0xC0,0x3F,0x30,0xF0,0xC0,0x81,0x1F,0x38,0xF0,0x00,
- 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
- 0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,0x00,0x06,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*雾霾*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x80,0x1F,0x00,0xF0,0x00,0xE0,0x7F,0x00,0xF0,0x00,
- 0x70,0xE0,0x00,0xF0,0x00,0x18,0x80,0x01,0xF0,0x00,0x0C,0x00,0x03,0xF0,0x00,0x04,0x00,0x02,
- 0xF0,0xC0,0x07,0x00,0x06,0xF0,0xE0,0x03,0x00,0x04,0xF0,0x70,0x00,0x00,0x04,0xF0,0x18,0x00,
- 0x00,0x7C,0xF0,0x1C,0x00,0x00,0xFC,0xF0,0x0C,0x00,0x00,0xC0,0xF1,0x0C,0x00,0x00,0x80,0xF3,
- 0x0C,0x00,0x00,0x00,0xF3,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0xF0,0xFF,0x8F,0xFF,0xF0,0xF0,0xFF,0x8F,0xFF,0xF0,0x00,
- 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
- 0xF0,0xF0,0xF8,0xFF,0xFF,0xF0,0xF0,0xF8,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*夜晚晴*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x18,0x00,0xF0,0x00,0x06,0x3C,0x00,0xF0,0x00,
- 0x0F,0xFF,0x00,0xF0,0x80,0x0F,0x7E,0x00,0xF0,0xC0,0x0D,0x3C,0x00,0xF0,0xC0,0x0C,0x7E,0x00,
- 0xF0,0x60,0x0C,0x66,0x00,0xF0,0x60,0x0C,0x00,0x00,0xF0,0x30,0x0C,0x00,0x00,0xF0,0x30,0x1C,
- 0x00,0x00,0xF0,0x30,0x18,0x00,0x00,0xF0,0x30,0x18,0x00,0x08,0xF0,0x30,0x30,0x00,0x3E,0xF0,
- 0x30,0x70,0x00,0x1C,0xF0,0x30,0xE0,0x00,0x14,0xF0,0x30,0xC0,0x01,0x00,0xF0,0x30,0x80,0x03,
- 0x00,0xF0,0x30,0x00,0x07,0x00,0xF0,0x60,0x00,0x3E,0x00,0xF0,0x60,0x00,0xF8,0x1F,0xF0,0xC0,
- 0x00,0xE0,0x3F,0xF0,0xC0,0x01,0x00,0x38,0xF0,0x80,0x03,0x00,0x1C,0xF0,0x00,0x07,0x00,0x0E,
- 0xF0,0x00,0x1E,0x80,0x07,0xF0,0x00,0xF8,0xFF,0x01,0xF0,0x00,0xE0,0x7F,0x00,0xF0,0x00,0x00,
- 0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0},
- /*夜晚多云*/
- { 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x0C,0x00,0xF0,0x00,0x00,0x0E,
- 0x00,0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,0x00,0x0F,0x00,0xF0,0x00,0x80,0x0F,0x00,0xF0,0x00,
- 0x80,0x1D,0x00,0xF0,0x00,0xC0,0x19,0x00,0xF0,0x00,0xC0,0x18,0x00,0xF0,0x00,0xC0,0x30,0x00,
- 0xF0,0x00,0xC0,0x70,0x00,0xF0,0x00,0xC0,0xE0,0x00,0xF0,0x00,0x80,0xC1,0x01,0xF0,0x00,0x80,
- 0x81,0x03,0xF0,0x00,0x80,0x07,0x1F,0xF0,0x00,0xC0,0x3F,0xFC,0xF3,0x00,0xE0,0x7F,0xF0,0xF3,
- 0x00,0x70,0xE0,0xE0,0xF1,0x00,0x38,0xC0,0xF9,0xF0,0x00,0x18,0x80,0x3F,0xF0,0x00,0x1E,0x80,
- 0x0F,0xF0,0xC0,0x0F,0x00,0x03,0xF0,0xE0,0x0F,0x00,0x03,0xF0,0x70,0x00,0x00,0x07,0xF0,0x30,
- 0x00,0x00,0x7F,0xF0,0x30,0x00,0x00,0xFE,0xF0,0x30,0x00,0x00,0xC0,0xF0,0x30,0x00,0x00,0xC0,
- 0xF0,0x70,0x00,0x00,0xC0,0xF0,0xE0,0xFF,0xFF,0xFF,0xF0,0xC0,0xFF,0xFF,0x7F,0xF0,0x00,0xFE,
- 0xFF,0x1F,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xF0}
- };
- //天气图标列表[阴,雪,雨,多云,晴,雾霾,夜晚晴,夜晚多云]
- void weatherData(const String & data){ //更新天气数据
- DynamicJsonDocument jsonBuffer(256);
- //BLINKER_LOG("weather: ", data);
- if(data[0]=='{'){
- //BLINKER_LOG("成功获取天气信息!");
- first_label=false;
- DeserializationError error = deserializeJson(jsonBuffer, data);
- JsonObject doc = jsonBuffer.as<JsonObject>();
- //Serial.print("sizeof(doc):");
- //Serial.println(sizeof(doc));
- if(error!= NULL){ //天气获取失败
- //BLINKER_LOG("weatherData_error!");
- delay(60000);
- return;
- }
- else{
- //BLINKER_LOG("分解天气信息");
- //doc["city"] //城市或地区
- //doc["province"] //
- strcpy(w_weather,doc["weather"]); //天气现象 "阴"
- strcpy(w_temp,doc["temp"]); //温度 "24"
- strcat(w_temp," °C");
- //doc["humi"] //湿度 "86"
- //doc["wind"] //风向 "北"
- //doc["power"] //风力 "≤3"
- //doc["updateTime"]: //更新时间 "2020-06-21 18:26:00"
- //天气图标列表[阴,雪,雨,多云,晴,雾霾,夜晚晴,夜晚多云]
- //BLINKER_LOG("分解天气信息2");
- if((ch=strstr(w_weather,"雪"))!=NULL){
- weather_code=1;
- }
- else if((ch=strstr(w_weather,"雨"))!=NULL){
- weather_code=2;
- }
- else if((ch=strstr(w_weather,"阴"))!=NULL){
- weather_code=0;
- }
- else if((ch=strstr(w_weather,"云"))!=NULL){
- if (Blinker.hour()<18){
- weather_code=3;
- }
- else{
- weather_code=7;
- }
- }
- else if((ch=strstr(w_weather,"晴"))!=NULL){
- if (Blinker.hour()<18){
- weather_code=4;
- }
- else{
- weather_code=6;
- }
- }
- else if(((ch=strstr(w_weather,"雾"))!=NULL)or((ch=strstr(w_weather,"霾"))!=NULL)){
- weather_code=5;
- }
- }
- }
- else{
- //BLINKER_LOG("天气信息获取失败!请检查地址变量w_addr[]=",w_addr,",或获取天气数据次数用尽!");
- //BLINKER_LOG("1分钟后重试");
- delay(60000);
- }
- }
- void airData(const String & data){ //更新空气数据
- //BLINKER_LOG("AQI: ", data);
- if(data[0]=='{'){
- DynamicJsonDocument jsonBuffer(512);
- DeserializationError error = deserializeJson(jsonBuffer, data);
- JsonObject doc = jsonBuffer.as<JsonObject>();
- if(error != NULL){ //天气获取失败
- //BLINKER_LOG("airData_error!");
- //Serial.println(error.c_str());
- delay(1000);
- //return;
- }
- else{
- strcpy(aqi_level,doc["level"]);
- //Serial.println(strlen(doc["pm25"]));
- if(strlen(doc["pm25"])<2){
- strcpy(aqi_pm25," ");
- }
- strcat(aqi_pm25,doc["pm25"]);
- strcpy(aqi_pm10,doc["pm10"]);
- //Serial.println(strlen(doc["SO2"]));
- if(strlen(doc["SO2"])<2){
- strcpy(aqi_SO2," ");
- }
- strcat(aqi_SO2,doc["SO2"]);
- strcpy(aqi_NO2,doc["NO2"]);
- //Serial.println(strlen(doc["CO"]));
- if(strlen(doc["CO"])<2){
- strcpy(aqi_CO," ");
- }
- strcat(aqi_CO,doc["CO"]);
- strcpy(aqi_O3,doc["O3"]);
- //BLINKER_LOG("成功获取空气数据!");
- //BLINKER_LOG("aqi_level=",aqi_level);
- //BLINKER_LOG("aqi_pm25=",aqi_pm25);
- //BLINKER_LOG("aqi_pm10=",aqi_pm10);
- //BLINKER_LOG("aqi_SO2=",aqi_SO2);
- //BLINKER_LOG("aqi_NO2=",aqi_NO2);
- //BLINKER_LOG("aqi_CO=",aqi_CO);
- //BLINKER_LOG("aqi_O3=",aqi_O3);
- }
- }
- }
- void dis_air_weather(){ //显示空气质量数据
- char str_temp[26]="";
- do {
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_wqy12_t_gb2312a); // 汉字 14*26
- u8g2.setCursor(6, 15);
- strcpy(str_temp,"空气质量: ");
- strcat(str_temp,aqi_level);
- u8g2.print(str_temp);
- //BLINKER_LOG(str_temp);
-
- u8g2.setCursor(6, 31);
- strcpy(str_temp,"pm2.5: ");
- strcat(str_temp,aqi_pm25);
- //strcat(str_temp, "μg/m³");
- strcat(str_temp," pm10: ");
- strcat(str_temp,aqi_pm10);
- // strcat(str_temp, "μg/m³");
- u8g2.print(str_temp);
- //BLINKER_LOG(str_temp);
-
- u8g2.setCursor(5, 47);
- strcpy(str_temp,"SO2 : ");
- strcat(str_temp,aqi_SO2);
- //strcat(str_temp, "μg/m³");
- strcat(str_temp," NO2 : ");
- strcat(str_temp,aqi_NO2);
- //strcat(str_temp, "μg/m³");
- u8g2.print(str_temp);
- //BLINKER_LOG(str_temp);
-
- u8g2.setCursor(5, 63);
- strcpy(str_temp,"CO : ");
- strcat(str_temp,aqi_CO);
- //strcat(str_temp, "mg/m³");
- strcat(str_temp," O3 : ");
- strcat(str_temp,aqi_O3);
- //strcat(str_temp, "μg/m³");
- u8g2.print(str_temp);
- //BLINKER_LOG(str_temp);
- } while ( u8g2.nextPage() );
- }
- void dis_Time_weather(){ //显示时间及天气信息
- //BLINKER_LOG("dis_Time_weather......");
- char str_date[10]="";
- char str_big_Time[7]="";
- char str_temp[6]="";
- //BLINKER_LOG("format_str");
- do {
- u8g2.clearBuffer();
- itoa(num_hour,str_temp,10); //时分
- if(num_hour<10){
- strcat(str_big_Time,"0");
- }
- strcat(str_big_Time,str_temp);
- itoa(num_minute,str_temp,10);
- strcat(str_big_Time,":");
- if(num_minute<10){
- strcat(str_big_Time,"0");
- }
- strcat(str_big_Time,str_temp);
- u8g2.setFont(u8g2_font_logisoso28_tf ); // 大字 14*26
- u8g2.setCursor(0, 34);
- u8g2.print(str_big_Time);
-
- //BLINKER_LOG("时间:",str_big_Time);
- if(num_year<10){
- strcat(str_date,"0");
- }
- itoa(num_year,str_temp,10); //日期
- strcat(str_date,str_temp);
- itoa(num_month,str_temp,10);
- strcat(str_date,"-");
- if(num_month<10){
- strcat(str_date,"0");
- }
- strcat(str_date,str_temp);
- itoa(num_mday,str_temp,10);
- strcat(str_date,"-");
- if(num_mday<10){
- strcat(str_date,"0");
- }
- strcat(str_date,str_temp);
- strcat(str_date,"星期");
- switch(Blinker.wday()){
- case 0:
- strcat(str_date,"日");
- break;
- case 1:
- strcat(str_date,"一");
- break;
- case 2:
- strcat(str_date,"二");
- break;
- case 3:
- strcat(str_date,"三");
- break;
- case 4:
- strcat(str_date,"四");
- break;
- case 5:
- strcat(str_date,"五");
- break;
- case 6:
- strcat(str_date,"六");
- break;
- }
- u8g2.setFont(u8g2_font_wqy12_t_gb2312a); // 汉字 14*26
- u8g2.setCursor(0, 54);
- u8g2.print(str_date);
- //BLINKER_LOG("日期:",str_date);
- u8g2.setCursor(96, 54);
- u8g2.print(w_temp);
- u8g2.drawLine(88, 2, 88, 61); //分割线
- //BLINKER_LOG("分割线:");
- u8g2.drawXBM(92,2,36,36,weather_icon[weather_code]); //天气图标
- //BLINKER_LOG("天气图标:",weather_code);
- } while ( u8g2.nextPage() );
- }
- void dataRead(const String & data){
- //BLINKER_LOG("Blinker readString: ", data);
- uint32_t BlinkerTime = millis();
- Blinker.vibrate();
- Blinker.print("millis", BlinkerTime);
- digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- Blinker.aqi(w_addr);
- delay(10000);
- }
- void setup(){
- Serial.begin(57600);
- BLINKER_DEBUG.stream(Serial);
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachData(dataRead);
- Blinker.attachWeather(weatherData);
- Blinker.attachAir(airData);
- Blinker.setTimezone(8.0);
- u8g2.begin(); //选择U8G2模式,或者U8X8模式
- u8g2.enableUTF8Print(); // UTF8 输出(汉字显示不是很好,勉强够用)
- first_label=true;
- do {
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_wqy12_t_gb2312a); // 汉字 14*26
- u8g2.setCursor(36, 38);
- u8g2.print("连接WIFI...");
- } while ( u8g2.nextPage() );
- }
- void loop(){
- Blinker.run();
- if((first_label==true)){
- delay(1000);
- Blinker.run();
- delay(1000);
- if (Blinker.init()){ //开机第一次更新天气信息
- delay(1000);
- //BLINKER_LOG("开机第一次更新天气信息,w_addr=",w_addr);
- num_second=Blinker.second();
- num_minute=Blinker.minute();
- num_hour=Blinker.hour();
- num_year=Blinker.year()%100;
- num_month=Blinker.month();
- num_mday=Blinker.mday();
- dis_Time_weather(); //先显示时间,再获取天气信息(获取天气时间相对比较长)
-
- Blinker.weather(w_addr);
- //BLINKER_LOG("请求天气数据完毕,first_label=",first_label);
- delay(1000);
- Blinker.aqi(w_addr);
- //BLINKER_LOG("请求空气数据完毕");
- dis_Time_weather(); //联网成功先显示1秒时间
- delay(1000);
- dis_air_weather(); //显示1秒空气信息
- delay(1000);
- }
- }
- else{
- num_second=Blinker.second();
- //BLINKER_LOG("正常工作,num_second=",num_second);
- if(num_second==0){
- num_minute=Blinker.minute();
- dis_Time_weather(); //每分钟更新一次时间
- if(num_minute==0){
- num_hour=Blinker.hour();
- Blinker.weather(w_addr); //每小时(整点)更新一次天气信息
- if(num_hour=0){
- num_year=Blinker.year()%100; //每天(零点)更新日期数据
- num_month=Blinker.month();
- num_mday=Blinker.mday();
- }
- }
- if(num_minute==30){
- Blinker.aqi(w_addr);//每小时(半点)更新一次天气信息
- }
- }
- if(num_second==50){
- dis_air_weather(); //显示空气信息
- }
- delay(1000);
- }
- }
复制代码
--------2020-07-05更新-----最近工作太忙,赶上周日把之前的问题同意处理一下,问题主要出现在API访问次数限制上,由于调试反复重启,所以每天50次限制马上就使用没了,所以会获取不到天气信息。之前的程序没考虑那么多,原来是获取到天气信息后才显示时间天气画面所以会卡开机动画。新程序开机动画被删除了,增加空气质量数据画面,dis_air_weather(),看大家喜欢用哪个自己选吧。开机动画void dis_refresh()我放在附件里.目前还有点问题,就是刚开机会重启几次才能正常工作,目前还没找到问题,由于白天还得上班,大家谁有解决办法分享一下万分感谢
|