Last active
May 18, 2020 15:16
-
-
Save dennisheitmann/c3356a567bc411a9d86d701dd2d23170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <DNSServer.h> | |
#include <ESP8266WebServer.h> | |
#include <WiFiManager.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
//#define LED_BUILTIN 2 | |
//unsigned long previousMillis = 0; | |
//const long interval = 1000; | |
//int ledState = LOW; | |
#define HOSTNAME "my_esp8266" | |
//#define MQTT_Enabled | |
#ifdef MQTT_Enabled | |
#include <PubSubClient.h> | |
const char mqtt_server[] = "mqtt.example.com"; | |
const int mqtt_port = 1883; | |
const char mqtt_name[] = "home_sensor-xxxx"; | |
const char mqtt_user[] = "home_sensor-xxxx"; | |
const char mqtt_pass[] = "xxxxxxxxxxxxxxxx"; | |
const char mqtt_topic_1[] = "HOME/Sensor/xxxx"; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
WiFiClient espClient; | |
PubSubClient client(mqtt_server, mqtt_port, callback, espClient); | |
boolean reconnect() { | |
// Loop until we're reconnected | |
int trycount = 0; | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect(mqtt_name, mqtt_user, mqtt_pass)) { | |
Serial.println("connected"); | |
trycount = 0; | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
unsigned long lastMillis = millis(); | |
while (lastMillis + 3000 > millis()) { | |
yield(); | |
} | |
trycount++; | |
if (trycount > 600) { | |
ESP.reset(); | |
} | |
} | |
} | |
return client.connected(); | |
} | |
#endif | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Booting..."); | |
Serial.println("\r\n"); | |
Serial.print("Chip ID: 0x"); | |
Serial.println(ESP.getChipId(), HEX); | |
delay(100); | |
/* WiFi start-up */ | |
Serial.println("Starting WiFi..."); | |
WiFiManager wifiManager; | |
wifiManager.setTimeout(900); | |
if (!(wifiManager.autoConnect())) { | |
delay(100); | |
ESP.restart(); | |
delay(100); | |
} | |
delay(100); | |
// Port defaults to 8266 | |
ArduinoOTA.setPort(8266); | |
// Hostname defaults to esp8266-[ChipID] | |
ArduinoOTA.setHostname(HOSTNAME); | |
// No authentication by default | |
// ArduinoOTA.setPassword("admin"); | |
ArduinoOTA.onStart([]() { | |
String type; | |
if (ArduinoOTA.getCommand() == U_FLASH) { | |
type = "sketch"; | |
} else { // U_FS | |
type = "filesystem"; | |
} | |
// NOTE: if updating FS this would be the place to unmount FS using FS.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(); | |
#ifdef MQTT_Enabled | |
// MQTT | |
client.loop(); | |
#endif | |
// Add additional setup code below this comment | |
//pinMode(LED_BUILTIN, OUTPUT); | |
Serial.println("Setup Done"); | |
} | |
void loop() { | |
#ifdef MQTT_Enabled | |
if (! client.connected()) { | |
reconnect(); | |
yield(); | |
} | |
client.loop(); | |
#endif | |
ArduinoOTA.handle(); | |
// Add mormal loop below this comment | |
// if (millis() - previousMillis >= interval) { | |
// previousMillis = millis(); | |
// if (ledState == LOW) { | |
// ledState = HIGH; | |
// } else { | |
// ledState = LOW; | |
// } | |
// digitalWrite(LED_BUILTIN, ledState); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switch off your firewall (or configure it properly) for flashing, as the "espota.py" uses a random port for the esp to download the .bin file from your computer.