I have an ESP8266 on a module board. It is connected to a relay board that will control a couple of 12v wiper motors. It is also connected to 2 limit switches. I wrote the code using some examples and a LOT of reading/searching. I have totally confused myself on the code to the point I am not sure I have it anywhere close to correct. Could someone look through it and tell me if I am at least in the ballpark?
I want it to listen for the door switch to be changed in Home Assistant. If switched to "ON", I want it to open a door and run a feed auger for 15 seconds. If it is switched to off, I want it to close the door. Of course I need it to stop the door motor when the limit switch is hit.
//Included Libs
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//Pin Definitions
#define PowerPin 4
#define PowerInvPin 5
#define OpenLimitPin 12
#define ClosedLimitPin 13
#define FeederPin 14
// Update these with values suitable for your network.
const char* ssid = "wirelessssid";
const char* password = "password";
const char* mqtt_server = "192.168.0.101";
WiFiClient espClient;
PubSubClient client(espClient);
String door;
String strTopic;
String strPayload;
String DoorStatus;
String DoorStatus1;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//Callback Function for Door
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
strTopic = String((char*)topic);
if(strTopic == "ha/door")
{
door = String((char*)payload);
if(door == "ON")
{
Serial.println("ON");
digitalWrite(PowerPin, HIGH);
DoorStatus = digitalRead(OpenLimitPin);
while(DoorStatus != "LOW")
{
DoorStatus = digitalRead(OpenLimitPin);
delay(250);
}
digitalWrite(PowerPin, LOW);
digitalWrite(FeederPin, HIGH);
delay(1500);
digitalWrite(FeederPin, LOW);
}
else
{
Serial.println("OFF");
DoorStatus1 = digitalRead(ClosedLimitPin);
if (DoorStatus1 != "LOW")
{
digitalWrite(PowerInvPin, HIGH);
delay(500);
digitalWrite(PowerPin, HIGH);
while (DoorStatus1 != "LOW")
{
DoorStatus1 = digitalRead(OpenLimitPin);
delay(250);
}
digitalWrite(PowerPin, LOW);
delay(500);
digitalWrite(PowerInvPin, LOW);
}
}
}
}
// Reconnect Function
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("ha/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//Setup Function
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
//Pin Modes
pinMode(PowerPin, OUTPUT);
digitalWrite(PowerPin, LOW);
pinMode(PowerInvPin, OUTPUT);
digitalWrite(PowerInvPin, LOW);
pinMode(OpenLimitPin, INPUT);
pinMode(ClosedLimitPin, INPUT);
pinMode(FeederPin, OUTPUT);
digitalWrite(FeederPin, LOW);
}
//Loop Function
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
Thanks!