Bharat Pi Wireless Update Guide (Over the Air)

by bharatpi in Circuits > Computers

82 Views, 0 Favorites, 0 Comments

Bharat Pi Wireless Update Guide (Over the Air)

1_4ALCljNoBHm1XUK2pOf94Q.png

OTA stands for "Over-The-Air." In the context of programming or updating devices, OTA refers to the capability to remotely update or modify the firmware or software of a device without requiring a physical connection, such as a USB cable.

OTA updates are particularly common in IoT (Internet of Things) devices, embedded systems, and microcontroller-based projects where it may be impractical or inconvenient to physically access the device for updates. Instead, OTA enables users to deploy updates to multiple devices simultaneously and even automate the update process.

For example, in the context of the Bharat Pi boards, OTA programming allows you to upload new firmware to the board wirelessly over Wi-Fi, eliminating the need for a direct serial connection between the board and your computer. This is especially useful for devices deployed in remote or hard-to-reach locations.

Supplies

Arduino Ide

https://www.arduino.cc/en/software

Bharat Pi Boards

https://bharatpi.net/shop/

What Is the Need of OTA?

Over-The-Air (OTA) updates fulfill several crucial needs in the realm of software and firmware deployment, especially in the context of connected devices and embedded systems. Some of the primary needs and benefits of OTA updates include:

  1. Convenience and Accessibility: OTA updates allow for remote deployment of software updates without requiring physical access to the device. This is particularly valuable for devices deployed in inaccessible or remote locations, or in situations where updating manually would be inconvenient.
  2. Efficiency and Scalability: With OTA updates, developers can push updates to multiple devices simultaneously, making it highly efficient for managing fleets of connected devices or large-scale deployments. This scalability helps ensure that all devices are running the latest software versions with minimal effort.
  3. Cost Savings: Eliminating the need for manual updates, especially for devices deployed across wide geographic areas or in large numbers, can result in significant cost savings in terms of time, labor, and resources.
  4. Improved Security: OTA updates enable prompt delivery of security patches and software fixes, reducing the window of vulnerability for connected devices. This helps in addressing security vulnerabilities and mitigating potential threats promptly.
  5. Enhanced User Experience: By ensuring that devices are always up-to-date with the latest features and improvements, OTA updates contribute to a better user experience and customer satisfaction. Users benefit from access to new functionalities and bug fixes without any downtime or inconvenience.
  6. Adaptability and Flexibility: OTA updates allow for iterative development and continuous improvement of software and firmware. Developers can easily roll out updates in response to user feedback, changing requirements, or emerging market trends, ensuring that devices remain relevant and adaptable over time.
  7. Compliance and Regulation: In certain industries, compliance with regulatory standards and certifications may require the ability to remotely update device firmware. OTA updates provide a mechanism for maintaining compliance and meeting regulatory requirements effectively.


Ways to Implement OTA in Bharat Pi

There are two ways to implement OTA functionality in the Bharat Pi

  • Basic OTA – updates are delivered using the Arduino IDE.
  • Web Updater OTA – updates are delivered via a web browser.

Each one has its own benefits, so you can use whichever one works best for your project.


Upload Basic OTA Firmware Serially

Screenshot 2024-04-01 223403.png

The Arduino IDE includes an OTA library as well as a BasicOTA example. Simply navigate to 

File > Examples > ArduinoOTA > BasicOTA.

Before you begin uploading the sketch, you must modify the following two variables with your network credentials so that the Bharat Pi can connect to an existing network.

const char* ssid = "..........";
const char* password = "..........";

When you’re finished, go ahead and upload the sketch.


Code


#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
const char* password = "..........";

void setup() {
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();
}

// Port defaults to 3232
// ArduinoOTA.setPort(3232);

// Hostname defaults to esp3232-[MAC]
// ArduinoOTA.setHostname("myesp32");

// No authentication by default
// ArduinoOTA.setPassword("admin");

// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

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

Serial Monitor

Screenshot 2024-04-01 223729.png

Now, open the Serial Monitor at 115200 baud rate and press the EN button on the bharat pi. If everything is fine, you should see the dynamic IP address assigned by your router. Make a note of it.



Upload New Sketch Over-The-Air

Now, let’s upload a new sketch over-the-air.

Remember that you must include the OTA code in every sketch you upload. Otherwise, you will lose OTA capability and will be unable to perform the next over-the-air upload. Therefore, it is recommended that you modify the preceding code to include your new code.

As an example, we will include a simple Blink sketch in the Basic OTA code. Remember to modify the SSID and password variables with your network credentials.

Changes in the Basic OTA program

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
const char* password = "..........";


//variabls for blinking an LED with Millis
const int led = 2; // ESP32 Pin to which onboard LED is connected
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
int ledState = LOW; // ledState used to set the LED

void setup() {


pinMode(led, OUTPUT);

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

// Port defaults to 3232
// ArduinoOTA.setPort(3232);

// Hostname defaults to esp3232-[MAC]
// ArduinoOTA.setHostname("myesp32");

// No authentication by default
// ArduinoOTA.setPassword("admin");

// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

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

//loop to blink without delay
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = not(ledState);
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}

}

Next Step

Screenshot 2024-04-01 224027.png

After copying the above sketch to your Arduino IDE, navigate to Tools > Port option. Look for something like: esp32-xxxxxx at your_esp_ip_address. If you are unable to locate it, you may need to restart your IDE.

Choose the port and press the Upload button. The new sketch will be uploaded in a matter of seconds. The on-board LED should start blinking.