Esp8266网页配网添加云端密钥连接到巴法云的完整版示例
时间: 2023-09-15 10:18:39 浏览: 689
WiFI Manager 实现WiFi自动配网,连接周边WiFi热点
5星 · 资源好评率100%
以下一个完整的示例,演示如何在ESP8266的网页配网页面中添加云端密钥并连接到巴法云:
```cpp
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <PubSubClient.h>
// 定义EEPROM地址
#define EEPROM_SIZE 512
#define CLOUD_KEY_ADDR 0
// 定义WiFi和MQTT参数
char wifiSSID[32];
char wifiPassword[32];
char mqttServer[32] = "mqtt.bafangyun.com";
int mqttPort = 1883;
char mqttUsername[32] = "";
char mqttPassword[32] = "";
char deviceId[32] = "";
char deviceSecret[32] = "";
// 定义MQTT客户端
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void setup() {
// 初始化串口
Serial.begin(115200);
// 读取云端密钥
String cloudKey = readCloudKey();
// 初始化WiFiManager
WiFiManager wifiManager;
wifiManager.autoConnect("ESP8266AP");
// 等待WiFi连接
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// 输出WiFi信息
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// 设置Web服务器回调函数
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.setWebServerCallback(webServerCallback);
// 启动Web服务器
wifiManager.startWebPortal();
// 连接到巴法云
connectToBafangyun(cloudKey);
}
void loop() {
// 在循环中检查MQTT连接
if (!mqttClient.connected()) {
connectToBafangyun(readCloudKey());
}
mqttClient.loop();
}
// 保存配置回调函数
void saveConfigCallback () {
strcpy(wifiSSID, WiFiManagerParameter("wifi_ssid").getValue());
strcpy(wifiPassword, WiFiManagerParameter("wifi_password").getValue());
strcpy(deviceId, WiFiManagerParameter("device_id").getValue());
strcpy(deviceSecret, WiFiManagerParameter("device_secret").getValue());
writeCloudKey(WiFiManagerParameter("cloud_key").getValue());
}
// Web服务器回调函数
void webServerCallback (WiFiManager *wifiManager) {
// 添加表单项
WiFiManagerParameter wifiSSIDParam("wifi_ssid", "WiFi SSID", wifiSSID, 32);
wifiManager->addParameter(&wifiSSIDParam);
WiFiManagerParameter wifiPasswordParam("wifi_password", "WiFi Password", wifiPassword, 32);
wifiManager->addParameter(&wifiPasswordParam);
WiFiManagerParameter deviceIdParam("device_id", "Device ID", deviceId, 32);
wifiManager->addParameter(&deviceIdParam);
WiFiManagerParameter deviceSecretParam("device_secret", "Device Secret", deviceSecret, 32);
wifiManager->addParameter(&deviceSecretParam);
WiFiManagerParameter cloudKeyParam("cloud_key", "Cloud Key", readCloudKey().c_str(), 64);
wifiManager->addParameter(&cloudKeyParam);
}
// 读取云端密钥
String readCloudKey () {
String cloudKey = "";
for (int i = 0; i < 64; i++) {
char c = EEPROM.read(CLOUD_KEY_ADDR + i);
if (c == 0) {
break;
}
cloudKey += c;
}
return cloudKey;
}
// 写入云端密钥
void writeCloudKey (String cloudKey) {
for (int i = 0; i < cloudKey.length(); i++) {
EEPROM.write(CLOUD_KEY_ADDR + i, cloudKey[i]);
}
EEPROM.write(CLOUD_KEY_ADDR + cloudKey.length(), 0);
EEPROM.commit();
}
// 连接到巴法云
void connectToBafangyun (String cloudKey) {
if (WiFi.status() == WL_CONNECTED) {
// 初始化MQTT客户端
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(mqttCallback);
// 生成设备ID和设备密钥
String deviceIdWithSecret = deviceId;
deviceIdWithSecret += ":";
deviceIdWithSecret += deviceSecret;
// 连接到MQTT服务器
if (mqttClient.connect(deviceIdWithSecret.c_str(), mqttUsername, mqttPassword)) {
// 订阅设备控制命令
char commandTopic[64];
sprintf(commandTopic, "/devices/%s/commands/#", deviceId);
mqttClient.subscribe(commandTopic);
// 发布设备状态信息
char stateTopic[64];
sprintf(stateTopic, "/devices/%s/state", deviceId);
mqttClient.publish(stateTopic, "online");
} else {
Serial.println("MQTT connection failed");
}
}
}
// MQTT回调函数
void mqttCallback (char* topic, byte* payload, unsigned int length) {
// 处理设备控制命令
Serial.println("Received command:");
Serial.println((char*)payload);
}
```
在这个示例中,我们使用了WiFiManager库来实现网页配网功能,并添加了多个表单项,用于输入WiFi、巴法云和设备的参数。在保存配置回调函数中,我们读取了各个表单项中的内容,并将其保存到EEPROM中。在Web服务器回调函数中,我们添加了多个表单项,以便在网页配网页面中显示各个参数。
在连接到巴法云时,我们使用了PubSubClient库来实现MQTT连接,并在连接成功后订阅设备控制命令,发布设备状态信息。在MQTT回调函数中,我们处理设备控制命令,并将其输出到串口。
请注意,这个示例中的代码仅供参考,您需要根据实际情况进行修改和优化。
阅读全文