• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Can I add more messages to an mqtt data packet to be recieved by an esp32

Q&A Forum › Category: ESP32 › Can I add more messages to an mqtt data packet to be recieved by an esp32
0 Vote Up Vote Down
gary1 asked 4 years ago

Starting with your code of chapter 7 of “Learn ESP32 with Arduino…” Had example up and running as advertised.

What I would like to do is send the esp32 an mqtt msg that has (and excuse any misuse of teminology here) extra sets of what I would call JSON pairs. It seems the esp32 mqtt function

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {

can only receive data in the msg.payload portion of the communication between RPi and ESP32. I would like to send several other pairs in form of msg.pin, msg.typ , etc.

I have modified the Node Red flow to output this data to the mqtt out node, but I cannot get the esp32 to receive it.

attached is the node red flow, you only need look at the flow that uses “Hall Light”

https://pastebin.com/raw/7QYs6iii

below is a snippet of the debug window showing the message I believe I am sending to the esp32

Question Tags: esp32 mqtt payload
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Gary.
Can you show the snippet of the debug window?
It is missing.
Regards,
Sara

0 Vote Up Vote Down
gary1 answered 4 years ago

I seem to have have had an epic failure to understand  how to use this posting service. Will try again.
snippet of debug window.
https://imgur.com/4wjb4Ob

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi. Thanks! I got it this time.

You need to store all the information that you want to publish in MQTT in the .payload of your JavaScript objects.

So, instead of:

out3 = {payload: "on", topic:msg.topic, req:msg.req, pin:msg.pin, typ:msg.typ, dat:msg.dat};

You should have

out3.payload = {payload: "on", topic:msg.topic, req:msg.req, pin:msg.pin, typ:msg.typ, dat:msg.dat};

I recommend importing this flow to your Node-RED: https://pastebin.com/raw/X8fiMDav

Here’s how the function looks like:

msg.pin = 2;
msg.typ = 1;
msg.req = 1;
msg.topic = "C";
msg.dat = "D";
var out2, out3 = {};

if (msg.payload == "on") {
  // Sends an object (JSON string): {payload: "on"}
  msg.payload = {payload: "on"};

  // Sends just a String: off
  out2 = {payload: "off"};

  // Sends a full object that will look like this
  // { payload: "on", topic: "C", req: 1, pin: 2, typ: 1, dat: "D" }
  out3.payload = {payload: "on", topic:msg.topic, req:msg.req, pin:msg.pin, typ:msg.typ, dat:msg.dat};
}
// Your MQTT Out node will publish what's stored in msg.payload, out.payload, out3.payload
return [msg, out2, out3];

In the JavaScript comments, you can see exactly what’s going to be sent via MQTT.

0 Vote Up Vote Down
gary1 answered 4 years ago

Yes that works just fine. I had done something similar. I was hoping that the messages thru mqtt to esp would work like other messages in node red, that is that the entire set of data pairs would flow thru every node. It was my understanding that good programming practice for all nodes was to pass these data pairs thu the node unmodified even if they were untouched by the node. I guess that just isn’t so with mqtt. I assume that the only way to pass these variables thru the system is with flow variables (or global depending on scope).  
Thanks for your prompt reply.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
Yes, in Node-RED the correct way to pass messages trough the nodes is using the payload. So you can pass an object with pairs through the payload.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.