There's no worse feeling than walking into your kitchen in the morning and realizing the fridge door's been left open overnight. Not only is it a waste of energy (the fridge has been trying to cool the whole kitchen all night!), but it also puts you at risk of spoilage and food borne illnesses if everything inside the fridge isn't thrown out immediately. 🤢
Now imagine this same sort of scenario but on a much larger scale in a food distribution warehouse or commercial kitchen, for instance. Up to now, monitoring multiple refrigerators' internal environmental conditions and open/closed door state often requires human intervention to spot a door left open or deduce that fridge temp may be too high or low based on the condition of food in the fridge. This can be costly and a potentially hazardous health issue.
To try and prevent such unfortunate events, I built a LoRa-based monitoring system to track the internal state of my fridge and freezer, as well as the open/closed state of each appliance's door. Each device reports back to a single gateway, which then relays the data to a cloud dashboard that lets me monitor conditions across appliances easily.
NOTE:LoRa (from "long range") is a radio communication technique, and is an alternative to cellular, Wi-Fi, and bluetooth for connectivity.
Here's what my dashboard homepage looks like, displaying my monitoring devices, and their current stats, at a glance.
And here's a more detailed view of one my sensor's readings: current readings at the top, readings over a period of time at the bottom.
In this article, I'll show you how it all works, and how to set up your own LoRa fridge monitors for yourself. Let's begin with the hardware I used.
Hardware AssemblySince I wanted to build a LoRa-based fridge monitoring setup, I chose to start with the Blues Wireless Sparrow Development Kit, which provides almost everything I needed right out of the box.
(Full disclosure: I work for Blues Wireless, an IoT startup dedicated to making wireless IoT simple, and I do so because we're making awesome, flexible tech like this.)
A little background on Sparrow
Sparrow refers to the Blues architecture that makes it easy to set up a LoRa network, where a single gateway that can speak to multiple sensor nodes. This architecture allows you to add cloud connectivity to a local cluster of sensor nodes, while keeping per-device costs reasonably low.
The Sparrow Development Kit contains everything you need to set this network up, including a gateway and two sensor nodes.
When set up, the gateway is made up of two boards connected by a Qwiic cable.
- The Sparrow Essentials Board (left, in image below) is a microcontroller that acts as the radio that performs the LoRa-based communication with the sensor nodes.
- The Notecarrier-A (right, in image below) houses the Notecard, a system-on-module that makes network communication simple. The Sparrow Developer Kit comes with a Wi-Fi Notecard, but you can swap in a cellular Notecard if you want your Sparrow network to work outside the range of a Wi-Fi network.
The Sparrow sensor nodes come preassembled, and all I needed to do was insert a pair of AAA batteries to get going. (I left the backs off of these sensor enclosures because I made some modifications that needed access to the Sparrow's pins.)
I used the full instructions on how to set up the dev kit available in the Blues Sparrow quickstart guide. And in addition to those steps, I needed to connect the Wi-Fi Notecard to my home Wi-Fi network, and pair the sensor nodes to the gateway (which works a lot like Bluetooth pairing).
Each Sparrow sensor node contains both a PIR motion sensor and a BME280 environmental sensor to start. Once I had my sensor nodes paired, they automatically started reporting motion, connectivity, and environmental data to the Blues cloud backend, Notehub.
With those minimal steps complete, I actually already had a LoRa network set up and reporting data to me. By default, the sensor nodes take an environmental reading (temperature, humidity, and air pressure) every hour, and the motion sensors report on motion immediately at first, and then every 15 minutes afterwards to save on battery.
But not so fast, motion isn't something that was relevant to monitoring the door status for a refrigerator, so in addition to setting up all the goodies the development kit comes with, I also bought two Adafruit magnetic contact switches: these will connect to each sensor node and determine the state of the fridge door at any given time.
NOTE: You might be wondering how I could add an additional contact switch sensor to the Sparrow node and expect it to work. Well, that's a cool thing about the Sparrow network: it's highly configurable. The device’s firmware is open source, and it can be tweaked to add your own custom behavior (or new sensors in my case). But if all you want to do is monitor environmental data or motion, everything you need comes in the development kit.
So with contact switches in hand, I attached the wires of each switch to the A1
and GND
pins on the Sparrow node board: the order doesn't matter in this case.
And now, with my gateways and nodes assembled and connected to Notehub, it was time to get on to the firmware portion of this project.
FirmwareSince my use case involves a custom contact switch sensor, the firmware that the Sparrow comes preinstalled with won't work as is. But luckily, the Sparrow firmware is open source, and features a variety of common sensor support including BME, button, and contact switch, among others.
NOTE: You're also encouraged to modify the firmware to add your own custom behavior or new sensors too!
In order to enable the BME280 and contact switch sensor configurations and upload these settings to my nodes, I needed to download the firmware repo locally, change the code, and then flash the newly compiled code to the Sparrow nodes.
Setup to modify the firmware locally
Relying on the Sparrow firmware updates documentation, I downloaded the Visual Studio Code IDE and the STM32CubeProgrammer software, and followed the custom installation instructions for STM32CubeProgrammer - it can be a bit fiddly at first.
Modify and build the firmware
Once that was complete, I cloned the Sparrow reference firmware repo from GitHub. (Be sure to include the --recursive
line at the end of the command to grab the contents of the submodules contained within the firmware repo.)
git clone https://github.com/blues/sparrow-reference-firmware --recursive
Then, I opened the init.c
file within the sparrow-application
folder in VS Code and made the following changes to it.
// Scheduled App Header(s)
#include "bme/bme.h"
#include "contact-switch/contact-switch.h"
void schedAppInit (void) {
// Will not initialize if BME280 is not detected
bmeInit();
// Will not initialize door sensor if contact switch sensor is not detected
if(!contactSwitchInit()) {
APP_PRINTF("ERROR: Failed to initialize contact switch application!\r\n");
}
}
By including these two .h
files at the top of the file, and then initializing them within the schedAppInit
function, both the BME and contact switch sensors will be enabled in the newly compiled version of the firmware.
Flash the firmware
Finally, I opened VS Code's task runner by accessing Terminal > Run Task
from my computer's menu bar, and built the firmware with the task Sparrow build firmware using CMake and Make
.
After the firmware successfully compiled, it was time to flash it to the Sparrow nodes. I connected a Sparrow node to my computer using the STLINK-V3MINI programmer and a micro-USB to USB-A cable.
I reopened the VS Code task runner and selected the command Sparrow Flash Firmware using STM32_Programmer_CLI
.
This uploaded my new firmware to my Sparrow nodes and now they were ready to be installed and start monitoring fridge conditions.
NOTE: If you'd like more detailed information on uploading firmware to the Sparrow nodes with the STM32_Programmer_CLI
, refer to the Sparrow Firmware Updates documentation on our site.
Hardware InstallationSince the goal of this project was to both monitor conditions inside of a refrigerator and the status of its door, I decided to attach my sensor nodes (and door switches) to my fridge and freezer using Command strips for ease of installation.
Door switch installation
To install my contact switches on the fridge door, I used the small adhesive Command strips to attach the two switch pieces at the edge of my fridge door and the main body of the fridge itself.
Notice that I stacked several strips on top of each other for the switch piece on the fridge body because it's imperative that the switches line up as closely as possible so the magnets inside each piece signal to the sensor node that they're together ("closed door").
Here's how the switches looked when the doors are closed.
And here's how the switches look when the door is opened. Luckily, the soft seal around the door did not impede the wires connecting the sensor to the Sparrow node mounted inside the fridge at all.
With the contact switches mounted, I turned my attention to installing the actual reference nodes inside the fridge.
Inside the fridge installation
To get the best possible adhesion in a cold climate like my fridge or freezer, I first wiped down the spot I planned to stick the node with rubbing alcohol, then attached a larger, velcro Command strip to the fridge as far back as I could where the node's wires would reach (this helped keep the wires taut and out of the way too), and I attached a second Command strip to the front of the Sparrow node enclosure just below the motion sensor.
Here's the node installed in my fridge.
And here's the other node installed in my freezer.
NOTE: This worked well enough for a proof of concept, but if you're planning on using these nodes on a larger scale, I'd recommend thinking about alternate power sources as batteries and cold generally don't work well together for extended periods of time.
I'll admit that these nodes look pretty rough and ready with their batteries and wires exposed, and I would definitely invest in some enclosures if these were going to be long term installations.
But despite their appearance, once I had the nodes in place, door and environmental events started flowing into Notehub immediately.
The final task I had was to take this data in Notehub and display it in some sort of dashboard that made it easy for me to see how my fridge and freezer were doing and if anything was open that shouldn't be.
Blues offers several ways that data can be easily routed out of Notehub to other cloud services like AWS or Azure, to an IoT platform like Datacake or Ubidots, or even to a custom HTTP/HTTPS endpoint.
For Sparrow projects in particular though, Blues provides an open-source reference web application that can easily be configured with your own device identifiers, and immediately view that data in an online dashboard.
Out of the box, it displays humidity, pressure, temperature, motion and voltage for each node associated with a particular gateway.
As I needed to display door status instead of motion, I downloaded the repo locally, opened up VS Code again, and went to town swapping out motion events for door switch events.
Here's what my final dashboard looked like.
If you'd like to use my dashboard code with its door status displays as your starting point, you're welcome to download the repo here - the README.md
should have everything outlined that you need to get started.
There's even documentation on how to deploy this web app to a number of different cloud providers when you're ready.
Testing it outI set this system up in my own home in an afternoon and it worked surprisingly well.
The sensors, within seconds of the door being opened or closed, would shoot off a new switch.qo
event to Notehub reporting their current status, the fridge or freezer environmental readings from the BME sensors reported in regularly even though the nodes were communicating with the gateway from inside the appliances, and the batteries and wires functioned just fine in the very cold conditions.
And it definitely gave me a little extra peace of mind to know that my fridge and freezer were both keeping my food as cold as they should be, and there were no doors accidentally left open.
ConclusionAccidents like leaving the freezer door open are bound to happen, but relying purely on people to recognize and correct the problem before it's too late shouldn't be your only line of defense.
The Blues Sparrow architecture dramatically simplifies setting up a LoRa network, and getting data flowing from a cluster of sensors up to a cloud of your choice.
And the flexible Sparrow reference firmware and web app combined with the Sparrow Development kit, means that in short order you can have a completely custom monitoring system for your particular use case.
If you're looking to get started with your own Sparrow network:
- Check out our Sparrow Development Kit,
- Walk through the Sparrow quickstart docs and this project's README on GitHub,
- Share what you build on social and hit us up with questions on our forum.
Happy monitoring!
P.S. This is one of a number of IoT application accelerators the team at Blues Wireless is building. Sign up for the newsletter if you want to get notified of new apps as they're released.
Comments