134浏览
查看: 134|回复: 0

[ESP8266/ESP32] FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二

[复制链接]
本帖最后由 御坂10032号 于 2025-10-22 20:24 编辑

简介

在写上一篇ESP32C5连接WIFI 5G的时候发现正好群里一个老师发了一个帖子是在Arduino上实现BLE键盘的,于是我想起来ESP-IDF上也有一个Example是可以实现低功耗蓝牙的HID设备。 因此我将在这篇文章中带着大家快速的根据上述的example 修改出一个键盘设备来实现键盘↓的功能。


BLE HID 设备 是一种使用 低功耗蓝牙(Bluetooth Low Energy, BLE) 通信的 人机交互设备(Human Interface Device)
常用于向手机、电脑、平板等主机发送输入数据,例如:键盘、鼠标、遥控器、游戏手柄等。 (来源互联网)


结果

不浪费大家流量,这个IDF的支持还是有限。编译和烧录正常。但是烧录之后蓝牙设备不启动。


FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图1


正文【 ESP32-C5 MQTT 数据上报


1- 首先按照DF,WIKI 的教程进行Arduino C5开发板的安装,切后切换版本到3.3.0 Alpha版本
FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图2
2- 烧录完教程中的代码即设置GPIO15来使板载LED达到常亮成功即可验证环境正常

3- 打开WIFI6的demo进行连接测试,并且修改wifi账号密码为本地5G wifi密码
FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图3

修改密码

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图4

烧录代码

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图5

当前的程序行为是连接路由器,作为STA设备,然后进行NTC校时并且每秒向控制台打印当前的时间。同时作为AP设备允许其他设备连接。但是如果使用的是Arduino的ide的话,我们无法看到串口的输出。
FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图6


猜测是因为系统启动需要时间,连接串口的时候日志正好打印好了。但是其中的NTC打印却也没有正常的输出。进行检查, 发现是因为CDC没有打开从而导致了串口数据从TX和RX输出了。所以我们需要开启CDC使其从USB输出。

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图7

重新进行烧录,此时便可以看到开发板已经获取到了IP地址,并且获取了UTC的时间。

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图8

根据OPEN WRT的后台也可以看到,此时开发板的网络接入也正是5G频段。

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图9

然后将上述代码进行编辑,增加MQTT连接的部分,同时使用本地树莓派上的MQTT服务进行测试。

  1. #include <WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <MQTT.h>        // 256dpi/arduino-mqtt 库
  4. #include <NetworkUDP.h>
  5. #define STA_SSID  "ImmortalWrt-5G"
  6. #define STA_PASS  "mazha1997"
  7. #define AP_SSID   "esp32-v6"
  8. // ====== MQTT 配置 ======
  9. #define MQTT_HOST "192.168.1.153"  // 你可以改成自己的 MQTT 服务器地址
  10. #define MQTT_PORT 1884
  11. #define MQTT_USER "root"                     // 如果有用户名密码可以填上
  12. #define MQTT_PASS "123456"
  13. #define MQTT_CLIENT_ID "esp32-client"
  14. // ====== 全局变量 ======
  15. static volatile bool wifi_connected = false;
  16. NetworkUDP ntpClient;
  17. WiFiClient net;
  18. MQTTClient mqtt(256);   // 建议设置缓存区大小,例如256字节
  19. // ====== NTP 同步函数 ======
  20. void wifiConnectedLoop() {
  21.   const int NTP_PACKET_SIZE = 48;
  22.   byte ntpPacketBuffer[NTP_PACKET_SIZE];
  23.   IPAddress address;
  24.   WiFi.hostByName("time.nist.gov", address);
  25.   memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
  26.   ntpPacketBuffer[0] = 0b11100011;  // LI, Version, Mode
  27.   ntpPacketBuffer[1] = 0;
  28.   ntpPacketBuffer[2] = 6;
  29.   ntpPacketBuffer[3] = 0xEC;
  30.   ntpPacketBuffer[12] = 49;
  31.   ntpPacketBuffer[13] = 0x4E;
  32.   ntpPacketBuffer[14] = 49;
  33.   ntpPacketBuffer[15] = 52;
  34.   ntpClient.beginPacket(address, 123);
  35.   ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
  36.   ntpClient.endPacket();
  37.   delay(1000);
  38.   int packetLength = ntpClient.parsePacket();
  39.   if (packetLength >= NTP_PACKET_SIZE) {
  40.     ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
  41.     ntpClient.clear();
  42.     uint32_t secsSince1900 =
  43.         (uint32_t)ntpPacketBuffer[40] << 24 |
  44.         (uint32_t)ntpPacketBuffer[41] << 16 |
  45.         (uint32_t)ntpPacketBuffer[42] << 8 |
  46.         ntpPacketBuffer[43];
  47.     uint32_t epoch = secsSince1900 - 2208988800UL;
  48.     uint8_t h = (epoch % 86400L) / 3600;
  49.     uint8_t m = (epoch % 3600) / 60;
  50.     uint8_t s = (epoch % 60);
  51.     Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
  52.   }
  53. }
  54. // ====== MQTT 回调函数 ======
  55. void mqttMessageReceived(String &topic, String &payload) {
  56.   Serial.print("MQTT message received: ");
  57.   Serial.print(topic);
  58.   Serial.print(" => ");
  59.   Serial.println(payload);
  60. }
  61. // ====== MQTT 初始化连接 ======
  62. void mqttConnect() {
  63.   Serial.print("Connecting to MQTT... ");
  64.   while (!mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS)) {
  65.     Serial.print(".");
  66.     delay(1000);
  67.   }
  68.   Serial.println("\nMQTT connected!");
  69.   mqtt.subscribe("esp32/test");
  70.   mqtt.publish("esp32/status", "ESP32 connected successfully!");
  71. }
  72. // ====== WiFi 事件处理 ======
  73. void wifiOnConnect() {
  74.   Serial.println("STA Connected");
  75.   Serial.print("STA IPv4: ");
  76.   Serial.println(WiFi.localIP());
  77.   ntpClient.begin(2390);
  78.   // 初始化 MQTT
  79.   mqtt.begin(MQTT_HOST, MQTT_PORT, net);
  80.   mqtt.onMessage(mqttMessageReceived);
  81.   mqttConnect();
  82. }
  83. void wifiOnDisconnect() {
  84.   Serial.println("STA Disconnected");
  85.   delay(1000);
  86.   WiFi.begin(STA_SSID, STA_PASS);
  87. }
  88. void WiFiEvent(WiFiEvent_t event) {
  89.   switch (event) {
  90.     case ARDUINO_EVENT_WIFI_AP_START:
  91.       WiFi.softAPsetHostname(AP_SSID);
  92.       break;
  93.     case ARDUINO_EVENT_WIFI_STA_START:
  94.       WiFi.setHostname(AP_SSID);
  95.       break;
  96.     case ARDUINO_EVENT_WIFI_STA_GOT_IP:
  97.       wifi_connected = true;
  98.       wifiOnConnect();
  99.       break;
  100.     case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
  101.       wifi_connected = false;
  102.       wifiOnDisconnect();
  103.       break;
  104.     default:
  105.       break;
  106.   }
  107. }
  108. // ====== 初始化 ======
  109. void setup() {
  110.   Serial.begin(115200);
  111.   WiFi.disconnect(true);
  112.   WiFi.onEvent(WiFiEvent);
  113.   WiFi.mode(WIFI_MODE_APSTA);
  114.   WiFi.softAPenableIPv6();
  115.   WiFi.softAP(AP_SSID);
  116.   WiFi.enableIPv6();
  117.   WiFi.begin(STA_SSID, STA_PASS);
  118. }
  119. // ====== 主循环 ======
  120. void loop() {
  121.   if (wifi_connected) {
  122.     mqtt.loop(); // MQTT 保持连接和接收消息
  123.     if (!mqtt.connected()) {
  124.       mqttConnect();
  125.     }
  126.     static unsigned long lastMsg = 0;
  127.     if (millis() - lastMsg > 10000) {
  128.       lastMsg = millis();
  129.       mqtt.publish("esp32/test", "Hello from ESP32!");
  130.       wifiConnectedLoop(); // 每10秒打印时间
  131.     }
  132.   }
  133.   while (Serial.available()) {
  134.     Serial.write(Serial.read());
  135.   }
  136. }
复制代码


然后我们可以成功的在MQTTX中查看到上传的消息。

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图10



整合BH1750

接下来我们把BH1750也整合进去,实现光照数据的上报。

  1. #include <WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <MQTT.h>          // 256dpi/arduino-mqtt 库
  4. #include <NetworkUDP.h>
  5. #include <Wire.h>
  6. #include <BH1750.h>        // BH1750 光照传感器库
  7. #define STA_SSID  "ImmortalWrt-5G"
  8. #define STA_PASS  "mazha1997"
  9. #define AP_SSID   "esp32-v6"
  10. // ====== MQTT 配置 ======
  11. #define MQTT_HOST "192.168.1.153"
  12. #define MQTT_PORT 1884
  13. #define MQTT_USER "root"
  14. #define MQTT_PASS "123456"
  15. #define MQTT_CLIENT_ID "esp32-client"
  16. // ====== 全局变量 ======
  17. static volatile bool wifi_connected = false;
  18. NetworkUDP ntpClient;
  19. WiFiClient net;
  20. MQTTClient mqtt(256);
  21. BH1750 lightMeter;  // BH1750 实例
  22. // ====== NTP 同步函数 ======
  23. void wifiConnectedLoop() {
  24.   const int NTP_PACKET_SIZE = 48;
  25.   byte ntpPacketBuffer[NTP_PACKET_SIZE];
  26.   IPAddress address;
  27.   WiFi.hostByName("time.nist.gov", address);
  28.   memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
  29.   ntpPacketBuffer[0] = 0b11100011;  // LI, Version, Mode
  30.   ntpPacketBuffer[1] = 0;
  31.   ntpPacketBuffer[2] = 6;
  32.   ntpPacketBuffer[3] = 0xEC;
  33.   ntpPacketBuffer[12] = 49;
  34.   ntpPacketBuffer[13] = 0x4E;
  35.   ntpPacketBuffer[14] = 49;
  36.   ntpPacketBuffer[15] = 52;
  37.   ntpClient.beginPacket(address, 123);
  38.   ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
  39.   ntpClient.endPacket();
  40.   delay(1000);
  41.   int packetLength = ntpClient.parsePacket();
  42.   if (packetLength >= NTP_PACKET_SIZE) {
  43.     ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
  44.     ntpClient.clear();
  45.     uint32_t secsSince1900 =
  46.         (uint32_t)ntpPacketBuffer[40] << 24 |
  47.         (uint32_t)ntpPacketBuffer[41] << 16 |
  48.         (uint32_t)ntpPacketBuffer[42] << 8 |
  49.         ntpPacketBuffer[43];
  50.     uint32_t epoch = secsSince1900 - 2208988800UL;
  51.     uint8_t h = (epoch % 86400L) / 3600;
  52.     uint8_t m = (epoch % 3600) / 60;
  53.     uint8_t s = (epoch % 60);
  54.     Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
  55.   }
  56. }
  57. // ====== MQTT 回调函数 ======
  58. void mqttMessageReceived(String &topic, String &payload) {
  59.   Serial.print("MQTT message received: ");
  60.   Serial.print(topic);
  61.   Serial.print(" => ");
  62.   Serial.println(payload);
  63. }
  64. // ====== MQTT 初始化连接 ======
  65. void mqttConnect() {
  66.   Serial.print("Connecting to MQTT... ");
  67.   while (!mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS)) {
  68.     Serial.print(".");
  69.     delay(1000);
  70.   }
  71.   Serial.println("\nMQTT connected!");
  72.   mqtt.subscribe("esp32/test");
  73.   mqtt.publish("esp32/status", "ESP32 connected successfully!");
  74. }
  75. // ====== WiFi 事件处理 ======
  76. void wifiOnConnect() {
  77.   Serial.println("STA Connected");
  78.   Serial.print("STA IPv4: ");
  79.   Serial.println(WiFi.localIP());
  80.   ntpClient.begin(2390);
  81.   // 初始化 MQTT
  82.   mqtt.begin(MQTT_HOST, MQTT_PORT, net);
  83.   mqtt.onMessage(mqttMessageReceived);
  84.   mqttConnect();
  85. }
  86. void wifiOnDisconnect() {
  87.   Serial.println("STA Disconnected");
  88.   delay(1000);
  89.   WiFi.begin(STA_SSID, STA_PASS);
  90. }
  91. void WiFiEvent(WiFiEvent_t event) {
  92.   switch (event) {
  93.     case ARDUINO_EVENT_WIFI_AP_START:
  94.       WiFi.softAPsetHostname(AP_SSID);
  95.       break;
  96.     case ARDUINO_EVENT_WIFI_STA_START:
  97.       WiFi.setHostname(AP_SSID);
  98.       break;
  99.     case ARDUINO_EVENT_WIFI_STA_GOT_IP:
  100.       wifi_connected = true;
  101.       wifiOnConnect();
  102.       break;
  103.     case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
  104.       wifi_connected = false;
  105.       wifiOnDisconnect();
  106.       break;
  107.     default:
  108.       break;
  109.   }
  110. }
  111. // ====== 初始化 ======
  112. void setup() {
  113.   Serial.begin(115200);
  114.   Serial.println("ESP32 Booting...");
  115.   // ===== 初始化 I2C 和 BH1750 =====
  116.   Wire.begin(9, 10); // SDA=9, SCL=10
  117.   if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
  118.     Serial.println("BH1750 initialized successfully.");
  119.   } else {
  120.     Serial.println("BH1750 not detected! Check wiring.");
  121.   }
  122.   // ===== 初始化 Wi-Fi =====
  123.   WiFi.disconnect(true);
  124.   WiFi.onEvent(WiFiEvent);
  125.   WiFi.mode(WIFI_MODE_APSTA);
  126.   WiFi.softAPenableIPv6();
  127.   WiFi.softAP(AP_SSID);
  128.   WiFi.enableIPv6();
  129.   WiFi.begin(STA_SSID, STA_PASS);
  130. }
  131. // ====== 主循环 ======
  132. void loop() {
  133.   if (wifi_connected) {
  134.     mqtt.loop(); // MQTT 保持连接
  135.     if (!mqtt.connected()) {
  136.       mqttConnect();
  137.     }
  138.     static unsigned long lastMsg = 0;
  139.     if (millis() - lastMsg > 10000) {
  140.       lastMsg = millis();
  141.       // ===== 读取 BH1750 光照强度 =====
  142.       float lux = lightMeter.readLightLevel();
  143.       Serial.printf("Light intensity: %.2f lux\n", lux);
  144.       // ===== 组装 JSON 并发布到 MQTT =====
  145.       String payload = String("{"lux":") + String(lux, 2) + "}";
  146.       mqtt.publish("esp32/bh1750", payload);
  147.       Serial.println("Published: " + payload);
  148.       // ===== 同步 NTP 时间 =====
  149.       wifiConnectedLoop();
  150.     }
  151.   }
  152.   while (Serial.available()) {
  153.     Serial.write(Serial.read());
  154.   }
  155. }
复制代码


此时可以看到控制台已经成功的发送了MQTT的消息

FireBeetle 2 ESP32-C5 MQTT 数据上报 [5GHZ] 二图11

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail