OTA PROBLEM with esp32

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 ?

what is the problem is there any problem in my forum writing
I am not sharing anyone's code . I shared those links that open for all public GitHub repo and he give the tutorial too on YouTube so I am not stealing anyone's code

help!!!!!!!!!!!

A schematic might help, you say:

So something is going wrong when you attach those parts, perhaps a wiring mistake, but without a schematic and photos that show your wiring it's impossible for anyone to say. Your photo shows your ESP32 not connected to anything.

You say it's 'not working' but don't say what that means: what should happen? What happens instead?

That's about the only advice I can give as I know nothing about the ESP32 or OTA updates.

It's possible no one has replied because no one knows anything about what you are asking about.

I think wiring are proper because RFID and OLED function in which I scanned RFID and print UID number on display ..worked perfectly .
but when I upload the above sketch … and press the boot button firmware didn't update.
but sketch worked fine with esp32 dev module without any connection with other device like (button ,display, rfid, relay).
if anyone know …then reply to this … :cold_sweat:

Sorry, I looked at this post a few times..
Your ESP32 scares me..
Don't by chance have another one that came with the headers already soldered??

Forgetting that for a minute..
The isr in the sketch is wrong..
Buttons bounce and you have to protect access to the structure..

Don't use interrupt for the button, just check it in the loop and might need a millis timer to debounce..

You say "not working", need more info than that..
I see one Serial print in setup, did you see that??
note: this is after you fired up that poorly constructed isr, so it's possible it froze right there..

good luck.. ~q

sorry for confusion :cold_sweat:
I have check the device many times I saw that OTA update worked sometimes ,didn't work sometimes.
I got the error from HTTPClient.h library stream timeout ….
I don't know how to solve it . may be there is a issue of network ...
because I connect my esp32 with my phone network.

ok I will change this one.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.