You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.1 KiB
C++
101 lines
3.1 KiB
C++
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
// WiFi Credentials
|
|
const char *ssid = "rehome"; // Replace with your WiFi name
|
|
const char *password = "Ruihong123"; // Replace with your WiFi password
|
|
|
|
// MQTT Broker Settings
|
|
const char *mqtt_broker = "mqtt.fileview123.com";
|
|
const char *mqtt_topic = "/device/esp8266/+";
|
|
const char *mqtt_username = "admin";
|
|
const char *mqtt_password = "publish452131wW452131wW$";
|
|
const int mqtt_port = 1883;
|
|
const char *mqtt_topic_publish = "/device/esp8266/status"; // MQTT topic publish
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient mqtt_client(espClient);
|
|
|
|
// Function Declarations
|
|
void connectToWiFi();
|
|
|
|
void connectToMQTT();
|
|
|
|
void mqttCallback(char *mqtt_topic, byte *payload, unsigned int length);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
connectToWiFi();
|
|
mqtt_client.setServer(mqtt_broker, mqtt_port);
|
|
mqtt_client.setKeepAlive(60);
|
|
mqtt_client.setCallback(mqttCallback); // Corrected callback function name
|
|
connectToMQTT();
|
|
}
|
|
|
|
void connectToWiFi() {
|
|
delay(10);
|
|
Serial.println();
|
|
Serial.print("正在连接: ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
Serial.print("Connecting to WiFi");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("\nConnected to WiFi");
|
|
Serial.println("");
|
|
Serial.println("WiFi 连接成功");
|
|
Serial.println("IP 地址: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void connectToMQTT() {
|
|
while (!mqtt_client.connected()) {
|
|
Serial.print("尝试 MQTT 连接...");
|
|
String client_id = "esp32-client-" + String(WiFi.macAddress());
|
|
Serial.printf("Connecting to MQTT Broker as %s.....\n", client_id.c_str());
|
|
if (mqtt_client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
|
|
Serial.println("连接成功");
|
|
Serial.println("Connected to MQTT broker");
|
|
mqtt_client.subscribe(mqtt_topic);
|
|
mqtt_client.publish(mqtt_topic_publish, "Hi EMQX I'm ESP32 ^^"); // Publish message upon successful connection
|
|
} else {
|
|
Serial.print("Failed, rc=");
|
|
Serial.print(mqtt_client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void mqttCallback(char *mqtt_topic, byte *payload, unsigned int length) {
|
|
Serial.print("Message received on mqtt_topic: ");
|
|
Serial.println(mqtt_topic);
|
|
Serial.print("Message: ");
|
|
for (unsigned int i = 0; i < length; i++) {
|
|
Serial.print((char) payload[i]);
|
|
}
|
|
Serial.println("\n-----------------------");
|
|
}
|
|
|
|
|
|
void loop() {
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
connectToWiFi();
|
|
}
|
|
if (!mqtt_client.connected()) {
|
|
connectToMQTT();
|
|
}
|
|
mqtt_client.loop();
|
|
// 发布消息示例
|
|
static unsigned long lastMsg = 0;
|
|
if (millis() - lastMsg > 5000) {
|
|
lastMsg = millis();
|
|
|
|
String message = "Hello from esp32 - esp32_connect_mqtt.ino_" + String(millis());
|
|
mqtt_client.publish(mqtt_topic_publish, message.c_str());
|
|
|
|
Serial.println("消息已发布: " + message);
|
|
}
|
|
} |