Introduction
Many people around the world enjoy the convenience of refrigerators in their homes for storing and keeping food fresh longer. Unfortunately, as appliances age, their internal part will eventually wear out or break down. A potential problem with older refrigerators is that the fridge temperature may not be cool enough to keep stored food cold or frozen. The elevated temperature may cause stored food to go bad if the malfunction goes unnoticed for more than a few hours. By monitoring the refrigerator temperature continuously using IoT technology, temperature issues could be detected in real-time and a refrigerator owner could be notified via email to alert him/her to potential refrigerator problems. Once notified, the owner could take action quickly to prevent food waste and reduce financial loss. The purpose of this blog is to document a prototype IoT refrigerator sensor system built using the Azure Sphere kit for the Avnet Sensing the World Challenge.
This projects builds on the Avnet Azure Sphere Starter-Kit: Advanced Tutorial and this write-up assumes that the reader has been able to completed the Advanced Tutorial by getting it to work successfully. This document shows how the Advanced Tutorial source code and Azure IoT Central application could be extended to meet the requirements of this project.
Project Requirements
- Measure a refrigerator's internal temperature and light condition every second. Log the measurements to Azure IoT Central.
- Notify the refrigerator owner when the temperature goes above some predetermined threshold value (say around 42 degrees F).
- Notify the refrigerator owner when the refrigerator door has been opened (as detected by an increase in light) for more than 5 minutes.
Hardware Setup
Hardware Items Required
- Avnet Azure Sphere MT3620 Starter Kit Product LinkProduct Link
- Male to Female jumper wires (10 cm length)
- 3 Pin KY-013 Analog Temperature Sensor Module for arduino DIY Kits
e.g. from eBay or Amazon...
https://www.amazon.com/dp/B07P87M5GW/ref=cm_sw_em_r_mt_dp_U_u6W2DbHDYFA0X
- 128x64 Yellow Blue SSD1306 I2C OLED Display
- Clear plastic case with approximate dimensions (L x W x H) of (2.5" x 3" x 1")
- USB cable and power adapter
Connect the OLED Display and Temperature Sensor to Azure Sphere Starter Kit
Use the jumper wires to connect SSD1306 and KY-013 to the Starter Kit per the diagram below:
We used KY-013 to measure the ambient temperature independently from the electronic devices on the Starter Kit. Although the LSM6DSO and LPS22HH devices on the Starter Kit have embedded temperature sensors, their measured values is sensitive to heat generated by the electronic devices. The Vcc pin of the KY-013 is connected to the 2.5 V pin of the J6 jumper header. Note: do not use the 3.3 V pin on the mikroBus because the ADC reference voltage is currently constrained to be 2.5 V.
To protect the electronics, the assembled hardware can be housed in a clear plastic case as shown below:
Drilled some holes in the plastic case in order to connect the USB port on the Starter Kit to an external power adapter via a USB cable .
Software Setup
Prepare Azure Sphere SDK and the Starter Kit according to the Advanced Tutorial..
Pull the project folder from GitHub
The source code for this project are on GitHub https://github.com/vicatar/Azure_Sphere_SK_ADC_RTApp
It has been forked from https://github.com/CloudConnectKits/Azure_Sphere_SK_ADC_RTApp. You may clone this project per the usual git clone command. Below we briefly describe some of the modifications or extensions to meet this project's requirements.
Modifying the Visual Studio M4 Embedded Application
The Avnet Azure Sphere Starter-Kit: Advanced Tutorial shows how to build and run an example M4 application to periodically sample (once per second) the ADC inputs connected to the Ambient Light Sensor and post the ADC0 light sensor measurements for use by the high-level Arm Cortex A7 application. In this section, we extend the bare-metal companion application to periodically sample ADC1 which is connected analog temperature sensor.
The basic instruction for modifying the M4 embedded application called is as follows:
- Open the app_manifest.json file.
- Under the "Capabilities" section, modify one line: "Adc": [ "ADC-CONTROLLER-0" ]. This modification is needed to support the Azure Sphere OS 19.09 release.
- Open the main.c file to add code to read ADC channel 1 and return its value to the A7 Embedded Application.
- The code section below Reads ADC channel 1 and outputs its value to the M4-dedicated "IO0" UART on the MT3620. The raw ADC channel 1 readings are returned to the A7 embedded application for further processing as shown next.
// Read ADC channel 1 analog_data1.u32 = ReadAdc(1); mV = (analog_data1.u32 * 2500) / 0xFFF; Uart_WriteStringPoll("ADC channel 1: "); Uart_WriteIntegerPoll(mV / 1000); Uart_WriteStringPoll("."); Uart_WriteIntegerWidthPoll(mV % 1000, 3); Uart_WriteStringPoll(" V"); Uart_WriteStringPoll("\r\n");
Modify the Visual Studio A7 Embedded Application
The Avnet Azure Sphere Starter-Kit: Advanced Tutorial also shows how to build and run an example A7 application to communicate with the M4 application and IoT Central. We extended this code by adding equations to convert the thermistor resistance to temperature in Fahrenheit. Part of the A7 application code for processing the ADC1 value sent from the M4 embedded application is shown below:
// Copy data from Rx buffer to analog_data1 union for (int i = 0; i < sizeof(analog_data1); i++) { analog_data1.u8[i] = rxBuf[i + 4]; } // get resistance of the thermister and convert to temperature in Farenheit double Temp = log((double) 10000.0 * (4095.0 / analog_data1.u32 - 1)); // determine resistance Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp); Temp = Temp - 273.15; // Convert Kelvin to Celcius temperature_sensor = (Temp * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
To fully see all the other modifications to https://github.com/CloudConnectKits/Azure_Sphere_SK_ADC_RTApp, the reader may use the compare change feature in Github.
Modify the Azure IoT Central custom application
Configure Azure IoT Central Rules (Email Alerts)
As shown in the advanced tutorial, under Device Template one can configure rules to enable email alerts when some trigger is activated. For this project, a trigger is set for temperature going above a certain threshold. Another trigger is when the light sensor detects an increase in light intensity for more than 5 minutes. Below is a screen shot showing the configuration of a telemetry rule for temperature.
Testing
We were able to successful test that the Azure IoT Central application worked as expected when integrated with embedded applications running on the Starter Kit. When the temperature was above a certain threshold or when the light sensor detected increased light intensity for more than 5 minutes, we received an email notification. The demonstration system meets the project requirements.
Summary
This project demonstrates the power IoT for monitoring and alerting a refrigerator owner of possible fridge temperature malfunction. It is a relatively straightforward extension of the azure-sphere-starter-kit-advanced-tutorial. Hope that you enjoyed this cool project (No puns intended). There are many ways to improve upon this simple demonstration of the power of the Azure Sphere IoT platform for sensing the world.
Potential Future Work
Reduce device power consumption by optimizing the embedded code
Allow the display to be turned off remotely via IoT Central setting.