基于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.png (6.47 KB, 下載次數: 77)
下載附件
2021-5-28 21:10 上傳
所有資料51hei提供下載:
代碼,素材,文檔及工具.rar
(249.46 KB, 下載次數: 100)
2021-5-28 19:10 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|