mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
添加依赖库
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
SimpleMQTTClient.ino
|
||||
The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
|
||||
Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
|
||||
It will also send a message delayed 5 seconds later.
|
||||
*/
|
||||
|
||||
#include "EspMQTTClient.h"
|
||||
|
||||
EspMQTTClient client(
|
||||
"WifiSSID",
|
||||
"WifiPassword",
|
||||
"192.168.1.100", // MQTT Broker server ip
|
||||
"MQTTUsername", // Can be omitted if not needed
|
||||
"MQTTPassword", // Can be omitted if not needed
|
||||
"TestClient", // Client name that uniquely identify your device
|
||||
1883 // The MQTT port, default to 1883. this line can be omitted
|
||||
);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
// Optionnal functionnalities of EspMQTTClient :
|
||||
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
|
||||
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
|
||||
client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
|
||||
}
|
||||
|
||||
// This function is called once everything is connected (Wifi and MQTT)
|
||||
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
|
||||
void onConnectionEstablished()
|
||||
{
|
||||
// Subscribe to "mytopic/test" and display received message to Serial
|
||||
client.subscribe("mytopic/test", [](const String & payload) {
|
||||
Serial.println(payload);
|
||||
});
|
||||
|
||||
// Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
|
||||
client.subscribe("mytopic/wildcardtest/#", [](const String & topic, const String & payload) {
|
||||
Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
|
||||
});
|
||||
|
||||
// Publish a message to "mytopic/test"
|
||||
client.publish("mytopic/test", "This is a message"); // You can activate the retain flag by setting the third parameter to true
|
||||
|
||||
// Execute delayed instructions
|
||||
client.executeDelayed(5 * 1000, []() {
|
||||
client.publish("mytopic/wildcardtest/test123", "This is a message sent 5 seconds later");
|
||||
});
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
client.loop();
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
twoMQTTClientHandling.ino
|
||||
The purpose of this exemple is to illustrate how to handle more than one MQTT connection a the same time in the same sketch.
|
||||
Getting into "SimpleMQTTClient.ino" before this one is recommended (there is more comments)
|
||||
*/
|
||||
|
||||
#include "EspMQTTClient.h"
|
||||
|
||||
void onConnectionEstablishedClient2();
|
||||
|
||||
// The client #1 will handle wifi connection (connecting, retrying, etc) and MQTT connection to 192.168.1.100
|
||||
EspMQTTClient client1(
|
||||
"WifiSSID",
|
||||
"WifiPassword",
|
||||
"192.168.1.100",
|
||||
"MQTTUsername",
|
||||
"MQTTPassword",
|
||||
"TestClient1"
|
||||
);
|
||||
|
||||
// The client #2 will handle MQTT connection to 192.168.1.101.
|
||||
EspMQTTClient client2(
|
||||
"192.168.1.101",
|
||||
1883,
|
||||
"MQTTUsername",
|
||||
"MQTTPassword",
|
||||
"TestClient2"
|
||||
);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
// We redirect the connection established callback of client2 to onConnectionEstablishedClient2.
|
||||
// This will prevent the two client from calling the same callback (default to onConnectionEstablished)
|
||||
client2.setOnConnectionEstablishedCallback(onConnectionEstablishedClient2);
|
||||
}
|
||||
|
||||
// For client1
|
||||
void onConnectionEstablished()
|
||||
{
|
||||
client1.subscribe("mytopic/test", [](const String & payload) {
|
||||
Serial.println(payload);
|
||||
});
|
||||
client1.publish("mytopic/test", "This is a message from client1");
|
||||
}
|
||||
|
||||
// For client2
|
||||
void onConnectionEstablishedClient2()
|
||||
{
|
||||
client2.subscribe("mytopic/test", [](const String & payload) {
|
||||
Serial.println(payload);
|
||||
});
|
||||
client2.publish("mytopic/test", "This is a message from client2");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
client1.loop();
|
||||
client2.loop();
|
||||
}
|
||||
Reference in New Issue
Block a user