I like to use flow variables because all nodes have access to them. You can take the flow variable to the next step by creating a flow object.
var device1= flow.get('device1') || {};
device1={type:"ADC1",
ip:"0.0.0.0",
name:"new module",
ch0:0,
ch1:0,
ch2:0,
ch3:0,
ch4:0,
ch5:0,
out1:0,
out2:0,
out3:0,
link:"down"
};
flow.set('device1', device1);
msg.payload ="";
return msg;
First, you create the variable “flow.get (… , but the more interesting part of the code transforms this simple variable into an object. In the lines below, characteristics value pairs are created. type: “ADC1”, ch0:0. As you can see, some are text and others are numeric, that’s the beauty of JavaScript. At the end, you save (set) the object. This gives you a neat container for all the characteristics of a module or device.
var device1= flow.get('device1') || {};
device1.ip = msg.ip;
Changing an object’s characteristic is as simple as shown in the code above.
if(device1.link != "up"){
msg= {payload : "Found " + device1.type + " at " + device1.ip }
return msg;
}
// if adc1 found and ip link is now active or up
if((msg.ip != "0.0.0.0") &&(device1.type=== "ADC1"))
{
device1.link="up";
msg={payload:null};
}
flow.set('device1', device1);
You can use the different characteristics for display or use them in logical statements like if-then or while loops. You always have to remember to set the object after changing it.
var configMsg= flow.get('configMsg') || {};
var device1= flow.get('device1') || {};
configMsg.type=device1.type;
configMsg.name = device1.name;
if(msg.topic == "ch0"){
configMsg.ench0 =msg.payload;
}
if(msg.topic == "ch1"){
configMsg.ench1 =msg.payload;
}
if(msg.topic == "ch2"){
configMsg.ench2 =msg.payload;
}
if(msg.topic == "ch3"){
configMsg.ench3 =msg.payload;
}
if(msg.topic == "ch4"){
configMsg.ench4 =msg.payload;
}
//if(msg.topic == "ch5"){
// configMsg.ench5 =msg.payload;
//}
if(msg.topic === "delay"){
configMsg.delay =msg.payload;
}
flow.set('configMsg', configMsg);
//if( msg.topic== "send")
//{
msg.payload = configMsg;
return msg;
//}
Here we have 2 objects in the function node “Format enable/disable messages”. The configMsg object relays the change in the state of the ADC channels when switches are toggled. In the third and fourth lines, characteristics are passed from one object to the other configMsg.type=device1.type; Further along, the switch states in the msg.payload are passed the configMsg.enchx characteristics. The fun part is at the end. msg.payload = configMsg; , wow! The whole object is passed out in the node payload.

This part is really cool. Our payload is sent to a Json node that transforms the JavaScript object into a Json object before sending it out on a UDP port.
There are a few things to remember about objects. To delete it or one of the characteristics, you have to go to the context menu, refresh the flow and delete the entire object. If you hover to the extreme right, you will see the icon appear.

Now that we have a nice Json object going to our ESP32C3 ADC, we can look at extracting the info in an Arduino sketch. That is the next post.