Data Logger using STM32 and ESP8266
I have covered a tutorial about Webserver using STM32 and ESP8266, where ESP8266 was used to create a webserver to control the LED on STM32. This tutorial will also cover another application of ESP8266, and here we will use it to log the data into the Thingspeak server.
This data will be collected from the ADC, potentiometer in my case, and some other local variables, and the values will be updated on the Thingspeak server.
************* UPDATE 2 ***************
L4 series and G series MCUs have some issues with UART_ISR_TXE. If you are having issues even after making changes according to UPDATE 1, then Replace the Uart_isr with the file https://controllerstech.com/wp-content/uploads/2020/08/Uart_Isr_single_new.c
************* UPDATE 1 ***************
Those, who have errors in the Uart_isr function, eg- F7 series MCU, replace the Uart_isr functon with the one provided in this file https://controllerstech.com/wp-content/uploads/2020/04/Uart_Isr_single.c
Setup part is very simple. All we need to do is enable the UART at 115200 baud rate. And also make sure to enable the interrupt, because the entire ring buffer concept works on interrupt itself
Also for the ADC, I am using only 1 channel, where the potentiometer is connected. For more details on ADC, and how to set it up and use it, check the link https://controllerstech.com/stm32-adc-multi-channels/
Next, we need to setup the Thingspeak for receiving data. Create an account on Thingspeak.com if you don’t have one.
Go to channels, and create NEW CHANNEL
You can setup the number of fields you want, as in the picture below I will send the 4 variables and that’s why selected 4 fields to store them
you can give name to these field charts, and arrange their positions. My final setup looks as shown below
Finally, we need to include the library files into the project. Just copy the ESPDataLogger.c and UartRingbuffer.c files into the src and ESPDataLogger.h and UartRingbuffer.h files into the inc directories of your project.
We need to modify the UART interrupt handler, and redirect it to our interrupt handler. Modify it as shown in the picture below
The connection of ESP8266 with the STM32 will be same as always. Connect the TX to RX pin, and RX to TX pin as shown below
This is it for the setup, now let’s take a look at how this library works
Some insight into the code
ESP_INIT will initialize the ESP8266 by connecting it with the provided Access Point
AT+CIPMUX = 0 is to disable the Multiple connections. This is necessary for setting up the TCP connection
ESP_Send_Multi is to send the data to the multiple fields
@APIKey is the unique key that you can get from the Thingspeak channel setting
@numberoffields are the number of fields in your channel, where you want to update the data
@value is the array of values that you want to send to the thingspeak channel
Updating multiple fields at once becomes a requirement because Thingspeak only allows to communicate once in every 15 seconds. So it’s better if we update all the fields at once.
As mentioned above, first of all I am polling for the ADC value
Then converting the value to the respective voltage
Then incrementing the count variable
Next, storing the values to the respective positions in the array, in the order that I want to send them
And finally, send the values using the ESP_Send_Multi function
You can see above, the values are updating every 15 seconds. There is change in ADC value, as I was rotating the potentiometer, and also the voltage follow the same change. The count, and count X 2 increments every 15 seconds too.