Building a weather station with an ESP8266

published on in electronics - 4 min read

Some time ago I decided that I wanted to track some weather data like temperature, humidity and CO2 in my apartment. The data should be imported into a InfluxDB so that I can use Grafana in order to create diagrams based on this data. After looking over the pre-built options I decided to just built the sensor from scratch with a ESP8266 and a couple of sensors.

What is needed?

Preparation

SGP 30

The first thing to do is to solder the header pins onto the SGP30. Once that is done the first thing to do is to connect the SGP30 to the ESP8266 and use the test programm from Adafruit in order to calibrate the co2 sensor by calculating the baseline. I used the following pins for the connection:

SGP 30 PinESP8266 Pin
VIN3V
GNDGND
SCLD1
SDAD2

There are two options to do the calibration:

  1. Use the Arduino IDE to install the sgp30test library, run it and then use the serial monitor to get the right data out. This is described in the article from Adafruit.
  2. Download the example from the SGP30 Github and use your preferred tooling to upload it to the ESP8266.
    As I will use PlatformIO for the code anyways I will show the second option.

PlatformIO

An installation guide for PlatformIO can be found on their page. I will use visual studio code so it basically comes down to installing the PlatformIO extension from the visual studio code store.

The first thing is to create a new Project in PlatformIO with the following settings:
PlatformIO-Settings

The next step is to add the Adafruit SGP30 library from the Library Page:
Installation-Of-SGP30-Library

You will directly find the sgp30test example in the library page so you can copy it from there (if not get it from the github link above) and replace the src/main.cpp with the sgp30test example. Then you can connect your NodeMCU to your computer and flash the software by using the “PlatformIO: Upload icon” in the bottom bar of visual studio code PlatformIO-Upload.
Before you can then use the Serial Monitor you need to adjust the Serial Monitor Speed to 115200 baud. That can be done by adding monitor_speed = 115200 to the platformio.ini inside the project. Afterwards you can click on the “PlatformIO: Serial Monitor” icon and should be able to see data flooding in:
SGP30-Baseline
Now you just need to open a window and wait for 10-15 minutes while the baseline gets calibrates. After this time note down the baseline values that get printed (in the screenshot they are 0x8E42 and 0x9265).

DHT21

The DHT21 does not need any solder and can directly be connected to the NodeMCU. I used the following pins:

DHT21 PinESP8266 Pin
VDD3V
DataD6
GNDGND

MQTT

To push the data from the NodeMCU into InfluxDB I decided to use MQTT as I can attach Telegraf to MQTT and also use MQTT to display the data on other devices. In order to publish data to MQTT a MQTT broker like Eclipse Mosquitto is needed. As I use Docker for all my self-hosting needs I used the following docker-compose.yml to create a instance of Eclipse Mosquitto on my server:

version: "3"

services:
  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    volumes:
      - $PWD/config:/mosquitto/config
      - mosquitto-data:/mosquitto/data
    ports:
      - 1883:1883
    restart: always

volumes:
    mosquitto-config:
    mosquitto-data:

By default mosquitto does not use authentication. To fix this add password_file /mosquitto/config/password_file to config/mosquitto.conf inside the folder where the docker file is defined. You can then add users with docker exec -it mosquitto mosquitto_passwd /mosquitto/config/password_file <username>.

Flashing

Now that everything is set up you can get the code from the repository. A couple of things need to be changed.

  1. There is a cred.h.template in the include folder.
    This file contains secrets like the wifi password for the ESP8266 and the credentials for the MQTT broker.
    Rename the file to cred.h and enter the values for your wifi and MQTT broker.
  2. The setup method of the src/main.cpp contains a call to sgp.setIAQBaseline. The arguments are the baseline values that you gathered from the sgp30test util in the preparation.
  3. If you want to check the topic names for the data in MQTT you need to change the second argument of the Adafruit_MQTT_Publish objects on top of src/main.cpp.

Afterwards you can flash the code on the ESP8266. You can use the serial monitor to check if the ESP can correctly connect to your wifi and mqtt broker.

InfluxDB

The MQTT data can easily be imported into InfluxDB by using Telegraf and using the mqtt_consumer input plugin.

[[inputs.mqtt_consumer]]
  servers = ["tcp://broker_url:1883"]

  topics = [
    "weather/aucubin/temp",
    "weather/aucubin/humidity",
    "weather/aucubin/tvoc",
    "weather/aucubin/eco2"
  ]

  client_id = "client_name"

  username = "username"
  password = "password"

  data_format = "value"
  data_type = "float"