引言
開發板介紹
功能實現
按鍵操作
代碼實現框圖

板卡設置

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

燒錄

代碼分析
void setup() {Serial.begin(115200);u8g2.begin();u8g2.enableUTF8Print();// 初始化按鍵引腳pinMode(button_1,INPUT);pinMode(button_2,INPUT);pinMode(button_3,INPUT);pinMode(button_4,INPUT);WiFi.begin(ssid, password);Serial.println("Connecting");while(WiFi.status() != WL_CONNECTED) {// 未連接時u8g2.setFont(u8g2_font_micro_mr);u8g2.setCursor(0,6);u8g2.print("Initializing...");u8g2.sendBuffer();delay(1000);u8g2.setCursor(0,12);u8g2.print("Initialized successfully");u8g2.sendBuffer();delay(1000);u8g2.setCursor(0,18);u8g2.print("Loading...");u8g2.sendBuffer();delay(100);u8g2.drawXBMP(9 ,21 ,logo0_width ,logo0_height ,logo0_bits);u8g2.setFont(u8g2_font_wqy12_t_gb2312);u8g2.setCursor(9,63);u8g2.print("硬禾學堂");u8g2.drawXBMP(73 ,30 ,logo1_width ,logo1_height ,logo1_bits);delay(1000);u8g2.sendBuffer();}u8g2.clearBuffer();Serial.println("Connected");//init and get the timeconfigTime(gmtOffset_sec, daylightOffset_sec, ntpServer);// update_ip_location();// printLocalInfo();}
// WiFi賬號密碼const char* ssid = "ssid";const char* password = "password";void setup(){WiFi.begin(ssid, password);Serial.println("Connecting");}
// 時間變量定義const char* ntpServer = "edu.ntp.org.cn";const long gmtOffset_sec = 8*60*60;//東八區,UTC計時const int daylightOffset_sec = 8*60*60;//東八區,UTC計時//init and get the timeconfigTime(gmtOffset_sec, daylightOffset_sec, ntpServer);struct tm timeinfo; //定義獲取網絡時間結構體 tm 變量 timeinfoif(!getLocalTime(&timeinfo)){Serial.println("Failed to obtain time");return;}Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");u8g2.setFont(u8g2_font_freedoomr25_tn);u8g2.setCursor(5,47);u8g2.print(&timeinfo,"%H:%M");u8g2.setFont(u8g2_font_freedoomr10_tu);u8g2.print(&timeinfo,"%S");
struct tm timeinfo; //定義獲取網絡時間結構體 tm 變量 timeinfoif(!getLocalTime(&timeinfo)){Serial.println("Failed to obtain time");return;}// Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");u8g2.setFont(u8g2_font_freedoomr25_tn);u8g2.setCursor(5,47);u8g2.print(&timeinfo,"%H:%M");u8g2.setFont(u8g2_font_freedoomr10_tu);u8g2.print(&timeinfo,"%S");
// json變量定義const char* province0;const char* city0;const char* weather0;const char* weather1;const char* weather2;const char* weather3;String temperature0;String temperature1;String temperature2;String temperature3;const char* winddirection0;const char* winddirection1;const char* winddirection2;const char* winddirection3;const char* date0;const char* date1;const char* date2;const char* date3;const char* date01;const char* date11;const char* date21;const char* date31;char date02[5]={'0'};char date12[5]={'0'};char date22[5]={'0'};char date32[5]={'0'};int week_num;int flag = 0;JSONVar myObject;String city_code,key = "*******************************";String jsonBuffer;String ip_jsonBuffer;String httpGETRequest(const char* serverName){WiFiClient client;HTTPClient http;http.begin(client, serverName);int httpResponseCode = http.GET();String payload = "{}";if (httpResponseCode>0){Serial.print("HTTP Response code: ");Serial.println(httpResponseCode);payload = http.getString();}else{Serial.print("Error code: ");Serial.println(httpResponseCode);}// Free resourceshttp.end();return payload;}String serverPath = "http://restapi.amap.com/v3/weather/weatherInfo?city=" + city_code + "&key=" + key + "&extensions=all";jsonBuffer = httpGETRequest(serverPath.c_str());myObject = JSON.parse(jsonBuffer);if (JSON.typeof(myObject) == "undefined"){Serial.println("Parsing input failed!");return;}// 獲取省份province0 = myObject["forecasts"][0]["province"];// 獲取城市city0 = myObject["forecasts"][0]["city"];// 獲取今日天氣weather0 = myObject["forecasts"][0]["casts"][0]["dayweather"];// 獲取今日溫度const char* temp0 = myObject["forecasts"][0]["casts"][0]["daytemp"];String temp01 = temp0;temperature0 = temp01 + " C";// 獲取今日風向winddirection0 = myObject["forecasts"][0]["casts"][0]["daywind"];// 獲取今日日期date0 = myObject["forecasts"][0]["reporttime"];date01 = &date0[5];for(int k=0;k<5;k++){date02[k] = date01[k];}// 獲取明日天氣weather1 = myObject["forecasts"][0]["casts"][1]["dayweather"];// 獲取明日溫度const char* temp1 = myObject["forecasts"][0]["casts"][1]["daytemp"];String temp11 = temp1;temperature1 = temp11 + " C";// 獲取明日風向winddirection1 = myObject["forecasts"][0]["casts"][1]["daywind"];// 獲取明日日期date1 = myObject["forecasts"][0]["casts"][1]["date"];date11 = &date1[5];// 獲取后日天氣weather2 = myObject["forecasts"][0]["casts"][2]["dayweather"];// 獲取后日溫度const char* temp2 = myObject["forecasts"][0]["casts"][2]["daytemp"];String temp21 = temp2;temperature2 = temp21 + " C";// 獲取后日風向winddirection2 = myObject["forecasts"][0]["casts"][2]["daywind"];// 獲取后日日期date2 = myObject["forecasts"][0]["casts"][2]["date"];date21 = &date2[5];// 獲取大后日天氣weather3 = myObject["forecasts"][0]["casts"][3]["dayweather"];// 獲取大后日溫度const char* temp3 = myObject["forecasts"][0]["casts"][3]["daytemp"];String temp31 = temp3;temperature3 = temp31 + " C";// 獲取大后日風向winddirection3 = myObject["forecasts"][0]["casts"][3]["daywind"];// 獲取大后日日期date3 = myObject["forecasts"][0]["casts"][3]["date"];date31 = &date3[5];
void update_ip_location(){String ip_location = "http://restapi.amap.com/v3/ip?key=" + key;ip_jsonBuffer = httpGETRequest(ip_location.c_str());JSONVar ip_myObject = JSON.parse(ip_jsonBuffer);if (JSON.typeof(ip_myObject) == "undefined"){Serial.println("Parsing input failed!");return;}city_code = ip_myObject["adcode"];}
if(digitalRead(button_1)||digitalRead(button_2)||digitalRead(button_3)||digitalRead(button_4) == 0){if(digitalRead(button_1) == 0){flag = 0;}else if(digitalRead(button_2) == 0){flag = 1;}else if(digitalRead(button_3) == 0){flag = 2;}else if(digitalRead(button_4) == 0){flag = 3;}}if(flag == 0){printLocalInfo();// 每顯示一次,i++}if(flag == 1){printtomorrowInfo();}if(flag == 2){nexttomorrowInfo();}if(flag == 3){nextnexttomorrowInfo();}
存在的Bug
結果展示





END
硬禾學堂
硬禾團隊一直致力于給電子工程師和相關專業的同學,帶來規范的核心技能課程,幫助大家在學習和工作的各個階段,都能有效地提升自己的職業能力。

硬禾學堂
我們一起在電子領域探索前進
關注硬禾公眾號,隨時直達課堂

點擊閱讀原文查看更多