How to set up non-lorawan application & sensors with MQTT

kigehron
Sat, 15 Apr 2023 10:30:16 +0000
How to get started

Hello everyone,

I have some questions regarding setting up a non-lorawan gateway (ESP23) with non-lorawan sensors in TTN using MQTT. Firstly, I have created the ESP32 as an “Application” with an ID, is this correct? Secondly, I believe I don’t need to create the sensors in “End devices” since they are not lorawan-compatible, is that correct?

Moreover, I’m wondering what the publish and subscribe commands should be for my setup. Is this format only for lorawan sensors or should I use theem:

#Publish sensor data
client.publish(“v3/esp32-mqtt@ttn/devices/+/up”)
#Subscribe to sensor data
client.subscribe(“v3/esp32-mqtt@ttn/devices/+/down”)

Here are some excerpts from my code:

#include <PubSubClient.h>
#include <WiFi.h>
const char* ssid = "Vodafone";
const char* password = "password";
const char* ttn_server = "eu1.cloud.thethings.network";
const char* ttn_user = "esp32-mqtt@ttn";
const char* ttn_password = "password";
const int ttn_port = 1883;
WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  client.setServer(ttn_server, 1883);
  client.setCallback(callback_led);
   while (!client.connected()) {
    if (client.connect("ESP-32a", ttn_user, ttn_password)) {
      Serial.println("Connected to MQTT-Broker.");
      client.setBufferSize(2048);
      client.subscribe("Room1/Temperature");
    }  else {
      Serial.println("Connection failed.");
      delay(2000);
    }
  }
}
oid callback_led(char* topic, byte* payload, unsigned int length){
  String payloadString = "";
  actTopic = topic;
  if (actTopic == "Temperature") {
    for (int i = 0; i < length; i++) {
      payloadString += (char)payload[i];
    }
    temperature = payloadString.toFloat();
    if (temperature>25){
      digitalWrite (redLed,HIGH);
      }
    else {
      digitalWrite (redLed,LOW);
      }
  }

Most of the time, I can only find information on Lorawan-compatible gateways and sensors on Google or in forums. Therefore, I would appreciate it if you could give me a hint on similar topics. Any insights or suggestions would be greatly appreciated. Thank you in advance!

4 posts - 2 participants