DIY Connected Espresso Machine: Over-the-Air Updates (Part 6)
As Main Class controlling the Espresso Machine is completed, we can start moving into the “Connected” features! So in this article, we will set up Wi-Fi communication using NodeMCU but also introduce Over-the-Air updates to be able to update the firmware of the Espresso Machine in assembled state.
The information provided will be relevant not only to this project but to any project you’d like to have similar features using the NodeMCU microcontroller.
As Wi-Fi is an essential feature of the connected device, I drop mentioning Arduino and focus only on NodeMCU since a single microcontroller of this type will be enough to cover all requirements of this project, however, it can also be used as a module adjacent to Arduino to enable Wi-Fi capabilities.
If you are new to NodeMCU you can start with another article describing Quick start with NodeMCU v3 (ESP8266), Arduino ecosystem, and PlatformIO IDE.
Firmware
Firmware is the only part we’ll touch on in this article, so let’s start!
Setup Wi-Fi
The first step is to enable Wi-Fi communication and we’ll use the WiFiManager library for that.
Introducing a new dependency to the PlatformIO project is as easy as going to platformio.ini
and adding a reference as below:
lib_deps =
tzapu/WiFiManager @ ^0.16.0
Next, we go to the main.cpp
and implement a method to set up Wi-Fi:
This will configure NodeMCU hostname as Connected-Espresso-Machine
(works intermittently actually) and start Wi-Fi Access Point with SSID Connected-Espresso-Machine
if no credentials to connect to your Wi-Fi network are yet configured in NodeMCU.
All further code is paused until you connect to this access point and configure credentials via the captive portal to connect to your home Wi-Fi network, however, this will be demonstrated later.
After the credentials are provided, this method will try to connect to the Wi-Fi network and continue code execution. All subsequent microcontroller restarts will try to connect to the Wi-Fi network using credentials first and fall back to the access point in case of failure.
Such logic and only a few code lines, magic!
Setup Over-the-Air Updates
Over-the-Air updates are a really powerful feature when it comes to upgrading the IoT device which is hard to get to as in the case of this project: microcontroller will be placed inside the espresso machine shell (I hope it fits) without a way to connect to it using USB wire.
To get more details on the matter, follow the OTA Updates document of the ESP8266 Arduino Core documentation.
Thanks to PlatformIO IDE it is an out-of-the-box supported feature and firmware can be flashed right from the IDE if the computer running it is inside the same Wi-Fi network, more details on that can be found here.
So let’s implement another method to support Over-the-Air updates on the microcontroller side:
This will use the connected-espresso-machine
hostname for PlatformIO IDE to be able to connect to it based on the hostname, not using the IP address which can change. The rest are callbacks triggered by the flashing process and taken as-is from the example.
Test
Well, that’s basically it!
Let’s trigger the methods in the setup()
function and add a handler for the Over-the-Air updates in main.cpp
:
Connect to Wi-Fi Network
The first time we still need to use USB wire to flash the new firmware, let’s see how it goes in the serial terminal:
Named access point shows up:
And captive portal launched as soon as we connect to this access point:
Click on “Configure Wi-Fi”, I am using my phone access point as home Wi-Fi network:
After the credentials are saved, take a look at the serial terminal:
It’s alive! And connected to the home Wi-Fi network with the IP address output in the terminal.
Over-the-Air Update
Next, let’s open the serial terminal separate from PlatformIO as during flashing it will be closed by the IDE.
In macOS I use screen
command to directly connect to the serial port of NodeMCU, however, you can use a serial terminal of your choice:
screen /dev/cu.usbserial-1430 9600
After that, we can configure PlatformIO to flash the firmware over Wi-Fi using the following settings in the platformio.ini
:
upload_protocol = espota
upload_port = connected-espresso-machine.local
Final platformio.ini
:
Double-check your computer running PlatformIO is on the same network as NodeMCU, and build and upload firmware as usual in PlatformIO, this will output:
Switch back to the serial terminal to check how the update went:
Hooray! Firmware was updated and we don’t need to use USB wire to flash the NodeMCU anymore! Well done!
Next Steps
As critical firmware has been completed, NodeMCU is able to connect to the home Wi-Fi network and the rest of the firmware improvements can be done Over-the-Air we can assemble the hardware, so that will be done in the next article!
The project code is available here: https://github.com/loginov-rocks/Connected-Espresso-Machine
That’s all for today, see you next time!
Next part: https://loginov-rocks.medium.com/diy-connected-espresso-machine-assembly-part-7-7b590ef343fd