本帖最后由 誓言1997 于 2020-8-9 23:11 编辑
本代码借鉴这两位
1.https://www.arduino.cn/forum.php ... =%E5%A4%A9%E6%B0%94
2.https://www.arduino.cn/thread-90648-1-2.html
请使用blinker新版本库 v0.3.4 以及 v2.7 以后的8266 package。
ArduinoJson6库在第二个链接后面有。
[pre]#define BLINKER_WIFI
#include <ArduinoJson.h>
#include <Blinker.h>
char auth[] = "秘钥";
char ssid[] = "wifi名称";
char pswd[] = "WiFi密码";
bool first_label=false; //开机标签,开机第一时间更新天气信息
void airData(const String & data)
{
BLINKER_LOG("air: ", data);
const size_t capacity = JSON_OBJECT_SIZE(11) + 120;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, data);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return; }
const char* city = doc["city"]; // "隆安县"
const char* province = doc["province"]; // "广西"
const char* updateTime = doc["updateTime"]; // "2020-08-03 18:11"
const char* aqi = doc["aqi"]; // "27"
const char* level = doc["level"]; // "优"
const char* pm25 = doc["pm25"]; // "8"
const char* pm10 = doc["pm10"]; // "27"
const char* SO2 = doc["SO2"]; // "4"
const char* NO2 = doc["NO2"]; // "24"
const char* CO = doc["CO"]; // "1"
const char* O3 = doc["O3"]; // "43"
Serial.print("省份:");
Serial.println(province);
Serial.print("城市:");
Serial.println(city);
Serial.print("更新时间:");
Serial.println(updateTime);
Serial.println("---------空气质量----------");
Serial.print("aqi:");
Serial.println(aqi);
Serial.print("污染程度:");
Serial.println(level);
Serial.print("pm25:");
Serial.println(pm25);
Serial.print("pm10:");
Serial.println(pm10);
Serial.print("SO2:");
Serial.println(SO2);
Serial.print("NO2:");
Serial.println(NO2);
Serial.print("CO:");
Serial.println(CO);
Serial.print("O3:");
Serial.println(O3);
}
void weatherForecastData(const String & data)
{
BLINKER_LOG("weather: ", data);
const size_t capacity = JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(4) + 4*JSON_OBJECT_SIZE(10) + 650;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, data);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return; }
const char* city = doc["city"]; // "隆安县"
const char* province = doc["province"]; // "广西"
const char* updateTime = doc["updateTime"]; // "2020-08-03 18:11"
JsonArray forecasts = doc["forecasts"];
JsonObject forecasts_0 = forecasts[0];
const char* forecasts_0_date = forecasts_0["date"]; // "2020-08-09"
const char* forecasts_0_week = forecasts_0["week"]; // "7"
const char* forecasts_0_dayweather = forecasts_0["dayweather"]; // "阴"
const char* forecasts_0_nightweather = forecasts_0["nightweather"]; // "多云"
const char* forecasts_0_daytemp = forecasts_0["daytemp"]; // "34"
const char* forecasts_0_nighttemp = forecasts_0["nighttemp"]; // "26"
const char* forecasts_0_daywind = forecasts_0["daywind"]; // "南"
const char* forecasts_0_nightwind = forecasts_0["nightwind"]; // "南"
const char* forecasts_0_daypower = forecasts_0["daypower"]; // "4"
const char* forecasts_0_nightpower = forecasts_0["nightpower"]; // "4"
JsonObject forecasts_1 = forecasts[1];
const char* forecasts_1_date = forecasts_1["date"]; // "2020-08-10"
const char* forecasts_1_week = forecasts_1["week"]; // "1"
const char* forecasts_1_dayweather = forecasts_1["dayweather"]; // "多云"
const char* forecasts_1_nightweather = forecasts_1["nightweather"]; // "多云"
const char* forecasts_1_daytemp = forecasts_1["daytemp"]; // "34"
const char* forecasts_1_nighttemp = forecasts_1["nighttemp"]; // "26"
const char* forecasts_1_daywind = forecasts_1["daywind"]; // "南"
const char* forecasts_1_nightwind = forecasts_1["nightwind"]; // "南"
const char* forecasts_1_daypower = forecasts_1["daypower"]; // "≤3"
const char* forecasts_1_nightpower = forecasts_1["nightpower"]; // "≤3"
JsonObject forecasts_2 = forecasts[2];
const char* forecasts_2_date = forecasts_2["date"]; // "2020-08-11"
const char* forecasts_2_week = forecasts_2["week"]; // "2"
const char* forecasts_2_dayweather = forecasts_2["dayweather"]; // "阵雨"
const char* forecasts_2_nightweather = forecasts_2["nightweather"]; // "多云"
const char* forecasts_2_daytemp = forecasts_2["daytemp"]; // "33"
const char* forecasts_2_nighttemp = forecasts_2["nighttemp"]; // "26"
const char* forecasts_2_daywind = forecasts_2["daywind"]; // "南"
const char* forecasts_2_nightwind = forecasts_2["nightwind"]; // "南"
const char* forecasts_2_daypower = forecasts_2["daypower"]; // "≤3"
const char* forecasts_2_nightpower = forecasts_2["nightpower"]; // "≤3"
JsonObject forecasts_3 = forecasts[3];
const char* forecasts_3_date = forecasts_3["date"]; // "2020-08-12"
const char* forecasts_3_week = forecasts_3["week"]; // "3"
const char* forecasts_3_dayweather = forecasts_3["dayweather"]; // "阵雨"
const char* forecasts_3_nightweather = forecasts_3["nightweather"]; // "多云"
const char* forecasts_3_daytemp = forecasts_3["daytemp"]; // "33"
const char* forecasts_3_nighttemp = forecasts_3["nighttemp"]; // "25"
const char* forecasts_3_daywind = forecasts_3["daywind"]; // "南"
const char* forecasts_3_nightwind = forecasts_3["nightwind"]; // "南"
const char* forecasts_3_daypower = forecasts_3["daypower"]; // "≤3"
const char* forecasts_3_nightpower = forecasts_3["nightpower"]; // "≤3"
Serial.print("省份:");
Serial.println(province);
Serial.print("城市:");
Serial.println(city);
Serial.print("更新时间:");
Serial.println(updateTime);
Serial.println("---------今天天气----------");
Serial.print("时间:");
Serial.println(forecasts_0_date);
Serial.print("星期:");
Serial.println(forecasts_0_week);
Serial.print("白天天气现象:");
Serial.println(forecasts_0_dayweather);
Serial.print("晚上天气现象:");
Serial.println(forecasts_0_nightweather);
Serial.print("白天温度:");
Serial.println(forecasts_0_daytemp);
Serial.print("晚上温度:");
Serial.println(forecasts_0_nighttemp);
Serial.print("白天风向:");
Serial.println(forecasts_0_daywind);
Serial.print("晚上风向:");
Serial.println(forecasts_0_nightwind);
Serial.print("白天风力:");
Serial.println(forecasts_0_daypower);
Serial.print("晚上风力:");
Serial.println(forecasts_0_nightpower);
}
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.weatherForecast();
Blinker.aqi();
Blinker.delay(60000);
}
void setup()
{
first_label=true;
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachAir(airData);
Blinker.attachWeatherForecast(weatherForecastData);
Blinker.delay(1000);
}
void loop()
{
Blinker.run();
Blinker.delay(1000);
if((first_label==true)){
if (Blinker.init())
{
Blinker.weatherForecast("guangxi-longanxian");//城市标签,查询地址https://diandeng.tech/doc/weather-and-air
Blinker.aqi("guangxi-longanxian");
Blinker.delay(2000);
first_label=false;
}
}
}[/pre]
实验工具
1.esp8266
实验现象
|