Close but no cigar . . .

  • Greetings,

    I have made a variation of the Serial Gateway and I have inserted the following in the void serial.Event Loop:

    void serialEvent() {
    while (Serial.available()) {

     //  look for the next valid integer in the incoming serial stream - this is  the node:
    int node = Serial.parseInt(); 
    // do it again, but throw it away:
    int w = Serial.parseInt(); 
    // do it again, but throw it away:
    int x = Serial.parseInt(); 
    // do it again, but throw it away:    
    int y = Serial.parseInt(); 
    // do it again, this is the Payload:
    int z = Serial.parseInt(); 
    // do it again, this is the current reading:
    int current = Serial.parseInt();
      // print the node and payload
      Serial.print("    The Node is :");
      Serial.print("    The Command is :");
      Serial.print("    The Payload is :");


    I am certain that someone with programming knowledge will immediately see my problem. My payload can be up to four digits long, yet the above process seems to only grab the last byte of the 1 to 4-digit (depending on the analog value) number that is the payload. Can someone show me how to tweak the last Serial.parseInt() statement into something that captures the entire payload? I also see how the same thing will occur when I have more than 10 nodes (0-9).

    Appreciatively - Baran

  • Contest Winner

    so, are you trying to blow up the existing MySensors parsing and create a whole new method?

    if so have a look at the parseAndSend( ) function in MyGateway.h. You may be able to just handle the message the way your want, or just use the parsing in the function and decide on another way to handle the payload you are sending based on modifying the code that handles that position.

    void MyGateway::parseAndSend(char *commandBuffer) {
      boolean ok = false;
      char *str, *p, *value=NULL;
      uint8_t bvalue[MAX_PAYLOAD];
      uint8_t blen = 0;
      int i = 0;
      uint16_t destination = 0;
      uint8_t sensor = 0;
      uint8_t command = 0;
      uint8_t type = 0;
      uint8_t ack = 0;
      // Extract command data coming on serial line
      for (str = strtok_r(commandBuffer, ";", &p);       // split using semicolon
      		str && i < 6;         // loop while str is not null an max 5 times
      		str = strtok_r(NULL, ";", &p)               // get subsequent tokens
    				) {
    	switch (i) {
    	  case 0: // Radioid (destination)
    	 	destination = atoi(str);
    	  case 1: // Childid
    		sensor = atoi(str);
    	  case 2: // Message type
    		command = atoi(str);
    	  case 3: // Should we request ack from destination?
    		ack = atoi(str);
    	  case 4: // Data type
    		type = atoi(str);
    	  case 5: // Variable value
    		if (command == C_STREAM) {
    			blen = 0;
    			uint8_t val;
    			while (*str) {
    				val = h2i(*str++) << 4;
    				val += h2i(*str++);
    				bvalue[blen] = val;
    		} else {
    			value = str;
    			// Remove ending carriage return character (if it exists)
    			uint8_t lastCharacter = strlen(value)-1;
    			if (value[lastCharacter] == '\r')
    				value[lastCharacter] = 0;

  • I would really rather not re-invent this wheel . . . I just might get run over. If there is a better (read: already known) method for isolating the node ID and the payload, I would really appreciate it if you could point me in th right direction. If you know where such an example may be found, I would love to see it. I said, when I hit node 11, I am going to experience this problem at th beginning, as well as the end of the string of data.

    Thanks Again - Baran

  • Hi Again Bulldog,

    For whatever reason, your code example did not show before I wrote that last post. Thank you for the clarity that you have brought, as well as your patience. This example, along with the radically different approach that you suggested in Troubleshooting, gives me plenty to chew on.

    Respectfully - Baran

  • Hi Bulldog,

    I hope you see this post. I like the way that you incorporated the switch statement into this example but I cannot quite get it to compile. I have included myGateway and mySensor libraries but I still have an error compiling and the error code (in red) looks like this:

    MySensors\MyGateway.cpp.o: In function MyGateway::parseAndSend(char*)': C:\Users\Baran\Documents\Arduino\libraries\MySensors/MyGateway.cpp:128: multiple definition of MyGateway::parseAndSend(char*)'
    sketch_may20a.cpp.o:C:\Program Files (x86)\Arduino/sketch_may20a.ino:6: first defined here
    core.a(main.cpp.o): In function main': C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:40: undefined reference to setup'
    C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:43: undefined reference to `loop'

    • list item

    If this makes any sense to you (or anyone) and you can tell me what I am forgetting in order to successfully compile (not necessarily run) this portion of code (given by Bulldog above), please enlighten me.

    Respectfully - Baran

  • Contest Winner

    The function exists already in the libraries used by MySensors.

    Post your entire sketch and let's have a look.

  • Good Day Bulldog,

    The code I used is the example you gave two days ago. Perhaps it needs some supporting code to actually operate but I was unable to even get it to compile. I have all the basic libraries in place so I am at a lose as to why I cannot compile the code you submitted. Any suggestions?

    Respectfully - Baran

  • Contest Winner


    because the function exists in the MySensors library, you are likely having a conflict with the names.

    Post your entire sketch, then we can comment. I posted a function, not an entire sketch.

Log in to reply

Suggested Topics

  • 5
  • 1
  • 2
  • 5
  • 2
  • 3
  • 13
  • 8