Battery powered weather station with ESP8266 and BME280

I used to use web services such as OpenWeatherMap to monitor outside temperature in openHAB. I found out that sometimes they’re not very accurate and naturally they fail when the internet is down. So I decided to measure the temperature myself. After some searching, I decided to settle with battery powered ESP8266 wireless solution. The advantages are the following:

  • no wires;
  • low cost;
  • easy programming via familiar Arduino IDE;
  • long battery operation with deep sleep mode.

I’ve chosen ESP-01 module mostly due to its popularity and low cost. However, to reduce power consumption it requires two hardware modifications: removing power LED (which is always on otherwise) and soldering pin #8 of ESP8266 chip to RST pin of the module. The latter is especially tricky—the chip pins are very tiny—so if you’re not comfortable you should probably choose ESP-03 module instead.

To measure temperature I use BME280 environmental sensors which also provide humidity and pressure readings. I prefer them over DHT-22 which are less accurate, very slow and generally less reliable. You can get BME280s in neat ready to use modules.

To program ESP-01 I used UART module I already had which supports 3.3V, but I’d highly recommend a specialized one. To enter programming mode, you need to connect GPIO0 to GND and CH_PD pin to VCC, and this module has a special switch for that. This makes debugging via serial connection much easier.

I need the setup to operate in cold temperatures (down to -20°C), so I chose 3×AA Ni-MH rechargeable batteries which are ubiquitous and behave better in such environment than Li-ion, for example. Three batteries connected in series give more than 4V fully charged which is above 3–3.6V datasheet range but seem to work nevertheless.

To summarize, you’ll need the following:

  • ESP-01 (or ESP-03, see above) module;
  • BME280 sensor module;
  • 3.3V USB UART;
  • 3×AA Ni-MH batteries with holder;
  • soldering iron, wires (breadboard, jump wires, etc.—optional);
  • PC with USB port and Arduino IDE.

On the software side, I used existing openHAB installation. To communicate with ESP-01 it needs running MQTT broker and corresponding addon. Fortunately, broker installation is simple:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo apt-get install mosquitto

sudo systemctl enable mosquitto
sudo systemctl start mosquitto

openHAB addon can (probably) be installed via Paper UI, but I used legacy config:

sudo nano /etc/openhab2/services/addons.cfg

binding = ..., mqtt1

sudo rm /var/lib/openhab2/config/org/openhab/mqtt.config
sudo nano /etc/openhab2/services/mqtt.cfg

mosquitto.url=tcp://localhost:1883

I found that the best way to debug Mosquitto is to run it in terminal:

sudo systemctl stop mosquitto
sudo mosquitto -p 1883 -v

I did the wiring and debugging in following steps:

  1. Connected ESP-01 to UART and check if it works. I used WiFiScan example for that.
  2. I started with a great sketch by Marcel Akkerman but replaced BME280 code with dummy random values. Don’t forget to create config.h file with your parameters!
  3. When I got that running, I enabled BME280 code and connected the sensor. I was pleasantly surprised when it worked and started sending data.
  4. Then I soldered deep sleep pins and brutally destroyed power LED on ESP-01 and added ESP.deepSleep(10*1000*1000) call to the sketch which sends it to sleep for just 10 seconds. For debugging purposes, I disabled BME280 readout again and was removing GND wire from CH_PD pin of ESP as soon as the sketch was 100% uploaded. This way I could see the serial output.
  5. When I was happy with results, I increased the interval to 900 seconds, disabled serial output and enabled BME280 again.

As the last step, I used RX (GPIO3) pin on ESP-01 to power BME280 sensor. That prevents it from draining battery when the deep sleep is active.

Here’s the final wiring scheme:

AA battery powered ESP-01 with BME280 and deep sleep - scheme

And here’s the sketch for ESP-01:

openHAB

Unfortunately, MQTT operates with strings and to draw plots and create conditions in openHAB we need numbers. So I created a set of hacky rules to convert values on each change:

The first test with some old used batteries showed that the setup worked down to at least 2.3V. I’ll update this post with long term testing results.

4 comments:

  1. Marco says:

    Hi,

    I try to use your sketch but I use a username an password to connect mqtt. Unfortunately I am not a programmer is there a line where I just can add password and username?

    Thank you & regards

    Marco

Your comment: