ESP32 - OTA - 2Buttons+servo+RGB (Angle update+ color updat)

This tutorial instructs you how to use OTA with ESP32 to make a project that enables you to change the code over the air

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
2×Button
1×servo
1×RGB led
1×Breadboard
n×Jumper Wires

Introduction to OTA

What is OTA programming in ESP32?

The OTA programming allows updating/uploading a new program to ESP32 using Wi-Fi instead of requiring the user to connect the ESP32 to a computer via USB to perform the update.

OTA functionality is extremely useful in case of no physical access to the ESP module. It helps reduce the amount of time spent for updating each ESP module at the time of maintenance.

One important feature of OTA is that one central location can send an update to multiple ESPs sharing same network.

The only disadvantage is that you have to add an extra code for OTA with every sketch you upload, so that you're able to use OTA in the next update.

How To Program OTA

Ways To Implement OTA In ESP32

There are two ways to implement OTA functionality in ESP32.

  • Basic OTA - Over-the-air updates are sent through Arduino IDE.
  • Web Updater OTA - Over-the-air updates are sent through a web browser.

Each one has its own advantages. You can implement any one according to your project's requirement.

Below tutorial covers Basic OTA implementation.

3 Simple Steps To Use Basic OTA with ESP32

  • Install Python 2.7.x series The first step is to install Python 2.7.x series in your computer.
  • Upload Basic OTA Firmware SeriallyUpload the sketch containing OTA firmware serially. It's a mandatory step, so that you're able to do the next updates/uploads over-the-air.
  • Upload New Sketch Over-The-AirNow, you can upload new sketches to the ESP32 from Arduino IDE over-the-air.

Wiring Diagram

Breadboard Connections

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>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position


#define PIN_RED    23 // GIOP23
#define PIN_GREEN  22 // GIOP22
#define PIN_BLUE   21 // GIOP21

const int freq = 5000;
const int redChannel = 10;
const int greenChannel = 11;
const int blueChannel = 12;

const int resolution = 8;


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

void setup() {    

  myservo.attach(4);  // attaches the servo on pin 4 to the servo object
  pinMode(5,INPUT_PULLUP);
  pinMode(18,INPUT_PULLUP);


  ledcSetup(redChannel, freq, resolution);
  ledcSetup(greenChannel, freq, resolution);
  ledcSetup(blueChannel, freq, resolution);

  ledcAttachPin(PIN_RED, redChannel);
  ledcAttachPin(PIN_GREEN, greenChannel);
  ledcAttachPin(PIN_BLUE, blueChannel);

  
  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();

   if(digitalRead(5)==LOW)
  {
    setColor(0, 0, 250);
    pos=90;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    
  }

  if(digitalRead(18)==LOW)
  {
    setColor(250, 0, 250);
    pos=0;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    
        
  }
}

void setColor(int R, int G, int B) {
  ledcWrite(redChannel, R);
  ledcWrite(greenChannel, G);
  ledcWrite(blueChannel, B);
}           

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!