Displaying data from TTN using ESP8266

Hello,

I have a few nodes running properly using TTN V3. At the moment I’m using Cayenne to display the data on a PC.
But I’m looking for a solution displaying the data using a display like the Nextion without using a PC or Laptop running permanently.
I’m thinking of a solution using ESP8266. Sending data from ESP8266 to the display is not my problem, but how to get the data from TTN to the ESP8266?
Best regards
Thomas

MQTT?

1 Like

I never have used MQTT until now… perhaps I should try!
I think I will find a step by step introduction to MQTT, this is a really new thing for me.

Hello Thomas,
I have tried to get data from TTN to the ESP8266 over MQTT. Unfortunately, it does not work. The callback method is never called. If you find a solution, please let me know how you got it working.

Best regards Cris

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 // WiFi
const char *ssid = "SSID"; // Enter your WiFi name
const char *password = "PASSWORD";  // Enter WiFi password
// MQTT Broker
const char *client_id = "esp8266";
const char *mqtt_broker = "eu1.cloud.thethings.network";
const char *topic = "v3/XXX@ttn/devices/+/up";
const char *mqtt_username = "XXX@ttn";
const char *mqtt_password = "NNSXS.XXX";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message:");
    String message;
   for (int i = 0; i < length; i++) {
        message = message + (char) payload[i];  // convert *byte to string
    }
    Serial.print(message);
    Serial.println();
    Serial.println("-----------------------");
}

void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    // connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    Serial.println("Connected to the WiFi network");
    //connecting to a mqtt broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
    
        //client_id += String(WiFi.macAddress());
        Serial.println("Connecting to ttn mqtt broker.....");
        if (client.connect(client_id, mqtt_username, mqtt_password)) {
            Serial.println("ttn mqtt broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }
    client.subscribe(topic);
}

void loop() {
    client.loop();
}

First you should probably get a command line version working on a PC, to make sure you understand how things are supposed to work. You might also play with wildcard topic subscriptions to initially get everything your account is allowed to, and avoid any possible mistakes in subscribing to the wrong topic, then narrow it once you know you’re doing it right.

You had better use MQTT over TLS (SSL) as well! If you don’t do that, the MQTT password goes out in cleartext. And you also need to change the password it seems you’ve posted here, even though you may have obfuscated the account.

Once you have things working well with MQTT over TLS from a PC, then you can try to recreate it in ESP-IDF. I know there’s good MQTT over TLS support in that - have only tried it for other purposes on an ESP32 but the software stack should be the same and my guess is it will fit.

I’ve tried a similar scetch, same result, i cannot get any data coming from TTN by MQTT.
But when I try this scetch in connection with another MQTT broker (broker.emqx.io) and MQTTX the scetch is running, I can get messages send from MQTTX via broker.emqx.io.
So why I do not get messages from TTN MQTT?
The device is sending data via MQTT, I got the data in MQTTX.
Where is the problem?
Does anybody have a scetch running to receive MQTT data coming from TTN and received by ESP8266?

Same advice as to the previous asker: first get it working from a PC…

Hello Christian,

perhaps you have to enlarge the buffer size!
Insert the following line just before the subscribe line:

client.setBufferSize(2048);
client.subscribe(“v3/app-name@ttn/devices/device-name/up”);

I tried it, it’s running now!

Best regards
Thomas

1 Like

Hello Thomas

Thank you very much for the information :smiley: :ok_hand:. I will try it out.

Best regards Christian

Hello everyone.
can I send the data from lora gateway to my server and process the data and how can I load this data to a Json object, thank you

Welcome,

I think you need read documentation and do the lear LoraWAN course.

It will become very apparent of how the data flow from your sensors all the way through all the parts of the ecosystems to you server.

2 Likes