Rename some functions

This commit is contained in:
cazagen 2019-02-22 20:38:43 +00:00
parent dc5a0e0307
commit 10f4283469
Signed by: cazagen
GPG Key ID: C326FEEE25FF7915

View File

@ -1,4 +1,5 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <Schedule.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "config.h" #include "config.h"
@ -28,6 +29,12 @@ volatile unsigned long last_micros;
long lastTimesignalTime = 0; long lastTimesignalTime = 0;
bool button1_pressed = 0;
bool button2_pressed = 0;
bool button3_pressed = 0;
bool button4_pressed = 0;
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
@ -36,7 +43,6 @@ void setup_ota() {
// Hostname defaults to esp8266-[ChipID] // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname(wifi_hostname); ArduinoOTA.setHostname(wifi_hostname);
ArduinoOTA.setPassword(ota_password); ArduinoOTA.setPassword(ota_password);
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
@ -74,12 +80,11 @@ void setup_ota() {
} }
void setup_wifi() { void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
Serial.println(); Serial.println();
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.println(ssid); Serial.println(ssid);
WiFi.hostname(wifi_hostname); WiFi.hostname(wifi_hostname);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -107,7 +112,7 @@ boolean reconnect() {
client.publish(willTopic, "1", true); client.publish(willTopic, "1", true);
client.subscribe(timeTopic); client.subscribe(timeTopic);
} else { } else {
Serial.print("failed, rc = "); Serial.print("MQTT failed, rc=");
Serial.print(client.state()); Serial.print(client.state());
Serial.println("Trying again"); Serial.println("Trying again");
} }
@ -121,7 +126,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
} }
void setup() { void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output pinMode(BUILTIN_LED, OUTPUT);
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
pinMode(INPUT1, INPUT_PULLUP); pinMode(INPUT1, INPUT_PULLUP);
pinMode(INPUT2, INPUT_PULLUP); pinMode(INPUT2, INPUT_PULLUP);
@ -143,56 +148,59 @@ void setup() {
} }
void debounceInterrupt1() { void debounceInterrupt1() {
Serial.println("debounce 1");
if((long)(micros() - last_micros) >= debouncing_time * 1000) { if((long)(micros() - last_micros) >= debouncing_time * 1000) {
Interrupt1(); schedule_function(Button1);
last_micros = micros(); last_micros = micros();
} }
} }
void debounceInterrupt2() { void debounceInterrupt2() {
Serial.println("debounce 2");
if((long)(micros() - last_micros) >= debouncing_time * 1000) { if((long)(micros() - last_micros) >= debouncing_time * 1000) {
Interrupt2(); schedule_function(Button2);
last_micros = micros(); last_micros = micros();
} }
} }
void debounceInterrupt3() { void debounceInterrupt3() {
Serial.println("debounce 3");
if((long)(micros() - last_micros) >= debouncing_time * 1000) { if((long)(micros() - last_micros) >= debouncing_time * 1000) {
Interrupt3(); schedule_function(Button3);
last_micros = micros(); last_micros = micros();
} }
} }
void debounceInterrupt4() { void debounceInterrupt4() {
Serial.println("debounce 4");
if((long)(micros() - last_micros) >= debouncing_time * 1000) { if((long)(micros() - last_micros) >= debouncing_time * 1000) {
Interrupt4(); schedule_function(Button4);
last_micros = micros(); last_micros = micros();
} }
} }
void Interrupt1() { void Button1() {
Serial.println("Button 1 pressed"); Serial.println("Button 1 pressed");
client.publish(outTopic, "1", true); client.publish(outTopic, "1", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void Interrupt2() { void Button2() {
Serial.println("Button 2 pressed"); Serial.println("Button 2 pressed");
client.publish(outTopic, "2", true); client.publish(outTopic, "2", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void Interrupt3() { void Button3() {
Serial.println("Button 3 pressed"); Serial.println("Button 3 pressed");
client.publish(outTopic, "3", true); client.publish(outTopic, "3", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void Interrupt4() { void Button4() {
Serial.println("Button 4 pressed"); Serial.println("Button 4 pressed");
client.publish(outTopic, "4", true); client.publish(outTopic, "4", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
@ -201,6 +209,7 @@ void Interrupt4() {
void loop() { void loop() {
if (millis() - lastTimesignalTime > 180000) { if (millis() - lastTimesignalTime > 180000) {
Serial.println("Timesignal is missing, restarting");
ESP.restart(); ESP.restart();
} }
@ -217,13 +226,12 @@ void loop() {
if (!client.connected()) { if (!client.connected()) {
if (millis() - lastReconnectAttempt > 5000) { if (millis() - lastReconnectAttempt > 5000) {
Serial.println("checking MQTT connection");
lastReconnectAttempt = millis(); lastReconnectAttempt = millis();
// Attempt to reconnect // Attempt to reconnect
Serial.println("Attempting MQTT reconnect"); Serial.println("Attempting MQTT reconnect");
if (reconnect()) { if (reconnect()) {
reconnectAttempts = 0; reconnectAttempts = 0;
lastReconnectAttempt = 0; lastReconnectAttempt = millis();
} }
if (reconnectAttempts > 5) { if (reconnectAttempts > 5) {
Serial.println("mqtt failed too many times, restarting wifi"); Serial.println("mqtt failed too many times, restarting wifi");