物联网现在是方兴未艾啊,很多单片机均具备了物联网开发的基本功能,选择最快捷的开发方式可以缩短研发周期,降低研发成本,下面介绍ESP8266+Arduino的开发方式
一、环境搭建(这块最难,但是这里不细讲了,原因和人脸识别部分一样,环境搭建过程就是熟悉的过程,很重要,自己动手要比别人给有收获,有意义)
https://www.arduino.cn/forum.php?mod=viewthread&tid=81948
二、要读源代码,做到读懂会用啊。
(上面是从知心天气网站获取并且解析天气信息的方法。)
#include <WiFiUdp.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
// pins
const int8_t RST_PIN = D2;
const int8_t CE_PIN = D1;
const int8_t DC_PIN = D6;
//const int8_t DIN_PIN = D7; // Uncomment for Software SPI
//const int8_t CLK_PIN = D5; // Uncomment for Software SPI
const int8_t BL_PIN = D0;
//const char* AP_SSID = "LAFALINK-2.4G"; // XXXXXX -- 使用时请修改为当前你的 wifi ssid
//const char* AP_SSID = "huaxiangmianzhou";
const char* AP_SSID = "Motel168-506";
const char* AP_PSK = "leesky2008"; // XXXXXX -- 使用时请修改为当前你的 wifi 密码
Adafruit_PCD8544 display = Adafruit_PCD8544(DC_PIN, CE_PIN, RST_PIN);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
static const unsigned char PROGMEM tutu[] =
{
0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,
0xC0,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xF6,0x3F,0x70,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0x88,0x3F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,
0x91,0xFF,0xD8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xEF,0xF2,0x7D,0xDF,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xFF,0xE4,0xFF,0xBF,0x80,0x00,0x00,0x00,0x00,0x00,0x07,
0xFF,0xC9,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0x7F,0x60,
0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFD,0x01,0xFF,0xB0,0x00,0x00,0x00,0x00,0x00,
0x3F,0xFF,0xF8,0x03,0xFF,0x18,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFB,0xFC,0xFE,
0xE8,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xED,0x36,0xEC,0x00,0x00,0x00,0x00,
0x00,0xFF,0xFF,0xFF,0x87,0x36,0xE4,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x02,
0xB2,0x44,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFE,0x00,0x12,0x46,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFF,0xFC,0x00,0x4A,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xD8,
0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x03,0x02,0x00,0x00,
0x00,0x00,0x00,0xFF,0xFE,0x00,0x00,0x01,0x82,0x00,0x00,0x00,0x00,0x00,0xFF,0xE0,
0x00,0x00,0x00,0x82,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xC4,0x00,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x74,0x00,0x00,0x00,0x00,0x00,0x7C,
0x00,0x00,0x3F,0xE0,0x34,0x00,0x00,0x00,0x00,0x00,0x7C,0x07,0xC0,0x00,0x00,0x24,
0x00,0x00,0x00,0x00,0x00,0xBC,0x1F,0xC0,0x00,0x00,0x12,0x00,0x00,0x00,0x00,0x00,
0x3C,0x38,0x00,0x03,0xA0,0x14,0x80,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x07,0xC0,
0x10,0x00,0x00,0x00,0x00,0x00,0x18,0x01,0x80,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x3C,0x1F,0xC0,0x00,0x00,0x10,0x20,0x00,0x00,0x00,0x00,0x46,0x1F,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x00,0x06,0x00,0x00,0x10,0x80,0x00,0x00,
0x00,0x00,0x7E,0x00,0x06,0x04,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x3A,0x00,0x04,
0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1B,0x00,0x04,0x03,0x80,0x18,0x00,0x00,
0x00,0x00,0x00,0x09,0x00,0x1C,0x30,0xC0,0x10,0x00,0x00,0x00,0x00,0x00,0x0D,0x00,
0x3F,0xE0,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x1F,0x80,0x60,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0xF0,0x20,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0xFE,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x1F,0x80,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xB0,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x77,0xD8,0x00,0x00,0x10,
0xE0,0x00,0x00,0x00,0x00,0x60,0x0F,0xFE,0x00,0x00,0x20,0x61,0x20,0x00,0x00,0x00,
0x00,0x0E,0x7B,0xE0,0x00,0x80,0x00,0x02,0x00,0x00,0x00,0x00,0x3C,0x1C,0xFC,0x06,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x9E,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,
};/*"中",0*/
static const unsigned char PROGMEM str1[] =
{
0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06
};/*"中",0*/
static const unsigned char PROGMEM str2[] =
{
0x02,0x08,0x21,0x10,0x10,0x00,0x17,0xFC,0x00,0x80,0x03,0xF8,0xF2,0x08,0x13,0xF8,
0x12,0x08,0x13,0xF8,0x12,0x08,0x13,0xF8,0x12,0x08,0x28,0x00,0x47,0xFE,0x00,0x00
};/*"文",1*/
static const unsigned char PROGMEM str3[] =
{
0x00,0x04,0xFE,0x44,0x28,0x54,0x28,0x54,0xFE,0x54,0xAA,0x54,0xAA,0xDC,0xAB,0x74,
0xAE,0x54,0xC2,0x54,0x82,0x54,0xFE,0x54,0x82,0x54,0x82,0x54,0xFE,0x84,0x83,0x04
};/*"显",2*/
static const unsigned char PROGMEM str4[] =
{
0x22,0x20,0x22,0x20,0xFF,0xA0,0x22,0x20,0x3E,0xFC,0x08,0x24,0x7F,0x24,0x49,0x24,
0x7F,0x24,0x08,0x24,0x7F,0x24,0x08,0x44,0x7F,0x44,0x08,0x84,0x0F,0x28,0xF2,0x10
};/*"示",3*/
static const unsigned char PROGMEM hu[] =
{
0x00,0x08,0x00,0x3C,0x7B,0xE0,0x48,0x20,0x49,0x24,0x48,0xA4,0x48,0xA8,0x48,0x20,
0x4B,0xFE,0x48,0x20,0x78,0x20,0x48,0x20,0x00,0x20,0x00,0x20,0x00,0xA0,0x00,0x40
};/*"示",3*/
static const unsigned char PROGMEM he[] =
{
0x04,0x00,0x0E,0x00,0x78,0x00,0x08,0x7C,0x08,0x44,0xFF,0x44,0x08,0x44,0x18,0x44,
0x1C,0x44,0x2A,0x44,0x2A,0x44,0x48,0x44,0x88,0x7C,0x08,0x44,0x08,0x00,0x08,0x00
};/*"示",3*/
static const unsigned char PROGMEM hao[] =
{
0x00,0x40,0x22,0x40,0x12,0x40,0x13,0xFC,0x84,0x40,0x48,0x40,0x40,0x40,0x17,0xFE,
0x10,0x00,0x20,0x00,0xE3,0xF8,0x22,0x08,0x22,0x08,0x22,0x08,0x23,0xF8,0x02,0x08
};/*"示",3*/
static const unsigned char PROGMEM te[] =
{
0x10,0x20,0x10,0x20,0x50,0x20,0x51,0xFC,0x7C,0x20,0x50,0x20,0x93,0xFE,0x10,0x08,
0x1C,0x08,0xF1,0xFE,0x50,0x08,0x10,0x88,0x10,0x48,0x10,0x08,0x10,0x28,0x10,0x10
};/*"示",3*/
static const unsigned char PROGMEM feng[] =
{0x00,0x00,0x3F,0xF0,0x20,0x10,0x20,0x10,0x28,0x50,0x24,0x50,0x22,0x90,0x22,0x90,
0x21,0x10,0x21,0x10,0x22,0x90,0x22,0x92,0x24,0x4A,0x48,0x4A,0x40,0x06,0x80,0x02};/*"风",0*/
static const unsigned char PROGMEM li[] =
{
0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x7F,0xF8,0x02,0x08,0x02,0x08,0x02,0x08,
0x02,0x08,0x04,0x08,0x04,0x08,0x08,0x08,0x08,0x08,0x10,0x88,0x20,0x50,0x40,0x20
};
static const unsigned char PROGMEM ji[] =
{
0x10,0x00,0x13,0xFC,0x20,0x84,0x20,0x88,0x48,0x88,0xF8,0x90,0x10,0x9C,0x20,0x84,
0x41,0x44,0xF9,0x44,0x41,0x28,0x01,0x28,0x1A,0x10,0xE2,0x28,0x44,0x44,0x01,0x82
};/*"示",3*/
static const unsigned char PROGMEM qi[] =
{
0x10,0x00,0x10,0x00,0x3F,0xFC,0x20,0x00,0x4F,0xF0,0x80,0x00,0x3F,0xF0,0x00,0x10,
0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x0A,0x00,0x0A,0x00,0x06,0x00,0x02
};/*"示",3*/
static const unsigned char PROGMEM wen[] =
{
0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00
};/*"示",3*/
static const unsigned char PROGMEM shi[] =
{
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04
};/*"示",3*/
static const unsigned char PROGMEM si[] =
{
0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,
0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10
};/*"示",3*/
static const unsigned char PROGMEM jin[] =
{
0x01,0x00,0x01,0x00,0x02,0x80,0x04,0x40,0x08,0x20,0x12,0x10,0x21,0x08,0xC1,0x06,
0x00,0x00,0x1F,0xF0,0x00,0x10,0x00,0x20,0x00,0x20,0x00,0x40,0x00,0x80,0x01,0x00
};/*"示",3*/
static const unsigned char PROGMEM ming[] =
{
0x00,0x00,0x00,0xFC,0x7C,0x84,0x44,0x84,0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,0x01,0x04,0x01,0x04,0x02,0x14,0x04,0x08
};/*"示",3*/
static const unsigned char PROGMEM ri[] =
{
0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10
};/*"示",3*/
static const unsigned char PROGMEM shesi[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x02,0x00,0x00,0x00,0x0D,0x80,0xFC,0x00,0x08,0x83,0xC3,0x00,0x08,0x86,0x00,0xE0,
0x08,0x8C,0x00,0x60,0x07,0x18,0x00,0x20,0x00,0x18,0x00,0x20,0x00,0x30,0x00,0x20,
0x00,0x30,0x00,0x20,0x00,0x30,0x00,0x20,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x00,
0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x00,
0x00,0x30,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x1C,0x00,0x20,
0x00,0x0E,0x00,0x40,0x00,0x07,0x01,0x80,0x00,0x03,0xFF,0x00,0x00,0x00,0xF8,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
WiFiClient client; //创建一个网络对象
/* 请求的Json数据格式如下:
* {
* "results": [
* {
* "location": {
* "id": "WX4FBXXFKE4F",
* "name": "北京",
* "country": "CN",
* "path": "北京,北京,中国",
* "timezone": "Asia/Shanghai",
* "timezone_offset": "+08:00"
* },
* "now": {
* "text": "多云",
* "code": "4",
* "temperature": "23"
* },
* "last_update": "2019-10-13T09:51:00+08:00"
* }
* ]
*}
*/
void wifi_start_connect() //连接WIFI
{
WiFi.mode(WIFI_STA);//设置esp8266 工作模式
//写几句提示
WiFi.begin(AP_SSID, AP_PSK); //连接wifi
WiFi.setAutoConnect(true);
while (WiFi.status()!= WL_CONNECTED) //这个函数是wifi连接状态,返回wifi链接状态
{
delay(500);
Serial.print(".");
}
}
void parseUserData(String content) // Json数据解析并串口打印.可参考https://www.bilibili.com/video/av65322772
{
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 3*JSON_OBJECT_SIZE(14) + 800;
DynamicJsonBuffer jsonBuffer(capacity);
//const char* json = "[{\"location\":{\"id\":\"WRR2Q2Z7CXWM\",\"name\":\"呼和浩特\",\"country\":\"CN\",\"path\":\"呼和浩特,呼和浩特,内蒙古,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"daily\":[{\"date\":\"2020-03-16\",\"text_day\":\"晴\",\"code_day\":\"0\",\"text_night\":\"晴\",\"code_night\":\"1\",\"high\":\"16\",\"low\":\"-1\",\"rainfall\":\"0.0\",\"precip\":\"\",\"wind_direction\":\"西北\",\"wind_direction_degree\":\"315\",\"wind_speed\":\"25.20\",\"wind_scale\":\"4\",\"humidity\":\"17\"},{\"date\":\"2020-03-17\",\"text_day\":\"晴\",\"code_day\":\"0\",\"text_night\":\"晴\",\"code_night\":\"1\",\"high\":\"16\",\"low\":\"-1\",\"rainfall\":\"0.0\",\"precip\":\"\",\"wind_direction\":\"西\",\"wind_direction_degree\":\"248\",\"wind_speed\":\"25.20\",\"wind_scale\":\"4\",\"humidity\":\"16\"},{\"date\":\"2020-03-18\",\"text_day\":\"多云\",\"code_day\":\"4\",\"text_night\":\"晴\",\"code_night\":\"1\",\"high\":\"14\",\"low\":\"-4\",\"rainfall\":\"0.0\",\"precip\":\"\",\"wind_direction\":\"北\",\"wind_direction_degree\":\"0\",\"wind_speed\":\"34.20\",\"wind_scale\":\"5\",\"humidity\":\"20\"}],\"last_update\":\"2020-03-16T11:17:52+08:00\"}]";
JsonObject& root = jsonBuffer.parseObject(content);
JsonObject& root_0 = root["results"][0];
JsonObject& root_0_location = root_0["location"];
const char* root_0_location_id = root_0_location["id"]; // "WRR2Q2Z7CXWM"
const char* root_0_location_name = root_0_location["name"]; // "呼和浩特"
//const char* root_0_location_country = root_0_location["country"]; // "CN"
//const char* root_0_location_path = root_0_location["path"]; // "呼和浩特,呼和浩特,内蒙古,中国"
//const char* root_0_location_timezone = root_0_location["timezone"]; // "Asia/Shanghai"
//const char* root_0_location_timezone_offset = root_0_location["timezone_offset"]; // "+08:00"
JsonArray& root_0_daily = root_0["daily"];
//JsonObject& root_0_daily = root["results"][1];
JsonObject& root_0_daily_0 = root_0_daily[0];
const char* root_0_daily_0_date = root_0_daily_0["date"]; // "2020-03-16"
const char* root_0_daily_0_text_day = root_0_daily_0["text_day"]; // "晴"
const char* root_0_daily_0_code_day = root_0_daily_0["code_day"]; // "0"
const char* root_0_daily_0_text_night = root_0_daily_0["text_night"]; // "晴"
const char* root_0_daily_0_code_night = root_0_daily_0["code_night"]; // "1"
const char* root_0_daily_0_high = root_0_daily_0["high"]; // "16"最高温度
const char* root_0_daily_0_low = root_0_daily_0["low"]; // "-1"最低温度
const char* root_0_daily_0_rainfall = root_0_daily_0["rainfall"]; // "0.0"
const char* root_0_daily_0_precip = root_0_daily_0["precip"]; // ""
const char* root_0_daily_0_wind_direction = root_0_daily_0["wind_direction"]; // "西北"风向
const char* root_0_daily_0_wind_direction_degree = root_0_daily_0["wind_direction_degree"]; // "315"
const char* root_0_daily_0_wind_speed = root_0_daily_0["wind_speed"]; // "25.20"
const char* root_0_daily_0_wind_scale = root_0_daily_0["wind_scale"]; // "4"等级
const char* root_0_daily_0_humidity = root_0_daily_0["humidity"]; // "17"
//JsonObject& root_0_daily1 = root["results"][2];
JsonObject& root_0_daily_1 = root_0_daily[1];
const char* root_0_daily_1_date = root_0_daily_1["date"]; // "2020-03-17"
const char* root_0_daily_1_text_day = root_0_daily_1["text_day"]; // "晴"
const char* root_0_daily_1_code_day = root_0_daily_1["code_day"]; // "0"
const char* root_0_daily_1_text_night = root_0_daily_1["text_night"]; // "晴"
const char* root_0_daily_1_code_night = root_0_daily_1["code_night"]; // "1"
const char* root_0_daily_1_high = root_0_daily_1["high"]; // "16"
const char* root_0_daily_1_low = root_0_daily_1["low"]; // "-1"
const char* root_0_daily_1_rainfall = root_0_daily_1["rainfall"]; // "0.0"
const char* root_0_daily_1_precip = root_0_daily_1["precip"]; // ""
const char* root_0_daily_1_wind_direction = root_0_daily_1["wind_direction"]; // "西"
const char* root_0_daily_1_wind_direction_degree = root_0_daily_1["wind_direction_degree"]; // "248"
const char* root_0_daily_1_wind_speed = root_0_daily_1["wind_speed"]; // "25.20"
const char* root_0_daily_1_wind_scale = root_0_daily_1["wind_scale"]; // "4"
const char* root_0_daily_1_humidity = root_0_daily_1["humidity"]; // "16"
Serial.println(root_0_location_name); //呼和浩特 //通过串口打印出需要的信息
Serial.println(root_0_daily_0_date);//当天日期
// display.setCursor(5,30);
// display.println(root_0_daily_0_date);
// display.display();
Serial.println(root_0_daily_0_text_day);//天气情况
Serial.println(root_0_daily_0_code_day);
Serial.println(root_0_daily_0_high);//最高温度
Serial.println(root_0_daily_0_low);//最低温度
Serial.println(root_0_daily_0_wind_direction);//风向
Serial.println(root_0_daily_0_wind_scale);//风力等级
display.drawBitmap(10, 0,jin, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,0, ri, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(44, 0,qi, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(61,0, wen, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(10,32, feng, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,32, li, 16, 16, 1); //画出字符对应点阵数据
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(10,17);
display.println(root_0_daily_0_low);
display.setCursor(28,17);
display.println("--");
display.setCursor(50,17);
display.println(root_0_daily_0_high);
display.setCursor(45,33);
display.println(root_0_daily_0_wind_scale);
display.display();
delay(2000),
display.clearDisplay();
display.display();
//显示明天的天气情况
Serial.println(root_0_location_name); //呼和浩特 //通过串口打印出需要的信息
Serial.println(root_0_daily_1_date);//明天日期
Serial.println(root_0_daily_1_text_day);//天气情况
Serial.println(root_0_location_name);
Serial.println(root_0_daily_1_high);//最高温度
Serial.println(root_0_daily_1_low);//最低温度
Serial.println(root_0_daily_1_wind_direction);//风向
Serial.println(root_0_daily_1_wind_scale);//风力等级
display.drawBitmap(10, 0,ming, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,0, ri, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(44, 0,qi, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(61,0, wen, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(10,32, feng, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,32, li, 16, 16, 1); //画出字符对应点阵数据
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(10,17);
display.println(root_0_daily_1_low);
display.setCursor(28,17);
display.println("--");
display.setCursor(50,17);
display.println(root_0_daily_1_high);
display.setCursor(45,33);
display.println(root_0_daily_1_wind_scale);
display.display();
delay(2000);
display.clearDisplay();
display.display();
// Serial.print("\r\n");
}
void parseUserData1(String content1) // Json数据解析并串口打印.可参考https://www.bilibili.com/video/av65322772
{
const size_t capacity = JSON_ARRAY_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 210;
DynamicJsonBuffer jsonBuffer(capacity);
//const char* json = "[{\"location\":{\"id\":\"WRR2Q2Z7CXWM\",\"name\":\"呼和浩特\",\"country\":\"CN\",\"path\":\"呼和浩特,呼和浩特,内蒙古,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"now\":{\"text\":\"晴\",\"code\":\"0\",\"temperature\":\"16\"},\"last_update\":\"2020-03-16T14:50:00+08:00\"}]";
JsonObject& root = jsonBuffer.parseObject(content1);
JsonObject& root_0 = root["results"][0];
JsonObject& root_0_location = root_0["location"];
const char* root_0_location_id = root_0_location["id"]; // "WRR2Q2Z7CXWM"
const char* root_0_location_name = root_0_location["name"]; // "呼和浩特"
//const char* root_0_location_country = root_0_location["country"]; // "CN"
//const char* root_0_location_path = root_0_location["path"]; // "呼和浩特,呼和浩特,内蒙古,中国"
//const char* root_0_location_timezone = root_0_location["timezone"]; // "Asia/Shanghai"
//const char* root_0_location_timezone_offset = root_0_location["timezone_offset"]; // "+08:00"
JsonObject& root_0_now = root_0["now"];
const char* root_0_now_text = root_0_now["text"]; // "晴"
const char* root_0_now_code = root_0_now["code"]; // "0"
const char* root_0_now_temperature = root_0_now["temperature"]; // "16"
const char* root_0_last_update = root_0["last_update"]; // "2020-03-16T14:50:00+08:00"
Serial.println(root_0_location_name); //呼和浩特 //通过串口打印出需要的信息
Serial.println(root_0_now_text);//天气情况
Serial.println(root_0_now_code);//天气情况代码
Serial.println(root_0_now_temperature);//实时温度
Serial.print("\r\n");
display.drawBitmap(10,0,hu, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,0, he, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(44,0, hao, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(61,0, te, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(10,20,shi, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,20, si, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(44,20, qi, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(61,20, wen, 16, 16, 1); //画出字符对应点阵数据
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(BLACK);
display.setCursor(10,15);
display.println(root_0_now_temperature);
display.drawBitmap(52,8, shesi, 32, 32, 1); //画出shesi
display.display();
delay(1000);
display.clearDisplay();
display.display();
}
void setup()
{
Serial.begin(9600);
wifi_start_connect();
client.setTimeout(3000);//设置服务器连接超时时间
//timeClient.begin();
pinMode(BL_PIN, OUTPUT);
digitalWrite(BL_PIN, HIGH);
display.begin();
display.setContrast(60); // Adjust for your displa
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(10,15);
display.println("Connecting to ");
display.clearDisplay();
display.display();
// display.drawBitmap(10, 10, str1, 16, 16, 1); //画出字符对应点阵数据
// display.drawBitmap(27,10, str2, 16, 16, 1); //画出字符对应点阵数据
// display.drawBitmap(44,10, str3, 16, 16, 1); //画出字符对应点阵数据
// display.drawBitmap(61,10, str4, 16, 16, 1); //画出字符对应点阵数据
// display.setCursor(5,30);
// display.println("Welcome!Mr Li");
// display.display();
// delay(1000),
// display.clearDisplay();
// display.display();
// display.drawBitmap(0, 0, tutu, 84, 48, 1);
// display.display();
// delay(1000),
// display.clearDisplay();
// display.display();
}
void xianshishijian()
{
timeClient.begin();
timeClient.update();
delay(500);
timeClient.getHours();
timeClient.getHours();
timeClient.getMinutes();
display.setTextSize(3);
display.setTextColor(BLACK);
display.setCursor(5,20);
display.println(timeClient.getHours()+8);
display.setCursor(33,20);
display.println(":");
display.setCursor(45,20);
if(timeClient.getMinutes()<10)
display.println(String(0)+timeClient.getMinutes());
else
display.println('0'+timeClient.getMinutes());
display.display();
delay(2000);
display.clearDisplay();
}
void kaiji()
{
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.drawBitmap(10, 10, str1, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(27,10, str2, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(44,10, str3, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(61,10, str4, 16, 16, 1); //画出字符对应点阵数据
display.setCursor(5,30);
display.println("Welcome!Mr Li");
display.display();
delay(1000);
display.clearDisplay();
display.display();
display.drawBitmap(0, 0, tutu, 84, 48, 1);
display.display();
delay(1000);
display.clearDisplay();
display.display();
}
void loop()
{
kaiji();
xianshishijian();
if(client.connect("api.seniverse.com", 80)==1) //连接服务器并判断是否连接成功,若成功就发送GET 请求数据下发
{ //换成你自己在心知天气申请的私钥//改成你所在城市的拼音
client.print("GET /v3/weather/daily.json?key=SbVrpI0gcYEBgHnd0&location=huhehaote&language=zh-Hans&unit=c&start=0&days=5 HTTP/1.1\r\n"); //心知天气的URL格式
client.print("Host:api.seniverse.com\r\n");
client.print("Accept-Language:zh-cn\r\n");
client.print("Connection:close\r\n\r\n"); //向心知天气的服务器发送请求。
String status_code = client.readStringUntil('\r'); //读取GET数据,服务器返回的状态码,若成功则返回状态码200
// Serial.println(status_code);
if(client.find("\r\n\r\n")==1) //跳过返回的数据头,直接读取后面的JSON数据,
{
String json_from_server=client.readStringUntil('\n'); //读取返回的JSON数据
//Serial.println("这是预报数据:");
//Serial.println(json_from_server);
parseUserData(json_from_server);//将读取的JSON数据,传送到JSON解析函数中进行显示。
}
// client.stop();
delay(500);
client.connect("api.seniverse.com", 80);
client.print("GET /v3/weather/now.json?key=SIwg7qqEKb2AEDb9y&location=huhehaote&language=zh-Hans&unit=c HTTP/1.1\r\n"); //心知天气的URL格式
client.print("Host:api.seniverse.com\r\n");
client.print("Accept-Language:zh-cn\r\n");
client.print("Connection:close\r\n\r\n"); //向心知天气的服务器发送请求。
String status_code1 = client.readStringUntil('\r'); //读取GET数据,服务器返回的状态码,若成功则返回状态码200
//Serial.println("lixing");
if(client.find("\r\n\r\n")==1) //跳过返回的数据头,直接读取后面的JSON数据,
{
String json_from_server1=client.readStringUntil('\n'); //读取返回的JSON数据
//Serial.println("这是实时数据");
//Serial.println(json_from_server1);
parseUserData1(json_from_server1);//将读取的JSON数据,传送到JSON解析函数中进行显示。
}
}
else
{
//Serial.println("connection failed this time");
delay(500); //请求失败等5秒
}
client.stop(); //关闭HTTP客户端,采用HTTP短链接,数据请求完毕后要客户端要主动断开
//delay(1000);
}