Background
I have a young son who has just turned one, and this summer the UK has been rather unusually hot.
This makes for a great summer however at night when my son is trying to sleep a warm room makes it difficult for him to sleep well. Warm rooms can also be dangerous with respects to Sudden Infant Death Syndrome.
Approach
Being an engineer and wanting to understand how I could connect with IoT frameworks using my Ultra96. I decided to create a simple application which would monitor the temperature in the nursery using the temperature sensor provided with the Grove Starter Kit for 96 boards.
This recorded temperature is then communicated to a cloud based service so a dashboard can be created which logs the temperature. This dashboard means anyone with access to it can view the temperature in the nursery.
However, when the temperature gets too warm the the dashboard may not be being observed. Therefore should the temperature go outside of defined limits he system will communicate with the Phillips hue light bulbs in my house and flash them to indicate an issue.
Ultra96 Development
The starting point for this project is the Ultra96 development board. To monitor the temperature and humidity I use the Grove Starter kit.
The starter kit provides a range of sensors and actuators which connect to a interface board, which connects to the Ultra96 using the low speed headers.
It is this interface board which reads the analogue temperature sensor, using a Arduino Uno-compatible ATmega328P. The Ultra96 board receives the data from the ATmega over a serial link.
As both PS UARTS are used for the Bluetooth and the UART on the Ultra96 board respectively a third UART is provided in the device PL.
The connectivity between the Ultra96 and the Grove Interface board enables not only the Serial communication but also the Ultra96 to reprogram the ATmega. Reprogramming is achieved using the SPI port.
As such when we develop our application we are going to need to write the following
- ATmega application to read the temperature sensor
- Ultra96 program to communicate with the ATmega and communicate with the cloud.
Luckily the Ultra96 BSP as provided includes the necessary libraries and compilers to generate the Ardunio application.
We will however need to create a make file and bash file along with the code for the Ultra96 and ATmega to run the application.
When it comes to cloud based services we will be using io.adafruit.com. Of course, to use this service we need to create an account and to download the Python 3 Adafruit io library to the Ultra96.
We can install the Adafruit IO library using the PIP3 command
Setting up the Cloud
Adafruit IOThe first of the cloud services we are going to set up is the io.adafruit.com this will allow us to store data communicated from the Ultra96.
With an account created your secret AIO keys required to connect to your account can be found under the left hand side marked "View AIO Key". Protect these values from others but they will need to be included in our Python application.
The next step is to create the feed to which the Ultra96 will push data. Note, this feed name is the one we will use in our Ultra96 application and is case sensitive.
Once we have a feed we can also create a dashboard to display the information in the feed.
Following creation we will want to display feeds in different formats which help us digest the information
We do this by adding a block to the dashboard.
Select the type of block you want and the number of feeds for the block.
For this example I added two blocks, one text feed and another graph, both connect to the temperature feed. The graph enabled me to see the change in temperature over time a little easier than scrolling through lines of text.
The completed dashboard displaying both blocks, can be seen below. In the capture below you can see variation in temperature on the graph quite easily.
We now have a dashboard but we still need to be able to communicate with other IoT based technologies.
To do this we will use IF This Then That (IFTTT)
To use IFTTT with the feeds in our Adafruit IO account we need to enable IFTTT integration such that our IFTTT can access our feed. This can be enabled under your IO Adafruit profile, you may need to create an IFTTT account first to connect to it.
IFTTTIf you do not have an IFTTT account you will need to create one first. Once logged in the next step is to create our own Applet.
This Applet will make use of the existing Adafruit feed monitoring and Phillips Hue Applets.
Stepping through the stages is very simple and a case of selecting applets we wish to work with.
The field label needs to be the name of the feed we wish to monitor
We also need to define a default value.
Once we have addressed the IF condition we need to set up the THEN condition.
Once the Applet is completed, ensure you turn it on and we will be ready to put all the code together and test the application.
Putting it all Together
Back on the Ultra96 we need to write the four files I mentioned earlier. The complete files can be seen below.
As we wish to work with languages other than Python we do not use the custom projects web interface. Instead for this application we use the command line, connecting to it using a program such as PuTTY.
Make File
MONITOR_PORT=/dev/ttyS2
include /usr/share/arduino/Arduino.mk
run: upload
python ada.py
Bash File
#!/bin/bash
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3.5/site-packages
cd /home/root
make run
ATmega Application
#include "DHT.h"
DHT dht(A0, DHT11);
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if valid, if NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
delay(20000);
}
Ultra96 Application
from Adafruit_IO import Client
import serial
aio = Client('USER', 'CHANGE')
ard = serial.Serial('/dev/ttyS2', 9600)
if __name__ == '__main__':
print("Welcome to the Humidity & Temperature reader!!!")
try:
while True:
ardOut = ard.readline()
ardHumid = ardOut.split('Temperature')[0]
ardTemp = ardOut.split('Temperature:')[1]
aio.send('test', ardTemp)
print "Temperature" + str(ardTemp)
except KeyboardInterrupt:
print("CTRL-C!! Exiting...")
With all the code completed, we can run the application by running the bash file (run_me.sh).
In the command line you will see the compilation and downloading of the application, before monitoring starts.
When this was run on my Ultra96 with the temperature set to 20C for testing the video below was recorded showing it working in action.
Comments