arduino + esp8266_01s + TM1637做一个WiFi时钟

看了很多的wifi时钟,于是就做了一个掌上的mini时钟,我没有安装esp8266的库文件和板级支持包,所以就打算直接用at指令来访问服务器,并从接收到的数据中获取当前时间。经测量大概3天误差在±10毫秒以内,然后系统每24小时自动进行校准时间,减少误差。最终效果还算理想。

硬件部分:

  1. arduino nano :体型较小,比较便宜
  2. esp8266_01s :01s相对于01在接线上可以不用把EN接高电平
  3. TM1637 :与其它四位数码管相比使用I2c接口,所需引脚大量减少
  4. usb转ttl:用于wifi模块的调试

软件:

1.arduino IDE
2.需要安装TM1637库文件
3.我并未安装arduino上的esp8266库文件,因此wifi模块使用at指令来进行调试的,具体操作在下面展示

库安装

打开arduino IDE再按快捷键 Crtrl + Shift + I 调出库管理,再
在输入栏上写上TM1637,点击安装。
在这里插入图片描述

连线部分

esp8266_01s arduino
3.3v 3.3v
GND GND
RX TX
TX RX
TM1637 arduino
3.3v 3.3v
GND GND
CLK 11
DIO 12

AT指令调试:

wifi模块接上usb转ttl,打开串口调试助手输入:
AT 若返回“ok”则表示能正常通讯,然后依次输入以下代码
(输出时在最后加上回车换行):
AT+CWMODE=3 配置成 AP+STA模式
AT+RST 重启生效
AT+CIFSR 查询设备IP
AT+CWJAP=“ssid”,“password” 连接你手机的热点“ssid”为热点的名称,"password"为密码。
AT+CIPSTART=“TCP”,“183.230.40.33”,80l连接的服务器ip 183.230.40.33/80
AT+CIPMODE=1开启透传模式
AT+CIPSEND开始透传

现在wifi模块就已经连上服务端了,这时我们在串口随便发送任意字符,都会收到服务器返回给我们的信息,这时只要把里面的时间给读取出来就行了。
在这里插入图片描述
这里我发送了一个’A’,服务器给我返回了带有时间的消息包。但这里的时间是美国时间,所以需要在小时上加个8小时的时差就行了。

wifi模块上电自动连接到服务器

如果我们想把wifi模块给配置好
AT+SAVETRANSLINK=1,“183.230.40.33”,80,“TCP”,100开机自动连接并进入透传。

输入这行命令这样就不用每次上电都重新发送AT指令了。

esp8266_01s获取时间

String receive = "";    //读取串口收到的字符
char info[380] = {
   0};   //char* 类型临时接收缓冲区
int hour_shi;
int hour_ge;
int min_shi;
int min_ge;
int sec_shi;
int sec_ge;

void setup() {
   
  Serial.begin(115200); //初始化WIFI串口
  Serial.print("a");
  delay(1000);//等待连接  
  while (Serial.available() > 0)
  {
   
  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
很高兴回答你的问题,以下是使用Arduino、lvgl、esp32和tft_espi的示例代码实现网络天气时钟的方法: ``` #include <Arduino.h> #include <TFT_eSPI.h> #include <lvgl.h> #include <WiFi.h> #include <ArduinoJson.h> #include <time.h> #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASS "your_wifi_password" #define OPENWEATHERMAP_APIKEY "your_openweathermap_api_key" TFT_eSPI tft = TFT_eSPI(135, 240); // 设置 TFT 屏幕的大小 lv_obj_t * label_temperature, * label_temperature_symbol, * label_weather_description, * label_city_name, * label_clock; void get_weather_data(String & temperature, String & weather_description, String & city_name) { WiFiClient client; if (!client.connect("api.openweathermap.org", 80)) { Serial.println("connection failed!"); return; } String url = "/data/2.5/weather?q=Shenzhen,CN&appid=" OPENWEATHERMAP_APIKEY "&units=metric"; client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: api.openweathermap.org\r\n" + "Connection: close\r\n\r\n"); String response = ""; while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } while (client.available()) { response += (char)client.read(); } const int capacity = JSON_OBJECT_SIZE(4) + 100; DynamicJsonDocument doc(capacity); deserializeJson(doc, response); JsonObject main = doc["main"]; temperature = main["temp"].as<String>(); JsonArray weather = doc["weather"]; weather_description = weather[0]["description"].as<String>(); city_name = doc["name"].as<String>(); } void get_time_string(String & time_string) { time_t now; struct tm timeinfo; time(&now); localtime_r(&now, &timeinfo); char strftime_buf[64]; strftime(strftime_buf, sizeof(strftime_buf), "%H:%M", &timeinfo); time_string = strftime_buf; } void setup() { Serial.begin(115200); lv_init(); tft.init(); tft.setRotation(3); tft.fillScreen(TFT_BLACK); lvgl_driver_init(&tft); lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.buffer = &lvgl_buffer; disp_drv.flush_cb = lvgl_flush; lv_disp_drv_register(&disp_drv); lv_theme_t * theme = lv_theme_material_init(210, NULL); lv_theme_set_current(theme); label_temperature = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_temperature, "temperature"); lv_obj_align(label_temperature, NULL, LV_ALIGN_CENTER, 0, -40); label_temperature_symbol = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_temperature_symbol, "°C"); lv_obj_set_pos(label_temperature_symbol, lv_obj_get_x(label_temperature) + lv_obj_get_width(label_temperature), lv_obj_get_y(label_temperature)); label_weather_description = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_weather_description, "weather_description"); lv_obj_align(label_weather_description, NULL, LV_ALIGN_CENTER, 0, 0); label_city_name = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_city_name, "city_name"); lv_obj_align(label_city_name, NULL, LV_ALIGN_CENTER, 0, 40); label_clock = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label_clock, "clock"); lv_obj_align(label_clock, NULL, LV_ALIGN_CENTER, 0, 80); lv_task_create([](lv_task_t * task) { String temperature; String weather_description; String city_name; get_weather_data(temperature, weather_description, city_name); lv_label_set_text(label_temperature, temperature.c_str()); lv_label_set_text(label_weather_description, weather_description.c_str()); lv_label_set_text(label_city_name, city_name.c_str()); }, 5000, LV_TASK_PRIO_LOW, NULL); lv_task_create([](lv_task_t * task) { String time_string; get_time_string(time_string); lv_label_set_text(label_clock, time_string.c_str()); }, 1000, LV_TASK_PRIO_MID, NULL); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { delay(1000); } Serial.println("setup complete!"); } void loop() { lv_task_handler(); delay(1); } ``` 该代码使用了 OpenWeatherMap API 获取深圳的当前天气数据,在屏幕上输出温度、天气描述和城市名称,通过 `lv_task_create` 函数设置定时任务每 5 秒钟更新一次天气信息和每秒钟更新一次当前时间,是一个简单而实用的网络天气时钟的实现方式。 注:这里使用的API Key只是演示,请勿用于商业用途,如有需要请自行注册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值