Browse Source

Implement OTA over local WiFi

arduinoota
Lomanic 8 months ago
parent
commit
87dc889a7b
1 changed files with 43 additions and 6 deletions
  1. 43
    6
      arduino.cpp

+ 43
- 6
arduino.cpp View File

@@ -7,6 +7,9 @@
7 7
 */
8 8
 
9 9
 #include <Arduino.h>
10
+#include <ESP8266mDNS.h>
11
+#include <WiFiUdp.h>
12
+#include <ArduinoOTA.h>
10 13
 
11 14
 /// Pins configuration
12 15
 
@@ -18,7 +21,7 @@
18 21
 #define MQTT_SERVER      "sonic.fuz.re"
19 22
 #define MQTT_SERVERPORT  1883
20 23
 
21
-
24
+#define ESP_HOSTNAME  "piedthon"
22 25
 
23 26
 #include "WifiCredFuz.cpp"              // Wifi and MQTT Credentials
24 27
 /* with
@@ -67,8 +70,8 @@ Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME,
67 70
 
68 71
 /****************************** Feeds ***************************************/
69 72
 
70
-// Setup a feed called 'pub_photocell' for publishing.
71
-Adafruit_MQTT_Publish pub_photocell = Adafruit_MQTT_Publish(&mqtt, "piedthon/debug");
73
+// Setup a feed called 'pub_piedthon' for publishing.
74
+Adafruit_MQTT_Publish pub_piedthon = Adafruit_MQTT_Publish(&mqtt, "piedthon/debug");
72 75
 
73 76
 // Setup a feed called 'sub_piedthon' for subscribing to changes.
74 77
 Adafruit_MQTT_Subscribe sub_piedthon = Adafruit_MQTT_Subscribe(&mqtt, "piedthon/input");
@@ -122,7 +125,7 @@ void messageToPiedthon(char *msg) {
122 125
       digitalWrite(GPIO_VERT, LOW);
123 126
       digitalWrite(GPIO_ROUGE, HIGH);
124 127
       break;
125
-    case '3' : // Rien
128
+    case '3'  : // Rien
126 129
       digitalWrite(GPIO_ROUGE, HIGH);
127 130
       digitalWrite(GPIO_VERT, HIGH);
128 131
       break;
@@ -135,7 +138,6 @@ void messageToPiedthon(char *msg) {
135 138
 
136 139
 
137 140
 void setup() {
138
-
139 141
   Serial.begin(115200);
140 142
   delay(10);
141 143
   Serial.println(F("Adafruit MQTT PiedThon remix"));
@@ -143,16 +145,51 @@ void setup() {
143 145
   pinMode(GPIO_VERT, OUTPUT);
144 146
   pinMode(GPIO_ROUGE, OUTPUT);
145 147
 
148
+  randomSeed(analogRead(0));
149
+
146 150
   setupWifi();
147 151
 
148 152
   // Setup MQTT subscription for onoff feed.
149 153
   mqtt.subscribe(&sub_piedthon);
154
+
155
+
156
+  ArduinoOTA.setHostname(ESP_HOSTNAME);
157
+  ArduinoOTA.onStart([]() { // switch off all the GPIOs during upgrade
158
+    digitalWrite(GPIO_ROUGE, HIGH);
159
+    digitalWrite(GPIO_VERT, HIGH);
160
+  });
161
+  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
162
+    /*if (progress % (total / 4) == 0) {
163
+      digitalWrite(GPIO_ROUGE, random(2) ? LOW : HIGH);
164
+      digitalWrite(GPIO_VERT, random(2) ? LOW : HIGH);
165
+      }*/
166
+
167
+  });
168
+
169
+  ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end
170
+    for (int i = 0; i < 10; i++) {
171
+      int RANDOM_GPIO = GPIO_ROUGE;
172
+      if (random(2)) { // random(2) returns 0 or 1 randomly
173
+        RANDOM_GPIO = GPIO_VERT;
174
+      }
175
+      digitalWrite(RANDOM_GPIO, !digitalRead(RANDOM_GPIO));
176
+      delay(400);
177
+    }
178
+    digitalWrite(GPIO_ROUGE, LOW);
179
+    digitalWrite(GPIO_VERT, LOW);
180
+  });
181
+
182
+  ArduinoOTA.onError([](ota_error_t error) {
183
+    (void)error;
184
+    ESP.restart();
185
+  });
186
+  ArduinoOTA.begin();
150 187
 }
151 188
 
152 189
 
153 190
 void loop() {
154
-
155 191
   MQTT_connect();
192
+  ArduinoOTA.handle();
156 193
 
157 194
   Adafruit_MQTT_Subscribe *subscription;
158 195
   while ((subscription = mqtt.readSubscription(5000))) {

Loading…
Cancel
Save