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


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts