Building a Homekit enabled weather station using Homebridge and an ESP8266

Beware, there’s a follow up on this article.

— — — —

My wife likes to grow her own vegetables on our balcony. The problem with this however is, that one also needs to water the plants (first world problems right?)

As the tech guy I am, I was going a little bit wild last summer and build a Homekit enabled solution to water the plants. Every day at 8 in the morning and 7 in the evening, it was watering the plants for a couple of minutes.

It works great, but the system was not aware of the temperature or if it was raining, that is why it also watered the plants when it rained (and not enough if it was a really hot day)

I wanted to solve this with the minimum effort (and costs), but buying a Homekit enabled rain sensor was surprisingly expensive. The only one I found was the Netatmo Rain Sensor (~70€).

This is why I decided to build my own system.

A few key points that it should fulfill

  • measuring the temperature, creating a chart out of the historical values and make Homekit aware of the current temperature
  • Using as little power as possible
  • Using a battery/Powerbank to be able to move the system to any place

Parts

The following list contains Amazon links to make it easier to buy the parts I’ve used, however, if I were you, I’d consider buying them from Ebay or Wish, as they are way cheaper than buying on Amazon.

All in all you should not spend more than max 15€, probably less…

Preparation

After setting up the IDE and powering on the ESP, I wanted to start simple as everybody and get the led to blink.. This is where everything started to go sideways.

Problem I, finding the matching usb driver

Problem II, Mac does not detect the ESP8266

Problem III, Powerbank turning itself off after x minutes

After I fixed those issues, I was finally able to start with the actual work.

Requirements

  • Using Homebridge as the receiver of the sensor data
  • Having historical data of the temperature and how much power I have left in the Powerbank

If I wouldn’t want to have historical data, things would have been simpler, the ESP would just have send the data to Homebridge and that’s it.

This is the architecture I’m using to enable both, historical data and forwarding data to Homekit.

Architecture to enable historic data with Homekit
Architecture to enable historic data with Homekit

Building

Hardware

Software

ESP8266
To flash the needed code to the ESP, you need to use the Arduino IDE. The code can be found here. After cloning the repository, you first have to rename configEXAMPLE.h to config.h and adjust the values to the ones matching your environment.
Note: If you don’t want historical data, you can simply point the urls in the config to Homebridge instead of pointing them to the server backend.
Once the code has been flashed to the ESP, it will try to read the data from the sensors and send them using your wifi every 25 minutes. In between it’s falling into a Deep Sleep.

The deep sleep basically shuts down the whole ESP (thus power consumption is extremely low). After a specific time, the ESP is being woken up (for this you need the connection between pin RST and D0). A wakeup is basically the same as if you would press the reset button, thus the setup method is executed again.

NodeJS Server

As said, I have decided to have some backend service as I’d like to have historical data. This is totally optional. The code is here. It’s a pretty simple node server, serving an api and a static page with 2 charts.

You can deploy anywhere you like, only requirement is that the ESP can send data to it and Homebridge can query it.
This is how the 2 charts looks for me.

Historical data of power consumption and temperature data

Homebridge

The 2 plugins we need to use to this project are this for the temperature data and this for the rain detection.

Note, if you decided to not use the server, you only need the last plugin.

homebridge-http-temperature-sensor Plugin
This plugin collects temperature data from somewhere. This somewhere can be anything as long as it’s a rest-GET endpoint. Perfect for us, because we query nodeJS to get the latest temperature value. Yes, I could have also let the ESP send the data to Homebridge directly, but this would have meant another HTTP call and as I wanted to save as much power as possible, I wanted to avoid this.

homebridge-http-webhooks
This extremely powerful plugin enables webhooks in Homebridge. You need to add a new webhook so that the ESP can send the rain detection data. As I did not need historical data for this, I could send it directly to Homebridge without sending it to the server first.
In the plugin, simply create a new contact sensor, name it “rain”, copy the given url and copy it into your config.h in the ESP code.
Obviously Homekit does not support the sensor type “rain” thus a contact sensor should do the trick. If it’s open, it doesn’t rain, if it’s closed, it’s raining.

Creating the needed contact sensor for the rain detection

Putting it all together

As the whole thing is running outside, I wanted to have it waterproof, thus I bought a waterproof case (see Parts section).

Everything waterproof
Will never win a price for the most beautiful installation, but hey, it works ;)

Power consumption / Powerbank

As I did not find a matching waterproof box for my Powerbank, I was getting a little creative and used an old tooth-brush box and wrapped it in duck-tape. I am using 2 USB cables, one to re-charge the Powerbank, the other one to power the ESP.

It works good.. Well, there’s not much you can’t solve without duck tape..

Power consumption data

In my code, the ESP is configured to read the V3.3 voltage by placing ADC_MODE(ADC_VCC);
Then, the battery voltage in mV is read by calling ESP.getVcc();

I found that my Powerbank at full capacity produces a value of 3.929 while a nearly empty Powerbank produced 2.854. Knowing those thresholds enables me to establish alertings if desired. The problem with this solution however is that you cannot use the analog pin A0 on your ESP anymore, thus you cannot read the analog temperature data. But this compromise should be ok as you still get the digital value (it’s raining, it’s not raining).

I hope this article helps you build your own weather station. Best of luck and happy hacking :)

Working @Instana, Loving tech {...life}