Go Down

Topic: ESP8266 MQTT Thingsboard - No telemetry problem (Read 243 times) previous topic - next topic


Oct 12, 2018, 04:11 am Last Edit: Oct 12, 2018, 05:43 am by davie_gravy
I'm using a local Thingsboard server to handle my MQTT requests from various test devices.

The device I'm having an issue with is a WeMos D1 mini pro.  I've had no issues getting it to connect to WiFi and to connect and publish messages using MQTT to a broker.  I have verified my code and setup works great with mosquitto.  But now with Thingsboard, I cannot get Thingsboard to read and interpret the MQTT message (the same message working with mosquitto). 

I used the following guides/resources for configuring Thingsboard and coding the WeMos chip:

Here's the thing, I know it's working and connecting to the Thingsboard server, because my debugging messages show connection success using the Thingsboard device token and in the server attributes I can see the update lastConnect and lastDisconnect values.  It just will not display anything under telemetry data for the device.

I can confirm the Thingsboard server is working as I installed node and mqtt and tested the javascript demo simulation script included in the description of the youtube video link above.  It works great, telemetry data shows up immediately, shows the 1 second updates, and I was able to create a real-time widget display.

Below is my code with sensitive information redacted.  It's an ESP8266 deep sleep example I have been working with for a bit now.  You can see I tried formatting the JSON string in many different ways... string, char array, various way of escaping the quotes, trying int, string, and boolean data types all of which Thingsboard would not pickup and display.

UPDATE: Thingsboard logs show the following... Thinking my deep sleep might be causing the issue.
2018-10-11 21:32:36,534 [nioEventLoopGroup-5-9] ERROR o.t.s.t.mqtt.MqttTransportHandler - [mqtt393] Unexpected Exception
java.io.IOException: An existing connection was forcibly closed by the remote host
   at sun.nio.ch.SocketDispatcher.read0(Native Method)
   at sun.nio.ch.SocketDispatcher.read(Unknown Source)
   at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
   at sun.nio.ch.IOUtil.read(Unknown Source)
   at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
   at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
   at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1108)
   at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:345)
   at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
   at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
   at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
   at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
   at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
   at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
   at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
   at java.lang.Thread.run(Unknown Source)
2018-10-11 21:32:36,799 [nioEventLoopGroup-5-10] INFO  o.t.s.t.mqtt.MqttTransportHandler - [mqtt394] Processing connect msg for client: ESP8266PIR01!
UPDATE2:  I've tried adding proper disconnect code for the subpub client and the WiFi client before going to deep sleep.  Still no telemetry.  Same error message as above.

UPDATE3:  I reverted back to the commented out JSON output using ID and motion keys.  I'm not getting the Java exception anymore.  It seems like I'm close but I can't seem to get a valid publish msg.
2018-10-11 22:40:41,218 [nioEventLoopGroup-5-6] INFO  o.t.s.t.mqtt.MqttTransportHandler - [mqtt414] Processing connect msg for client: ESP8266PIR01!
2018-10-11 22:40:41,233 [nioEventLoopGroup-5-6] INFO  o.t.s.t.mqtt.MqttTransportHandler - [mqtt414] Closing current session due to invalid publish msg [DeepSleepDemo/PIR][-1]
Any help is greatly appreciated.
Code: [Select]
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>

Ticker ticker;

#define TOKEN ""
#define NB_TRYWIFI    10

const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";

WiFiClient espClient;
PubSubClient client(espClient);

void tick()
  //toggle state
  int state = digitalRead(BUILTIN_LED);  // get the current state of GPIO1 pin
  digitalWrite(BUILTIN_LED, !state);     // set pin to the opposite state

void sendMQTTMessage(){
  if (!client.connected()) {

  Serial.println("Collecting temperature data.");

  // Reading temperature or humidity takes about 250 milliseconds!
  long h = random(25, 85);
  // Read temperature as Celsius (the default)
  long t = random(0, 110);

  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(" *C ");

  String temperature = String(t);
  String humidity = String(h);

  // Just debug messages
  Serial.print( "Sending temperature and humidity : [" );
  Serial.print( temperature ); Serial.print( "," );
  Serial.print( humidity );
  Serial.print( "]   -> " );

  // Prepare a JSON payload string
  String payload = "{";
  payload += "\"temperature\":"; payload += temperature; payload += ",";
  payload += "\"humidity\":"; payload += humidity;
  payload += "}";

  // Send payload
  char attributes[100];
  payload.toCharArray( attributes, 100 );
  client.publish( "v1/devices/me/telemetry", attributes );
  Serial.println( attributes );
  String payload = "\"{ID\":\"PIR001\",\"motion\":\"1\"}";
  char attributes[100];
  payload.toCharArray(attributes, 100);
  client.publish("DeepSleepDemo/PIR", attributes);
  Serial.print("Sending: ");
  String payload2 = "\"{ID\":\"PIR001\",\"motion\":\"0\"}";
  char attributes2[100];
  payload2.toCharArray(attributes2, 100);
  client.publish("DeepSleepDemo/PIR", attributes2);
  Serial.print("Sending: ");

void setup() {
  int start = millis();
  // Fait clignoter la LED intégré durant la connexion au réseau WiFi - Blink Bleu Led during WiFi connexion
  ticker.attach(0.5, tick);
  // Raison du réveil - restart reason
  Serial.println(""); Serial.print("Reason startup :");Serial.println(ESP.getResetReason());
  WiFi.begin(ssid, password);

  Serial.println("Connecting to WiFi.");
  int _try = 0;
  while (WiFi.status() != WL_CONNECTED) {
    if ( _try >= NB_TRYWIFI ) {
        Serial.println("Impossible to connect WiFi network, go to deep sleep");
  Serial.println("Connected to the WiFi network");
  Serial.println("Send PIR Status to MQTT broker");
  client.setServer(mqtt_server, 1883);
  // Durée du réveil - waking time
  Serial.println("Waking time: ");Serial.print(millis()-start);Serial.println("ms");

  digitalWrite(BUILTIN_LED, LOW);
  Serial.println("Go to deep sleep");

void loop() {

boolean reconnect() {
 // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Connecting to ThingsBoard node ...");
    // Attempt to connect (clientId, username, password)
    if ( client.connect("ESP8266PIR01", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.print( client.state() );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );

Go Up