OTAwebupdate / ESP32

Hi friends, do you know why I’m suddenly not able to do OTAwebupdate to my ESP32 dev board without pushing the enable button on the board after uploading the project. The updates were working fine until i tried uploading a new code today. Here is the code

#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>

WebServer server(80);

const char* loginIndex = 
 "<form name='loginForm'>"
    "<table width='20%' bgcolor='A09F9F' align='center'>"
        "<tr>"
            "<td colspan=2>"
                "<center><font size=4><b>ESP32 Login Page</b></font></center>"
                "
"
            "</td>"
            "
"
            "
"
        "</tr>"
        "<td>Username:</td>"
        "<td><input type='text' size=25 name='userid'>
</td>"
        "</tr>"
        "
"
        "
"
        "<tr>"
            "<td>Password:</td>"
            "<td><input type='Password' size=25 name='pwd'>
</td>"
            "
"
            "
"
        "</tr>"
        "<tr>"
            "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
        "</tr>"
    "</table>"
"</form>"
"<script>"
    "function check(form)"
    "{"
    "if(form.userid.value=='nabofarm' && form.pwd.value=='nabofarm')"
    "{"
    "window.open('/serverIndex')"
    "}"
    "else"
    "{"
    " alert('Error Password or Username')/*displays error message*/"
    "}"
    "}"
"</script>";
 
/*
 * Server Index Page
 */
 
const char* serverIndex = 
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
   "<input type='file' name='update'>"
        "<input type='submit' value='Update'>"
    "</form>"
 "<div id='prg'>progress: 0%</div>"
 "<script>"
  "$('form').submit(function(e){"
  "e.preventDefault();"
  "var form = $('#upload_form')[0];"
  "var data = new FormData(form);"
  " $.ajax({"
  "url: '/update',"
  "type: 'POST',"
  "data: data,"
  "contentType: false,"
  "processData:false,"
  "xhr: function() {"
  "var xhr = new window.XMLHttpRequest();"
  "xhr.upload.addEventListener('progress', function(evt) {"
  "if (evt.lengthComputable) {"
  "var per = evt.loaded / evt.total;"
  "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  "}"
  "}, false);"
  "return xhr;"
  "},"
  "success:function(d, s) {"
  "console.log('success!')" 
 "},"
 "error: function (a, b, c) {"
 "}"
 "});"
 "});"
 "</script>";

/*
 * setup function
 */

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";

// Your WiFi credentials.
// Set password to "" for open networks.
const char* host = "esp32";
const char* ssid = "nabofarm";
const char* password = "regnbuer";




#define DHTPIN 4        // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);








{
const int ledpin = 16; 

const int freq= 20000; 

const int channel= 0; 

const int resolution= 8;
}
{
const int ledpin2 = 17; 

const int freq= 20000; 

const int channel= 1; 

const int resolution= 8;
}














  
}

void setup()
{


ledcSetup (0, 20000, 8); 

ledcAttachPin (16 , 0);

ledcSetup (1, 20000, 8); 

ledcAttachPin (17 , 1);


Serial.begin(115200);

  // Connect to WiFi network
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  /*use mdns for host name resolution*/
  if (!MDNS.begin(host)) { //http://esp32.local
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
  /*return index page which is stored in serverIndex */
  server.on("/", HTTP_GET, []() {
    server.sendHeader("Connection", "close");
    server.send(200, "text/html", loginIndex);
  });
  server.on("/serverIndex", HTTP_GET, []() {
    server.sendHeader("Connection", "close");
    server.send(200, "text/html", serverIndex);
  });
  /*handling uploading firmware file */
  server.on("/update", HTTP_POST, []() {
    server.sendHeader("Connection", "close");
    server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
    ESP.restart();
  }, []() {
    HTTPUpload& upload = server.upload();
    if (upload.status == UPLOAD_FILE_START) {
      Serial.printf("Update: %s\n", upload.filename.c_str());
      if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
        Update.printError(Serial);
      }
    } else if (upload.status == UPLOAD_FILE_WRITE) {
      /* flashing firmware to ESP*/
      if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
        Update.printError(Serial);
      }
    } else if (upload.status == UPLOAD_FILE_END) {
      if (Update.end(true)) { //true to set the size to the current progress
        Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
      } else {
        Update.printError(Serial);
      }
    }
  });
  server.begin();





  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, password);
 

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{


   server.handleClient();
  delay(1);
{
digitalRead (DHTPIN); 
if (dht.readHumidity() < 70)
 ledcWrite(0, 255);
  
    else if
    
 (dht.readHumidity() > 70)
    ledcWrite(0, 255);
}
{
  digitalRead (DHTPIN); 
if (dht.readHumidity() < 70)
 ledcWrite(1, 255);
  
    else if
    
 (dht.readHumidity() > 70)
    ledcWrite(1, 255);
}
{

  delay (1000);

}


{
  
  Blynk.run();
  timer.run();
}

}

may be you miss the    MDNS.addService("http", "tcp", 80);after the server.begin();

Side note:
What do you expect your digitalRead (DHTPIN); to do ??

I will try to add the code you suggested :slight_smile:
The digitalRead (DHTPIN) is for reading the humidity conditions and make my PWM react to the values.

best Frederik

And thank you for the advice for the OTA update. I will try to run it now :smiley:

frederiksolver:
The digitalRead (DHTPIN) is for reading the humidity conditions and make my PWM react to the values.

hum... think about it for while :slight_smile:

Its already reading I guess :smiley: now I just deleted the digitalRead. It's weird that ever since I add the code you suggested the update just keeps my ESP32 offline and the PWM is 0 although I added 255. And now it's not working even when I delete the code again.

It's so weird. :frowning: Im able to do the update with the USB connection and everything works as supposed but every time I send the .bin file through the WEBupdater the ESP32, Blynk and everything else just shuts down... it's both with the code you suggested and without.

May be read the discussion there and test their examples

Every time you flash an OTA sketch to an ESP the ESP requires a manual reboot before it will work. Once you have done the initial flash you never need to manually reset the ESP again if you update via OTA but if you flash the ESP at a later date it will require the manual reset.