Following on from my Basic Temperature Logging To The Internet With Raspberry Pi article I decided to upgrade my logging device. By coincidence Arthur Amarra launched a Kickstarter campaign for a “retroScreen” HAT which provided a Nokia 5110 screen, i2c header and four switches. This was exactly what I needed.
So here is a slightly modified temperature and pressure logger based on a Raspberry Pi Model A+ which provides :
- Temperature and pressure logging
- 84×48 LCD screen
- 3 buttons
- 5 display modes
- Backlight switch
The device consists of :
- Raspberry Pi Model A+
- Basic case
- retroScreen by Arthur Amarra
- BMP180 sensor module
- 3 tactile switches
- PiHut WiFi dongle
The BMP180 sensor is described in my BMP180 I2C Digital Barometric Pressure Sensor article. This sensor is small, cheap and provides temperature and pressure. The retroScreen provides a compatible header on its PCB so it’s a natural choice.
Previously I added a single switch to allow the device to be powered down gracefully. The retroScreen provides space for four switches so I soldered in three 2-pin tactile switches. This was a neater solution and gave me some more options for user input.
The switches connect the GPIO pins to ground so in the Python script they are pulled high. When the switches are pressed they are connected to Ground and pulled LOW. The script uses “callbacks” to take appropriate action when it detects these falling edges.
- Switch #1 (GPIO 22) allows the user to cycle through the 5 available display modes.
- Switch #2 (GPIO 27) allow the user to cycle through a set of contrast values so the screen can be fine tuned.
- Switch #3 (GPIO 17) tells the script to exit and/or shutdown the Pi. By setting the AUTO_SHUTDOWN flag to 1 the Pi can also shut itself down if required.
Thingspeak and The Internet of Things
To log data to the Internet I use the Thingspeak service. I explain this in a bit more detail in the previous article.
To use my example script you will need to setup a Thingspeak account, create a new channel and acquire the “Write API Key” from the API settings. See the official documentation for help.
Here are the sequence of steps I used to build the device :
- Created a fresh SD card using the latest Raspbian
- Configured WiFi via the LXDE desktop
- Ran “sudo apt-get update”
- Ran “sudo apt-get upgrade”
- Enabled SPI using this guide (including “py-spidev” and “python-dev”)
- Enabled i2c using this guide (including “python-smbus” and “i2c-tools”)
The Nokia screen needed a few more bits and pieces installed. I didn’t need all the retroScreen examples to work so I used a simplified installation procedure than the one described on the retroScreen site. Obviously if you want to explore the board in more detail you can follow the full process and try out the examples.
git clone https://github.com/adafruit/Adafruit_Nokia_LCD cd Adafruit_Nokia_LCD sudo python setup.py install
Temperature Logging Script
Make sure you are in your home directory :
Then download three files from my BitBucket repository direct to the Pi :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bmp180.py wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/templogger2.py wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/templogger2_cfg.py
The script also uses two font files (from fonts2u.com) which must be downloaded :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/5x7_practical.ttf wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/F25_Bank_Printer_Bold.ttf
Note : On the command line you can use the Up/Down arrow keys to cycle through previous commands and then just retype the file names to save a lot of typing!
Edit the config file and add your Thingspeak API key to the THINGSPEAKKEY parameter.
You can save and exit the nano editor using [CTRL-X], [Y] then [ENTER].
Running The Script
in your home directory when you run the “ls” command you should have the following files :
You should also have an “Adafruit_Nokia_LCD” directory.
You can run the logger using
sudo python templogger2.py
You can quit using CTRL-C or press Switch #3 to stop the script. If AUTO_SHUTDOWN is set to 1 then the Pi will shutdown. After 20 seconds the power can be disconnected.
Automatically Run On Boot
In order to avoid having to start the script manually you can configure it to launch when the Pi boots up. It was at this point I had problems using my usual “autoboot” techniques and instead used the systemd method for starting Python scripts.
I followed this tutorial : How to Autorun A Python Script On Boot Using systemd
My Unit file, templogger2.service looks like this :
Description=Templogger2 Service After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python /home/pi/templogger2.py > /home/pi/templogger2.log 2>&1 [Install] WantedBy=multi-user.target
Button 1 allows you to scroll through 5 different display modes. They look like this :
Once the device is up and running and you’ve configured your Thingspeak Channel you can produce outputs like this :
The graphs update these in realtime as new data arrives. The channel also allows you to download your data in CSV, XML and Json formats.
With an interval of 10 minutes that gives you 144 data points every 24 hours. You can set the graphs to display a set number of data points so you can adjust this to give you a suitable spread of data.
I only used two streams of data in my example (temperature and pressure) but Thingspeak will accept a total of eight.
Here is my public RPiSpy Temp Logger Channel.
In order to clearly display text on the screen I made use of two fonts :
You can use other fonts as long as you adjust the appropriate sections of Python. Some work better than others and there is plenty of room for experimenting.