How to send updated V_TEXT/S_INFO to controller?



  • I'm working on an alarm clock... my controller is Domotiocz. I'm using a text sensor to send the alarm time to my arduino.... the following post helped me get it all working properly -> lcd-clock-and-text-sensor-node-with-new-v_text

    My clock also has buttons (like a traditional alarm clock), I've written all the code to change the alarm using the buttons.

    Now I want to send the new alarm time set on the arduino to the controller (Domoticz), and have the text sensor updated. I thought this would simply be a send(....) command, but I can't get it working.

    Here is a sub-set sample code of what I thought I needed to do to send the update (I'm on mysensors 2.0.*)

    #define MY_DEBUG 
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 9
    #include <SPI.h>
    #include <MySensors.h>
    #define cID_ALARM_TIME 1
    
    MyMessage MySensors_MSG_Alarm1_Time(cID_ALARM_TIME, V_TEXT);
    
    void setup() {
    	Serial.begin(115200);
    
    	request(cID_ALARM_TIME, V_TEXT);
    }
    void presentation()  {
    	sendSketchInfo("WakeUp Clock", "0.0.1");
    	
    	present(cID_ALARM_TIME, S_INFO, "Alarm Time", true);
    }
    void loop() {
    
    	int newHour = 12;
    	int newMinute = 45;
    
    	String val = String(newHour) + "." + String(newMinute);
    
    	send(MySensors_MSG_Alarm1_Time.set(val), true);
    }
    void receive(const MyMessage &message) {
    	if (message.type==V_TEXT) {
    		if (message.sensor == cID_ALARM_TIME){
    			// Here is where the return value comes from Domoticz when requesting the V_TEXT
    		}
    	}
    }
    

    When I run it I get the following console message....

    35287 TSF:MSG:SEND,9-9-0-0,s=1,c=1,t=47,pt=1,l=1,sg=0,ft=0,st=OK:1
    35297 TSF:MSG:READ,0-0-9,s=1,c=1,t=47,pt=1,l=1,sg=0:1
    35302 TSF:MSG:ACK
    

    and then my controller (Domoticz) updates the text sensor to the value of 1

    I'm very confused, please help :)


  • Admin

    You should not use the String class in a Arduino environment (google why).

    The send function takes a char buffer. If you need to append/copy things into it, use strcpy or memcpy.

    http://www.cplusplus.com/reference/cstring/strcpy/


  • Mod


  • Admin

    @mfalkvidd
    Sure, if you'd like some more control over things :)


  • Mod

    @hek otherwise you might end up in this thread https://forum.mysensors.org/topic/5212/why-ioyt-matters

    Edit: well not centrally bricked but owned and part of a botnet



  • @hek
    Ok, I think I understand what you are saying, but I can't figure out the implementation.

    This didn't work

    char temp_Msg[3];
    strcpy (temp_Msg, char("1"));
    strcpy (temp_Msg, char("."));
    strcpy (temp_Msg, char("1"));
    send(MySensors_MSG_Alarm1_Time.set(temp_Msg), true);
    

    or this

    char temp_Msg[3];
    strcpy (temp_Msg, "1");
    strcpy (temp_Msg, ".");
    strcpy (temp_Msg, "1");
    send(MySensors_MSG_Alarm1_Time.set(temp_Msg), true);
    

    or this

    char temp_Msg[3];
    strcpy (temp_Msg, '1');
    strcpy (temp_Msg, '.');
    strcpy (temp_Msg, '1');
    send(MySensors_MSG_Alarm1_Time.set(temp_Msg), true);
    

    But this does work (message is sent and loaded into Domoticz)

    send(MySensors_MSG_Alarm1_Time.set("1.1"), true);
    

    What am I not understanding?


  • Admin

      char buf[20];
      strcpy (buf,"Hello ");
      strcat (buf,"world");
      strcat (buf,"!");
    
    


  • @hek

    Oh, I didn't know I needed to use both.

    Well I tried a million combinations after seemingly reading everything on the subject, and I still couldn't get {int + char + int} converted into a char[] using strcpy and strcat

    for some reason my brain is just not wrapping itself around the concept today... maybe I'll come back to it tomorrow.

    In the mean time, I tried the following and it worked.

    char buf[20];    
    sprintf(buf, "%d.%d", Alarm1_Time_Local[0], Alarm1_Time_Local[1]);
    send(MySensors_MSG_Alarm1_Time.set(buf), true);
    

    I've read that using sprintf increases the size of your sketch... but for now I'm fine with it, since it works and I can move on to being creative and productive.. lol



Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.