Lora to MQTT Server based Heltec V2

Hello @ all

I need to manage a Gateway to receive Data over Lora and paste it to my own MQTT Broker for use the Data in Homeassistant. I think that is not only for me useful so maybe some of you are interested too in doing this :slight_smile:

At the Moment I have copy and paste 2 codes together but it sends not the right infos to my broker I think it s only a little problem in the code but I see it not :confused: maybe some of you see it faster because you look the code first. time and not like me every day :smiley:

I want to let the code simple and little as possible because I donโ€™t want to mess up with many libraries and stuff I donโ€™t need for this โ€œsimpleโ€ read and past code.

Here is the gateway code :

/*
  SimpleMQTTClient.ino
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
  Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
  It will also send a message delayed 5 seconds later.
*/

#include "EspMQTTClient.h"

#include <SPI.h>
#include <LoRa.h>

#define PIN_NSS   18
#define PIN_RESET  14
#define PIN_DIO0   26


EspMQTTClient client(
  "...",
  "...",
  "192.168.2.200",  // MQTT Broker server ip
  "MQTT",   // Can be omitted if not needed
  "...",   // Can be omitted if not needed
  "TestClient",     // Client name that uniquely identify your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

void setup()
{
  Serial.begin(115200);

   LoRa.setPins(PIN_NSS, PIN_RESET, PIN_DIO0);

  while (!LoRa.begin(866E6)) {
    Serial.print(".");
    delay(500);
  }

  LoRa.setSyncWord(0xC6);
  Serial.println("[OK]");

  // Optionnal functionnalities of EspMQTTClient : 
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
  client.enableLastWillMessage("TestClient/lastwill", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  // Subscribe to "mytopic/test" and display received message to Serial
  client.subscribe("mytopic/test", [](const String & payload) {
    Serial.println(payload);
  });

  // Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
  client.subscribe("mytopic/wildcardtest/#", [](const String & topic, const String & payload) {
    Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
  });

  // Publish a message to "mytopic/test"
 // client.publish("mytopic/test", "This is a message"); // You can activate the retain flag by setting the third parameter to true

 
}

void loop()  {

   int packetSize = LoRa.parsePacket();
   
  if (packetSize) {
    Serial.print("Received packet: ");

    while (LoRa.available()) {
      Serial.print(LoRa.readString());
      client.publish("mytopic/heltec", LoRa.readString());
    }

    Serial.println(" (RSSI = " + String(LoRa.packetRssi()) + ")");
  }
  
  client.loop();
}


Notice: Please know ! Its NOT for act over TTN it is simple gateway only here for Lora packet like from the Sandeep Mistry send over a node then receive by this gateway build for heltec V2 board and parse over wifi to a MQTT Broker .

Hope somebody see my failure and maybe other can use this code too for there plans and stuff.

regards from Germany
Marc

If it is not TTN related this is not the right forum.