Monitoring your home temperature – Part 1: Setting up RuuviTags and Raspberry Pi

I actually did this a few years ago, but never wrote a detailed blog about it, so I thought now would be good time for it.

We moved to a new house three years ago and since then, we have had issues with floor temperatures. It’s hard to maintain steady temperature for all rooms and I wanted to build a solution to keep track of it and see temperature trend for a whole week. That way, I know exactly what’s happening.

I will guide how to setup your own environment using same method.

First, you need temperature sensors and those I recommend RuuviTags. You can find detailed information at their website, but they are very handy bluetooth beacons with battery that lasts multiple years. You can measure temperature, movement, humidity and air pressure. There is a mobile app for them also, but it only shows current status, so it didn’t fit to my purpose.

So, I needed to push data to somewhere and an obvious choice was Azure. I will write more about Azure side of things in the next part of the blog. But in this part, we will setup a single RuuviTag and Raspberry Pi for sending data. You can add more RuuviTags later, like I did when everything is working as expected.

First, I recommend updating RuuviTag to the latest firmware. This page has instructions for it:

https://lab.ruuvi.com/dfu/

Updating firmware to latest with nRF Connect

I used an iOS app called nRF Connect for it and it went quite smoothly. You can check that your RuuviTag still works after updating with Ruuvi Station app:

iOS or Android

You will also need Raspberry Pi for sending data to the cloud. I recommend using Raspberry Zero W, because we need only WiFi and Bluetooth for this. I have mine plugged in my kitchen at the moment, but you will just need it to be range of the RuuviTags bluetooth signal (and of course WiFi).

Raspberry Pi Zero W with clear plastic case

Mine has a clear acrylic case for protection, a power supply and a memory card. Data is not saved to Raspberry memory card, so no need for bigger memory card than usual. I installed Raspbian Buster, because at time, there were issues with Azure IoT Hub Python modules with the latest Raspbian image.

Here is a page with instructions how to install a Raspbian image to an SD card:

https://www.raspberrypi.org/documentation/installation/installing-images/

After you have installed image, boot up your Raspberry and do basic stuff like update it, change passwords etc..

You can find how to configure your Raspberry here:

https://www.raspberrypi.org/documentation/configuration/

After you have done everything, you need to setup Python scripts and modules. Two modules are needed: RuuviTag (ruuvitag_sensor) and Azure IoT Hub Client (azure-iot-device). And you need MAC of your RuuviTag, you can find it with Ruuvi Station App under Settings of your RuuviTag.

Then you need to create a Python script to get that temperature data, here is my script:

import asyncio
import os
from azure.iot.device.aio import IoTHubDeviceClient
from ruuvitag_sensor.ruuvitag import RuuviTag

#Replace 'xx:xx:xx:xx:xx:xx' with your RuuviTags MAC
sensor = RuuviTag('xx:xx:xx:xx:xx:xx')
state = sensor.update()
state = str(sensor.state)


async def main():
    # Fetch the connection string from an enviornment variable
    conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
    
    # Create instance of the device client using the connection string
    device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)

    # Send a single message
    try:
      print("Sending message...")
      await device_client.send_message(state)
      print("Message successfully sent!")
    
    except:
      print("Message sent failed!")

    # finally, disconnect
    await device_client.disconnect()


if __name__ == "__main__":
    asyncio.run(main())

Code gets current temperature from RuuviTag and sends it to Azure IoT Hub. You can see that this code needs IOTHUB_DEVICE_CONNECTION_STRING environment variable and you don’t have that yet, so we need to update it later.

You can put this code to crontab and run it like every 15min or whatever suits your needs.

Next time, we will setup Azure side…

Links:

https://lab.ruuvi.com/dfu/ (RuuviTag firmware)

https://github.com/ttu/ruuvitag-sensor (RuuviTag module)

https://github.com/Azure/azure-iot-sdk-python (Azure IoT module)

https://www.raspberrypi.org/ (Raspberry Pi)

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

<span>%d</span> bloggers like this: