Browse Source

Merge branch 'arduinoota' of Lomanic/PiedThon into master

Add OTA and wifi setup
master
jeanjack 8 months ago
parent
commit
a9174a59b8
2 changed files with 128 additions and 90 deletions
  1. 3
    1
      README.md
  2. 125
    89
      arduino.cpp

+ 3
- 1
README.md View File

@@ -1,3 +1,5 @@
1 1
 # PiedThon
2 2
 
3
-Feux PiedThon ESP8266
3
+Feux PiedThon ESP8266
4
+
5
+https://wiki.fuz.re/doku.php?id=projets:fuz:piedthon

+ 125
- 89
arduino.cpp View File

@@ -1,12 +1,15 @@
1 1
 
2 2
 /***
3
- * Fuz Piedthon
4
- * Pedestrian traffic lights, green and red, controlled over MQTT
5
- * Board:   ESP8266
6
- *
7
- */
3
+   Fuz Piedthon
4
+   Pedestrian traffic lights, green and red, controlled over MQTT
5
+   Board:   ESP8266
6
+
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,14 +21,14 @@
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
25
-#define WLAN_SSID       ""
26
-#define WLAN_PASS       ""
27
-#define MQTT_USERNAME    "" //omg no user
28
-#define MQTT_KEY         "" //omg no key
28
+  #define WLAN_SSID       ""
29
+  #define WLAN_PASS       ""
30
+  #define MQTT_USERNAME    "" //omg no user
31
+  #define MQTT_KEY         "" //omg no key
29 32
 */
30 33
 /// End of user config
31 34
 
@@ -38,20 +41,20 @@ WiFiClient client;
38 41
 
39 42
 void setupWifi() {
40 43
 
41
-    // Connect to WiFi access point.
42
-    Serial.print("Connecting to ");
43
-    Serial.println(WLAN_SSID);
44
+  // Connect to WiFi access point.
45
+  Serial.print("Connecting to ");
46
+  Serial.println(WLAN_SSID);
44 47
 
45
-    WiFi.begin(WLAN_SSID, WLAN_PASS);
46
-    while (WiFi.status() != WL_CONNECTED) {
47
-        delay(500);
48
-        Serial.print(".");
49
-    }
50
-    Serial.println();
48
+  WiFi.begin(WLAN_SSID, WLAN_PASS);
49
+  while (WiFi.status() != WL_CONNECTED) {
50
+    delay(500);
51
+    Serial.print(".");
52
+  }
53
+  Serial.println();
51 54
 
52
-    Serial.print("WiFi connected");
53
-    Serial.print(" IP address: ");
54
-    Serial.println(WiFi.localIP());
55
+  Serial.print("WiFi connected");
56
+  Serial.print(" IP address: ");
57
+  Serial.println(WiFi.localIP());
55 58
 
56 59
 }
57 60
 /// MQTT
@@ -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");
@@ -78,55 +81,55 @@ char *mqtt_message;
78 81
 
79 82
 // Function to connect and reconnect as necessary to the MQTT server.
80 83
 void MQTT_connect() {
81
-    int8_t ret;
82
-
83
-    // Stop if already connected.
84
-    if (mqtt.connected()) {
85
-        return;
86
-    }
87
-
88
-    Serial.print("Connecting to MQTT... ");
89
-
90
-    uint8_t retries = 3;
91
-    while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
92
-        Serial.println(mqtt.connectErrorString(ret));
93
-        Serial.println("Retrying MQTT connection in 5 seconds...");
94
-        mqtt.disconnect();
95
-        delay(5000);  // wait 5 seconds
96
-        retries--;
97
-        if (retries == 0) {
98
-            // basically die and wait for WDT to reset me
99
-            Serial.println("Fail to connect to MQTT after 3 tries ... ");
100
-            return; // will recall the function
101
-        }
84
+  int8_t ret;
85
+
86
+  // Stop if already connected.
87
+  if (mqtt.connected()) {
88
+    return;
89
+  }
90
+
91
+  Serial.print("Connecting to MQTT... ");
92
+
93
+  uint8_t retries = 3;
94
+  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
95
+    Serial.println(mqtt.connectErrorString(ret));
96
+    Serial.println("Retrying MQTT connection in 5 seconds...");
97
+    mqtt.disconnect();
98
+    delay(5000);  // wait 5 seconds
99
+    retries--;
100
+    if (retries == 0) {
101
+      // basically die and wait for WDT to reset me
102
+      Serial.println("Fail to connect to MQTT after 3 tries ... ");
103
+      return; // will recall the function
102 104
     }
103
-    Serial.println("MQTT Connected!");
105
+  }
106
+  Serial.println("MQTT Connected!");
104 107
 }
