Serial connection Moteino - Raspberry Pi [solved]

Started by Hermann, May 18, 2015, 02:13:43 PM

Hermann

I am not sure that the issue I am having comes from the Moteino or the Raspi but let me explain what I am trying to do:

I have a Moteino that acts as a sensor and transmits every minute temperature/humidity/voltage.

On the Raspi I have a Moteino-like board I made myself (I call it MotePi) that plugs into the Raspi's pins and communicates via serial connection (115200bd). This board receives the information from the sensor and passes them on to node-red.
For those of you not familiar with node-red: In the attached picture the serial information comes in through "MotePi in". The green node "msg.payload" is just there to show the serial message. This serial message looks like this: 2/25.00/33.00/0.00/4.01/0

On the other hand I also want to send some information to the MotePi via serial port. The gray nodes in the picture "inject" a string like 99/1/0/x into the serial port. This information is used in the MotePi to flash a WS2812 LED with the corresponding color.

All these functions work OK!

The weird thing is that the "injected" string gets somehow stuck in the serial buffer and is resend back to the Raspi with the next message from the sensor!
So when I click the "inject" node the string 99/1/0/x is sent to the Raspi and the LED flashes. The next time a message comes back in from the sensor it looks like this 99/1/0/x2/25.00/33.00/0.00/4.01/0. If I click the inject node three times the message from the sensor looks like this 99/1/0/x99/1/0/x99/1/0/x2/25.00/33.00/0.00/4.01/0.

The relevant part of the Moteino program is here:
void setup() {
  Serial.begin(115200);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.promiscuous(promiscuousMode);
}

void loop() { 

  int str_length, send_to_node;
  int i = 0;
  char test_array[20];
  int out_array[20];  

  // Check for incoming serial
  while (Serial.available() > 0) {
    char inChar = Serial.read(); 
    Serial.print(inChar);
    inputString += inChar;
    // if the incoming character is a 'x', set a flag
    // so the main loop can do something about it:
    if (inChar == 'x') {
		str_length = inputString.length()+1;
		inputString.toCharArray(test_array, str_length);  //save inputstring to char array	
		//Separate inputstring in values
		valPosition = strtok(test_array, delimiters);
		while(valPosition != NULL){
			out_array[i++] = atoi(valPosition); 
			valPosition = strtok(NULL, delimiters);
		} 
		send_to_node = out_array[0];         // first part: send to node no...

		if(send_to_node == 99){
			fade(out_array[2]);
		}
		//reset values
		inputString = "";  
    } 
  }
  
  if (radio.receiveDone())
  {
    if (radio.DATALEN != sizeof(Payload)) 
	{
      Serial.print("XXX/");  //Invalid payload received, not matching Payload struct!");
    }
	else
    {
      theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
      
      Serial.print(theData.nodeId);
      Serial.print("/");
      Serial.print(theData.float1);
      Serial.print("/");
      Serial.print(theData.float2);
      Serial.print("/");
      Serial.print(theData.float3);
      Serial.print("/");
      Serial.print(theData.volt);
      Serial.print("/");
      Serial.print(theData.byte1);
    }

    Serial.println();
    Blink(LED,30);
  }
 
}


Any ideas what produces this behavior?

Hermann

Hermann

Ok, found the solution.
The string sent from node-red was not terminated with "new line" so the buffer waited until this character showed up.

Felix

Thanks for the follow up. This is commonly overlooked and can take hours to debug.