You only need the include for ArduinoOTA.h.
ESP8266WiFi.h and WiFiUdp.h are included by ArduinoOTA.h
Here is how I use OTA.
In setup_wifi() I get the MAC address to use later to make a unique OTA hostname.
Here's a starter sketch that you can use. To keep the main program clean, I like to put my setup functions in a separate tab.
Also, my WiFi credentials are in a separate file. This way all of my sketches just need to include this file:
#ifndef Kaywinnet //include guards.
#define Kaywinnet
const char* my_ssid = "yourSSID";
const char* my_password = "yourPassword";
const char* mqtt_server = "192.168.1.124";
#endif
Here is my main tab:
(You could put it all into a single tab, but I like the cleanliness of using tabs)
#include <ArduinoOTA.h> //for OTA updates
//WiFi credentials
#include "D:\River Documents\Arduino\libraries\Kaywinnet.h"
#define hPrefix "Test-"
// setup_wifi globals
char macBuffer[24]; // Holds the last three digits of the MAC, in hex.
char hostNamePrefix[] = hPrefix;
char hostName[12]; // Holds hostNamePrefix + the last three bytes of the MAC address.
void setup()
{
Serial.begin(115200);
Serial.println();
setup_wifi();
start_OTA();
}
void loop (){
ArduinoOTA.handle();
// Do your loop stuff here
}
Here is my setup_wifi tab:
// setup_wifi
// ============================= Connect the ESP to the router =============================
// Connect to WiFi network so we can reach the MQTT broker and publish messages to topics.
/*
Make sure you include at the start of the sketch:
#include "ESP8266WiFi.h" // Not needed if also using the Arduino OTA Library...
#include "D:\River Documents\Arduino\libraries\Kaywinnet.h" \\ WiFi credentials
If using the OTA Library, put these at the start of the sketch.
char macBuffer[24]; // Holds the last three digits of the MAC, in hex.
char hostNamePrefix[] = "Drip-";
char hostName[12]; // Holds hostNamePrefix + the last three bytes of the MAC address.
*/
void setup_wifi() {
byte mac[6]; //// the MAC address of your Wifi shield
Serial.println(F("\n"));
Serial.print(F("Connecting to "));
Serial.println(my_ssid);
WiFi.mode(WIFI_STA);
WiFi.enableInsecureWEP();
WiFi.begin(my_ssid, my_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(WiFi.status()); Serial.print(F(" "));
}
Serial.println(F("\nWiFi connected, "));
Serial.print(F("MAC Address: "));
Serial.println(WiFi.macAddress());
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
#ifdef __ARDUINO_OTA_H
// Get the last three numbers of the mac address.
// "4C:11:AE:0D:83:86" becomes "0D8386" in macBuffer.
WiFi.macAddress(mac);
snprintf(macBuffer, sizeof(macBuffer), "%02X%02X%02X", mac[3], mac[4], mac[5]);
// Build hostNamePrefix + last three bytes of the MAC address.
strcpy(hostName, hostNamePrefix);
strcat(hostName, macBuffer);
Serial.print(F("hostName = \""));
Serial.print(hostName);
Serial.println(F("\""));
#endif
}
And here is my start_OTA tab:
void start_OTA() {
/*
Make sure this in at the top of the sketch:
#include <ArduinoOTA.h>
Start loop() with:
ArduinoOTA.handle();
*/
//Hostname defaults to esp8266-[MAC address]
ArduinoOTA.setHostname(hostName); // hostName is generated in 'setup_wifi'.
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
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");
}
Hope this helps.