105 108
 
106 109
 /// PiedThon
107 110
 
108 111
 void messageToPiedthon(char *msg) {
109 112
 
110
-    // Clignotement ? deux en même temps ?
111
-    switch (msg[0]) {
112
-        // LOW active le relay
113
-        case '0' : // Rouge et Vert
114
-            digitalWrite(GPIO_ROUGE, LOW);
115
-            digitalWrite(GPIO_VERT, LOW);
116
-            break;
117
-        case '1' : // ROUGE
118
-            digitalWrite(GPIO_VERT, HIGH);
119
-            digitalWrite(GPIO_ROUGE, LOW);
120
-            break;
121
-        case '2' : // VERT
122
-            digitalWrite(GPIO_VERT, LOW);
123
-            digitalWrite(GPIO_ROUGE, HIGH);
124
-            break;
125
-        case '3'​ : // Rien
126
-            digitalWrite(GPIO_ROUGE, HIGH);
127
-            digitalWrite(GPIO_VERT, HIGH);
128
-            break;
129
-    }
113
+  // Clignotement ? deux en même temps ?
114
+  switch (msg[0]) {
115
+    // LOW active le relay
116
+    case '0' : // Rouge et Vert
117
+      digitalWrite(GPIO_ROUGE, LOW);
118
+      digitalWrite(GPIO_VERT, LOW);
119
+      break;
120
+    case '1' : // ROUGE
121
+      digitalWrite(GPIO_VERT, HIGH);
122
+      digitalWrite(GPIO_ROUGE, LOW);
123
+      break;
124
+    case '2' : // VERT
125
+      digitalWrite(GPIO_VERT, LOW);
126
+      digitalWrite(GPIO_ROUGE, HIGH);
127
+      break;
128
+    case '3'  : // Rien
129
+      digitalWrite(GPIO_ROUGE, HIGH);
130
+      digitalWrite(GPIO_VERT, HIGH);
131
+      break;
132
+  }
130 133
 }
131 134
 
132 135
 //// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
@@ -135,32 +138,65 @@ void messageToPiedthon(char *msg) {
135 138
 
136 139
 
137 140
 void setup() {
138
-
139
-    Serial.begin(115200);
140
-    delay(10);
141
-    Serial.println(F("Adafruit MQTT PiedThon remix"));
142
-
143
-    pinMode(GPIO_VERT, OUTPUT);
144
-    pinMode(GPIO_ROUGE, OUTPUT);
145
-
146
-    setupWifi();
147
-
148
-    // Setup MQTT subscription for onoff feed.
149
-    mqtt.subscribe(&sub_piedthon);
141
+  Serial.begin(115200);
142
+  delay(10);
143
+  Serial.println(F("Adafruit MQTT PiedThon remix"));
144
+
145
+  pinMode(GPIO_VERT, OUTPUT);
146
+  pinMode(GPIO_ROUGE, OUTPUT);
147
+
148
+  randomSeed(analogRead(0));
149
+
150
+  setupWifi();
151
+
152
+  // Setup MQTT subscription for onoff feed.
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
-    MQTT_connect();
156
-
157
-    Adafruit_MQTT_Subscribe *subscription;
158
-    while ((subscription = mqtt.readSubscription(5000))) {
159
-        if (subscription == &sub_piedthon) {
160
-            mqtt_message = (char *) sub_piedthon.lastread;
161
-            Serial.println("Got: "+String(mqtt_message));
162
-            messageToPiedthon(mqtt_message);
163
-        }
191
+  MQTT_connect();
192
+  ArduinoOTA.handle();
193
+
194
+  Adafruit_MQTT_Subscribe *subscription;
195
+  while ((subscription = mqtt.readSubscription(5000))) {
196
+    if (subscription == &sub_piedthon) {
197
+      mqtt_message = (char *) sub_piedthon.lastread;
198
+      Serial.println("Got: " + String(mqtt_message));
199
+      messageToPiedthon(mqtt_message);
164 200
     }
201
+  }
165 202
 }
166
-

Loading…
Cancel
Save