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. My Project
  3. Concatenated String Doesn't Send Correctly

Concatenated String Doesn't Send Correctly

Scheduled Pinned Locked Moved My Project
2 Posts 2 Posters 40 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.
  • M Offline
    M Offline
    mrmuszynski
    wrote on last edited by mrmuszynski
    #1

    Hi all!

    I'm working with sending messages from one of my sensors back to my controller for the first time, and I'm a little stumped...

    I have this message defined:

    MyMessage msg(CHILD_ID_LIGHT, V_VAR1);
    

    And this code that uses it to send a message to the controller

    void open_scratchpad(int expectedScratchpadValues) {
      send(msg.set("Scratchpad is open"));
      send(msg.set("string " + String(expectedScratchpadValues)));
    }
    

    I expect to get the following messages back (in this example, expectedScratchpadValues=4):

    2;1;1;0;24;Scratchpad is open
    2;1;1;0;24;string 4
    

    Instead, I get:

    2;1;1;0;24;Scratchpad is open
    2;1;1;0;24;0
    

    But if I do

    Serial.println("string " + String(expectedScratchpadValues)
    

    then the arduino console correctly prints

    string 4
    

    Never mind the exact code here. I'm just playing around to learn the messaging protocol, so it really doesn't mean much of anything. I just want to be able to convert ints to strings, concatenate those strings, and then pass that concatenated result back to my controller.

    Thanks for you help!

    V 1 Reply Last reply
    0
    • M mrmuszynski

      Hi all!

      I'm working with sending messages from one of my sensors back to my controller for the first time, and I'm a little stumped...

      I have this message defined:

      MyMessage msg(CHILD_ID_LIGHT, V_VAR1);
      

      And this code that uses it to send a message to the controller

      void open_scratchpad(int expectedScratchpadValues) {
        send(msg.set("Scratchpad is open"));
        send(msg.set("string " + String(expectedScratchpadValues)));
      }
      

      I expect to get the following messages back (in this example, expectedScratchpadValues=4):

      2;1;1;0;24;Scratchpad is open
      2;1;1;0;24;string 4
      

      Instead, I get:

      2;1;1;0;24;Scratchpad is open
      2;1;1;0;24;0
      

      But if I do

      Serial.println("string " + String(expectedScratchpadValues)
      

      then the arduino console correctly prints

      string 4
      

      Never mind the exact code here. I'm just playing around to learn the messaging protocol, so it really doesn't mean much of anything. I just want to be able to convert ints to strings, concatenate those strings, and then pass that concatenated result back to my controller.

      Thanks for you help!

      V Offline
      V Offline
      virtualmkr
      wrote on last edited by virtualmkr
      #2

      @mrmuszynski
      Hi, there are some setters for message payload types but none for type String, see: Message manipulation.

      You need to convert the resulting String into const char* like this:

      send(msg.set(("string " + String(expectedScratchpadValues)).c_str()));
      

      But in general I would try to avoid usage of the String data type, especially for the use case in your code example. You could use e.g.:

        char str[80];
        sprintf(str, "string %d", expectedScratchpadValues);
        send(msg.set(str));
      

      For Arduino String read more e.g. here: The Evils of Arduino Strings

      HTH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      28

      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