Advertisement

Friday, January 23, 2015

Moteino to Raspberry Pi over Serial v3

I was able to get my Moteino host talking to my Raspberry Pi over the Pi's serial port.  Last time, it was working but the information coming over did not make sense.  The problem was in my sprintf statement in the Arduino sketch.  I had a %d parameter in the command.  Text was getting converted to numbers.  I replaced the %d with a %s and now the sketch sends the correct data to the Pi.

The other problem I was having is that the host was not getting all of the data from the node.  The node was sending three measuring points to the host in with about 100ms between each send.  The host was getting the first and third MPs but not the second.  The easy fix was to put a 500ms pause in between each send.  That gave the host enough time to process each piece of data.  However, that might not be a good solution.  What if two nodes send data to the same host within a half-second of the other?  I need to find a better solution.

After some experimentation, it looks like the problem was the time it took for the Moteino to blink the LED.  After each receipt, I was blinking the LED three times with a 100ms pause between each on-off cycle.  That was apparently too much time.  The middle transmission was getting lost.  I turn the blinking down to one per transmission and the host now receives all transmissions.

Also, the node was sending all three transmissions with three LED blinks with a 100ms pause.  I set the blinks to one per transmission and the host could once again not keep up.

I changed the node to blink once per transmission and pause for 150ms.  This limited transmissions to one every 1/4 of a second.  The host was able to read those.

After all of this, I set the node to only transmit one piece of data every five seconds.  That way the HESA will have time to process each reading.

More time experimenting
The HESA takes 10 seconds to look for water.  Plus, it needs a few seconds to get the temperature and humidity readings from the DHT22.  Finally, it has to update the web page every hour.  That takes a few seconds.

I set my test program on the Pi to sleep for ten seconds if there is nothing in the serial's input buffer.  I also set the node to send some data every few seconds.  When I do that, the host misses 2/3 transmissions each cycle.  That is not good.  Here is the code I am using:
import serial
import time
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
while True:
    rcv = ''
while port.inWaiting() > 0 :
    rcv = port.readline(255).strip();
if (rcv != "") :
    print ("Incoming transmission: " + rcv);
else :
    print ("Sleeping");
    time.sleep(10);

 I tried something different.  I set the timeout to 0 and read the port every ten seconds.  Here is the code in this case:
import serialimport time
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=0.0)
while True:    rcv = ''    rcv = port.read(9999)
if len(rcv) > 0 :    print ("Incoming transmission: " = rcv);else :    print ("Sleeping");    time.sleep(10);
Now, the program reads all of the data in the buffer, but it reads it as one big string.  Since I am sending the data in three transmissions with EOL, the data looks like this:

Incoming transmission: 002|101~3369.00
002|102~68.00
002|103~35.00

I will change the node program to do one transmission with all data in one long string.  That is for another day.

No comments:

Post a Comment