Basestation + Raspberry Pi

*Big thanks to Shawn from the Sensor the Earth project who helped me setup the Raspberry Pi and problem solve to get it talking to the Arduino!*

IMG_1533.JPG

With the buoy test mule up and running the next big step is to setup the basestation to push the sensor readings online. I had already setup an Arduino UNO + LoRa radio to act as the basestation and listen for the buoy transmission using one of the generic RadioHead example sketches. This displays the message from the buoy onto Serial Monitor and sends a confirmation message back to the buoy every-time the buoy sends out a broadcast. But this required that the basestation be plugged into my laptop every time I wanted to verify that the basestation was receiving a signal from the buoy.

So instead of using the laptop to power the basestation I decided to setup an RPi to be on my wifi network and communicate directly with the Arduino over USB. This way I could use Secure Shell protocol (SSH) to login to the RPi via my laptop and see the data streaming from the basestation WITHOUT ever having to physically connect to the basestation to my laptop.

Step 1: configure the RPi for headless operation on a wifi network

Download the latest Rasbian distribution and use ApplePi-Baker to upload the image file onto a 16 Gb SD card. I am running Rasbian Stretch Lite, this version does not have a graphical user interface (GUI) running desktop graphics as I plan to SSH into the RPi from my laptop and only use the terminal for communication and control.

Screen Shot 2018-06-04 at 8.40.17 PM.png

You can also use ApplePi-Baker to flash one of your backup image files onto the SD card.Screen Shot 2018-06-04 at 8.20.08 PM.png

If you are using a clean Rasbian install you will want to setup the RPi to allow SSH on boot and also configure the wifi setup so the RPi will connect to the wifi network on boot. To enable SSH on boot you will need to add an empty text file named “SSH” to the root directory of the boot disc and then delete the file extension. Drag and drop this into the boot disc. To setup the RPi to auto connect to the wifi create a new file wpa_supplicant.conf using the information below.

filename: wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="SSID"
    psk="PWD"
    key_mgmt=WPA-PSK
}

Be sure to change the values for SSID and PWD inside the quotes to match your local network settings.

Then boot up the RPI!

Step 2: boot up the RPi and find the IP

Plug in the RPi to a power source and wait for it to boot up. I used a program called IP Scanner (there is a free and pay-to-play version) that will scan all the devices on your local network and report their IP and MAC addresss. Pretty handy – so I bought the pro version for $30. The free version will display up to 6 devices, so if you have alot of traffic on your network the RPi may not show up if you use the free version.

Screen Shot 2018-06-04 at 7.47.32 PM.png

If you do not see the RPi IP on the network right away you may want to hook up an ethernet cable directly to the RPi and then try again. I had some issues at first getting all this to work, and was finally able to find the IP by directly connecting the RPi to my router with an ethernet cable. This will help finish configuring some of the time/region/language settings if this is the first time setting up the boot disc. In theory you shouldn’t need to connect to a router…but now you know.

Step 3: setup Coda2 for easy-peezy  SSH

The program Coda2 is a neat piece of software that makes it easy to SSH into multiple devices like the RPi. Once you know the device IP you can setup a profile for the specific device and save the username and password. This way every-time you want to connect to the device you simply pull up the saved profile and hit CONNECT.

Screen Shot 2018-06-04 at 9.27.44 PM.png

You can create multiple profiles for various devices and set it up for terminal access.

Screen Shot 2018-06-04 at 9.25.57 PM

Once connected, Coda2 shows you the working directory of the device so that you can create new files, edit existing files, and even delete files in the working directory of the device. This is all done remotely from you computer in real time and creates a backup on your local machine also.

Screen Shot 2018-06-04 at 9.29.50 PM.png

This is how I created a new python program to open the RPi USB port and read data from the Arduino.

*Edit July 8 ,2018*

To find the name of the Arduino serial port  (like /dev/ttyACM0)  you can type the following command into the terminal.

 ls /dev/ttyUSB*

And you will see something like the following. For is an example I had an nRF52 Feather plugged into the USB port on the RPi and it was assigned the port /dev/ttyUSB0.

Screen Shot 2018-07-08 at 9.23.47 PM

Simply right click on the directory (if this is a clean Rasbian install it will be empty) and then create a new file and name it basestation.py. Then copy and paste the code below into the new file you created in Coda2 and save the file.

filename: basestation.py

import time
import serial


ser = serial.Serial(

	port='/dev/ttyACM0',
	baudrate = 115200,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS,
	timeout=1
)
counter=0


while 1:
	x=ser.readline()
	print(x)

Screen Shot 2018-06-04 at 9.32.32 PMScreen Shot 2018-06-04 at 9.32.43 PMScreen Shot 2018-06-04 at 9.33.06 PM

Be sure to save this new file. You will see a small icon in the top right of the screen to indicate that the file was saved and pushed to the device.

Screen Shot 2018-06-04 at 9.34.25 PM

Before we can run the program we need to add a few things to the RPi to use the serial library.

Step 4: use the terminal in Coda2 to install some libraries

Another great feature of Coda2 is that you can open a terminal to the device  in a new tab and still keep all your working files open. Just click on the terminal icon to start working.

If you try to run the code right now, you will get an error.

To try it out type the following:

sudo python basestation.py

The code will not run because we are missing the library the allows the RPi to communicate with the USB serial port plugged into the Arduino running the basestation code.

Screen Shot 2018-06-04 at 9.36.04 PM.png

In the terminal type the following command to install the python serial library.

sudo apt-get install python-serial

Screen Shot 2018-06-04 at 9.43.41 PM.png

Once the install is complete, try to run the basestation.py program again by typing the following into the terminal. Or hit the up arrow on the keyboard to access your previously entered commands:

sudo apt-get install python-serial

You should now see data streaming from the basestation onto the terminal in Coda2.

Screen Shot 2018-06-04 at 9.45.52 PM.png

Thats all for today. In the near future I’ll continue to work on posting this data from the buoy onto a website. Shawn from Sensor the Earth has been making some awesome progress on a web-service and API, you can see a few of Adafruit Show and Tell videos here and here fore more info.

Hope some of this was helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s