(Individual) design, build, and connect wired or
wireless node(s) with network or bus addresses
This week was an opportunity to build upon the simple Finite State Machine (FSM) that I built for Week 6 (Embedded Programming) in which the board takes in input from a push button and counts with reasonable time outs), then flashes an LED the same number of times the button has been pressed.
I wanted to build upon this system by instead using an ESP32 Board. Instead of flashing an LED, it would instead send an HTTP GET request to an API that generates facts about numbers.
This is now the revised state machine (very similar to the previous state machine, but with a different state for performing HTTP requests):
There are a total of four states:
State 0, IDLE (essentially our counting state): initially, we are at count = 0, and each time the button is pressed (importantly, not released), we then transition to the PRESSED state.
State 1, PRESSED: this is a transition state. Once the button is released, we immediately transition to the TIMER state. This state is necessary because we only want to increment the counter after the button is pressed AND released.
State 2, TIMER: using a timer, we see if the button is pressed in < timeout. If it is (aka, the button presses are in relatively short succession to one another), we transition back to the PRESSED state and increment our counter. If it isn't (aka, there has been a long pause/break with no button presses), we transition to the FLASH state.
State 3, GET (essentially, connecting to a trivia numbers API): here, we go through a blocking HTTP request in which we send to numbersapi for a trivia fact. Once it has finished the request, it will then print onto the serial monitor. Then, the count will reset and we will return to the IDLE state of our state machine.
The Numbers API ("An API for interesting facts about numbers") is extremely easy to interface with because you simply put the query after the backslash of the link when sending a GET request to the API. For example, simply visiting this link (with the url: http://numbersapi.com/5) about the number 5 will give you the response "5 is the most common number of gears for automobiles with manual transmission."
This is a picture of the ESP32 Board, with the push buttons. I connected a push button to pin 5.
As always when using pushbuttons, I incorporated a function to deal with debouncing.
This is a picture of the code used to format HTTP GET:
This is a picture of the code used (within the GET state) to send an HTTP GET request:
This is a picture of what is printed on the Serial Monitor, for example, when two button presses are received: