5
\$\begingroup\$

I bought one of the cheap ESP8266-01 module and I have succesfully load sketch using Arduino IDE. Module is great (in 5 minutes I have a working simple web server) but I found a little bit tricky switching between programming mode/running mode. In details:

To upload firmware

  1. power off module
  2. connect GPIO_0 to GND
  3. turn power on and upload from Arduino IDE

To run sketch after upload

  1. power off module again
  2. remove bridge GPIO_0-GND
  3. power on again

When you develop and made frequent firmware uploads, this process become very tedious. So I want to ask: is there any way to automate the process with custom circuit?

Like Arduino board which when upload start it perfom it automatically then reset the board and run sketch.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

ESP8266 boards that have at least 1MB of flash (for the ESP-01, usually indicated by a black PCB) support Over-The-Air updates, which happen to be natively supported by the Arduino IDE! The example in Arduino OTA > BasicOTA has the following basic code:

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

const char* ssid = "..........";
const char* password = "..........";

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
  // ArduinoOTA.setHostname("myesp8266");

  // No authentication by default
  // ArduinoOTA.setPassword((const char *)"123");

  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();
}

By changing the commented code // ArduinoOTA.setPassword((const char *)"123");, you can set a password. If you merge this code with your existing code, instead of selecting the ESP8266's serial port when programming, your ESP8266 will show up in the "Ports" menu. You will be prompted for a password if you set one. Then, just click "Upload" like normal. After the upload is complete, the ESP8266 will automatically restart and execute your new code. You don't connect GPIO 0 to a special mode when programming, just leave it in its normal position.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.