Minimal PlatformIO ESP8266 ArduinoOTA example
This simple firmware will connect to Wifi and enable over-the-air update (OTA) using ArduinoOTA on any ESP8266 module. Use it as a starting point for your own firmware to enable OTA.
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
/**
* Connet to Wifi
*/
WiFi.begin("MyWifiSSID", "MyWifiPassword");
uint32_t notConnectedCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Wifi connecting...");
notConnectedCounter++;
if(notConnectedCounter > 150) { // Reset board if not connected after 5s
Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();
}
}
Serial.print("Wifi connected, IP address: ");
Serial.println(WiFi.localIP());
/**
* Enable OTA update
*/
ArduinoOTA.begin();
}
void loop() {
// Check for over the air update request and (if present) flash it
ArduinoOTA.handle();
}
[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_speed = 115200
[env:d1_mini_ota]
extends = env:d1_mini
upload_protocol = espota
upload_port = 192.168.178.166
For more details on how the two targets (d1_mini
and d1_mini_ota
) work, see How to handle both OTA and serial upload in platformio.ini. Note that you need to enter the ESP8266’s IP address or mDNS name in order for OTA to work.
The firmware will print the Wifi IP address on the serial port, so you can use PlatformIO’s Monitor feature to view it.