Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. //*****************************************************************************
  2. #include <SoftwareSerial.h> //TODO need to set due to some weird wire language linker, should we absorb this whole library into smartthings
  3. #include <SmartThings.h>
  4.  
  5. //*****************************************************************************
  6. // Pin Definitions | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  7. // V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
  8. //*****************************************************************************
  9. #define PIN_LED 13
  10.  
  11. //Smartthings Shield Assignments (required)
  12. #define PIN_THING_TX 2
  13. #define PIN_THING_RX 3
  14. //Garage Door Assignments
  15. #define PIN_RIGHT 4
  16. #define PIN_RIGHT_CONTACT 5
  17. #define PIN_LEFT 6
  18. #define PIN_LEFT_CONTACT 7
  19. //House Door Assignments
  20. #define PIN_FRONT_DOOR_CONTACT 8
  21. #define PIN_BACK_DOOR_CONTACT 9
  22. #define PIN_KITCHEN_DOOR_CONTACT 10
  23. #define PIN_GARAGESIDE_DOOR_CONTACT 11
  24.  
  25. #define OPEN HIGH
  26. #define CLOSED LOW
  27. #define PUSH_DELAY 1000 // milliseconds to keep the garage door button "pushed"
  28. #define UPDATE_DELAY 30000 //millseconds between update to SmartThings Cloud
  29.  
  30. //*****************************************************************************
  31. // Global Variables | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  32. // V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
  33. //*****************************************************************************
  34. SmartThingsCallout_t messageCallout; // call out function forward decalaration
  35. SmartThings smartthing(PIN_THING_RX, PIN_THING_TX, messageCallout); // constructor
  36.  
  37. bool leftClosed, rightClosed;
  38. bool frontClosed, backClosed, kitchenClosed, garagesideClosed;
  39.  
  40. bool isDebugEnabled; // enable or disable debug in this example
  41. int stateLED; // state to track last set value of LED
  42. int stateNetwork; // state of the network
  43. unsigned long timeLastUpdate;
  44. unsigned long timeSinceLastUpdate;
  45. //*****************************************************************************
  46. // Local Functions | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  47. // V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
  48. //*****************************************************************************
  49. void pushLeft()
  50. {
  51. if(leftClosed)
  52. {
  53. smartthing.send("leftDoor opening");
  54. Serial.println("leftDoor opening");
  55. } else {
  56. smartthing.send("leftDoor closing");
  57. Serial.println("leftDoor closing");
  58. }
  59.  
  60. timeLastUpdate = millis();
  61. smartthing.shieldSetLED(0, 0, 2); // blue
  62. digitalWrite(PIN_LEFT,LOW);
  63. delay(PUSH_DELAY);
  64. digitalWrite(PIN_LEFT,HIGH);
  65. smartthing.shieldSetLED(0, 0, 0); // off
  66. }
  67.  
  68. //*****************************************************************************
  69. void pushRight()
  70. {
  71. if(rightClosed)
  72. {
  73. smartthing.send("rightDoor opening");
  74. Serial.println("rightDoor opening");
  75. } else {
  76. smartthing.send("rightDoor closing");
  77. Serial.println("rightDoor closing");
  78. }
  79.  
  80. timeLastUpdate = millis();
  81. smartthing.shieldSetLED(0, 0, 2); // blue
  82. digitalWrite(PIN_RIGHT,LOW);
  83. delay(PUSH_DELAY);
  84. digitalWrite(PIN_RIGHT,HIGH);
  85. smartthing.shieldSetLED(0, 0, 0); // off
  86. }
  87.  
  88. bool isClosed(int pin)
  89. {
  90. return (digitalRead(pin) == CLOSED);
  91. }
  92.  
  93. void updateDoorState()
  94. {
  95. if (millis() > timeLastUpdate)
  96. {
  97. timeSinceLastUpdate = millis() - timeLastUpdate;
  98. }
  99. else //handle the overflow issue when millis() resets to zero
  100. {
  101. timeLastUpdate = millis();
  102. timeSinceLastUpdate = 0;
  103. }
  104.  
  105. if ((leftClosed != isClosed(PIN_LEFT_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  106. {
  107. leftClosed = isClosed(PIN_LEFT_CONTACT);
  108. if(leftClosed)
  109. {
  110. smartthing.send("leftDoor closed");
  111. Serial.println("leftDoor closed");
  112. } else {
  113. smartthing.send("leftDoor open");
  114. Serial.println("leftDoor open");
  115. }
  116. }
  117. if ((rightClosed != isClosed(PIN_RIGHT_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  118. {
  119. rightClosed = isClosed(PIN_RIGHT_CONTACT);
  120. if(rightClosed)
  121. {
  122. smartthing.send("rightDoor closed");
  123. Serial.println("rightDoor closed");
  124. } else {
  125. smartthing.send("rightDoor open");
  126. Serial.println("rightDoor open");
  127. }
  128. }
  129.  
  130. if ((frontClosed != isClosed(PIN_FRONT_DOOR_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  131. {
  132. frontClosed = isClosed(PIN_FRONT_DOOR_CONTACT);
  133. if(frontClosed)
  134. {
  135. smartthing.send("frontDoor closed");
  136. Serial.println("frontDoor closed");
  137. } else {
  138. smartthing.send("frontDoor open");
  139. Serial.println("frontDoor open");
  140. }
  141. }
  142.  
  143. if ((backClosed != isClosed(PIN_BACK_DOOR_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  144. {
  145. backClosed = isClosed(PIN_BACK_DOOR_CONTACT);
  146. if(backClosed)
  147. {
  148. smartthing.send("backDoor closed");
  149. Serial.println("backDoor closed");
  150. } else {
  151. smartthing.send("backDoor open");
  152. Serial.println("backDoor open");
  153. }
  154. }
  155.  
  156. if ((kitchenClosed != isClosed(PIN_KITCHEN_DOOR_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  157. {
  158. kitchenClosed = isClosed(PIN_KITCHEN_DOOR_CONTACT);
  159. if(kitchenClosed)
  160. {
  161. smartthing.send("kitchenDoor closed");
  162. Serial.println("kitchenDoor closed");
  163. } else {
  164. smartthing.send("kitchenDoor open");
  165. Serial.println("kitchenDoor open");
  166. }
  167. }
  168.  
  169. if ((garagesideClosed != isClosed(PIN_GARAGESIDE_DOOR_CONTACT)) || timeSinceLastUpdate >= UPDATE_DELAY)
  170. {
  171. garagesideClosed = isClosed(PIN_GARAGESIDE_DOOR_CONTACT);
  172. if(garagesideClosed)
  173. {
  174. smartthing.send("garagesideDoor closed");
  175. Serial.println("garagesideDoor closed");
  176. } else {
  177. smartthing.send("garagesideDoor open");
  178. Serial.println("garagesideDoor open");
  179. }
  180. }
  181. if (timeSinceLastUpdate >= UPDATE_DELAY)
  182. {
  183. timeLastUpdate = millis();
  184. }
  185.  
  186. }
  187.  
  188. //*****************************************************************************
  189. void setNetworkStateLED()
  190. {
  191. SmartThingsNetworkState_t tempState = smartthing.shieldGetLastNetworkState();
  192. if (tempState != stateNetwork)
  193. {
  194. switch (tempState)
  195. {
  196. case STATE_NO_NETWORK:
  197. if (isDebugEnabled) Serial.println("NO_NETWORK");
  198. smartthing.shieldSetLED(2, 0, 0); // red
  199. break;
  200. case STATE_JOINING:
  201. if (isDebugEnabled) Serial.println("JOINING");
  202. smartthing.shieldSetLED(2, 0, 0); // red
  203. break;
  204. case STATE_JOINED:
  205. if (isDebugEnabled) Serial.println("JOINED");
  206. smartthing.shieldSetLED(0, 0, 0); // off
  207. break;
  208. case STATE_JOINED_NOPARENT:
  209. if (isDebugEnabled) Serial.println("JOINED_NOPARENT");
  210. smartthing.shieldSetLED(2, 0, 2); // purple
  211. break;
  212. case STATE_LEAVING:
  213. if (isDebugEnabled) Serial.println("LEAVING");
  214. smartthing.shieldSetLED(2, 0, 0); // red
  215. break;
  216. default:
  217. case STATE_UNKNOWN:
  218. if (isDebugEnabled) Serial.println("UNKNOWN");
  219. smartthing.shieldSetLED(0, 2, 0); // green
  220. break;
  221. }
  222. stateNetwork = tempState;
  223. }
  224. }
  225.  
  226. //*****************************************************************************
  227. // API Functions | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  228. // V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
  229. //*****************************************************************************
  230. void setup()
  231. {
  232. // setup default state of global variables
  233. isDebugEnabled = true;
  234. stateLED = 0; // matches state of hardware pin set below
  235. stateNetwork = STATE_JOINED; // set to joined to keep state off if off
  236.  
  237. // setup hardware pins
  238. pinMode(PIN_LED, OUTPUT); // define PIN_LED as an output
  239. pinMode(PIN_RIGHT, OUTPUT);
  240. pinMode(PIN_LEFT, OUTPUT);
  241. digitalWrite(PIN_RIGHT, HIGH);
  242. digitalWrite(PIN_LEFT, HIGH);
  243. digitalWrite(PIN_LED, LOW); // set value to LOW (off) to match stateLED=0
  244.  
  245. pinMode(PIN_LEFT_CONTACT, INPUT_PULLUP);
  246. pinMode(PIN_RIGHT_CONTACT, INPUT_PULLUP);
  247.  
  248. pinMode(PIN_FRONT_DOOR_CONTACT, INPUT_PULLUP);
  249. pinMode(PIN_BACK_DOOR_CONTACT, INPUT_PULLUP);
  250. pinMode(PIN_KITCHEN_DOOR_CONTACT, INPUT_PULLUP);
  251. pinMode(PIN_GARAGESIDE_DOOR_CONTACT, INPUT_PULLUP);
  252.  
  253. if (isDebugEnabled)
  254. { // setup debug serial port
  255. Serial.begin(9600); // setup serial with a baud rate of 9600
  256. Serial.println("setup.."); // print out 'setup..' on start
  257. }
  258.  
  259. // Get the Current State of the Doors
  260. Serial.println("Getting Door State...");
  261. if (isClosed(PIN_LEFT_CONTACT))
  262. {
  263. leftClosed = true;
  264. smartthing.send("leftDoor closed");
  265. Serial.println("leftDoor closed");
  266. } else {
  267. leftClosed = false;
  268. smartthing.send("leftDoor open");
  269. Serial.println("leftDoor open");
  270. }
  271.  
  272. delay(1000);
  273.  
  274. if (isClosed(PIN_RIGHT_CONTACT))
  275. {
  276. rightClosed = true;
  277. smartthing.send("rightDoor closed");
  278. Serial.println("rightDoor closed");
  279. } else {
  280. rightClosed = false;
  281. smartthing.send("rightDoor open");
  282. Serial.println("rightDoor open");
  283. }
  284.  
  285. delay(1000);
  286.  
  287. if (isClosed(PIN_FRONT_DOOR_CONTACT))
  288. {
  289. frontClosed = true;
  290. smartthing.send("frontDoor closed");
  291. Serial.println("frontDoor closed");
  292. } else {
  293. frontClosed = false;
  294. smartthing.send("frontDoor open");
  295. Serial.println("frontDoor open");
  296. }
  297.  
  298. delay(1000);
  299.  
  300. if (isClosed(PIN_BACK_DOOR_CONTACT))
  301. {
  302. backClosed = true;
  303. smartthing.send("backDoor closed");
  304. Serial.println("backDoor closed");
  305. } else {
  306. backClosed = false;
  307. smartthing.send("backDoor open");
  308. Serial.println("backDoor open");
  309. }
  310.  
  311. delay(1000);
  312.  
  313. if (isClosed(PIN_KITCHEN_DOOR_CONTACT))
  314. {
  315. kitchenClosed = true;
  316. smartthing.send("kitchenDoor closed");
  317. Serial.println("kitchenDoor closed");
  318. } else {
  319. kitchenClosed = false;
  320. smartthing.send("kitchenDoor open");
  321. Serial.println("kitchenDoor open");
  322. }
  323.  
  324. delay(1000);
  325.  
  326. if (isClosed(PIN_GARAGESIDE_DOOR_CONTACT))
  327. {
  328. garagesideClosed = true;
  329. smartthing.send("garagesideDoor closed");
  330. Serial.println("garagesideDoor closed");
  331. } else {
  332. garagesideClosed = false;
  333. smartthing.send("garagesideDoor open");
  334. Serial.println("garagesideDoor open");
  335. }
  336.  
  337. timeLastUpdate = millis();
  338. timeSinceLastUpdate = 0;
  339. }
  340.  
  341. //*****************************************************************************
  342. void loop()
  343. {
  344. // run smartthing logic
  345. smartthing.run();
  346.  
  347. // Check the open/closed state of the doors
  348. updateDoorState();
  349.  
  350. // Code left here to help debut network connections
  351. setNetworkStateLED();
  352. }
  353.  
  354. //*****************************************************************************
  355. void messageCallout(String message)
  356. {
  357. // if debug is enabled print out the received message
  358. if (isDebugEnabled)
  359. {
  360. Serial.print("Received message: '");
  361. Serial.print(message);
  362. Serial.println("' ");
  363. }
  364.  
  365. if (message.equals("pushLeft"))
  366. {
  367. pushLeft();
  368. }
  369. else if (message.equals("pushRight"))
  370. {
  371. pushRight();
  372. }
  373.  
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement