Advertisement

Friday, January 16, 2015

Connect to Raspberry Pi Over Serial Connection

Tonight at my weekly me time at the Milwaukee Makerspace, I am going to attempt to get my Moteino connected to my Raspberry Pi using the serial connection on the Raspberry Pi.  Also, to be a bit different, I am going to try to take notes on the blog as I go.  This will either help me take better notes or make this blog entry completely unintelligible to any  reader who does not think like me.

There are instructions to work with the Raspberry Pi's serial port at this web page: http://elinux.org/RPi_Serial_Connection.  I scrolled down to the section titled "Connection to a microcontroller or other peripheral".  There are several things to do to stop the Linux kernel from using the serial port.  This is a prerequisite.  It looks like there is a script someone made to do all of these things automatically.  I am going to try to do the steps myself.

I edited the /boot/cmdline.txt file.  Also, commented out a line in /etc/inittab that opens a login prompt ("getty") on the serial port.

Rebooted.  Logged in with no problems.  There are two checks on the web page that you can run to verify that you did it right.  Both checks passed.  

The next step is to try interacting with the serial port using Python code.  I installed the PySerial package by typing 
sudo apt-get install python-serial 
at the prompt.  While the install was running, I noticed a message saying that there were some packages that were no longer used that could be uninstalled.  I ran 
sudo apt-get autoremove
to get rid of those packages.

This page has a very small python program to test the serial connection.  I used nano to create it. 

The first test I did was to connect the Raspberry Pi's serial pins to the serial pins on the Moteino --
 Tx to Rx, Rx to Tx, and Gnd to Gnd.  Then, I connected the Moteino to my Linux laptop using the FTDI adapter.  I loaded the Arduino IDE on the laptop, uploaded the HESA_Host sketch, plugged in the USB to the Moteino, and ran the python program.  Nothing happened.  I don't know if the problem was that the Moteino does not know what to do when there are two serial connections or if my sketch is bad.



The next test I did was to bypass the Moteino and connect the serial wires from the Raspberry Pi directly to the FTDI adapter.  That test worked.  The terminal screen on the Arduino displayed the output from the Pi and the Pi received the text I typed in on the terminal screen.  Cool!

Tried again with the FTDI connected to the Moteino and the Moteino connected to the Pi.  Made some small modifications to the sketch and tried uploading it to the Moteino.  The sketch would not upload.  

Unplugged the TxRx wires from the Pi.  Now, the sketch will upload.

For the next test, I changed the python code so it will send the letter "t" to the serial port every second, read the results on the serial port, and print the results, if any to the screen.  I also connected a wire from one of the 5v outputs on the Pi to the VIn pin on the Moteino.  I ran the python code and it worked perfectly.  The Moteino faithfully sent its temperature back to the Pi everytime it received a T.

Time to try the whole solution.  I setup my node with the DHT11 temperature sensor.  Then, I started the python program and waited a few seconds.  Next, I powered up the Moteino host.  It transmitted its messages from the setup part of the sketch.  Finally, I powered up the Moteino node.  It started transmitting its data every few seconds.  Everything worked.  The host received the transmissions and sent them to the Raspberry Pi.  The python program displayed the transmissions on the screen.


One problem is that it looks like the python program started displaying the ascii text as hex characters, ex. 0\xe0\x00.  That seems to have started after it received a "]" character.

Tried changing the baudrate on both devices to 9600.  That did not help.
Tried adding a \n to the end of each transmission to the serial port.  That did not help.
Tried putting a 1ms delay after every transmission.  That did not help.

The weird thing is that if I send a T to the Moteino, the sketch returns the temperature text with no problems.  Anything else looks like hex.

Figured it out.  I had some code in the sketch turn off the Moteino in between transmissions to save power.  This was the cause of the garbled code.  Turning off the power save feature fixed the problem.  I don't really need the power save feature since the host Moteino will get power from the Pi.

The new problem is that the data being sent to the PI does not appear to be the data the node is sending.  The data should be something like "002|102~87", but it is some other combination of number.  I'll have to look into that later.  

Also, need to figure out how to know when a batch of data is from one node.  Maybe I could put some text around each end of the package.  For example, ">>>002|102~67<<<".

I'll have to figure that out some other time.  That's enough for one night.