How to Implement OTA Updates for ESP32 with FreeRTOS on Google Cloud IoT Core?

@Middleware & OS Hello guys am interested in over the air (OTA) firmware update of an ESP32 microcontroller running FreeRTOS and connect it to Google Cloud IoT Core for data monitoring, please i would need any form of guidance i can get i.e code examples or documentation and suggested libraries that can aid me in OTA updates for ESP32 with FreeRTOS
Solution:
Your code should be identical or similar to 👇 ```cpp #include <WiFi.h> #include <PubSubClient.h> #include <ArduinoOTA.h>...
Jump to solution
5 Replies
Marvee Amasi
Marvee Amasi•5mo ago
Hi @Boss lady HTTP and MQTT are popular protocols for OTA updates on an ESP32 , which are u leaning on to?
Boss lady
Boss lady•5mo ago
MQTT
Dtynin
Dtynin•5mo ago
@Boss lady Create an account on their official site, install necessary libraries like ArduinoOTA library, PubSubClient library, etc then configure the network by connecting your ESP32 to the wifi network using the Wi-Fi library, then you implement OTA firmware update functionality
Solution
Enthernet Code
Enthernet Code•5mo ago
Your code should be identical or similar to 👇
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqttServer = "mqtt.googleapis.com";
const char* mqttUsername = "unused";
const int mqttPort = 8883;
const char* devicePrivateKey = "YOUR_DEVICE_PRIVATE_KEY";

WiFiClientSecure wifiClient;
PubSubClient client(wifiClient);

void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}

void connectMQTT() {
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
if (client.connect("ESP32-device", mqttUsername, devicePrivateKey)) {
client.subscribe("your_topic");
} else {
delay(1000);
}
}
}

void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages here
}

void setup() {
Serial.begin(115200);

connectWiFi();

ArduinoOTA.setHostname("ESP32-device");
ArduinoOTA.setPassword("Your_OTA_Password");

// Callbacks to handle OTA updates
ArduinoOTA.onStart([]() {
Serial.println("OTA update started...");
});

ArduinoOTA.onEnd([]() {
Serial.println("\nOTA update complete!");
ESP.restart();
});

ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));

// You can use progress/total to publish update progress to Google Cloud IoT Core
});

ArduinoOTA.begin();

connectMQTT();
}

void loop() {
ArduinoOTA.handle();

if (!client.connected()) {
connectMQTT();
}
client.loop();

// Perform your regular tasks here

delay(1000);
}
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqttServer = "mqtt.googleapis.com";
const char* mqttUsername = "unused";
const int mqttPort = 8883;
const char* devicePrivateKey = "YOUR_DEVICE_PRIVATE_KEY";

WiFiClientSecure wifiClient;
PubSubClient client(wifiClient);

void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}

void connectMQTT() {
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
if (client.connect("ESP32-device", mqttUsername, devicePrivateKey)) {
client.subscribe("your_topic");
} else {
delay(1000);
}
}
}

void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages here
}

void setup() {
Serial.begin(115200);

connectWiFi();

ArduinoOTA.setHostname("ESP32-device");
ArduinoOTA.setPassword("Your_OTA_Password");

// Callbacks to handle OTA updates
ArduinoOTA.onStart([]() {
Serial.println("OTA update started...");
});

ArduinoOTA.onEnd([]() {
Serial.println("\nOTA update complete!");
ESP.restart();
});

ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));

// You can use progress/total to publish update progress to Google Cloud IoT Core
});

ArduinoOTA.begin();

connectMQTT();
}

void loop() {
ArduinoOTA.handle();

if (!client.connected()) {
connectMQTT();
}
client.loop();

// Perform your regular tasks here

delay(1000);
}
youcef_ali
youcef_ali•5mo ago
Which frame works Arduino or esp-idf?
Want results from more Discord servers?
Add your server