Raspberry Pi + MQTT + ThingSpeak + IFTTT

The Hacker's Choice
7 min readMay 13, 2020

--

Disclaimer: Until last week I have never used python, MQTT, ThingSpeak or IFTTT. This however, was fun.

This tutorial explains how to build a temperature sensor for a freezer. The temperature is then displayed on a retro 4 digit display on a Raspberry Pi. I’ll further explain how to set up alerts via push-notifications to the phone if there is a power outage or the temperature rises above -5ºC. I’ll then explain how to add a ‘widget’ to a phone that when pressed displays ‘OWND” on the 4 digit display connected to the freezer.

This is achieved by using free cloud services. This tutorial uses:

  • Raspberry Pi Zero WH (Pi Zero).
  • MQTT as a bi-directional lightweight messaging protocol.
  • ThingSpeak to collect the temperature readings (via MQTT).
  • ThingSpeak’s ‘React’ to analyse if those readings go out of bound or no data is received and what action to trigger.
  • ThingHTTP to forward events to other websites (IFTTT in this tutorial).
  • IFTTT to send push messages to the phone and to send widget actions from the phone back to the Pi Zero (bi-directional).

Overview

From Pi Zero to Phone: Data is read on the Pi Zero and securely submitted (via MQTT over TLS) to ThingSpeak. ThingSpeak makes decisions on the data and triggers IFTTT events if needed. IFTTT then send a push notification to the mobile phone.

Pi Zero-> ThingSpeak-> React-> ThingHTTP-> IFTTT-> Push-> Phone

From Phone to Pi Zero: The phone’s IFTTT widget triggers an event on the IFTTT platform which then triggers an event on ThingSpeak which then sends a message down the MQTT channel to our Pi Zero.

Phone Widget-> IFTTT-> ThingSpeak-> Pi Zero

Raspberry Pi

Get a Raspberry Pi Zero WH (Pi Zero). Make sure to get the WH version (with WiFi and Header). The Pi Zero is a tiny (6.5 x 3 cm) and cheap ($5) but powerful Linux based computer to read and control everything. (Tip: It comes with a full shell and ssh and most of THC’s network attack tools run off the Pi Zero — and so does nmap).

Get the Four Letter pHat, the Waterproof DS18B20 sensor and a bare DS18B20 sensor. These sensors submit a digital temperature reading to the Pi Zero using the ‘1-Wire’ protocol.

Raspberry Pi 4 Model B works as well

Firstly solder the Four Letter pHat and test it. Next solder the bare DS18B20 to the end of the Waterproof DS18B20 cable: The centre leg of the DS18B20 is the signal and is soldered to the yellow cable. Black is soldered to GND and Red is 3.3Vdc. Next solder the 4.7kOhm resistor between GND (black) and Signal (yellow) and extend the 3 cables. This makes two temperature sensors — one for the freezer and one for ambient temperature. Now solder the three cables from the 2 temperature sensors to the 4-Letter pHat to BCM Pin #1 (3.3V), #6 (GND) and #7 (signal) (see picture above). Test it.

DS18B20 + 4.7kOhm under white heatshrink on a waterproof DS18B20 cable

ThingSpeak

ThingSpeak acts as gateway for the data. ThingSpeak supports MathWorks to analyse the data, plot charts and 3D graphs, use MathWorks scripting language on the data and share the data with others. We will only be needing ThingSpeak for the most basic functionality.

Register and login to ThingSpeak. Create a ‘channel’ (Channels -> My Channels -> New Channel). A channel can have up to 8 different variables and each variable can store an indefinite amount of data points. We only need two variables to store our two temperature readings (Field 1 and Field 2).

Go to Channels -> My Channels -> Temperature and write down your ‘Channel ID’ (top-left). Click on the ‘API Keys’ tab and write down the ‘Write API Key’.

