Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base thingsboard mqtt example #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/ThingsBoard/README.md
Original file line number Diff line number Diff line change
@@ -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.

191 changes: 191 additions & 0 deletions examples/ThingsBoard/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@

#include <ThingsBoard.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <AutoConnect.h>
#include <ArduinoNvs.h>


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""""(
<h1>Menu</h1>
<a href="/_ac">Network Config</a>
)"""";
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();
}
19 changes: 19 additions & 0 deletions examples/ThingsBoard/platformio.ini
Original file line number Diff line number Diff line change
@@ -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