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×Button
1×OLED
1×Buzzer
1×Breadboard
n×Jumper Wires

About This project

We will make an OTA project that include RTOS , 3 Buttons , OLED and Buzzer

Review Topics

Please review this topics to get a better understand of this project

  • Basic OTA project. go to this link
  • Basic RTOS project. go to this link

Wiring Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Copy

// 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


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


int BUZZER_PIN = 13;
int BUZZER_CHANNEL = 0;

int Button1Pin = 14;
int Button1Status = 0;
int Button2Pin = 15;
int Button2Status = 0;
int Button3Pin = 18;
int Button3Status = 0;




void TaskButton( void *pvParameters );
void TaskOLED( void *pvParameters );
void TaskBuzzer( void *pvParameters );

const char* ssid = "WE_87B9D0";
const char* password = "jae05161";

void setup() {

  // Now set up three tasks to run independently.
  xTaskCreatePinnedToCore(
    TaskButton
    ,  "TaskButton"   // 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(
    TaskOLED
    ,  "TaskOLED"   // A name just for humans
    ,  5000  // 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(
    TaskBuzzer
    ,  "TaskBuzzer"   // 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();
  }
  //Hostname defaults to esp3232-[MAC]
  ArduinoOTA.setHostname("myesp32");

  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else // U_SPIFFS
        type = "filesystem";

      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      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();
}

void TaskOLED(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

/*
  OLED
  Show status of all elements on the projects
*/

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  vTaskDelay(2000); // Pause for 2 seconds

  display.clearDisplay();                 // clear display
  display.setTextSize(1);                 // set text size
  display.setTextColor(WHITE, BLACK);     // set text color

  for (;;) // A Task shall never return or exit.
  {
    if(Button1Status==1)
    {
      display.setCursor(5, 5);                   // set position to display
      display.println("button1 status : ON ");    // set text
      display.display();                         // display on OLED
    }
    else
    {
      display.setCursor(5, 5);                   // set position to display
      display.println("button1 status : OFF");    // set text
      display.display();                         // display on OLED
    }

    if(Button2Status==1)
    {
      display.setCursor(5, 20);                     // set position to display
      display.println("button2 status : ON ");    // set text
      display.display();                         // display on OLED
    }
    else
    {
      display.setCursor(5, 20);                     // set position to display
      display.println("Button2 status : OFF");    // set text
      display.display();                         // display on OLED
    }

    if(Button3Status==1)
    {
      display.setCursor(5, 35);                     // set position to display
      display.println("button3 status : ON ");    // set text
      display.display();                         // display on OLED
    }
    else
    {
      display.setCursor(5, 35);                     // set position to display
      display.println("Button3 status : OFF");    // set text
      display.display();                         // display on OLED
    }
    
  }
}


void TaskBuzzer(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
*/

  ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL);

  for (;;) // A Task shall never return or exit.
  {
    ledcSetup(BUZZER_CHANNEL, 635, 8);
    ledcWrite(BUZZER_CHANNEL, 50);
    vTaskDelay(700);
    ledcSetup(BUZZER_CHANNEL, 911, 8);
    ledcWrite(BUZZER_CHANNEL, 50);
    vTaskDelay(700);
  }
}


void TaskButton(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

/*
  Button
  read the state of the button
*/

  // initialize Button pin as an INPUT.
  pinMode(Button1Pin,INPUT_PULLUP);
  pinMode(Button2Pin,INPUT_PULLUP);
  pinMode(Button3Pin,INPUT_PULLUP);


  for (;;) // A Task shall never return or exit.
  {
    if (digitalRead(Button1Pin)== LOW)
    {
        Button1Status = 1;
        vTaskDelay(10);
    }
    else
    {
      Button1Status = 0;
      vTaskDelay(10);
    }

    if (digitalRead(Button2Pin)== LOW)
    {
        Button2Status = 1;
        vTaskDelay(10);
    }
    else
    {
      Button2Status = 0;
      vTaskDelay(10);
    }

    if (digitalRead(Button3Pin)== LOW)
    {
        Button3Status = 1;
        vTaskDelay(10);
    }
    else
    {
      Button3Status = 0;
      vTaskDelay(10);
    }
  }
}           

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • See the changes you made

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!