Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. int led = 2; //onboard LED pin
  5. char vInp13 = 0; //sensor for left garage door - D7 on the board
  6. char vInp14 = 0; //sensor for right garage door - D5 on the board
  7. String rx; //string to hold the payload from mqtt
  8. String mtopic; //string to hold the topic from mqtt
  9. int rxLength = 0; //length of the payload from mqtt
  10. int leftDoorPin = 4; //relay for left garage door - D1 on the board
  11. int rightDoorPin = 5; //relay for right garage door - D2 on the board
  12. int switchPin;
  13. String garageDoor;
  14. String currState;
  15. int timeToClose = 7; //thought about doing something while the door was opening/closing... this isn't used
  16.  
  17. //Configuration, enter your own values here
  18. char ssid[] = "<SSID>"; // your network name also called SSID
  19. char password[] = "<password>"; // your network password
  20. char server[] = "<IP address>"; // MQTT Server IP or machine name
  21. char leftdoorstate[] = "GarageDoorLeft/state"; // MQTT State Topic - Left door
  22. char rightdoorstate[] = "GarageDoorRight/state"; // MQTT State Topic - Right door
  23. char leftdoorset[] = "GarageDoorLeft/set"; // MQTT Set Topic - Left door
  24. char rightdoorset[] = "GarageDoorRight/set"; // MQTT Set Topic - Right door
  25. char mQTTID[] = "ESP8266GarageDoor"; // MQTT ID. Make this unique for each device connecting to the MQTT Broker or they will disconnect eachother!
  26.  
  27. // Callback function header for mqtt
  28. // this is craeted here due to a bug... the actual callback function for mqtt is later in the code
  29. void callback(char* topic, byte* payload, unsigned int length);
  30.  
  31. //initialize wifi and mqtt clients
  32. WiFiClient wifiClient;
  33. PubSubClient client(server, 1883, callback, wifiClient);
  34.  
  35. void reconnect() {
  36. // Loop until we're reconnected to MQTT
  37. while (!client.connected()) {
  38. Serial.print("Attempting MQTT connection...");
  39. // Attempt to connect to MQTT
  40. if (client.connect(mQTTID)) {
  41. Serial.println("connected");
  42. //subscribe to the set topics so we receive messages to open/close
  43. client.subscribe(leftdoorset);
  44. client.subscribe(rightdoorset);
  45. } else {
  46. Serial.print("failed, rc=");
  47. Serial.print(client.state());
  48. Serial.println(" try again in 5 seconds");
  49. // Wait 5 seconds before retrying
  50. delay(5000);
  51. }
  52. }
  53. }
  54.  
  55. void setup() {
  56. //put your setup code here, to run once:
  57. //set the pin modes
  58. pinMode(led, OUTPUT); //onboard LED
  59. pinMode(leftDoorPin, OUTPUT); //D1 on the board - left garage door
  60. pinMode(rightDoorPin, OUTPUT); //D2 on the board - right garage door
  61. pinMode(13, INPUT_PULLUP); //D7 on the board - left door
  62. pinMode(14, INPUT_PULLUP); //D5 on the board - right door
  63.  
  64. //
  65. digitalWrite(rightDoorPin,1); //activate the right door relay pin
  66. digitalWrite(leftDoorPin,1); //activate the left door relay pin
  67.  
  68. //initiate serial output for testing...
  69. Serial.begin(115200);
  70. //connect to wifi
  71. Serial.print("Attempting to connect to Network named: ");
  72. Serial.println(ssid);
  73. while ( WiFi.status() != WL_CONNECTED)
  74. {
  75. WiFi.setOutputPower(0);
  76. WiFi.begin(ssid, password);
  77. Serial.print("."); //print dots while we wait to connecting.
  78. digitalWrite(led,HIGH); //flash onboard LED while connecting.
  79. delay(500);
  80. digitalWrite(led,LOW);
  81. delay(300);
  82. }
  83. //turn off LED because we successfully connected
  84. digitalWrite(led,HIGH);
  85.  
  86. Serial.println("\nYou're connected to the network");
  87. Serial.println("Waiting for an ip address");
  88. while (WiFi.localIP() == INADDR_NONE)
  89. {
  90. Serial.print("."); // print dots while we wait for an ip addresss
  91. delay(300);
  92. }
  93. Serial.println("IP Address obtained");
  94. Serial.println(WiFi.localIP());
  95. client.setServer(server, 1883);
  96. client.setCallback(callback);
  97. }
  98.  
  99. void loop() {
  100. // put your main code here, to run repeatedly:
  101.  
  102. //check open/close of left garage door
  103. if (digitalRead(13) != vInp13)
  104. {
  105. vInp13 = digitalRead(13);
  106. if (vInp13 == LOW)
  107. {
  108. client.publish(leftdoorstate, "closed", true);
  109. Serial.println("TX: Left Door closed");
  110. }
  111. else
  112. {
  113. client.publish(leftdoorstate, "open", true);
  114. Serial.println("TX: Left Door open");
  115. }
  116. }
  117.  
  118. //check open/close of right garage door
  119. if (digitalRead(14) != vInp14)
  120. {
  121. vInp14 = digitalRead(14);
  122. if (vInp14 == LOW)
  123. {
  124. client.publish(rightdoorstate, "closed", true);
  125. Serial.println("TX: Right Door closed");
  126. }
  127. else
  128. {
  129. client.publish(rightdoorstate, "open", true);
  130. Serial.println("TX: Right Door open");
  131. }
  132. }
  133.  
  134. if (!client.connected()) {
  135. reconnect();
  136. }
  137. client.loop();
  138. }
  139.  
  140. //##### Subroutines #####
  141. ///// VOID CALLBACK - prints to the serial monitor if we recieve a MQTT message /////
  142. void callback(char* topic, byte* payload, unsigned int length)
  143. {
  144. //Convert and clean up the MQTT payload messsage into a String
  145. rx = String((char *)payload); //Payload is a Byte Array, convert to char to load it into the "String" object
  146. rxLength = rx.length(); //Figure out how long the resulting String object is
  147. for (int i = length; i <= rxLength; i++) //Loop through setting extra characters to null as garbage may be there
  148. {
  149. rx.setCharAt(i, ' ');
  150. }
  151. rx.trim(); //Use the Trim function to finish cleaning up the string
  152.  
  153. mtopic = String(topic);
  154.  
  155. Serial.println("Message received from MQTT");
  156. Serial.print("Topic:");
  157. Serial.print(mtopic);
  158. Serial.println();
  159. Serial.print("Message:");
  160. Serial.print(rx);
  161. Serial.println();
  162.  
  163. //get the door that is sending a command
  164. if(mtopic == "GarageDoorLeft/set"){
  165. switchPin = leftDoorPin;
  166. garageDoor = "left";
  167. //get the current state of that door
  168. if(vInp13 == HIGH){currState = "open";}
  169. else if (vInp13 == LOW){currState = "closed";}
  170. Serial.print("Door is currently: ");
  171. Serial.println(currState);
  172. }
  173. else if(mtopic == "GarageDoorRight/set"){
  174. switchPin = rightDoorPin;
  175. garageDoor = "right";
  176. //get the current state of that door
  177. if(vInp14 == HIGH){currState = "open";}
  178. else if (vInp14 == LOW){currState = "closed";}
  179. Serial.print("Door is currently: ");
  180. Serial.println(currState);
  181. }
  182.  
  183. if((rx == "open") && (currState == "closed")){
  184. Serial.print("Opening the ");
  185. Serial.print(garageDoor);
  186. Serial.println(" garage door");
  187. //action to open and door is closed:
  188. digitalWrite(switchPin, 0);
  189. Serial.println();
  190. delay(1000);
  191. digitalWrite(switchPin, 1);
  192. delay(1000);
  193. //originally... i published the state. now that i have sensors, the sensors will publish the state
  194. //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"open", true);}
  195. //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"open", true);}
  196. }
  197. else if ((rx == "close") && (currState == "open")){
  198. Serial.print("Closing the ");
  199. Serial.print(garageDoor);
  200. Serial.println(" garage door");
  201. //action to close and door is open:
  202. digitalWrite(switchPin, 0);
  203. Serial.println();
  204. delay(1000);
  205. digitalWrite(switchPin, 1);
  206. delay(1000);
  207. //originally... i published the state. now that i have sensors, the sensors will publish the state
  208. //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"closed", true);}
  209. //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"closed", true);}
  210. }
  211. //do something if the set is stop... currently not handling this... no need for now.
  212. //else if (rx == "stop"){
  213. //stop sent... do nothing
  214. //digitalWrite(switchPin, 0);
  215. //Serial.println();
  216. //delay(1000);
  217. //digitalWrite(switchPin, 1);
  218. //delay(1000);
  219. //if(currState == "open"){
  220. //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"closed", true);}
  221. //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"closed", true);}
  222. //}
  223. //else if (currState == "closed"){
  224. //if(mtopic == "GarageDoorLeft/set"){client.publish(leftdoorstate,"open", true);}
  225. //else if(mtopic == "GarageDoorRight/set"){client.publish(rightdoorstate,"open", true);}
  226. //}
  227. //}
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement