firstly I tried to do OTA by REST API 's following some forums and GitHub private repo
these are links...
I am using esp32 dev module .
#include <WiFi.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <WiFiClientSecure.h>
#include "cert.h"
const char * ssid = "yourSSID";
const char * password = "yourPASSWORD";
String FirmwareVer = {
"0.0"
};
// USING GITHUB API: https://api.github.com/repos/<user>/<repo>/contents/<path to the .bin>
//https://api.github.com/repos/Nazneenashrafi/esp32_ota/contents/sketch_may1b.ino.bin(link for bin file)
//https://api.github.com/repos/Nazneenashrafi/esp32_ota/contents/version.txt(link for text file )
#define URL_fw_Version "https://api.github.com/repos/Nazneenashrafi/ota/contents/version.txt"
#define URL_fw_Bin "https://api.github.com/repos/Nazneenashrafi/ota/contents/sketch_may1b.ino.bin"
void connect_wifi();
void firmwareUpdate();
int FirmwareVersionCheck();
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis_2 = 0;
const long interval = 60000;
const long mini_interval = 1000;
void repeatedCall() {
static int num=0;
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis) >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (FirmwareVersionCheck()) {
firmwareUpdate();
}
}
if ((currentMillis - previousMillis_2) >= mini_interval) {
previousMillis_2 = currentMillis;
Serial.print("idle loop...");
Serial.print(num++);
Serial.print(" Active fw version:");
Serial.println(FirmwareVer);
if(WiFi.status() == WL_CONNECTED)
{
Serial.println("wifi connected");
}
else
{
connect_wifi();
}
}
}
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
Button button_boot = {
14,
0,
false
};
void IRAM_ATTR isr() {
button_boot.numberKeyPresses += 1;
button_boot.pressed = true;
}
void setup() {
pinMode(button_boot.PIN, INPUT);
attachInterrupt(button_boot.PIN, isr, RISING);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.print("Active firmware version:");
Serial.println(FirmwareVer);
pinMode(4, OUTPUT);
connect_wifi();
}
void loop() {
if (button_boot.pressed) { //to connect wifi via Android esp touch app
Serial.println("Firmware update Starting..");
firmwareUpdate();
button_boot.pressed = false;
}
repeatedCall();
}
void connect_wifi() {
Serial.println("Waiting for WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void firmwareUpdate(void) {
WiFiClientSecure client;
client.setCACert(rootCACertificate);
httpUpdate.setLedPin(4,LOW);
t_httpUpdate_return ret = httpUpdate.update(client, URL_fw_Bin);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
int FirmwareVersionCheck(void) {
String payload;
int httpCode;
String fwurl = "";
fwurl += URL_fw_Version;
fwurl += "?";
fwurl += String(rand());
Serial.println(fwurl);
WiFiClientSecure * client = new WiFiClientSecure;
if (client)
{
client -> setCACert(rootCACertificate);
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
https.addHeader(F("Accept"), "application/vnd.github.v3.raw");
https.addHeader(F("authorization"), "Bearer XXXXXXXXXXXXXXXXXXXXX"); //auth key
if (https.begin( * client, fwurl))
{ // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
delay(100);
httpCode = https.GET();
delay(100);
if (httpCode == HTTP_CODE_OK) // if version received
{
payload = https.getString(); // save received version
} else {
Serial.print("error in downloading version file:");
Serial.println(httpCode);
}
https.end();
}
delete client;
}
if (httpCode == HTTP_CODE_OK) // if version received
{
payload.trim();
if (payload.equals(FirmwareVer)) {
Serial.printf("\nDevice already on latest firmware version:%s\n", FirmwareVer);
return 0;
}
else
{
Serial.println(payload);
Serial.println("New firmware detected");
return 1;
}
}
return 0;
}
so it is working properly with esp32 dev module without any sensor or device but not working device that connect with rfid oled and buttons .................... Anyone can help ?