Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Array in send message command

Array in send message command

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 41 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yourry
    wrote on last edited by
    #1

    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,

    H 1 Reply Last reply
    0
    • Y yourry

      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,

      H Offline
      H Offline
      hard-shovel
      wrote on last edited by
      #2

      @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
             
      }
      
      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yourry
        wrote on last edited by
        #3

        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 Reply Last reply
        1
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        15

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular