Indaba Maker Session 2019
In this session we will develop software to collect data from sensors connected to the NUCLEO F446RE board. We will also visualise the data collected and perform interpolation using Gaussian process regression.
Preparation
Before the session, please do the following
- Install Python 3. Anaconda is a good choice https://www.anaconda.com/distribution/
- Sign up for an Mbed account https://os.mbed.com/account/signup/.
- Clone this repository and cd into it
- Create a virtual environment
python3 -m venv ttn
- Activate it
On Linux
source ttn/bin/activate
On Windowsttn\Scripts\activate.bat
- Install the requirements
pip install -r requirements.txt
- Install InfluxDB as shown here (Linux and Mac) - Instructions for Windows
- Install software to obtain console output - we will need this to see the output from the microcontrollers
Software to obtain console output
Windows
If you are on Windows, install:
- ST Link - serial driver for the board.
- Run
dpinst_amd64
on 64-bits Windows,dpinst_x86
on 32-bits Windows.
- Run
- Tera term - to see debug messages from the board.
Linux
If you're on Linux, install:
- screen - e.g. via
sudo apt install screen
MacOS
Nothing required.
Firmware Development
We will deploy two programs on the Nucleo board
- The hardware hello world program Blinky that turns an LED on and off.
- Temperature and humidity sensor
Data Transmission
Data transmission from the sensors to the application will be via LoRaWAN. This is a long range low power sensor system ideal for the internet of things.
Hardware requirements
- NUCLEO-F446RE
- LoRaWAN Transceiver Shield (Custom made for DSA by ARM!)
- USB Connector
- Temperature sensor
- Soil Moisture Sensor
Set Up
Follow these instructions from DSA 2018 Nyeri by Jan Jongboon to set up. Refer to the original repo here.
-
Go to the NUCLEO-F446RE platform page and click Add to your Mbed compiler.
-
Import the example program into the Arm Mbed Compiler by clicking this link.
-
Click Import.
-
In the top right corner make sure you selected 'NUCLEO-F446RE'.
This has cloned the repository.
-
Click Compile.
-
A binary (.bin) file downloads, use drag-and-drop to copy the file to the NODE_F446RE device (like a USB mass storage device).
Note: Here's a video.
-
When flashing is complete, hit the RESET button on the shield.
-
You should notice the led on the nucleo board flashing. We have programmed the board to blink every second.
Modify Blinky
-
Open
select_program.h
. -
Note that we have set the program to be compiled here. Later we will change this.
#define PROGRAM HELLO_WORLD
-
Open
hello_world.cpp
and change the value of the constantconst float BLINK_PERIOD_S = 1;
to a smaller or larger value and recompile the program and drag-and-drop the .bin to the Nucleo board. Confirm that the blinking rate has now changed.
Temperature and humidity measurement
-
Let's connect up the hardware.
-
On the online compiler, open
select_program.h
. -
Set:
#define PROGRAM TEST_TEMP
-
Click Compile.
-
A binary (.bin) file downloads, use drag-and-drop to copy the file to the NODE_F446RE device (like a USB mass storage device).
-
We need to view the program output with temperature and humidity values on the console. In linux we do the following
$ ls /dev/ttyACM*
/dev/ttyACM0
Then connect to the board using screen:
sudo screen /dev/ttyACM0 9600 # might not need sudo if set up lsusb rules properly
On Windows
- Unplug your board and plug it back in.
- (Not sure if it configured correctly? Look in 'Device Manager > Ports (COM & LPT)', should list as STLink Virtual COM Port.
The output will look something like this
Temperature is 28.00 C
Humidity is 5.00
Temperature is 28.00 C
Humidity is 5.00
Temperature is 28.00 C
Humidity is 5.00
Err 6
Sometimes reading the sensor is unsuccessful and the error is reported.
Soil Moisture Measurement
-
For those with soil moisture sensors, connect the hardware as follows Connect red to 3.3V, black to GND, yellow to A2.
-
For this you will need to create a male-female jumper from the male-male and female-female jumpers provided.
-
On the online compiler, open
select_program.h
. -
Set:
#define PROGRAM TEST_SOIL
-
Compile, flash and view the output on the console
-
If you hold the sensor, the moisture in your palm should cause the percentage to rise. If you can get a wet rag the moisture level will rise even higher.
The output will look something like
Soil Moisture Sensor Test program
******************
Moisture Level: 0.0%
Moisture Level: 0.0%
Moisture Level: 36.2%
Moisture Level: 34.3%
Moisture Level: 35.7%
Moisture Level: 36.2%
Data Transmission over LoRa
Follow these instructions from Jan's repo
Grabbing credentials from The Things Network
We have a LoRaWAN network set up here but you need some credentials to connect to it. Let's grab some credentials from The Things Network.
-
Log in to the The Things Network console.
-
Use the following credentials:
- Username:
indaba2019
. - Password:
indaba2019
.
- Username:
-
Click Applications.
-
Click on
maker-session
. -
Click Devices.
-
Click Register device.
On the register device page:
-
First click the generate button below 'Device EUI'.
-
Enter a nice name for your device and click Register.
-
Click Settings.
-
Switch to ABP.
-
Disable (or uncheck) frame counter checks.
-
Click Save.
Configuring your device
Get the device address, network session key and application session key.
-
Click the Copy button next to 'Device Address' to copy to clipboard.
-
Click the
< >
button of the Network session key and Application session key values to show the value as C-style array. -
Click the Copy button on the right of the value to copy to clipboard.
Paste these keys into the file device_addresses.h
in the appropriate sections:
static uint32_t DEVADDR = 0x2601112A;
static uint8_t NWKSKEY[] = { 0xD1, 0x8F, 0xB8, 0x4A, 0xB1, 0x1C, 0xAF, 0x3E, 0xBD, 0xC2, 0xB6, 0x84, 0xEF, 0xD4, 0x41, 0xE4 };
static uint8_t APPSKEY[] = { 0xAF, 0x05, 0x16, 0x6F, 0x17, 0x34, 0xAD, 0xC0, 0x51, 0xD1, 0xE9, 0x7B, 0xF5, 0xFA, 0x33, 0x6E };
- Put Device Address on the first line, prefixed with
0x
! - Put Network Session Key on the second line, don't forget to add
;
at the end. - Put Application Session Key on the third line, don't forget to add
;
at the end.
-
Connect the temperature (or soil moisture) sensor as you did earlier.
-
Connect the LoRa sheild on top of the Nucleo board.
-
The correct orientation of the LoRa shield is when all the logos are on the top.
-
On the online compiler, open
select_program.h
. -
Set:
#define PROGRAM TEMP_TRANSMIT
Or
```
#define PROGRAM SOIL_TRANSMIT
```
Or
```
#define PROGRAM TEMP_SOIL_TRANSMIT
```
Depending on whether you have the temperature sensor, soil moisture sensor or both.
- Compile, flash, ...
- View the output on the console. You get something similar to
=======================================
Temperature and Humidity Sensors
=======================================
Sending every 60 seconds
[DBG ][LSTK]: Initializing MAC layer
[DBG ][LSTK]: Initiating ABP
[DBG ][LSTK]: Frame Counters. UpCnt=0, DownCnt=0
[DBG ][LSTK]: ABP connection OK.
Connection - In Progress ...
Connection - Successful
Ambient Temp=21.000000 Ambient Humi=62.000000
Sending 7 bytes
[INFO][LMAC]: RTS = 7 bytes, PEND = 0, Port: 15
[DBG ][LMAC]: Frame prepared to send at port 15
[DBG ][LMAC]: TX: Channel=1, DR=0
7 bytes scheduled for transmission
[DBG ][LSTK]: Transmission completed
[DBG ][LMAC]: Opening RX1 Window
[DBG ][LMAC]: Opening RX2 Window, Frequency = 869525000
Message Sent to Network Server
Going to sleep!
- You should see the data on the console also appear on TTN in your device under the data tab.
Writing Data to a Database
TTN does not store data and for us to use the data in any application, we must store it in a database we configure ourselves. We will use an InfluxDB which is well suited to time series data. We will use the MQTT protocol to transfer data from TTN to our database via a python SDK provided by TTN. We will write the data to a local InfluxDB on our machines which we will create.
- Run
python ttn_example.py
This should create an InfluxDB on your local machine named indaba_session
and populate it with data whenever your device transmits data. It will also print out messages with the json of the uplink
Received uplink from dev-01
{'time': '2019-08-13T09:44:17.171780715Z', 'fields': {'data_rate': 'SF12BW125', 'rssi': -103.0, 'snr': 4.2, 'Temperature': 19.0, 'Relative Humidity': 67.0}, 'measurement': 'Indaba Session', 'tags': {'sensor': 'dev-01'}}
These fields include temperature and humidity as well as radio transmission parameters.
-
Now we can examine the database.
-
Open the database
influx -precision rfc3339 -database indaba_session
- Display the data collected so far
Connected to http://localhost:8086 version 1.7.4
InfluxDB shell version: 1.7.4
Enter an InfluxQL query
> SELECT * FROM "Indaba Session"
Data Analysis
We will now get the data into a Jupyter notebook and perform some visualisation and analysis. We will work in the same virtual environment.
- Activate the environment (On Linux
source ttn/bin/activate
On Windowsttn\Scripts\activate.bat
) - Open the data analysis notebook (data_analysis.ipynb) in this repo and follow the instructions.
Battery Powered Deployment
-
Take battery holder and place 4 AAA batteries.
-
Connect the jumpers to Vin (red) and GND (black), these are on the left side of the shield
-
Then remove the USB cable, and remove the shield, and find jumper JP5.
-
Move it from U5V to E5V.
-
Place the shield back.
-
The device should be back online.
-
We'll explore how far we can take the sensor