This is my journey on creating a project to determine when it is best to mow your lawn. The idea was to be able to read a few sensors and take the values and display them on the WIO Terminal. And also upload the values to an MQTT broker for another application to process them.
Now with this project, I wanted to challenge myself by coding it using the Arduino IDE and FreeRTOS. This was both a good and bad idea. The good part is I was able to read the sensors and display the data using FreeRTOS tasks on the WIO terminal, but the bad part was I could not get any communications to work with FreeRTOS, I mainly tried to use Wi-Fi but could not get it to connect to my Wi-Fi network. I know there wasn't an issue with the hardware as I was able to connect to the network if I just loaded a simple Wi-Fi sample app.
For this project I use the Arduino IDE, that means you have to add some libraries to be able to compile the code. Before beginning, I would strongly suggest following the instructions on how to setup the WIO Terminal and also find each of the sensors on the SeeedStudio site and following the instructions to install any extra libraires necessary. I also would suggest loading up the example programs for any sensor you're using to make sure they work.
Now for my project I started with a sample FreeRTOS example and modified it, to work with the sensors and the WIO Terminal.
I setup the code to have a separate task for each sensor.
There are a few things to note here, the parameters that are important are the method to call (1st parameter), the number of bytes to allocate (3rd parameter), priority (5th parameter) and the handle (6 parameter).
Notice the DISPLAY_TASK has a lower priority then the rest, this is because we want to make sure sensor tasks are executed before updating the display.
The image above is how task are created. Notice they are similar to the loop() method in an Arduino sketch. Also note that there is an endless for loop. This is because FreeRTOS will manage when and how much CPU the task gets to use. Also in the code, nothing is ever put in the loop() method, this is because it never gets executed when using FreeRTOS
This also brings me to one of my first issues. Normally you should use a semaphore around the call to the sensor and setting the global variables. This is to make sure that the variable gets set before any other task uses it. Every time I tried using one, it would lock up the WIO Terminal. I would have to do a manual reset. No idea why this happened as any example of FreeRTOS code seems to be able to use them. I think my app worked without them because of how small the values being returned are.
Each of the task are similar on how they are setup, you can initialize the sensor in the setup() method. then write up the task method and finally add the task to the scheduler.
The one task that is a little different is the DISPLAY_TASK, here I created a method called drawScreen() that will draw all the details that doesn't change, this is called from the setup() and then I created a DISPLAY_TASK that will display all the values from the sensors. The code in the DISPLAY_TASK uses sprites, those will basically create update only the section defined in the sprite. It allows the screen to be redrawn without drawing the whole screen.
That is the basics of how I used FreeRTOS to create my app.
Now comes my second issue, Communications. When trying to setup the app to use a WIFI connection and MQTT. Before I did this, I loaded one of the examples sketches and configured it to use my WIFI settings. After a lot of head scratching, I remembered something I read in the setup. The WIFI device needed to have the firmware updated. The instruction on how to update the firmware are here. Once the firmware was updated and I reloaded the example sketch, the WIFI connection worked.
Now I just needed to add it to my application. Easier said than done, no matter what I did, it would not work in FreeRTOS. Every attempt locked up the WIO Terminal. After a lot of time debugging, I narrowed it down the rpcWiFi.h file. If I comment out the include file the app works, uncomment it, it locks up. I still can't figure out why.
The second part of the app was to send the data to MQTT and have a raspberry pi read the data to determine the best time to mow your lawn then notify you. I did use another contributors work on how to setup HiveMQTT on the WIO. So, I setup the MQTT part but without the communication link working I didn't have time to build the raspberry pi part.
The image below shows the running app, notice the Light sensor is showing 0, this is because the sensor is on the back of the WIO Terminal, and it is laying on the table. So, it won't read any light.
I think if I were to do it all over again, I would build the simple app first to have a better project to turn in, incase I'm not able to do what I really wanted to do.
I would like the thank the SeeedStudio and IoT Into the Wild Contest for Sustainable Planet 2022 for giving me the chance to build this project. It was very informative as I have never built an app using FreeRTOS or one that uses a TFT graphics like this before. And even though I didn't get it work like I wanted it to I'm sure I will be able to take what I have learned and use it in other projects.
Thanks
Dana.
Comments
Please log in or sign up to comment.