ESP32 - OTA - RTOS
We are going to make a project that:
-
Update the firmware wirelessly (OTA).
-
Have a three tasks woring in parrallel (RTOS).
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
3 | × | LED | |
3 | × | 220 ohm resistor | |
1 | × | Breadboard | |
7 | × | Jumper Wires |
Wiring Diagram
Schematic Diagram
Image is developed using Fritzing. Click to enlarge image
Breadboard Connections
ESP32 Code
/* * * Created by Mohamed Ahraf * * This example code is in the public domain * */ // Include required libraries #include <WiFi.h> #include <ESPmDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> #if CONFIG_FREERTOS_UNICORE #define ARDUINO_RUNNING_CORE 0 #else #define ARDUINO_RUNNING_CORE 1 #endif void TaskBlink( void *pvParameters ); void TaskBlink2( void *pvParameters ); void TaskBlinkBuiltIn( void *pvParameters ); const char* ssid = "WE_87B9D0"; const char* password = "jae05161"; void setup() { // Now set up three tasks to run independently. xTaskCreatePinnedToCore( TaskBlink1 , "TaskBlink1" // A name just for humans , 1024 // This stack size can be checked & adjusted by reading the Stack Highwater , NULL , 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest. , NULL , ARDUINO_RUNNING_CORE); xTaskCreatePinnedToCore( TaskBlink2 , "TaskBlink2" // A name just for humans , 1024 // This stack size can be checked & adjusted by reading the Stack Highwater , NULL , 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest. , NULL , ARDUINO_RUNNING_CORE); xTaskCreatePinnedToCore( TaskBlink3 , "TaskBlink3" // A name just for humans , 1024 // This stack size can be checked & adjusted by reading the Stack Highwater , NULL , 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest. , NULL , ARDUINO_RUNNING_CORE); Serial.begin(115200); Serial.println("Booting"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } ArduinoOTA.setHostname("myesp32"); //Hostname defaults to esp3232-[MAC] ArduinoOTA //Set up the OTA configuration .onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else type = "filesystem"; Serial.println("Start updating " + type); }) .onEnd([]() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { ArduinoOTA.handle(); //Check for new update } void TaskBlink1(void *pvParameters) // This is a task. { /* Blink Turns on an LED on for one second, then off for one second, repeatedly. */ pinMode(13, OUTPUT); // initialize digital LED_BUILTIN on pin 13 as an output. for (;;) // A Task shall never return or exit. { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) vTaskDelay(100); // one tick delay (15ms) in between reads for stability digitalWrite(13, LOW); // turn the LED off by making the voltage LOW vTaskDelay(100); // one tick delay (15ms) in between reads for stability } } void TaskBlink2(void *pvParameters) // This is a task. { pinMode(12, OUTPUT); // initialize digital LED_BUILTIN on pin 13 as an output. for (;;) // A Task shall never return or exit. { digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level) vTaskDelay(200); // one tick delay (15ms) in between reads for stability digitalWrite(12, LOW); // turn the LED off by making the voltage LOW vTaskDelay(200); // one tick delay (15ms) in between reads for stability } } void TaskBlink3(void *pvParameters) // This is a task. { pinMode(14, OUTPUT); // initialize digital LED_BUILTIN on pin 13 as an output. for (;;) // A Task shall never return or exit. { digitalWrite(14, HIGH); // turn the LED on (HIGH is the voltage level) vTaskDelay(300); // one tick delay (15ms) in between reads for stability digitalWrite(14, LOW); // turn the LED off by making the voltage LOW vTaskDelay(300); // one tick delay (15ms) in between reads for stability } }
※ NOTE THAT:
At the first time you upload an OTA firmware you need to upload it via usb, then each time you want to change the code you can upload it wirelessly like these following steps.
Quick Steps
- power up your board
- Open Arduino IDE, select the right board
- Select the right port from Tools->port->Network ports
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Press and keep pressing the button several seconds
- See the changes you made
Code Explanation
Read the line-by-line explanation in comment lines of source code!
Book Tutorial
We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!