kopia lustrzana https://github.com/martin-ger/uMQTTBroker
125 wiersze
2.8 KiB
C++
125 wiersze
2.8 KiB
C++
/*
|
|
* uMQTTBroker demo for Arduino (C++-style)
|
|
*
|
|
* The program defines a custom broker class with callbacks,
|
|
* starts it, subscribes locally to anything, and publishs a topic every second.
|
|
* Try to connect from a remote client and publish something - the console will show this as well.
|
|
*/
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include "uMQTTBroker.h"
|
|
|
|
/*
|
|
* Your WiFi config here
|
|
*/
|
|
char ssid[] = "ssid"; // your network SSID (name)
|
|
char pass[] = "password"; // your network password
|
|
bool WiFiAP = false; // Do yo want the ESP as AP?
|
|
|
|
/*
|
|
* Custom broker class with overwritten callback functions
|
|
*/
|
|
class myMQTTBroker: public uMQTTBroker
|
|
{
|
|
public:
|
|
virtual bool onConnect(IPAddress addr, uint16_t client_count) {
|
|
Serial.println(addr.toString()+" connected");
|
|
return true;
|
|
}
|
|
|
|
virtual void onDisconnect(IPAddress addr, String client_id) {
|
|
Serial.println(addr.toString()+" ("+client_id+") disconnected");
|
|
}
|
|
|
|
virtual bool onAuth(String username, String password, String client_id) {
|
|
Serial.println("Username/Password/ClientId: "+username+"/"+password+"/"+client_id);
|
|
return true;
|
|
}
|
|
|
|
virtual void onData(String topic, const char *data, uint32_t length) {
|
|
char data_str[length+1];
|
|
os_memcpy(data_str, data, length);
|
|
data_str[length] = '\0';
|
|
|
|
Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'");
|
|
//printClients();
|
|
}
|
|
|
|
// Sample for the usage of the client info methods
|
|
|
|
virtual void printClients() {
|
|
for (int i = 0; i < getClientCount(); i++) {
|
|
IPAddress addr;
|
|
String client_id;
|
|
|
|
getClientAddr(i, addr);
|
|
getClientId(i, client_id);
|
|
Serial.println("Client "+client_id+" on addr: "+addr.toString());
|
|
}
|
|
}
|
|
};
|
|
|
|
myMQTTBroker myBroker;
|
|
|
|
/*
|
|
* WiFi init stuff
|
|
*/
|
|
void startWiFiClient()
|
|
{
|
|
Serial.println("Connecting to "+(String)ssid);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, pass);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: " + WiFi.localIP().toString());
|
|
}
|
|
|
|
void startWiFiAP()
|
|
{
|
|
WiFi.mode(WIFI_AP);
|
|
WiFi.softAP(ssid, pass);
|
|
Serial.println("AP started");
|
|
Serial.println("IP address: " + WiFi.softAPIP().toString());
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.println();
|
|
Serial.println();
|
|
|
|
// Start WiFi
|
|
if (WiFiAP)
|
|
startWiFiAP();
|
|
else
|
|
startWiFiClient();
|
|
|
|
// Start the broker
|
|
Serial.println("Starting MQTT broker");
|
|
myBroker.init();
|
|
|
|
/*
|
|
* Subscribe to anything
|
|
*/
|
|
myBroker.subscribe("#");
|
|
}
|
|
|
|
int counter = 0;
|
|
|
|
void loop()
|
|
{
|
|
/*
|
|
* Publish the counter value as String
|
|
*/
|
|
myBroker.publish("broker/counter", (String)counter++);
|
|
|
|
// wait a second
|
|
delay(1000);
|
|
}
|