diff --git a/examples/BasicClassExample/BasicClassExample.ino b/examples/BasicClassExample/BasicClassExample.ino new file mode 100644 index 0000000..3ce0c2f --- /dev/null +++ b/examples/BasicClassExample/BasicClassExample.ino @@ -0,0 +1,24 @@ +/* +* Basic example of using the library arduino-mqtt by 256dpi as a class +* +* Edit platformio.ini to match your board. By default it's set to be +* used with an Adafruit Feather Esp32-S3 with no psram. +* +* Edit the server settings in Connectivity.h to the Broker/server you +* want to use. There's more details in the README.md file. +*/ + +#include +#include "Connectivity.h" + +Connectivity connection; + +void setup() { + Serial.begin(115200); + delay(3000); // Give Serial a little time to connect + connection.begin(); +} + +void loop() { + connection.loop(); // Stay connected +} diff --git a/examples/BasicClassExample/Connectivity.cpp b/examples/BasicClassExample/Connectivity.cpp new file mode 100644 index 0000000..198404a --- /dev/null +++ b/examples/BasicClassExample/Connectivity.cpp @@ -0,0 +1,85 @@ +#include "Connectivity.h" + +Connectivity::Connectivity(){ + +} + +void Connectivity::begin(){ + // Setup connection + mqttClientID = WiFi.macAddress(); + mqttClient.ref = this; + mqttClient.begin(mqttServer.c_str(), mqttPort, networkClient); + reconnect(); +} + +void Connectivity::loop(){ + mqttClient.loop(); + + // Stay connected + if (!mqttClient.connected()) { + reconnect(); + } + + // Send ping now and then + if (millis() > nextStatusMessage) { + nextStatusMessage = millis()+10000; + sendStatus(); + } +} + +bool Connectivity::reconnect(){ + if( WiFi.status() != WL_CONNECTED ) + { + WiFi.mode(WIFI_STA); + WiFi.begin(ssid.c_str(), password.c_str()); + while (WiFi.status() != WL_CONNECTED){ + Serial.print("."); + delay(500); + } + log_i("Connected to %s", WiFi.SSID().c_str()); + } + + // Connect to MQTT + mqttClient.onMessageAdvanced( messageReceived ); + bool ret = mqttClient.connect(mqttClientID.c_str(), mqttUsername.c_str(), mqttPassword.c_str()); + if( ret ) + { + String topic = String("/device/") + mqttClientID+"/update"; + Serial.print("Subscribing to "); + Serial.println(topic.c_str()); + mqttClient.subscribe( topic.c_str() ); + + Serial.print("Open a tool like MQTT Explorer and send a message to "); + Serial.println(topic.c_str()); + + Serial.println("You should then see the message on the Serial Monitor (if you open it)"); + + sendStatus(); + } + return ret; +} + +void Connectivity::sendStatus(){ + String topic = String("/device/") + mqttClientID+"/status"; + String payload = "{\"status\": \"online\"}"; + mqttClient.publish(topic.c_str(), payload.c_str()); +} + +// See https://github.com/256dpi/arduino-mqtt/pull/220 for details + MQTTClientCallbackAdvancedFunction Connectivity::messageReceived(MQTTClient *client, char topic[], char bytes[], int length) + { + String cTopic = topic; + String cPayload = bytes; + Serial.print("messageReceived on topic: "); + Serial.print(cTopic); + Serial.print(" payload: "); + Serial.println(cPayload); + + auto parent = (Connectivity *)client->ref; + if( parent != nullptr ) + { + Serial.print("RefTest: "); + Serial.println(parent->mqttServer); + } + return NULL; +} diff --git a/examples/BasicClassExample/Connectivity.h b/examples/BasicClassExample/Connectivity.h new file mode 100644 index 0000000..cc6d6c8 --- /dev/null +++ b/examples/BasicClassExample/Connectivity.h @@ -0,0 +1,32 @@ + +#ifndef MQTT_CONNECTIVITY_h +#define MQTT_CONNECTIVITY_h + +#include +#include + +class Connectivity { + public: + Connectivity(); + void begin(); + void loop(); + void sendStatus(); + bool reconnect(); + static MQTTClientCallbackAdvancedFunction messageReceived(MQTTClient *client, char topic[], char bytes[], int length); + private: + // Edit your server settings here + WiFiClient networkClient; + String ssid = "EnterYourSsidHere"; + String password = "EnterYourWifiPasswordHere"; + + MQTTClient mqttClient; + String mqttServer = "test.mosquitto.org"; + String mqttUsername = ""; + String mqttPassword = ""; + String mqttClientID = "undefined"; + int mqttPort = 1883; + + unsigned long nextStatusMessage = 0; +}; + +#endif // MQTT_CONNECTIVITY_h \ No newline at end of file