Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- int led = 2; //onboard LED pin
- char vInp13 = 0; //sensor for left garage door - D7 on the board
- char vInp14 = 0; //sensor for right garage door - D5 on the board
- String rx; //string to hold the payload from mqtt
- String mtopic; //string to hold the topic from mqtt
- int rxLength = 0; //length of the payload from mqtt
- int leftDoorPin = 4; //relay for left garage door - D1 on the board
- int rightDoorPin = 5; //relay for right garage door - D2 on the board
- int switchPin;
- String garageDoor;
- String currState;
- int timeToClose = 7; //thought about doing something while the door was opening/closing... this isn't used
- //Configuration, enter your own values here
- char ssid[] = "<SSID>"; // your network name also called SSID
- char password[] = "<password>"; // your network password
- char server[] = "<IP address>"; // MQTT Server IP or machine name
- char leftdoorstate[] = "GarageDoorLeft/state"; // MQTT State Topic - Left door
- char rightdoorstate[] = "GarageDoorRight/state"; // MQTT State Topic - Right door
- char leftdoorset[] = "GarageDoorLeft/set"; // MQTT Set Topic - Left door
- char rightdoorset[] = "GarageDoorRight/set"; // MQTT Set Topic - Right door
- char mQTTID[] = "ESP8266GarageDoor"; // MQTT ID. Make this unique for each device connecting to the MQTT Broker or they will disconnect eachother!
- // Callback function header for mqtt
- // this is craeted here due to a bug... the actual callback function for mqtt is later in the code
- void callback(char* topic, byte* payload, unsigned int length);
- //initialize wifi and mqtt clients
- WiFiClient wifiClient;
- PubSubClient client(server, 1883, callback, wifiClient);
- void reconnect() {
- // Loop until we're reconnected to MQTT
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect to MQTT
- if (client.connect(mQTTID)) {
- Serial.println("connected");
- //subscribe to the set topics so we receive messages to open/close
- client.subscribe(leftdoorset);
- client.subscribe(rightdoorset);
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- //put your setup code here, to run once:
- //set the pin modes
- pinMode(led, OUTPUT); //onboard LED
- pinMode(leftDoorPin, OUTPUT); //D1 on the board - left garage door
- pinMode(rightDoorPin, OUTPUT); //D2 on the board - right garage door
- pinMode(13, INPUT_PULLUP); //D7 on the board - left door
- pinMode(14, INPUT_PULLUP); //D5 on the board - right door
- //
- digitalWrite(rightDoorPin,1); //activate the right door relay pin
- digitalWrite(leftDoorPin,1); //activate the left door relay pin
- //initiate serial output for testing...
- Serial.begin(115200);
- //connect to wifi
- Serial.print("Attempting to connect to Network named: ");
- Serial.println(ssid);
- while ( WiFi.status() != WL_CONNECTED)
- {
- WiFi.setOutputPower(0);
- WiFi.begin(ssid, password);
- Serial.print("."); //print dots while we wait to connecting.
- digitalWrite(led,HIGH); //flash onboard LED while connecting.
- delay(500);
- digitalWrite(led,LOW);
- delay(300);
- }
- //turn off LED because we successfully connected
- digitalWrite(led,HIGH);
- Serial.println("\nYou're connected to the network");
- Serial.println("Waiting for an ip address");
- while (WiFi.localIP() == INADDR_NONE)
- {
- Serial.print("."); // print dots while we wait for an ip addresss
- delay(300);
- }
- Serial.println("IP Address obtained");
- Serial.println(WiFi.localIP());
- client.setServer(server, 1883);
- client.setCallback(callback);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //check open/close of left garage door
- if (digitalRead(13) != vInp13)
- {
- vInp13 = digitalRead(13);
- if (vInp13 == LOW)
- {
- client.publish(leftdoorstate, "closed", true);
- Serial.println("TX: Left Door closed");
- }
- else
- {
- client.publish(leftdoorstate, "open", true);
- Serial.println("TX: Left Door open");
- }
- }
- //check open/close of right garage door
- if (digitalRead(14) != vInp14)
- {
- vInp14 = digitalRead(14);
- if (vInp14 == LOW)
- {
- client.publish(rightdoorstate, "closed", true);
- Serial.println("TX: Right Door closed");
- }
- else
- {
- client.publish(rightdoorstate, "open", true);
- Serial.println("TX: Right Door open");
- }
- }
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- }
- //##### Subroutines #####
- ///// VOID CALLBACK - prints to the serial monitor if we recieve a MQTT message /////
- void callback(char* topic, byte* payload, unsigned int length)
- {
- //Convert and clean up the MQTT payload messsage into a String
- rx = String((char *)payload); //Payload is a Byte Array, convert to char to load it into the "String" object
- rxLength = rx.length(); //Figure out how long the resulting String object is
- for (int i = length; i <= rxLength; i++) //Loop through setting extra characters to null as garbage may be there
- {
- rx.setCharAt(i, ' ');
- }
- rx.trim(); //Use the Trim function to finish cleaning up the string
- mtopic = String(topic);
- Serial.println("Message received from MQTT");
- Serial.print("Topic:");
- Serial.print(mtopic);
- Serial.println();
- Serial.print("Message:");
- Serial.print(rx);
- Serial.println();
- //get the door that is sending a command
- if(mtopic == "GarageDoorLeft/set"){
- switchPin = leftDoorPin;
- garageDoor = "left";
- //get the current state of that door
- if(vInp13 == HIGH){currState = "open";}
- else if (vInp13 == LOW){currState = "closed";}
- Serial.print("Door is currently: ");
- Serial.println(currState);
- }
- else if(mtopic == "GarageDoorRight/set"){
- switchPin = rightDoorPin;
- garageDoor = "right";
- //get the current state of that door
- if(vInp14 == HIGH){currState = "open";}
- else if (vInp14 == LOW){currState = "closed";}
- Serial.print("Door is currently: ");
- Serial.println(currState);
- }
- if((rx == "open") && (currState == "closed")){
- Serial.print("Opening the ");
- Serial.print(garageDoor);
- Serial.println(" garage door");
- //action to open and door is closed:
- digitalWrite(switchPin, 0);
- Serial.println();
- delay(1000);
- digitalWrite(switchPin, 1);
- delay(1000);
- //originally... i published the state. now that i have sensors, the sensors will publish the state
- //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"open", true);}
- //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"open", true);}
- }
- else if ((rx == "close") && (currState == "open")){
- Serial.print("Closing the ");
- Serial.print(garageDoor);
- Serial.println(" garage door");
- //action to close and door is open:
- digitalWrite(switchPin, 0);
- Serial.println();
- delay(1000);
- digitalWrite(switchPin, 1);
- delay(1000);
- //originally... i published the state. now that i have sensors, the sensors will publish the state
- //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"closed", true);}
- //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"closed", true);}
- }
- //do something if the set is stop... currently not handling this... no need for now.
- //else if (rx == "stop"){
- //stop sent... do nothing
- //digitalWrite(switchPin, 0);
- //Serial.println();
- //delay(1000);
- //digitalWrite(switchPin, 1);
- //delay(1000);
- //if(currState == "open"){
- //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"closed", true);}
- //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"closed", true);}
- //}
- //else if (currState == "closed"){
- //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"open", true);}
- //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"open", true);}
- //}
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement