From 3e80140101d608d7709d376e86f0fc8cf12c9da7 Mon Sep 17 00:00:00 2001 From: Mickey Corriveau Date: Sun, 20 Feb 2022 09:23:04 -0500 Subject: [PATCH] base thingsboard mqtt example --- examples/ThingsBoard/README.md | 18 +++ examples/ThingsBoard/main.cpp | 191 ++++++++++++++++++++++++++++ examples/ThingsBoard/platformio.ini | 19 +++ 3 files changed, 228 insertions(+) create mode 100644 examples/ThingsBoard/README.md create mode 100644 examples/ThingsBoard/main.cpp create mode 100644 examples/ThingsBoard/platformio.ini diff --git a/examples/ThingsBoard/README.md b/examples/ThingsBoard/README.md new file mode 100644 index 00000000..9d50c097 --- /dev/null +++ b/examples/ThingsBoard/README.md @@ -0,0 +1,18 @@ +# main.cpp + +Sketch example to show how to enable ThingsBoard MQTT. + +### Build the sketch and start project + +Run the build in the PlatformIO IDE. PlatformIO IDE will compile the sketch successfully and uploads it to the ESP module. You can open the serial monitor in advance to monitor the execution status of the sketch. + +On PlatformIO create a new project esp32dev and add the librairies for: +hieromon/AutoConnect +thingsboard/ThingsBoard +knolleary/PubSubClient +rpolitex/ArduinoNvs + +The first time you execute main, the captive portal will launch due to cannot connect to the Internet, and you will see the WiFi Access Point as `ESP-XXXXXX` or `esp8266ap`, `esp32ap` via your cellphone. Connect to it and use [Configure new AP](https://hieromon.github.io/AutoConnect/menu.html#configure-new-ap) from the [AutoConnect menu](https://hieromon.github.io/AutoConnect/menu.html) will allow the ESP module to connect to WiFi Access Point to reach the Internet. + +Please create a new device on the thingsboard server [Configure MQTT](https://thingsboard.io/docs/getting-started-guides/helloworld/) and then add the settings to AutoConnect MQTT settings tab, once saved the device should be connected to thingsboard and should send temperature values, which in this case are random integers. + diff --git a/examples/ThingsBoard/main.cpp b/examples/ThingsBoard/main.cpp new file mode 100644 index 00000000..80a40d24 --- /dev/null +++ b/examples/ThingsBoard/main.cpp @@ -0,0 +1,191 @@ + +#include +#include +#include +#include +#include +#include +#include + + +WebServer Server; +AutoConnect Portal(Server); +AutoConnectConfig Config; + +WiFiClient wifiClient; +ThingsBoard tb(wifiClient); +ArduinoNvs nvs; + +ACText(MqttTitle, "MQTT broker settings", "", "", AC_Tag_BR); +ACText(MqttSubTitle, "Publishing the WiFi...", "", "", AC_Tag_BR); +ACInput(MqttServer, "localhost", "Server (IP/FQDN):", "", "", AC_Tag_BR); +ACInput(MqttAccessToken, "", "AccessToken:", "", "", AC_Tag_BR); +ACInput(MqttPort, "1883", "Port:", "", "", AC_Tag_BR); +ACInput(MqttDeviceId, "", "DeviceId:", "", "", AC_Tag_BR); +ACInput(MqttPassword, "", "Password:", "", "", AC_Tag_BR); +ACSubmit(MqttSubmit, "SAVE", "/_mqtt_save"); +ACSubmit(MqttResetSettingsSubmit, "RESET SETTINGS", "/_mqtt_reset_settings"); +AutoConnectAux MqttPage("/_mqtt_setting", "MQTT Setting", true, { MqttTitle, MqttSubTitle, MqttServer, MqttAccessToken, MqttPort, MqttDeviceId, MqttPassword, MqttSubmit, MqttResetSettingsSubmit}); + +ACText(MqttSaveTitle, "Save parameters"); +ACSubmit(MqttSaveSubmit, "START", "/_mqtt_start"); +AutoConnectAux MqttSavePage("/_mqtt_save", "MQTT Setting", false, { MqttSaveTitle, MqttSaveSubmit }); + +ACText(MqttStartTitle, "Trying to connect to broker"); +AutoConnectAux MqttStartPage("/_mqtt_start", "MQTT Start", false, { MqttStartTitle }); + +ACText(MqttResetTitle, "Reset settings"); +AutoConnectAux MqttResetPage("/_mqtt_reset_settings","MQTT Setting",false,{MqttResetTitle},false); + +String mqttServer; +String mqttAccessToken; +String mqttPort; +String mqttDeviceId; +String mqttPassword; + +String nvsServer = ""; +String nvsAccessToken = ""; +String nvsPort = ""; +String nvsDeviceId = ""; +String nvsPassword = ""; + +bool tbConnectOnLoad = true; + +void rootPage() { + const char content[] = R""""( +

Menu

+ Network Config + )""""; + Server.send(200, "text/html", content); +} + +String onMqttSavePage(AutoConnectAux& aux, PageArgument& args) { + + String mqttServer = args.arg("MqttServer"); + String mqttAccessToken = args.arg("MqttAccessToken"); + String mqttPort = args.arg("MqttPort"); + String mqttDeviceId = args.arg("MqttDeviceId"); + String mqttPassword = args.arg("MqttPassword"); + + Serial.println(mqttServer); + + if(!tb.connect(mqttServer.c_str(),mqttAccessToken.c_str(),mqttPort.toInt(),mqttDeviceId.c_str(),mqttPassword.c_str())){ + Serial.println("failed"); + return String(); + } + + Serial.println("connected"); + + nvs.setString("MqServer", mqttServer); + nvs.setString("MqAccessToken", mqttAccessToken); + nvs.setString("MqPort", mqttPort); + nvs.setString("MqDeviceId", mqttDeviceId); + nvs.setString("MqPassword", mqttPassword); + + nvsServer = mqttServer; + nvsAccessToken = mqttAccessToken; + nvsPort = mqttPort; + nvsDeviceId = mqttDeviceId; + nvsPassword = mqttPassword; + + tbConnectOnLoad = true; + + return String(); +} + +String onMqttLoadSetting(AutoConnectAux& aux, PageArgument& args){ + if (nvsServer != ""){aux.getElement("MqttServer")->value = nvsServer;} + if (nvsAccessToken != ""){aux.getElement("MqttAccessToken")->value = nvsAccessToken;} + if (nvsPort != ""){aux.getElement("MqttPort")->value = nvsPort;} + if (nvsDeviceId != ""){aux.getElement("MqttDeviceId")->value = nvsDeviceId;} + if (nvsPassword != ""){aux.getElement("MqttPassword")->value = nvsPassword;} + + return String(); +} + +String onMqttResetPage(AutoConnectAux& aux, PageArgument& args){ + if(nvsServer != ""){nvs.erase("MqServer");} + if(nvsAccessToken != ""){nvs.erase("MqAccessToken");} + if(nvsPort != ""){nvs.erase("MqPort");} + if(nvsDeviceId != ""){nvs.erase("MqDeviceId");} + if(nvsPassword != ""){nvs.erase("MqPassword");} + + nvsServer = mqttServer; + nvsAccessToken = mqttAccessToken; + nvsPort = mqttPort; + nvsDeviceId = mqttDeviceId; + nvsPassword = mqttPassword; + + + MqttPage.setElementValue("MqttServer",nvsServer); + MqttPage.setElementValue("MqttAccessToken",nvsAccessToken); + MqttPage.setElementValue("MqttPort",nvsPort); + MqttPage.setElementValue("MqttDeviceId",nvsDeviceId); + MqttPage.setElementValue("MqttPassword",nvsPassword); + + tbConnectOnLoad = false; + + aux.redirect("/_mqtt_setting"); + return String(); +} + +void handleThingsboard(){ + if (tb.connected()){ + return; + } + + if(!WiFi.isConnected()){ + return; + } + + if( nvsServer == "" && nvsAccessToken == "" && nvsDeviceId == "" ){ + return; + } + + if (tbConnectOnLoad){ + Serial.println("Trying to automaticaly connect to mqtt"); + if(!tb.connect(nvsServer.c_str(),nvsAccessToken.c_str(),nvsPort.toInt(),nvsDeviceId.c_str(),nvsPassword.c_str())){ + Serial.println("Failed"); + tbConnectOnLoad = false; + return; + }else{ + Serial.println("Success"); + } + } + return; +} + +void setup() { + delay(1000); + Serial.begin(9600); + Serial.println(); + + Portal.join({MqttPage, MqttSavePage, MqttStartPage, MqttResetPage}); + + Config.autoReconnect=1; + + Server.on("/", rootPage); + Portal.on("/_mqtt_save", onMqttSavePage); + Portal.on("/_mqtt_reset_settings", onMqttResetPage); + MqttPage.on(onMqttLoadSetting); + + if (Portal.begin()) { + Serial.println("WiFi connected: " + WiFi.localIP().toString()); + } + + nvs.begin(); + nvsServer = nvs.getString("MqServer"); + nvsAccessToken = nvs.getString("MqAccessToken"); + nvsPort = nvs.getString("MqPort"); + nvsDeviceId = nvs.getString("MqDeviceId"); + nvsPassword = nvs.getString("MqPassword"); +} + +void loop() { + delay(1000); + Portal.handleClient(); + handleThingsboard(); + + tb.sendTelemetryInt("temperature", random(120)); + tb.loop(); +} \ No newline at end of file diff --git a/examples/ThingsBoard/platformio.ini b/examples/ThingsBoard/platformio.ini new file mode 100644 index 00000000..938a7bdf --- /dev/null +++ b/examples/ThingsBoard/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = + hieromon/AutoConnect@^1.3.3 + thingsboard/ThingsBoard@^0.6 + knolleary/PubSubClient@^2.8 + rpolitex/ArduinoNvs@^2.5