summaryrefslogtreecommitdiff
path: root/ota.h
diff options
context:
space:
mode:
Diffstat (limited to 'ota.h')
-rw-r--r--ota.h98
1 files changed, 98 insertions, 0 deletions
diff --git a/ota.h b/ota.h
new file mode 100644
index 0000000..28fb37b
--- /dev/null
+++ b/ota.h
@@ -0,0 +1,98 @@
1// Include what is needed to implement Over The Air Update
2// The WiFi connection is not done on this include file, so must be done
3// in the main program file
4//
5// This include file provides the functions
6// setupWifi(hostname)
7// hostname can be an empty string; this function will
8// establish the wifi connections, WiFi credentials are supplied
9// in the file "wifiinfo.h", see "wifiinfo.h.sample"
10// setupOTA(hostname)
11// hostname can be an empty string; will be used to retrieve the
12// correct board to upgrade Over The Air.
13// If an empty string the default name is esp8266-[ChipID]
14// The above 2 functions must be called in the setup() function
15//
16// in the loop function the function
17// ArduinoOTA.handle()
18// must be called
19
20#ifndef ota_h
21#define ota_h
22// needed include files
23#include <ESP8266WiFi.h>
24#include <ESP8266mDNS.h>
25#include <WiFiUdp.h>
26#include <ArduinoOTA.h>
27
28// include our Network SSID and PASSWORD
29#include "wifiinfo.h"
30
31// ==================================================================
32// Establish WiFi connection
33// ==================================================================
34void setupWifi(char hostname[]) {
35 int i = 0;
36 WiFi.mode(WIFI_STA);
37 if (sizeof(hostname) > 0) {
38 WiFi.hostname(hostname);
39 }
40 if (Serial) {Serial.println("Connecting ");}
41 WiFi.begin(ssid, password);
42 while (WiFi.status() != WL_CONNECTED) {
43 if (Serial) {Serial.print(".");}
44 delay(500);
45 }
46 if (Serial) {
47 Serial.println(".");
48 Serial.print("Connected! IP: ");
49 Serial.println(WiFi.localIP());
50 }
51}
52
53
54// ==================================================================
55// Setup for OTA update
56// ==================================================================
57
58void setupOTA(char hostname[]) {
59 // Port defaults to 8266
60 // ArduinoOTA.setPort(8266);
61
62 // Hostname defaults to esp8266-[ChipID]
63 if (sizeof(hostname) > 0) {
64 ArduinoOTA.setHostname(hostname);
65 }
66
67 // No authentication by default
68 // ArduinoOTA.setPassword((const char *)"123");
69
70 ArduinoOTA.onStart([]() {
71 if (Serial) {Serial.println("Start");}
72 });
73 ArduinoOTA.onEnd([]() {
74 if (Serial) {Serial.println("\nEnd");}
75 });
76 ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
77 if (Serial) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));}
78 });
79 ArduinoOTA.onError([](ota_error_t error) {
80 if (Serial) {
81 Serial.printf("Error[%u]: ", error);
82 if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
83 else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
84 else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
85 else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
86 else if (error == OTA_END_ERROR) Serial.println("End Failed");
87 }
88 });
89 ArduinoOTA.begin();
90 if (Serial) {
91 Serial.println("Ready");
92 Serial.print("IP address: ");
93 Serial.println(WiFi.localIP());
94 }
95}
96
97
98#endif