unable to get MQTT Telemetry Data in Thingsboard with and Wemos D1 Mini (ESP8266)
The following is my code to read a Soil Sensor value from Analog pin and send it to the locally hosted Thingsboard on a Raspberry Pi using MQTT
Somehow the code compiles and runs successfully in the Visual Studio Code Terminal but I am unable to receive data in the Thingsboard portal.
Am I going wrong anywhere??
**CODE START
#include <Arduino.h>
#include "ESP8266WiFi.h" // Enables the ESP8266 to connect to the local network (via WiFi)
#include "PubSubClient.h" // Allows us to connect to, and publish to the MQTT broker
#define SensorPin A0
// WiFi
const char* ssid = "Hoola";
const char* wifi_password = "password";
// MQTT
// Make sure to update this for your own MQTT Broker!
const char* mqtt_server = "10.10.1.110";
const char* plant_topic = "v1/devices/me/telemetry";
//const char* plant_topic = "plant";
const char* mqtt_username = "soil1";
const char* mqtt_password = "soil1";
// The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg).
const char* clientID = "phatak";
// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker
void connect_MQTT(){
Serial.print("Connecting to ");
Serial.println(ssid);
// Connect to the WiFi
WiFi.begin(ssid, wifi_password);
// Wait until the connection has been confirmed before continuing
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Debugging - Output the IP Address of the ESP8266
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Connect to MQTT Broker
// client.connect returns a boolean value to let us know if the connection was successful.
// If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
if (client.connect(clientID, mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT Broker!");
}
else {
Serial.println("Connection to MQTT Broker failed...");
}
}
void setup() {
Serial.begin(9600);
connect_MQTT();
Serial.setTimeout(2000);
float sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
// PUBLISH to the MQTT Broker
if (client.publish(plant_topic, String(sensorValue).c_str())) {
Serial.println("Moisture sent!");
Serial.println(plant_topic);
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Moisture failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(plant_topic, String(sensorValue).c_str());
}
delay(1000);
//ESP.deepSleep(30e6);
ESP.deepSleep(0.2*60e6);
}
void loop() {
}
**CODE END
The output in the VS Code terminal window is shown below...
--- Available ports:
--- 1: COM3 'Sierra Wireless EM7355 - Gobi(TM) 5000 HS-USB Diagnostics 901F (COM3)'
--- 2: COM4 'Sierra Wireless EM7355 - Gobi(TM) 5000 HS-USB NMEA 901F (COM4)'
--- 3: COM5 'Sierra Wireless EM7355 - Gobi(TM) 5000 HS-USB Modem 901F'
--- 4: COM11 'USB-SERIAL CH340 (COM11)'
--- Enter port index or full name: 4
--- Miniterm on COM11 9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
�En��b$���ld␆␘Yִp�h�Connecting to Hoola
...........WiFi connected
IP address: 10.10.1.107
Connected to MQTT Broker!
758.00
Moisture sent!
v1/devices/me/telemetry
Hi @saintofinternet ,
Could you try to use ThingsBoard library from Library Manager (Or use Thingsboard.cpp and Thingsboard.h after build). and try to use this example.
@imbeacon I think this issue can be closed, the user directly uses the PubSubClient to publish messages himself instead of using the given library functions. Furthermore they send data without even establishing a device on ThingsBoard with an Access Token or another authentication method.