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
3 out of 3
Suggested Topics
-
Arduino Celebrates 10 years (Malmö/Sweden)
Announcements • 29 Mar 2014, 17:08 • hek 29 Mar 2014, 17:08 -
Saving last known good state, but not in EEPROM
Development • 30 Jan 2024, 18:46 • OldSurferDude 15 Jan 2025, 08:51 -
LAN8720A - will mysensors work with this module
Development • 13 Nov 2024, 17:06 • Marcin 15 Nov 2024, 10:59 -
MQTT-Help me understand about the MQTT Gateway.
Development • 30 days ago • dpcons 19 days ago -
Radio waking up for no reason.
Development • 4 Jul 2020, 21:09 • Sasquatch 15 Jan 2025, 08:33 -
Adding Listen only device to my system.
Development • 26 Feb 2025, 00:39 • dpcons 26 Feb 2025, 06:26