山芷兰 发表于 2025-6-8 13:24:07

基于ESP8266网络天气时钟的OLED显示

基于ESP8266网络天气时钟的OLED显示

注意修改 WiFi 名称和密码
需要申请【心知天气】API私钥
心知天气URL:https://www.seniverse.com
/*参考:https://blog.csdn.net/weixin_44668788/article/details/120643078http://www.taichi-maker.com/homepage/iot-development/iot-platform/seniverse/esp8266-application/OLED connect with ESP8266 NodeMCUSCL -- D1SDA -- D2*///引入必要的头文件#include #include #include #include #include #include #include #include WiFiUDP Udp;unsigned int localPort = 8888; // 用于侦听UDP数据包的本地端口//网络校时的相关配置static const char ntpServerName[] = "ntp1.aliyun.com"; //NTP服务器,使用阿里云int timeZone = 8; //时区设置,采用东8区//保存断网前的最新数据int results_0_now_temperature_int_old;String results_0_now_text_str_old;int results_0_daily_1_high_int_old;int results_0_daily_1_low_int_old;String results_0_daily_1_text_day_str_old;//函数声明time_t getNtpTime();void sendNTPpacket(IPAddress &address);void oledClockDisplay();void sendCommand(int command, int value);void initdisplay();void connectWiFi();void parseInfo_now(WiFiClient client,int i);void parseInfo_fut(WiFiClient client,int i);//boolean isNTPConnected = false;const unsigned char xing[] U8X8_PROGMEM = {    0x00, 0x00, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x80, 0x00, 0x88, 0x00,    0xF8, 0x1F, 0x84, 0x00, 0x82, 0x00, 0xF8, 0x0F, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00}; /*星*/const unsigned char liu[] U8X8_PROGMEM = {    0x40, 0x00, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,    0x20, 0x02, 0x20, 0x04, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10, 0x04, 0x20, 0x02, 0x20, 0x00, 0x00}; /*六*/typedef struct{                  //存储配置结构体    int tz;      //时间戳} config_type;config_type config;WiFiClient clientNULL;DNSServer dnsServer;ESP8266WebServer server(80);//----------WIFI连接配置----------const char* ssid   = "xxxxxxx";       // 连接WiFi名,需要修改const char* password = "xxxxxxxx";          // 连接WiFi密码,需要修改                                          // 请将您需要连接的WiFi密码填入引号中//----------天气API配置----------const char* host = "api.seniverse.com";   // 将要连接的服务器地址const int httpPort = 80;            // 将要连接的服务器端口      // 心知天气HTTP请求所需信息String reqUserKey = "xxxxxx";   // 私钥,需要修改String reqLocation = "Shanghai";            // 城市String reqUnit = "c";                      // 摄氏/华氏//----------设置屏幕----------U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);int sta = 0;//----------初始化OLED----------void initdisplay(){    u8g2.begin();    u8g2.enableUTF8Print();}//----------用于获取实时天气的函数(0)----------void TandW(){String reqRes = "/v3/weather/now.json?key=" + reqUserKey +                  + "&location=" + reqLocation +                   "&language=en&unit=" +reqUnit;// 向心知天气服务器服务器请求信息并对信息进行解析httpRequest(reqRes,0);//延迟,需要低于20次/分钟delay(5000);}void display_1(int results_0_now_temperature_int,String results_0_now_text_str);//声明函数,用于显示温度、天气//----------获取3天预报(1)----------void threeday(){// 建立心知天气API当前天气请求资源地址String reqRes = "/v3/weather/daily.json?key=" + reqUserKey +                  + "&location=" + reqLocation + "&language=en&unit=" +                  reqUnit + "&start=0&days=3";// 向心知天气服务器服务器请求信息并对信息进行解析httpRequest(reqRes,1);delay(5000);}void clock_display(time_t prevDisplay){    server.handleClient();    dnsServer.processNextRequest();    if (timeStatus() != timeNotSet)    {      if (now() != prevDisplay)      { //时间改变时更新显示            prevDisplay = now();            oledClockDisplay();      }    }}void setup(){Serial.begin(9600);            Serial.println("");initdisplay();// 连接WiFi      u8g2.clearBuffer();u8g2.setFont(u8g2_font_unifont_t_chinese2);u8g2.setCursor(0, 14);u8g2.print("Waiting for WiFi");u8g2.setCursor(0, 30);u8g2.print("connection...");u8g2.sendBuffer();connectWiFi();Udp.begin(localPort);setSyncProvider(getNtpTime);setSyncInterval(300); //每300秒同步一次时间}time_t prevDisplay = 0; //当时钟已经显示 void loop(){    if (sta>=0 && sta 0)      ; // 丢弃以前接收的任何数据包    Serial.println("Transmit NTP Request");    // 从池中获取随机服务器    WiFi.hostByName(ntpServerName, ntpServerIP);    Serial.print(ntpServerName);    Serial.print(": ");    Serial.println(ntpServerIP);    sendNTPpacket(ntpServerIP);    uint32_t beginWait = millis();    while (millis() - beginWait < 1500)    {      int size = Udp.parsePacket();      if (size >= NTP_PACKET_SIZE)      {            Serial.println("Receive NTP Response");            isNTPConnected = true;            Udp.read(packetBuffer, NTP_PACKET_SIZE); // 将数据包读取到缓冲区            unsigned long secsSince1900;            // 将从位置40开始的四个字节转换为长整型,只取前32位整数部分            secsSince1900 = (unsigned long)packetBuffer
页: [1]
查看完整版本: 基于ESP8266网络天气时钟的OLED显示