commit
fd787a05b2
@ -0,0 +1 @@ |
|||||||
|
config.h |
@ -0,0 +1,215 @@ |
|||||||
|
#include <ESP8266WiFi.h> |
||||||
|
#include <PubSubClient.h> |
||||||
|
#include <ArduinoOTA.h> |
||||||
|
#include "config.h" |
||||||
|
|
||||||
|
#define WIFI_TIMEOUT 30000 // ms
|
||||||
|
|
||||||
|
#define LED_PIN D1 |
||||||
|
#define INPUT1 D2 |
||||||
|
#define INPUT2 D5 |
||||||
|
#define INPUT3 D6 |
||||||
|
#define INPUT4 D7 |
||||||
|
|
||||||
|
int input1_value = 0; |
||||||
|
int input2_value = 0; |
||||||
|
int input3_value = 0; |
||||||
|
int input4_value = 0; |
||||||
|
|
||||||
|
int led_timeout = 1000; |
||||||
|
|
||||||
|
long led_time = 0; |
||||||
|
|
||||||
|
long last_wifi_check_time = 0; |
||||||
|
|
||||||
|
long debouncing_time = 1000; |
||||||
|
volatile unsigned long last_micros; |
||||||
|
|
||||||
|
WiFiClient espClient; |
||||||
|
PubSubClient client(espClient); |
||||||
|
|
||||||
|
|
||||||
|
void setup_ota() { |
||||||
|
// Hostname defaults to esp8266-[ChipID]
|
||||||
|
ArduinoOTA.setHostname(wifi_hostname); |
||||||
|
|
||||||
|
|
||||||
|
ArduinoOTA.setPassword(ota_password); |
||||||
|
|
||||||
|
ArduinoOTA.onStart([]() { |
||||||
|
String type; |
||||||
|
if (ArduinoOTA.getCommand() == U_FLASH) { |
||||||
|
type = "sketch"; |
||||||
|
} else { // U_SPIFFS
|
||||||
|
type = "filesystem"; |
||||||
|
} |
||||||
|
|
||||||
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.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(); |
||||||
|
} |
||||||
|
|
||||||
|
void setup_wifi() { |
||||||
|
|
||||||
|
delay(10); |
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
Serial.println(); |
||||||
|
Serial.print("Connecting to "); |
||||||
|
Serial.println(ssid); |
||||||
|
WiFi.hostname(wifi_hostname); |
||||||
|
WiFi.mode(WIFI_STA); |
||||||
|
WiFi.begin(ssid, password); |
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) { |
||||||
|
delay(100); |
||||||
|
Serial.print("."); |
||||||
|
} |
||||||
|
|
||||||
|
randomSeed(micros()); |
||||||
|
|
||||||
|
Serial.println(""); |
||||||
|
Serial.println("WiFi connected"); |
||||||
|
Serial.println("IP address: "); |
||||||
|
Serial.println(WiFi.localIP()); |
||||||
|
} |
||||||
|
|
||||||
|
void reconnect() { |
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!client.connected()) { |
||||||
|
Serial.print("Attempting MQTT connection..."); |
||||||
|
// Create a random client ID
|
||||||
|
String clientId = "ESP8266Client-"; |
||||||
|
clientId += String(random(0xffff), HEX); |
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect(clientId.c_str())) { |
||||||
|
Serial.println("connected"); |
||||||
|
// Once connected, publish an announcement...
|
||||||
|
} else { |
||||||
|
Serial.print("failed, rc= "); |
||||||
|
Serial.print(client.state()); |
||||||
|
Serial.println(" try again in 1 second"); |
||||||
|
// Wait 1 seconds before retrying
|
||||||
|
delay(1000); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void setup() { |
||||||
|
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
||||||
|
pinMode(LED_PIN, OUTPUT); |
||||||
|
pinMode(INPUT1, INPUT_PULLUP); |
||||||
|
pinMode(INPUT2, INPUT_PULLUP); |
||||||
|
pinMode(INPUT3, INPUT_PULLUP); |
||||||
|
pinMode(INPUT4, INPUT_PULLUP); |
||||||
|
|
||||||
|
Serial.begin(115200); |
||||||
|
|
||||||
|
setup_wifi(); |
||||||
|
setup_ota(); |
||||||
|
|
||||||
|
client.setServer(mqtt_server, 1883); |
||||||
|
attachInterrupt(INPUT1, debounceInterrupt1, RISING); |
||||||
|
attachInterrupt(INPUT2, debounceInterrupt2, RISING); |
||||||
|
attachInterrupt(INPUT3, debounceInterrupt3, RISING); |
||||||
|
attachInterrupt(INPUT4, debounceInterrupt4, RISING); |
||||||
|
} |
||||||
|
|
||||||
|
void debounceInterrupt1() { |
||||||
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
||||||
|
Interrupt1(); |
||||||
|
last_micros = micros(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void debounceInterrupt2() { |
||||||
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
||||||
|
Interrupt2(); |
||||||
|
last_micros = micros(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void debounceInterrupt3() { |
||||||
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
||||||
|
Interrupt3(); |
||||||
|
last_micros = micros(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void debounceInterrupt4() { |
||||||
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
||||||
|
Interrupt4(); |
||||||
|
last_micros = micros(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Interrupt1() { |
||||||
|
client.publish(outTopic, "1", true); |
||||||
|
digitalWrite(LED_PIN, 1); |
||||||
|
led_time = millis(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void Interrupt2() { |
||||||
|
client.publish(outTopic, "2", true); |
||||||
|
digitalWrite(LED_PIN, 1); |
||||||
|
led_time = millis(); |
||||||
|
} |
||||||
|
|
||||||
|
void Interrupt3() { |
||||||
|
client.publish(outTopic, "3", true); |
||||||
|
digitalWrite(LED_PIN, 1); |
||||||
|
led_time = millis(); |
||||||
|
} |
||||||
|
|
||||||
|
void Interrupt4() { |
||||||
|
client.publish(outTopic, "4", true); |
||||||
|
digitalWrite(LED_PIN, 1); |
||||||
|
led_time = millis(); |
||||||
|
} |
||||||
|
|
||||||
|
void loop() { |
||||||
|
long now = millis(); |
||||||
|
|
||||||
|
if (!client.connected()) { |
||||||
|
reconnect(); // mqtt
|
||||||
|
} |
||||||
|
|
||||||
|
if (now - last_wifi_check_time > WIFI_TIMEOUT) { |
||||||
|
Serial.print("Checking WiFi... "); |
||||||
|
if (WiFi.status() != WL_CONNECTED) { |
||||||
|
Serial.println("WiFi connection lost. Reconnecting..."); |
||||||
|
setup_wifi(); |
||||||
|
} else { |
||||||
|
Serial.println("OK"); |
||||||
|
} |
||||||
|
last_wifi_check_time = now; |
||||||
|
} |
||||||
|
|
||||||
|
if (now - led_time > led_timeout) { |
||||||
|
digitalWrite(LED_PIN, 0); |
||||||
|
} |
||||||
|
|
||||||
|
client.loop(); // mqtt client
|
||||||
|
ArduinoOTA.handle(); |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
// Update these with values suitable for your network. |
||||||
|
|
||||||
|
const char* wifi_hostname = ""; |
||||||
|
const char* ssid = ""; |
||||||
|
const char* password = ""; |
||||||
|
|
||||||
|
const char* mqtt_server = ""; |
||||||
|
const char* outTopic = ""; |
||||||
|
|
||||||
|
const char* ota_password = ""; |
Loading…
Reference in new issue