3

我正在使用 ESP8266-12 wifi 模块访问我的家庭 wifi 网络来控制灯光。为了上传新固件(OTA:Over the Air)更新,我想使用 ESP8266 的热点 AccessPoint,因为在更改了我的 wifi 网络的密码后,我将无法这样做。这是我的 Arduino 代码:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>

//ssid and password of your wifi network
const char* ssid = "wifi_ssid";
const char* password = "wifi_password";

//ssid and password to connect to local hotspot of ESP8266
const char *esp_ssid = "your_desired_ssid";
const char *esp_password = "your_desired_password";

IPAddress ip(192, 168, 1, xx); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 254); // set gateway to match your wifi network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your wifi network

ESP8266WebServer server(80);
int status = LOW;
const int pin = 5;

const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";

//Authorization username and password before updating firmware
const char* www_username = "admin";
const char* www_password = "esp8266";

void connectToWifi() {
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while(WiFi.waitForConnectResult() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    //ESP.restart();
  }
  Serial.print("WiFi connected to ");
  Serial.println(WiFi.localIP());
}

void createAccessPoint() {
  Serial.println("Configuring access point for wifi network *your_desired_ssid*...");
  WiFi.softAP(esp_ssid, esp_password);
  IPAddress accessIP = WiFi.softAPIP();
  Serial.print("ESP AccessPoint IP address: ");
  Serial.println(accessIP);
  /* Go to http://192.168.4.1 in a web browser
   * connected to this access point to see it.
   */
}

void serverResponse(){
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Origin", "*");
  server.send(200, "text/plain", String(status));
}


void setup() {
  Serial.begin(115200);
  pinMode(pin, OUTPUT);
  connectToWifi();
  ArduinoOTA.begin();
  server.on("/fupdate", [](){
    if(server.authenticate(www_username, www_password)){
      createAccessPoint();
      server.sendHeader("Connection", "close");
      server.sendHeader("Access-Control-Allow-Origin", "*");
      server.send(200, "text/html", serverIndex);
    }
    else{
       return server.requestAuthentication();
    }

  });
  server.on("/status", [](){
    serverResponse();
  });
  server.on("/on", [](){
    status = HIGH;
    serverResponse();
  });
  server.on("/off", [](){
    status = LOW;
    serverResponse();
  });
  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.print("Open http://");
  Serial.print(WiFi.localIP());
  Serial.println("/ in your browser to see it working");
}

void loop() {
  digitalWrite(pin, status);
  ArduinoOTA.handle();
  server.handleClient();
  delay(1);
}

我有一个简单的 WifiWebServer 来处理请求。当我发送/fupdate请求时,ESP 模块应该创建热点,我可以通过接入点连接我的计算机以更新固件。为了更新固件,我设置了授权。如果授权将完成,则只有 T 可以上传新固件。但是我的 ESP8266 模块在发送 /fupdate**请求或授权之前创建了热点。当我通电时,它正在启用热点。有人可以解决这个问题吗?

4

2 回答 2

7

将这些行放在名为connectToWifi()的函数的顶部:

  WiFi.softAPdisconnect();
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  delay(100);

ESP 的 WiFi 模块将自己的配置存储在芯片上,他希望能清楚地覆盖它。不要让他尝试一些常见的东西,明确定义配置并停止不相关的先前操作。

于 2016-09-26T08:02:38.873 回答
1

看起来您从未关闭接入点。您可能在此处省略了该部分,但请确保在更新固件后正确关闭 AP。如果您像这样运行代码,第一次可能会运行良好,但是一旦您运行“/fupdate”命令,它将永远维护 AP,最终会在您第二次运行“/fupdate”时崩溃。

所以你也应该在调用“createAccessPoint”之前检查状态,因为如果它已经创建,软件可能会失败。

于 2016-12-28T06:18:01.033 回答