Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By n1c0l45
#57276 Hi everyone,

Some friends and I are working on a school project with a Wemos D1 mini (ESP012) but as we are arduino beginers we need help :)

We have a arduino program running, but we need to be able to start a wifi AP to do OAT update (with a smartphone or tablet) when a special input is activate.

We did a sub program (only activate on boot of the program by a special input during the first 3 sec) to start the Wifi to get update OTA from arduino examples available in IDE.
We try so many things, it seems to work but the wifi is always on and we don't want that (wifi on only when this sub program is activate).

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "AP_update";
const char *password = "azerty1234";

ESP8266WebServer server(80);
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Mettre a jour'></form>";

byte mode_update = 0;
byte Serverwifi_actif = 0;

void setup() {
  pinMode(D3, INPUT);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Attente bouton update?");
  delay(3000);
}

void loop() {
  if (mode_update == 0 && !digitalRead(D3) || mode_update == 1) {
    mode_update = 1;
    Mode_update();
  }
  else {
    mode_update = 2;
    Mode_manuel();
  }
}

void Mode_update() {
  Serial.println("Programme Update OTA");

  if (Serverwifi_actif == 0) {
    WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    server.begin();
    Serial.println("Serveur HTTP demarre");

    server.on("/", HTTP_GET, []() {
      server.sendHeader("Connection", "close");
      server.sendHeader("Access-Control-Allow-Origin", "*");
      server.send(200, "text/html", serverIndex);
    });
    server.on("/update", HTTP_POST, []() {
      server.sendHeader("Connection", "close");
      server.sendHeader("Access-Control-Allow-Origin", "*");
      server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
      ESP.restart();
    }, []() {
      HTTPUpload& upload = server.upload();
      if (upload.status == UPLOAD_FILE_START) {
        Serial.setDebugOutput(true);
        WiFiUDP::stopAll();
        Serial.printf("Update: %s\n", upload.filename.c_str());
        uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
        if (!Update.begin(maxSketchSpace)) { //start with max available size
          Update.printError(Serial);
        }
      } else if (upload.status == UPLOAD_FILE_WRITE) {
        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);
        }
        Serial.setDebugOutput(false);
      }
      yield();
    });
    server.begin();
    MDNS.addService("http", "tcp", 80);
    Serial.printf("Aller a l'adresse http://192.168.4.1\n");
    Serverwifi_actif = 1;
  }
  server.handleClient();
  delay(1);
}

void Mode_manuel() {
  Serial.println("boucle mode manuel");
}


Thanks folks !