Environmental Sensors Early Warning System at Home
July 25, 2022, by Charles Suggs
We keep a medium-sized home garden at home, and having moved north we wanted to extend our growing season using techniques like low tunnels and a small hoop house. Springtime temperatures can fluctuate significantly in the Midwest; you can quickly go from freezing to frying under plastic. An automated warning system has saved us a lot of stress as well as our baby plants.
Collecting Environmental Data over Bluetooth Low Energy (BLE)
Not wanting to spend money on cheaply made proprietary tech at the garden store, I looked to open source. Ruuvi tags are an excellent and affordable consumer grade option for Bluetooth LE connected environmental sensors inside a mostly water resistant case. Leveraging the RuuviCollector utility hosted on a Raspberry Pi, data collection is easy.
Storing and Visualizing Environmental Data
As the RuuviCollector pulls in data from the Ruuvi tags, it forwards that along to InfluxDB on an old laptop in the closet. Grafana visualizes the data stored in InfluxDB, and enables exploring the historical data.
InfluxDB is an open source temporal database focused on storing time series data, and Grafana is an open source data graphing, dashboard, and alerting web application.
Running this Java utility along with Nginx, Grafana, and InfluxDB was a bit too much for the Raspberry Pi, so I gave an old laptop a new life as a closet server. I moved Nginx, Grafana, and InfluxDB to the laptop and configured the RuuviCollector to pass on the data to InfluxDB on the laptop.
Custom Alerting based on Data
Grafana provides some alert functionality with several built-in notification channel types. To keep costs at $0 and minimize setup time, Slack was a good option. With the Slack mobile app, Grafana can alert me via smartphone when the low tunnel’s temperature is getting hot enough to open up the plastic, or when the refrigerator’s temperature or humidity keeps rising above its setting. In this case we knew something was up because the humidity kept rising inside the refrigerator over three days.
Sign up here to get tools, tips, and insights delivered to your inbox.
Checking Data from Outside the Network
The final piece was the ability to check on sensor readings while away from home. The easiest option would have been to pay my Internet service provider for a static IP, a small monthly fee, but I would rather keep the project cost at $0. Some DNS providers also expose a REST API for modifying your DNS. An Elixir GenServer in a supervision tree seemed like a logical choice for a small service that would inherently keep itself running, essentially for free. I decided to write a DNS Updater utility that checks for its public IP and, if it’s different from the last one it saw, makes an update to the DNS rules keeping the Grafana site available to the world.
This is the bulk of the business logic that keeps the DNS updated:
@impl true
def init(_opts) do
new_state =
get_ip_from_dns(%State{dns_ip: nil, ip: nil})
|> get_ip()
Logger.info("heartbeat @ 1 / #{@check_delay / 1000 / 60} minutes")
:timer.send_interval(@check_delay, :run)
{:ok, new_state}
end
@spec ensure_dns_updated(State) :: State
def ensure_dns_updated(%State{
:ip => ip,
:dns_ip => dns_ip} = state)
when ip != dns_ip do
Logger.info("different ip")
case update_dns(state[:ip]) do
:ok -> %State{state | dns_ip: state[:ip]}
:error -> state
end
end
@spec ensure_dns_updated(State) :: State
def ensure_dns_updated(state) do
Logger.info("same ip")
state
end
@impl GenServer
def handle_info(:run, state) do
new_state = get_ip(state)
|> ensure_dns_updated()
{:noreply, new_state}
end
Opportunities for Improvement
A couple improvements I have considered are adding an uninterruptible power supply and a more robust alerts system. The closet laptop server has a bad battery and the Raspberry Pi cannot collect data without electricity. While the Raspberry Pi restarts fine when electricity returns, including all services discussed in this post, the laptop requires manual intervention to bring it back online. At the time this project was implemented, Grafana’s alerts feature was not the most intuitive for alerts like what this project needs. Additionally, there’s no clear way to show the value of a variable in an alert message. Grafana has made significant updates to its alerts system since this project was implemented, but a quick exploration did not find a way to show variable values in alert messages.
Do you need a focused, dependable team to bring your environmental sensors project to the world?
Get In TouchCommercialization and Larger, More Resilient Implementations
The concepts of this setup could be applied to commercial, research, and teaching applications. Some changes and improvements for commercial and research applications may include:
- Writing an Elixir utility to collect the sensor data to replace the RuuviCollector to reduce computing resource needs and to enable edge processing of sensor data before it’s sent to a database.
- Write a more nuanced alerting system in Elixir to send different alerts for persistent, worsening or improving conditions, or only when other related conditions like weather are also in a particular range. More informative alert messages would also be possible.
- Use different sensors, including commercial or industrial grade sensors.
- Write Elixir firmware for storing sensor data close to the sensors to prevent data loss in the event of connectivity issues or other challenges.
- Add a solar and battery electric backup for any device operating on grid electricity.
- Trigger automated responses to measured environmental conditions.
Some other situations this could be applied to might include:
- Wine cellars or root cellars
- Garages, especially for woodworking
- Building performance uses: verify thermostats, investigate cold spots like my wife’s at-home office desk; measure humidity levels in a bathroom or kitchen
The time put into this system has really paid off by saving countless baby plants, preventing replacement of a refrigerator that was acting up, and preventing food spoilage while said fridge was acting up.
We would love to hear how you might be using environmental sensors, or your ideas on what else we can do with these.