Building an IoT Weather Station with MicroPython or Arduino
Getting started with IoT development can be intimidating, even for simple hobby projects. There are many options when it comes to both hardware and software and ways to integrate them to achieve the goals of your project.
To help you get started, this article will walk through some of the decisions we made at InfluxData when creating an IoT weather station as a prototype for potential customers and prizes for developers, as well as a reference architecture for those building their own IoT setups.
The weather station backs up sensor data to an instance of InfluxDB and can pull historical data from it to show on its OLED display. Data pulled from external APIs can be displayed by clicking on the external button to flip between different dashboards. It is open source and has implementations using both Arduino and MicroPython.
Purpose of the InfluxData Weather Station
Some of the problems solved by a time-series database and why they are useful can be abstract, even when explaining them to people with technical backgrounds. So we wanted to come up with something that would make things a bit more concrete conceptually, as well as make it easier to get up and running.
Here are some of the requirements we considered when designing the weather station:
- Cheap enough to build that we could send them to potential customers for testing or as gifts at events.
- Easy to set up and start generating time-series data.
- Source code is useful as reference material for developers looking to integrate InfluxDB with their IoT application.
- Stand out at events compared to usual demos that involved dashboards on a laptop.
Working with those requirements, this was the result:
Based on the positive response from developers, we have continued development and added more functionality that we thought would be helpful to users for learning purposes. Newer versions of the weather station also feature improved cases and dashboards to show more information.
There are three main components in the weather station: the microcontroller, display and sensor. For the microcontroller, we went with the ESP8266 due to the built-in Wi-Fi support, which would simplify the setup process by allowing users to connect to the device through that Wi-Fi access point and configure it through a web interface. The ESP8266 also has a number of SDKs available, which made the development process easier.
The second component is the OLED display, which is used for the dynamic dashboards that show different data from the sensors and external data sources. For this, we went with the SSD1306. For the sensor, we used a DHT11 sensor, which is able to collect temperature and humidity data.
The case that holds the components is 3D printed. The design is also available in the GitHub repo. For the newest version of the weather station, the sides have vents, which reduces the amount of material used while printing and also makes the temperature sensor more accurate. We now use a biodegradable material for printing rather than a standard plastic that was used on the first iteration. We’ve also improved the fasteners inside the case that hold the components in place so the device is more stable when moved around.
For software, the full-featured implementation is written in Arduino, which is essentially an extension of C/C++ with some features to make working with hardware easier. There is also a less complex MicroPython implementation. The source code for both is available on GitHub, along with installation instructions if you have compatible hardware.
The weather station takes advantage of a number of other open source libraries to add features. To make it easier to get started, we used the ESP Async Web Server to provide a web interface so users don’t have to use the command line to interact with the device.
You can find a list of the other libraries used in the GitHub repo, if you are interested.
Weather Station Dashboards
The weather station provides the following dashboards via its OLED display:
- Line chart showing the last 90 minutes of data collected by sensors
- Three-day weather forecast
- Local weather data based on geolocation
- Sunset and sunrise dashboard
- COVID spread risk dashboard generated based on humidity and temperature based on this research study
Dashboards that aren’t using direct sensor data pull information from the open weather map API. Here are some images of those dashboards in action:
The weather station will hold and display data for the last 90 minutes on its charts by default. If you want a more permanent way to store historical data, you will need to integrate with some sort of storage.
Because it was designed as a demo application for InfluxDB, that is currently the only built-in integration via the InfluxDB Arduino client library. Using the web interface, you can provide the URL and an API key for any InfluxDB instance that is accessible via network. Any other data store with Arduino support could also be added by forking and extending the project.
Here are a few examples of historical data being displayed using InfluxDB’s built-in visualization tools:
What Can You Do with the Weather Station?
The weather station isn’t intended to be a workhorse in terms of processing power by any means, but it does its job of being able to get data into InfluxDB. Once that happens, there is quite a bit you can do. Let’s look at a few potential ideas.
Create Automations and Alerts
Once your data is inside InfluxDB, you can easily create alerts and automations to act on your data. For example, you can schedule a query to run at a defined interval to check the humidity and temperature values. If they are above or below a certain threshold, you can create a task to take action on that accordingly.
Time-Series Forecasting and Analysis
Creating basic alerts and tasks is definitely useful, but the real value of time series is unlocked when you are able to start doing more detailed analysis and forecasting on your data. This could be done using a number of built-in methods provided by Flux or in your programming language of choice using InfluxDB’s client libraries. Python is a very common choice for this type of work using libraries like Pandas to manipulate and visualize data.
Hopefully this article has given you some potential ideas for your IoT project and how you can benefit from using a time-series database like InfluxDB. If you want some info on an IoT setup with more processing power and versatility than the weather station, you should also check out our breakdown of the Raspberry Pi cluster we use for generating demo data and running basic benchmarks.