MQTT via ESP32 - Sample Code for the forum

Dear fellows,

after quite some trial and error I finally got my MQTT client for Decawave and ESP32 working. I would like to share the source here as it might help one or the other realizing similar projects.

What does it do? It connects the Decawave module via serial to the ESP32. It sets the Decawave into the shell model after start.
The ESP32 will subscribe to an MQTT server and listen to commands. By sending the command lep it will continuously send the position data to the MQTT server.

Important: The commands must close with a CRLF. I used MQTT Explorer to test and just pressed Enter after the command and it worked well.

2 Likes

Great work.

I was working with ESP 8266 and DWM1001 to send the data to an MQTT broker. I am successful to a good extent. I found your post today and used the serial read part of your program and found it very helpful in getting the proper data. I was getting the correct data with my code before but It has junk values sometimes. However thank you so much for sharing this. It indeed helped me and saved my time a lot.

Below I am attaching the serial read part I was using.

void loop()
{
Serial.begin(115200);
Serial.flush();

if (Serial.available() > 0)
{

String data = Serial.readStringUntil('\r\n'); 
char output[50];
data.toCharArray(output, 50);
Serial.println(output);
client.publish("esp/test", output);
delay(100);

}
Serial.flush();
Serial.end();
}

Hi Ric137k I’m trying to conect DWM1001-dew via BLE with ESP32 as well but I’m unsuccesfull.
I’m using the Arduino IDE with a WROOM 32 chip from AZ Delivery.
I see DWM working from my mobile but I’m not able to do that using the Arduino libraries.
Can you help me? can you share you sketch for the 8266?
Thanks a lot,
Esperanto

Hi Esperanto,

if you use the code from the github link I’ve in my original message it should work on your ESP32. I found the ESP8266 not working as the software serial was too slow to capture the data stream. But I might be wrong. Anyway, you said you work on ESP32 which is no problem.

thanks a lot Skye,
I’ve downloaded your SW and will try it tonight.
Esperanto

I’m afraid but the sw you provided is not what I was looking for.
I normally connect to DWM via serial but now I would like to get these information via BLE. It is possible to read them using nrf-connect on an android phone. So I’m sure it is possible to do that but I’m not able to properly connect using ESP32.
I’ve been able to see all the characteristics but just once using N.kolban libraries with some modiications.
Can someone help me?
Thanks a lot.

Hi Esperanto,

I also planned to use BLE at that time. My guess is, that the Arduino IDE library for BLE is not capable of making that connection. You need to use the Espressif SDK in my opinion. I spent a long time testing NKolban’s library and was not successful. As you said it works from the mobile phone.

Hi,
Thanks a lot for the code because it is what I was looking for. Good people exists :slight_smile:
I wonder how did you connect via serial, i mean the pin connection.

Thanks again for your work
Carlos

Hi!
How did you manage to extent the code to ESP8266. I’m having some energy consumption problems because if DWM1001 is connected to ESP8266, then, the computer cannot connect to the ESP :frowning:

Can you share how did you do it?

Hi Carlos,

apologies for the late reply. To answe your last question first, the code I published on GitHub is for an ESP32. I experimented with the ESP8266 but I’m thinking that it can’t handle the 115200bps of the Decawave. Therefore you need to use an ESP32 which offers hardware (not software) serial. You will then have no problem to connect.
The pin connection: I opened the device and soldered the cables to the serial output. The PCB is documented by Decawave (check the PDFs that came with the evaluation kit on their website).

Hope this helps!

Hi @Skye,
I didn´t receive your answer until now :frowning:

Due to it wasn´t working, I tried to upload and adapt the code to Arduino Mega but it is not working neither. Maybe I will buy the ESP32 communicate the boards.

However, I don´t understand why with Arduino Mega is not working neither.

Best Regards,
Carlos

Yes, I suggest you buy an ESP32 as it’s a little invest. The code definitely runs on it, I demoed it many times.