Submit a test variable to ThingSpeak (replace ‘#APIKEY#’ with your key):

$ curl https://api.thingspeak.com/update?api_key=#APIKEY#&field1=-10

Go back to Channels -> My Channels -> Temperature -> Private to see the first data point (-10ºC) in the graph.

At the top-right of the browser click on Your Name -> My Profile and generate a ‘MQTT API Key’. Write down the ‘MQTT API Key’.

IFTTT

IFTTT allows IoT devices from different manufactures to interact with each other. For example it would be possible to trigger the Hue light in the kitchen to flicker red every time the freezer fails or to play a song or set off the house alarm. However, we will only make use of the ‘push notification’ in this tutorial.

Register and login to ifttt.com and install the App on the phone. At the top-right click on the user icon and then ‘Create’.

Now create the first webhook: To send a push-notification to a phone when a certain web request is made to the IFTTT platform.

Click on the ‘+’ and search for ‘webhooks’ and name the ‘Event Name’ “FreezerGoneOffline” before clicking ‘Create trigger’.

Then click on the second ‘+’ and search for ‘Notifications’ and next click on ‘Send a rich notification from the IFTTT app’. Replace the blacked-out field with your ThingSpeak’s ‘Channel ID’.

Add a ‘Link URL’ and replace the backed-out field with your channel ID. This adds a link to the push-notification (back to the temperature chart on the ThingSpeak platform).

Finish creating the webhook. Then at the top-right click on your user -> ‘My Services’ -> ‘Webhooks’ -> ‘Documentation’ and write down your ‘Webhook-Key’.

Test the push-notification to the phone:

$ curl https://maker.ifttt.com/trigger/FreezerGoneOffline/with/key/#WEBHOOK-KEY#&value1=-4

You should receive a push-notification within seconds.

ThingHTTP

Here we define the outgoing action: A web-request to the IFTTT platform when a certain event (e.g. freezer fails) happens. The web-request to IFTTT will then trigger a push-notification (which we configured above).

Go to Apps -> ThingHTTP -> New ThingHTTP.

Add the IFTTT webhook URL from above with your ‘Webhook-Key’ but without ‘&value1=-4’.

The data from ThingHTTP to IFTTT is submitted in a simple JSON container. The variable ‘value1’ under ‘Body’ must be lower case (!). Replace the blacked-out field with your Channel ID.

ThingSpeak REACT

Here we define when and how to react on the incoming data from our Pi Zero.

Go to Apps -> React -> New React:

Trigger an alarm if power outage for 60 minutes

Create a second React:

Trigger an alarm if temperature rises above -5ºC

Test the API. You should receive a push notification on your phone:

$ curl https://api.thingspeak.com/update?api_key=#APIKEY#&field1=-4

Pi Zero MQTT

MQTT is a lightweight messaging protocol.

Get our source from github and change ‘ts_mqtt_key’, ‘ts_key’ and ‘ts_topic’ to your values. MQTT is as simple as this:

Run the python script:

python3 /home/pi/thc-freezer-monitor.py 

Widget Action

We use MQTT’s ‘Subscription’ feature to listen for incoming data on the Pi Zero. See the source for more details.

We use IFTTT to send a request to ThingSpeak which then sends a message down the MQTT connection to the Pi Zero.

The open service of ThingSpeak has a rate-limit of 1 update every 15 seconds for each channel. Thus it is best to set up a new channel (write down the ‘Channel ID’ and the ‘API-KEY’ again) so that the widget action does not interfere with the rate-limit of the temperature submission — or pay ThingSpeak to remove the rate-limit.

Edit the python source code and set ‘ts_sub_enabled = True’ and add your api-key etc…

On IFTTT select ‘Create’ and search for ‘button’ and select ‘Button press’. Click on the second ‘+’ and search for ‘webhooks’ and select ‘Make a web request’. Replace the XXXXXXXXXXXXXX with your ThingSpeak’s API-KEY. The ‘Button’ is now available from within the phone’s App. (Tip: Click on ‘Add ingredient’ to also send your current location to the Pi Zero).

In the IFTTT phone App click on your user name and then on ‘Widgets’. Click on ‘>’ and enable ‘Show in Today View’.

Swipe right (iPhone) to see the IFTTT Widget. Press it.

The Pi Zero’s display will now display “OWND”.

Taking it further

Cleaning up (optional): Go to ThingSpeak -> Channels -> My Channels -> Widget and delete the ‘Field 1Chart’. Now click ‘Add Widget’ and ‘Lamp Indicator’ to act on ‘Field 1 is greater than 0'.

Check into Adafruit IO, Push Safer, Arduino and Particle for other IoT platforms.

Adafruit IO has a neater interface than ThingSpeak but does not support ‘No Data Check’ and the MQTT ‘Last Will and Testament’ (LWT) feature seems to be unreliable.

Shoutz: spoty/teso for providing great tunes that make me hack harder and Oscar2020 people.

--

--

The Hacker's Choice
The Hacker's Choice

Written by The Hacker's Choice

The oldest and still active security research group - The Hacker's Choice. (Founded 1995)

No responses yet