Created
April 22, 2019 23:10
-
-
Save jfierstein/d47b718ab638adeeeefa576458e5ecd2 to your computer and use it in GitHub Desktop.
Arduino sketch for controlling vents wired to ESP8266
This file contains 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 <PubSubClient.h> | |
#include <ESP8266mDNS.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
#define MOTOR_IN1 D1 | |
#define MOTOR_IN2 D2 | |
#define wifi_ssid "WIFISSID" //type your WIFI information inside the quotes | |
#define wifi_password "WIFIPW" | |
#define host_name "espvent1" | |
#define mqtt_server "192.168.1.1" | |
#define mqtt_user "MQTTUSER" | |
#define mqtt_password "MQTTPASS" | |
#define mqtt_port 1883 | |
#define mqtt_vent_status_topic "espvent1/status" | |
#define mqtt_client "espvent1" | |
#define OTApassword "OTAPASS" // change this to whatever password you want to use when you upload OTA | |
int OTAport = 8266; | |
const char* open_cmd = "OPEN"; | |
const char* close_cmd = "CLOSE"; | |
bool ventOpen = false; //false = closed, true = open; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void setup_wifi() { | |
int WIFI_ATTEMPTS = 10; | |
delay(10); | |
Serial.println(""); | |
Serial.print("Connecting to SSID: "); | |
Serial.print(wifi_ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(wifi_ssid, wifi_password); | |
int attempt = 1; | |
while (WiFi.status() != WL_CONNECTED && attempt <= WIFI_ATTEMPTS) { | |
attempt++; | |
delay(1000); | |
Serial.print("."); | |
} | |
if(WiFi.status() == WL_CONNECTED) { | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
else { | |
Serial.println("Error: Failed to connect to WiFi network! Trying again in 60 seconds."); | |
ESP.deepSleep(60 * 1000000); | |
} | |
} | |
void openVent() { | |
//if vent is not open | |
if(!ventOpen) { | |
Serial.println("Opening Vent"); | |
digitalWrite(MOTOR_IN2, LOW); | |
digitalWrite(MOTOR_IN1, HIGH); | |
delay(1000); | |
digitalWrite(MOTOR_IN1, LOW); | |
ventOpen = true; | |
} | |
else { | |
Serial.println("Vent is already open."); | |
} | |
} | |
void closeVent() { | |
if(ventOpen) { | |
Serial.println("Closing Vent"); | |
digitalWrite(MOTOR_IN1, LOW); | |
digitalWrite(MOTOR_IN2, HIGH); | |
delay(1000); | |
digitalWrite(MOTOR_IN2, LOW); | |
ventOpen = false; | |
} | |
else { | |
Serial.println("Vent is already closed."); | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
char message[length + 1]; | |
for (int i = 0; i < length; i++) { | |
message[i] = (char)payload[i]; | |
} | |
message[length] = '\0'; | |
Serial.println(message); | |
if(strcmp(message, open_cmd) == 0) { | |
openVent(); | |
} | |
else if(strcmp(message, close_cmd) == 0) { | |
closeVent(); | |
} | |
} | |
void setup_mqtt() { | |
int MAX_MQTT_CONN_ATTEMPTS = 3; | |
int attempt = 1; | |
//try for max attempts | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
while (!client.connected() && attempt <= MAX_MQTT_CONN_ATTEMPTS) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect(mqtt_client, mqtt_user, mqtt_password)) { | |
Serial.println("MQTT connection successful!"); | |
client.subscribe(mqtt_vent_status_topic); | |
} else { | |
attempt++; | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
if(!client.connected()) { | |
Serial.println("Error: Failed to connect to MQTT broker! Trying again in 60 seconds."); | |
ESP.deepSleep(60 * 1000000); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(MOTOR_IN1, OUTPUT); | |
pinMode(MOTOR_IN2, OUTPUT); | |
Serial.begin(115200); | |
delay(10); | |
ArduinoOTA.setPort(OTAport); | |
ArduinoOTA.setHostname(mqtt_client); | |
ArduinoOTA.setPassword((const char *)OTApassword); | |
Serial.println("Starting host " + String(host_name)); | |
setup_wifi(); | |
setup_mqtt(); | |
ArduinoOTA.onStart([]() { | |
Serial.println("Starting"); | |
}); | |
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 loop() { | |
ArduinoOTA.handle(); | |
if (!client.connected()) { | |
setup_mqtt(); | |
} | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment