基于arduino的ESP8266天气预报设计
附录:代码、天气相关图片、字样等素材。
单片机源程序如下:
- #include<U8g2lib.h>
- #include<ESP8266WiFi.h>
- #include<avr/pgmspace.h>
- #include<SPI.h>
- #include<SD.h>
- #include<ArduinoJson.h>
- /*按键模拟输入的值,按键的误差范围在之后会设置为正负30*/
- #define exit_bt 930
- #define left_bt 648
- #define right_bt 393
- #define enter_bt 144
- #define null_bt 22 //不按按键时的值(由于会受外界干扰不为0)
- #define City_Code_MAX 35 //城市数量(0~35)
- #define MAX_CONTENT_SIZE 1000 //接受http响应内容的最大字节数
- #define HTTP_TIMEOUT 5000 //最大响应时间
- /*几个要用到的类的对象定义*/
- File myFile; //读取sd卡数据
- U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 5); //驱动屏幕
- WiFiClient client; //客户端联接服务器
- /*全局变量*/
- unsigned char selected[200]; //用于存放取反(黑白倒置)后的图像,做成被选择的效果
- unsigned char weather_lb[64]; //存放从sd卡中读取的天气字样
- unsigned char weather_pb[512]; //存放从sd卡中读取的天气图像
- char response[MAX_CONTENT_SIZE]; //存放http响应的内容
- char endOfHeaders[] = "\r\n\r\n"; //http响应头部结束的标志
- bool has_net = false; //判断是否联上网络的标志
- bool sd_initial = false; //判断初始化时是否成功读取sd卡保存的上一次设置的标志
- short int s = 0; //主界面中选择的状态
- short int City_Code = 35; //当前设置城市对应的编码(开始时默认为佛山)
- short int weather_Code = 99; //http响应内容中天气对应的编码(开始默认为未知)
- short int temper; //http响应内容中的温度
- String City_ID = "WS06YNEMPP18"; //心知天气中城市对应的id,用于url合成
- String ssid = ""; //配网时保存账号
- String password = ""; //配网时保存密码
- const char* host = "api.seniverse点com"; //服务器网址
- const char* key = "smtq3n0ixdggurox"; //心知天气api的密匙
- const char* language = "zh-Hans"; //设置响应的语言-简体中文
- //此结构在解析json数据时存放json中的"code",“温度”
- struct WeatherData {
- char code[8]; //天气对应的编码
- char temperature[8]; //温度
- };
- /*函数*/
- void Get_initial_data(); //读取上一次设置的城市
- void main_Interface(); //主界面
- void main_interface_draw(); //主界面绘图函数
- void smart_Config(); //主界面中的“网络”,配网连接wifi
- void config_draw(short int); //“网络”绘图函数
- void city_Set(); //主界面中的“工具”,设置城市
- void city_set_draw(short int, bool = false); //“工具”绘图函数
- void draw_city_character(short int, short int, short int); //绘画城市字样函数,三个形参分别是基准点x,y坐标及城市字样对应的数字
- void city_set_store(); //将设置城市保存到sd卡中
- void weather_Report(); //主界面中的“天气”,发送请求接收响应内容并显示天气
- bool SendRequest(); //发送请求
- bool skipResponseHeaders(); //跳过响应头部
- void readReponseContent(char* ); //将响应的数据保存到content数组中
- bool AnalyseData(char* , struct WeatherData*); //解析content中的josn数据
- void printData(const struct WeatherData* ); //打印解析后的数据(主要用来debug)
- void exchange(const struct WeatherData* ); //将解析后存在struct中的code和温度从字符串转换为整数
- void weather_Readpic(); //读取sd卡中对应的天气图片及字样
- void weather_report_draw(); //绘制天气显示
- void request_fail(); //异常退出函数
- /*城市编码和城市ID的键值对数组*/
- //sizeof:36X12B
- static const char *ID[] = {
- "WX4FBXXFKE4F", "WTW3SJ5ZBJUY", "WS0E9D8WN298", "WS10730EM8EV", "WECNV8ZNE40W", "WEBTFUSKS7HB", "WSQQMXBCC2VS", "WWGQDCW6TBW1", "WM7B0X53DZW2",
- "YB1UX38K6DY1", "WZC1EXZ0P9HU", "WXRVB9QYXKY8", "TZY33C4YJBP3", "WRR2Q2Z7CXWM", "WQGDF7NXZRZB", "WWC2MYYCM6J5", "WW8P3NH2TPDT", "WQ82K43YVYU8",
- "WWE0TGW4PX6N", "WQ3V4QR6VR6G", "WW0V9QP93VS8", "WQJ6YY8MHZP0", "WTSQQYHVQ973", "WTEMH46Z5N09", "WTMKQ069CCJ7", "WT3Q0FW9ZJ3Q", "WM6N2PM3WY2K",
- "WJ2B9TWGFBT5", "WT47HJP3HEMP", "WT029G15ETRJ", "WKEZD7MXE04F", "WSSU6EXX52RE", "WK3N92NQV6RQ", "WKJ1F428HH2F", "W7W3YQKE4QDH", "WS06YNEMPP18"
- };
- /*图像(一般以pb开头)、文字(一般以lb开头)数组*/
- //开机时的图像: 88X50px length:550 sizeof:550B
- const unsigned char pb_start[] U8X8_PROGMEM= {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x03,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x04,0x00,0x00,0x00,0xC0,0x07,0x7F,
- 0xF0,0x8F,0x81,0x21,0x0B,0x00,0x00,0x00,0xE0,0x0F,0xFF,0xF0,0x8F,0x83,0xA1,0x0A,
- 0x00,0x00,0x00,0x70,0x1C,0xC3,0x31,0x80,0x83,0xA1,0x06,0x60,0x00,0x00,0x30,0x18,
- 0x83,0x31,0x80,0x87,0x21,0x03,0xF0,0x00,0x00,0x30,0x18,0x83,0x31,0x80,0x8F,0x41,
- 0x08,0xC2,0x01,0x00,0x30,0x18,0x83,0x31,0x80,0x8D,0x81,0x87,0x87,0x01,0x00,0x30,
- 0x18,0x83,0xF1,0x8F,0x99,0x01,0xC0,0x83,0x03,0x00,0x30,0x18,0xC3,0xF0,0x8F,0x99,
- 0x01,0xC0,0x87,0x03,0x00,0x30,0x18,0xFF,0x30,0x80,0xB1,0x01,0x80,0x8C,0x03,0x00,
- 0x30,0x18,0x7F,0x30,0x80,0xF1,0x01,0x00,0x98,0x03,0x00,0x30,0x18,0x03,0x30,0x80,
- 0xE1,0x01,0x20,0xF0,0x03,0x00,0x30,0x18,0x03,0x30,0x80,0xC1,0x01,0x60,0xE0,0x01,
- 0x00,0x30,0x18,0x03,0x30,0x80,0xC1,0x01,0xC0,0xF0,0x00,0x00,0x70,0x1C,0x03,0xF0,
- 0x8F,0x81,0x01,0xF0,0xFF,0x01,0x00,0xE0,0x0F,0x03,0xF0,0x8F,0x81,0x01,0x70,0x3F,
- 0x03,0x00,0xC0,0x07,0x03,0x00,0x00,0x00,0x00,0x70,0x00,0x06,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFB,0xF9,0xE3,0xE7,0xC7,0xC3,
- 0xE3,0xE3,0xC7,0x0F,0xFC,0xFB,0xF9,0xE7,0xEF,0xE7,0xE7,0xF7,0xE7,0xCF,0x0F,0x60,
- 0x18,0xD8,0x66,0x6C,0xE0,0x66,0x36,0x66,0xCC,0x00,0x60,0x18,0xD8,0x66,0x6C,0x60,
- 0x60,0x30,0x66,0xCC,0x00,0x60,0x18,0xD8,0x66,0x6C,0x60,0x60,0x30,0x66,0xCC,0x00,
- 0x60,0xF8,0xD9,0x66,0xEC,0xE7,0x63,0x30,0x66,0xCC,0x0F,0x60,0xF8,0xD9,0xE6,0xE7,
- 0xC7,0x67,0x30,0xE6,0xC7,0x0F,0x60,0x18,0xD8,0xE6,0x63,0x00,0x66,0x30,0xE6,0xC3,
- 0x00,0x60,0x18,0xD8,0x66,0x60,0x00,0x66,0x30,0x66,0xC0,0x00,0x60,0x18,0xD8,0x66,
- 0x60,0x60,0x66,0x36,0x66,0xC0,0x00,0x60,0xF8,0xD9,0x66,0xE0,0xE7,0xE7,0xF7,0x67,
- 0xC0,0x0F,0x60,0xF8,0xD9,0x66,0xE0,0xC7,0xC3,0xE3,0x63,0xC0,0x0F,0x60,0x00,0x00,
- 0x60,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00
- };
- /*主界面中的图像字样*/
- //主界面中的"工具"图像: 40X40px length:200 sizeof:200B
- const unsigned char pb_tool[] U8X8_PROGMEM = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,
- 0x00,0x7C,0x00,0x40,0x06,0x00,0x63,0x00,0x40,0x0C,0x00,0x31,0x00,0x40,0x30,0x80,
- 0x18,0x04,0x80,0x20,0x80,0x08,0x06,0x80,0x60,0x40,0x08,0x05,0x00,0x41,0xC0,0x90,
- 0x04,0x00,0x42,0x80,0x60,0x04,0x00,0x9E,0xC0,0x00,0x04,0x00,0x30,0x61,0x00,0x02,
- 0x00,0x40,0x32,0x00,0x01,0x00,0x80,0x1C,0xFC,0x00,0x00,0x00,0x09,0x36,0x00,0x00,
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
代码,素材,文档及工具.rar
(249.46 KB, 下载次数: 100)
|