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
Hi Gary.
Can you show the snippet of the debug window?
It is missing.
Regards,
Sara
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
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.
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.