, Part 7: The ESP32

Wire up your ESP32 and BME280

If you’ve messed around with microcontrollers, this will all be easy. In fact, the hardest part is intalling micropython onto the ESP32. A little walkthrough is provided by micropython themselves. I like to use the program Thonny to communiate with the controller from my desktop. There’s a little Thonny tutorial at random nerd tutorials if you’re interested.

Once you’ve confirmed that micropython is working (check with a simple blink program or something), you’ll wire up the BME280 breakout board. I like to use Adafruit’s libraries and tutorials, so I suppose it’s only fair to buy a board from them as well. They have a writeup on using the BME280 with micropython here on their website. If you’re really strapped for cash, there are cheaper BME280 breakout boards available, as well. Confirm that you can print the values.

Make your boot routine

The main purpose of the boot routine is to connect to your wifi network. The code can be viewed on my github for the ESP32 components under the micropython_scratch folder. There’s nothing to it. Note that you may want to look into reserving a static IP for this device if you have connectivity issues; sometimes this can be a solution.

Make your main routine

The main routine will include a little more “front stuff”: imports that belong here rather than in the boot routine, for example. I also add a script just to blink an LED to make sure I know the device has reset when I click the reset button (useful during troubleshooting or updating your scripts). The front stuff also includes the header information, including authentication for the http post request.

A note on this important package, urequests: this is our tool for sending our http POST request to our server. It’s worth noting that I believe it will honor an https request, making the POST encypted. Even though we’re “encoding” in base64 the username+password combination that lets us submit an authenticated POST to our API, this is not an encrypted encoding!!! We are relying on the https encryption. Also, my understanding is that you might have issues if you use an ESP8266, because it does not have enough memory to support certificate verification. Unfortunately, most of the documentation that I’ve been able to find on this issue has been spotty, and I haven’t spent the time double-checking that my connection is secure. This is part of why we used an intermediate python script on the server to ensure the ESP32 is not directly connecting to the SQL database, as well as made sure that the python script was using SQL credentials that have limited abilities.

The main routine then goes into a “while True:” section, which is much like the “void loop” section when you’re using the C-based Arduino programming language. First we check if we’re still connected to wifi, and remedy the situation if we’re not. Then, the data collection is simple with the Adafruit BME280 packages: once you set up the pin parameters for i2c communication, you can just sample the values using bme.temperature, bme.humidity, etc. I do some slight formatting before taking a ~20 second average reading, then format my http post request. We post that, with a try-catch loop (I had had some problems with sockets not closing: this remedied that. It may not be strictly necessary). Then, I free up memory, which again may not be necessary. Since I leave this ESP32 running for months at a time, I want to be sure it’ll keep working without my checking on it!

This post request is sent off to the server that we already set up. If everything previous to this was done correctly, it’ll be inserted into the SQL database, and become data available for plotting when people visit the landing page of the website.