基于ESP32的网络天气时钟(微服务器)

2024年国庆期间,假期无聊写的程序

主界面

主程序

#include < Arduino.h >
#include "Backend_service.h"
#include "SMG_4.h"

void setup() {
    Serial.begin(115200);
    SMG_4_Init();  // 初始化数码管显示
    connectToWiFi();  // 连接Wi-Fi
    initOLED();  // 初始化 OLED

    // 初始化 NTP 客户端
    timeClient.begin();
    
    // 等待时间同步
    while(!timeClient.update()) {
        Serial.println("Waiting for NTP time sync...");
        delay(1000);  // 每秒钟检查一次
    }

    // 处理网页请求
    server.on("/", handleRoot);  // 处理根路径
    server.on("/setCity", handleSetCity); // 处理设置城市请求
    server.on("/time", handleTime);    // 处理时间请求
    server.on("/weather", handleWeather)
### 使用 Arduino ESP32 制作网络天气时钟 #### 材料准备 为了构建基于ESP32网络天气时钟,所需材料如下: - ESP32开发板 × 1 - OLED显示屏模块 (可选用于显示更多信息) × 1 - 连接线若干 - USB转TTL串口下载线 × 1 #### 主要组件说明 ESP32具备Wi-Fi和蓝牙功能,能够连接互联网获取实时气象数据并解析JSON格式的数据流[^1]。 #### 获取API密钥 访问OpenWeatherMap网站注册账号,并创建应用来获得免费版API Key。此Key将在HTTP请求中作为参数传递给服务器以验证身份合法性。 #### 编写程序逻辑 以下是完整的Arduino IDE中的C++源码示例,展示了如何利用ESP32从在线服务读取当前温度和其他环境条件的信息,并将其展示出来: ```cpp #include <WiFi.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const char* ssid = "your SSID"; const char* password = "your PASSWORD"; const char* host = "api.openweathermap.org"; String apiKey = "Your_API_Key_Here"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to the Wi-Fi network"); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } } void loop() { if(WiFi.status()==WL_CONNECTED){ HTTPClient http; String url = "/data/2.5/weather?q=London&appid=" + apiKey; http.begin(host, 80, url.c_str()); int httpResponseCode = http.GET(); if(httpResponseCode>0){ String payload=http.getString(); StaticJsonDocument<256> doc; DeserializationError error = deserializeJson(doc,payload); if(error){ Serial.print("deserializeJson() failed: "); Serial.println(error.f_str()); return; } float temperature = doc["main"]["temp"] - 273.15; //-273.15 converts Kelvin to Celsius display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0,0); display.printf("Temp:%.2f C",temperature); display.display(); Serial.printf("Temperature is %.2f\n",temperature); }else{ Serial.print("Error on sending GET request:"); Serial.println(httpResponseCode); } http.end(); } } ``` 上述代码实现了基本的功能需求,即通过Wi-Fi联网查询指定城市的气温信息并通过OLED屏幕输出结果[^2]。 #### 注意事项 确保安装必要的库文件,比如`WiFi`, `Wire`, 和 `Adafruit_SSD1306`. 可以在Arduino IDE 的库管理器里找到这些依赖项。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值