基于arduino的ESP8266天氣預(yù)報(bào)設(shè)計(jì)
附錄:代碼、天氣相關(guān)圖片、字樣等素材。
單片機(jī)源程序如下:
- #include<U8g2lib.h>
- #include<ESP8266WiFi.h>
- #include<avr/pgmspace.h>
- #include<SPI.h>
- #include<SD.h>
- #include<ArduinoJson.h>
- /*按鍵模擬輸入的值,按鍵的誤差范圍在之后會(huì)設(shè)置為正負(fù)30*/
- #define exit_bt 930
- #define left_bt 648
- #define right_bt 393
- #define enter_bt 144
- #define null_bt 22 //不按按鍵時(shí)的值(由于會(huì)受外界干擾不為0)
- #define City_Code_MAX 35 //城市數(shù)量(0~35)
- #define MAX_CONTENT_SIZE 1000 //接受http響應(yīng)內(nèi)容的最大字節(jié)數(shù)
- #define HTTP_TIMEOUT 5000 //最大響應(yīng)時(shí)間
- /*幾個(gè)要用到的類(lèi)的對(duì)象定義*/
- File myFile; //讀取sd卡數(shù)據(jù)
- U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 5); //驅(qū)動(dòng)屏幕
- WiFiClient client; //客戶(hù)端聯(lián)接服務(wù)器
- /*全局變量*/
- unsigned char selected[200]; //用于存放取反(黑白倒置)后的圖像,做成被選擇的效果
- unsigned char weather_lb[64]; //存放從sd卡中讀取的天氣字樣
- unsigned char weather_pb[512]; //存放從sd卡中讀取的天氣圖像
- char response[MAX_CONTENT_SIZE]; //存放http響應(yīng)的內(nèi)容
- char endOfHeaders[] = "\r\n\r\n"; //http響應(yīng)頭部結(jié)束的標(biāo)志
- bool has_net = false; //判斷是否聯(lián)上網(wǎng)絡(luò)的標(biāo)志
- bool sd_initial = false; //判斷初始化時(shí)是否成功讀取sd卡保存的上一次設(shè)置的標(biāo)志
- short int s = 0; //主界面中選擇的狀態(tài)
- short int City_Code = 35; //當(dāng)前設(shè)置城市對(duì)應(yīng)的編碼(開(kāi)始時(shí)默認(rèn)為佛山)
- short int weather_Code = 99; //http響應(yīng)內(nèi)容中天氣對(duì)應(yīng)的編碼(開(kāi)始默認(rèn)為未知)
- short int temper; //http響應(yīng)內(nèi)容中的溫度
- String City_ID = "WS06YNEMPP18"; //心知天氣中城市對(duì)應(yīng)的id,用于url合成
- String ssid = ""; //配網(wǎng)時(shí)保存賬號(hào)
- String password = ""; //配網(wǎng)時(shí)保存密碼
- const char* host = "api.seniverse點(diǎn)com"; //服務(wù)器網(wǎng)址
- const char* key = "smtq3n0ixdggurox"; //心知天氣api的密匙
- const char* language = "zh-Hans"; //設(shè)置響應(yīng)的語(yǔ)言-簡(jiǎn)體中文
- //此結(jié)構(gòu)在解析json數(shù)據(jù)時(shí)存放json中的"code",“溫度”
- struct WeatherData {
- char code[8]; //天氣對(duì)應(yīng)的編碼
- char temperature[8]; //溫度
- };
- /*函數(shù)*/
- void Get_initial_data(); //讀取上一次設(shè)置的城市
- void main_Interface(); //主界面
- void main_interface_draw(); //主界面繪圖函數(shù)
- void smart_Config(); //主界面中的“網(wǎng)絡(luò)”,配網(wǎng)連接wifi
- void config_draw(short int); //“網(wǎng)絡(luò)”繪圖函數(shù)
- void city_Set(); //主界面中的“工具”,設(shè)置城市
- void city_set_draw(short int, bool = false); //“工具”繪圖函數(shù)
- void draw_city_character(short int, short int, short int); //繪畫(huà)城市字樣函數(shù),三個(gè)形參分別是基準(zhǔn)點(diǎn)x,y坐標(biāo)及城市字樣對(duì)應(yīng)的數(shù)字
- void city_set_store(); //將設(shè)置城市保存到sd卡中
- void weather_Report(); //主界面中的“天氣”,發(fā)送請(qǐng)求接收響應(yīng)內(nèi)容并顯示天氣
- bool SendRequest(); //發(fā)送請(qǐng)求
- bool skipResponseHeaders(); //跳過(guò)響應(yīng)頭部
- void readReponseContent(char* ); //將響應(yīng)的數(shù)據(jù)保存到content數(shù)組中
- bool AnalyseData(char* , struct WeatherData*); //解析content中的josn數(shù)據(jù)
- void printData(const struct WeatherData* ); //打印解析后的數(shù)據(jù)(主要用來(lái)debug)
- void exchange(const struct WeatherData* ); //將解析后存在struct中的code和溫度從字符串轉(zhuǎn)換為整數(shù)
- void weather_Readpic(); //讀取sd卡中對(duì)應(yīng)的天氣圖片及字樣
- void weather_report_draw(); //繪制天氣顯示
- void request_fail(); //異常退出函數(shù)
- /*城市編碼和城市ID的鍵值對(duì)數(shù)組*/
- //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開(kāi)頭)、文字(一般以lb開(kāi)頭)數(shù)組*/
- //開(kāi)機(jī)時(shí)的圖像: 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,
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
51hei.png (6.47 KB, 下載次數(shù): 77)
下載附件
2021-5-28 21:10 上傳
所有資料51hei提供下載:
代碼,素材,文檔及工具.rar
(249.46 KB, 下載次數(shù): 100)
2021-5-28 19:10 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|