Array in send message command
-
Hi there,
I am currently working on a node with openEnergy Monitor.
I'm measuring a total of 6 SCT013 current transformer and wondering how in the mySensor code to include everything in a for loop.
For example, to send the message, I hope to do:[...] MyMessage msgInA0(CHILD_ID_V_A0, V_WATT); MyMessage msgInA1(CHILD_ID_V_A1, V_WATT); MyMessage msgInA2(CHILD_ID_V_A2, V_WATT); MyMessage msgInA3(CHILD_ID_V_A3, V_WATT); MyMessage msgInA4(CHILD_ID_V_A4, V_WATT); MyMessage msgInA5(CHILD_ID_V_A5, V_WATT); #define NSENSORS 6 [...] //Read values and send to gateway for (int i=0; i<NSENSORS; i++) { watt[i] = (emon[i].calcIrms(1480))*230*0.9; send(msgInA[i].set(watt[i], 1)); //Error: not working }
But it doesn't work.
Do you have a trick to avoid writing the same code X times?
Thank you,
-
@yourry for an example of array usage see Temperature Sensors build page.
MyMessage msgInA(0, V_WATT); // only one message required #define NSENSORS 6 //Read values and send to gateway for (int i=0; i<NSENSORS; i++) { watt[i] = (emon[i].calcIrms(1480))*230*0.9; send(msgInA.setSensor(i).set(watt[i], 1)); //setSensor(i) is the magic }
-
Great, thanks for your reply and help.
I was able to do what I wanted, I give you some code snippets to make several messages via a for loop,
I'm not a code proffesional but this works:// Initialize messages MyMessage msgInA(0,V_CURRENT); MyMessage msgWhA(0,V_WATT); #define NSENSORS 6 //used current sensors void setup() { //initialize energy monitor CurrentSensor for (int i=0; i<NSENSORS; i++) { wh[i] = { 0.0 }; //initialize wh lwhtime[i] = {0}; //initialize time present(i, S_MULTIMETER); } for (int i=0; i<NSENSORS; i++) { present(i, S_POWER); } } void loop() { for (int i=0; i<NSENSORS; i++) { send(msgInA.setSensor(i).set(Irms[i], 1)); send(msgWhA.setSensor(i).set(wh[i], 1)); } }
Thanks
1 out of 3
Suggested Topics
-
Arduino Celebrates 10 years (Malmö/Sweden)
Announcements • 29 Mar 2014, 17:08 • hek 29 Mar 2014, 17:08 -
PJON and Minicore not working
Development • 21 Mar 2025, 19:51 • Trand 21 Mar 2025, 19:51 -
Counting Incoming and Outgoing Messages from a Gateway
Development • 10 Dec 2024, 21:57 • Trand 14 Dec 2024, 20:23 -
Home Assistant/MySensors quirks
Development • 17 Mar 2025, 02:35 • OldSurferDude 17 Mar 2025, 02:35 -
Adding Listen only device to my system.
Development • 26 Feb 2025, 00:39 • dpcons 26 Feb 2025, 06:26 -
Gateway without a radio
Development • 12 Jan 2025, 23:19 • OldSurferDude 14 Jan 2025, 22:07