Implement ArduinoOTA, format arduino.cpp #1
157
arduino.cpp
157
arduino.cpp
@ -1,10 +1,10 @@
|
|||||||
|
|
||||||
/***
|
/***
|
||||||
* Fuz Piedthon
|
Fuz Piedthon
|
||||||
* Pedestrian traffic lights, green and red, controlled over MQTT
|
Pedestrian traffic lights, green and red, controlled over MQTT
|
||||||
* Board: ESP8266
|
Board: ESP8266
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
#include "WifiCredFuz.cpp" // Wifi and MQTT Credentials
|
#include "WifiCredFuz.cpp" // Wifi and MQTT Credentials
|
||||||
/* with
|
/* with
|
||||||
#define WLAN_SSID ""
|
#define WLAN_SSID ""
|
||||||
#define WLAN_PASS ""
|
#define WLAN_PASS ""
|
||||||
#define MQTT_USERNAME "" //omg no user
|
#define MQTT_USERNAME "" //omg no user
|
||||||
#define MQTT_KEY "" //omg no key
|
#define MQTT_KEY "" //omg no key
|
||||||
*/
|
*/
|
||||||
/// End of user config
|
/// End of user config
|
||||||
|
|
||||||
@ -38,20 +38,20 @@ WiFiClient client;
|
|||||||
|
|
||||||
void setupWifi() {
|
void setupWifi() {
|
||||||
|
|
||||||
// Connect to WiFi access point.
|
// Connect to WiFi access point.
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(WLAN_SSID);
|
Serial.println(WLAN_SSID);
|
||||||
|
|
||||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
Serial.print("WiFi connected");
|
Serial.print("WiFi connected");
|
||||||
Serial.print(" IP address: ");
|
Serial.print(" IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
}
|
}
|
||||||
/// MQTT
|
/// MQTT
|
||||||
@ -78,55 +78,55 @@ char *mqtt_message;
|
|||||||
|
|
||||||
// Function to connect and reconnect as necessary to the MQTT server.
|
// Function to connect and reconnect as necessary to the MQTT server.
|
||||||
void MQTT_connect() {
|
void MQTT_connect() {
|
||||||
int8_t ret;
|
int8_t ret;
|
||||||
|
|
||||||
// Stop if already connected.
|
// Stop if already connected.
|
||||||
if (mqtt.connected()) {
|
if (mqtt.connected()) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("Connecting to MQTT... ");
|
||||||
|
|
||||||
|
uint8_t retries = 3;
|
||||||
|
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||||
|
Serial.println(mqtt.connectErrorString(ret));
|
||||||
|
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||||
|
mqtt.disconnect();
|
||||||
|
delay(5000); // wait 5 seconds
|
||||||
|
retries--;
|
||||||
|
if (retries == 0) {
|
||||||
|
// basically die and wait for WDT to reset me
|
||||||
|
Serial.println("Fail to connect to MQTT after 3 tries ... ");
|
||||||
|
return; // will recall the function
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Serial.print("Connecting to MQTT... ");
|
Serial.println("MQTT Connected!");
|
||||||
|
|
||||||
uint8_t retries = 3;
|
|
||||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
|
||||||
Serial.println(mqtt.connectErrorString(ret));
|
|
||||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
|
||||||
mqtt.disconnect();
|
|
||||||
delay(5000); // wait 5 seconds
|
|
||||||
retries--;
|
|
||||||
if (retries == 0) {
|
|
||||||
// basically die and wait for WDT to reset me
|
|
||||||
Serial.println("Fail to connect to MQTT after 3 tries ... ");
|
|
||||||
return; // will recall the function
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.println("MQTT Connected!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// PiedThon
|
/// PiedThon
|
||||||
|
|
||||||
void messageToPiedthon(char *msg) {
|
void messageToPiedthon(char *msg) {
|
||||||
|
|
||||||
// Clignotement ? deux en même temps ?
|
// Clignotement ? deux en même temps ?
|
||||||
switch (msg[0]) {
|
switch (msg[0]) {
|
||||||
// LOW active le relay
|
// LOW active le relay
|
||||||
case '0' : // Rouge et Vert
|
case '0' : // Rouge et Vert
|
||||||
digitalWrite(GPIO_ROUGE, LOW);
|
digitalWrite(GPIO_ROUGE, LOW);
|
||||||
digitalWrite(GPIO_VERT, LOW);
|
digitalWrite(GPIO_VERT, LOW);
|
||||||
break;
|
break;
|
||||||
case '1' : // ROUGE
|
case '1' : // ROUGE
|
||||||
digitalWrite(GPIO_VERT, HIGH);
|
digitalWrite(GPIO_VERT, HIGH);
|
||||||
digitalWrite(GPIO_ROUGE, LOW);
|
digitalWrite(GPIO_ROUGE, LOW);
|
||||||
break;
|
break;
|
||||||
case '2' : // VERT
|
case '2' : // VERT
|
||||||
digitalWrite(GPIO_VERT, LOW);
|
digitalWrite(GPIO_VERT, LOW);
|
||||||
digitalWrite(GPIO_ROUGE, HIGH);
|
digitalWrite(GPIO_ROUGE, HIGH);
|
||||||
break;
|
break;
|
||||||
case '3' : // Rien
|
case '3' : // Rien
|
||||||
digitalWrite(GPIO_ROUGE, HIGH);
|
digitalWrite(GPIO_ROUGE, HIGH);
|
||||||
digitalWrite(GPIO_VERT, HIGH);
|
digitalWrite(GPIO_VERT, HIGH);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
//// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||||
@ -136,31 +136,30 @@ void messageToPiedthon(char *msg) {
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(10);
|
delay(10);
|
||||||
Serial.println(F("Adafruit MQTT PiedThon remix"));
|
Serial.println(F("Adafruit MQTT PiedThon remix"));
|
||||||
|
|
||||||
pinMode(GPIO_VERT, OUTPUT);
|
pinMode(GPIO_VERT, OUTPUT);
|
||||||
pinMode(GPIO_ROUGE, OUTPUT);
|
pinMode(GPIO_ROUGE, OUTPUT);
|
||||||
|
|
||||||
setupWifi();
|
setupWifi();
|
||||||
|
|
||||||
// Setup MQTT subscription for onoff feed.
|
// Setup MQTT subscription for onoff feed.
|
||||||
mqtt.subscribe(&sub_piedthon);
|
mqtt.subscribe(&sub_piedthon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
MQTT_connect();
|
MQTT_connect();
|
||||||
|
|
||||||
Adafruit_MQTT_Subscribe *subscription;
|
Adafruit_MQTT_Subscribe *subscription;
|
||||||
while ((subscription = mqtt.readSubscription(5000))) {
|
while ((subscription = mqtt.readSubscription(5000))) {
|
||||||
if (subscription == &sub_piedthon) {
|
if (subscription == &sub_piedthon) {
|
||||||
mqtt_message = (char *) sub_piedthon.lastread;
|
mqtt_message = (char *) sub_piedthon.lastread;
|
||||||
Serial.println("Got: "+String(mqtt_message));
|
Serial.println("Got: " + String(mqtt_message));
|
||||||
messageToPiedthon(mqtt_message);
|
messageToPiedthon(mqtt_message);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user