引言
开发板介绍
功能实现
按键操作
代码实现框图
板卡设置
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 time
configTime(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 time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo; //定义获取网络时间结构体 tm 变量 timeinfo
if(!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 变量 timeinfo
if(!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 resources
http.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
硬禾学堂
硬禾团队一直致力于给电子工程师和相关专业的同学,带来规范的核心技能课程,帮助大家在学习和工作的各个阶段,都能有效地提升自己的职业能力。
硬禾学堂
我们一起在电子领域探索前进
关注硬禾公众号,随时直达课堂
点击阅读原文查看更多