Advertisement
domiflichi

Arduino code

Jul 6th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. /*
  5. UPGRADES / TODOs:
  6.  
  7. 1. Email someone when this is used
  8. ORRRRR
  9. 2. Write a log to the SD Card (much faster)
  10.  
  11. */
  12.  
  13. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  14. byte ip[] = { 192,168,0, 111 }; // Ip address to assign to this 'server' (ip that it listens on)
  15. byte gateway[] = { 192,168,0, 1 }; // Gateway ip
  16. byte subnet[] = { 255, 255, 255, 0 }; // Subnet mask
  17.  
  18. Server server(4999); // port that it listens on
  19.  
  20. //boolean gotAMessage = false; // whether or not you got a message from the client yet
  21. boolean isAuthenticated = false;
  22.  
  23. String myLittleBuffer = "";
  24. String myPass = "password"; // *** Any string comparisons to Android commands must be declared up here as Strings !!! ***
  25. String cmdGarageDoorStatusRequest = "cmd=statusRequest";
  26. String cmdGarageDoorToggle = "cmd=gdToggle";
  27. String cmdGarageDoorCrack = "cmd=gdCrack"; // Note that this will not include the milliseconds to pause for the crack obviously
  28. String disconnect = "disconnect";
  29.  
  30. char thisChar = 'A';
  31.  
  32. int garage_door_state = 0;
  33. //int milsToPauseForCrack = 0;
  34.  
  35. #define RELAY 9 // the pin for the RELAY
  36. #define BUTTON 7 // the input pin where the
  37. // reed/magnetic switch is connected
  38. // to detect if the garage door is closed/open
  39.  
  40.  
  41. void setup() {
  42. // initialize the ethernet device
  43. Ethernet.begin(mac, ip, gateway, subnet);
  44. // start listening for clients
  45. server.begin();
  46. // open the serial port
  47. Serial.begin(9600);
  48.  
  49. pinMode(RELAY, OUTPUT); // tell Arduino RELAY is an output
  50. pinMode(BUTTON, INPUT); // and BUTTON is an input
  51. }
  52.  
  53. void loop() {
  54. // wait for a new client:
  55. Client client = server.available();
  56.  
  57. // when the client sends the first byte, say hello:
  58. if (client) {
  59.  
  60. if (!isAuthenticated) {
  61. thisChar = client.read();
  62. }
  63.  
  64. if (thisChar == '*') {
  65.  
  66. if (myLittleBuffer.equals(myPass)) {
  67. Serial.println(myLittleBuffer);
  68. Serial.println("correct password!");
  69.  
  70.  
  71.  
  72. // get the initial status of the garage door, send the status to the client
  73. garage_door_state = digitalRead(BUTTON);
  74. if (garage_door_state == HIGH) {
  75. server.println("status:closed"); // Send the initial garage door status
  76. Serial.println("Sent: status:closed");
  77. } else {
  78. server.println("status:open"); // Send the initial garage door status
  79. Serial.println("Sent: status:open");
  80. }
  81.  
  82.  
  83. myLittleBuffer = ""; // Make sure that this string starts out empty
  84. client.flush(); // Flush any garbage characters out
  85. Serial.println(thisChar);
  86.  
  87. do
  88. {
  89. // THE EVERY OTHER CHARACTER PROBLEM IS CAUSED BY THE BELOW IF STATEMENT!
  90. //if (client.read() != (char)-1) {
  91. thisChar = client.read();
  92. //}
  93.  
  94. //Serial.print("thisChar = " + thisChar);
  95. Serial.println("myLittleBuffer = " + myLittleBuffer + ", thisChar = " + thisChar);
  96.  
  97. if (thisChar == '@') {
  98.  
  99. // *** THIS IS THE MAIN BLOCK OF CODE THAT ACTUALLY DOES STUFF HERE ***
  100. if (myLittleBuffer.equals(cmdGarageDoorStatusRequest)) {
  101. // TODO - Respond to Garage Door Status Request
  102. Serial.println("Garage Door Status Request Block:");
  103. Serial.println(myLittleBuffer);
  104. } else if (myLittleBuffer.equals(cmdGarageDoorToggle)) {
  105. // Respond to Garage Door Toggle Command
  106. Serial.println("Garage Door Toggle Command Block:");
  107. Serial.println(myLittleBuffer);
  108.  
  109. digitalWrite(RELAY, HIGH); // 'Push the button'
  110. delay(250); // Wait 1/4 second
  111. digitalWrite(RELAY, LOW); // 'Release the button'
  112. } else if (myLittleBuffer.substring(6,9) == "Cra") {
  113. // TODO - Respond to Garage Door Crack Command
  114. Serial.println("Garage Door Crack Command Block:");
  115. Serial.println(myLittleBuffer);
  116.  
  117. int lengthOfMLB;
  118. int posOfColon;
  119. String milsToPauseForCrack = "";
  120.  
  121. lengthOfMLB = myLittleBuffer.length();
  122. posOfColon = myLittleBuffer.indexOf(":");
  123.  
  124. posOfColon++; // We actually need to start at the very next character after the colon, because we don't care about the colon
  125. for (int i=posOfColon; i <= lengthOfMLB; i++){
  126. milsToPauseForCrack = milsToPauseForCrack + myLittleBuffer.charAt(i);
  127. }
  128. Serial.println("milsToPauseForCrack: " + milsToPauseForCrack);
  129. long x = stringToLong(milsToPauseForCrack);
  130.  
  131. digitalWrite(RELAY, HIGH); // 'Push the button'
  132. delay(100); // Wait 1/10 second
  133. digitalWrite(RELAY, LOW); // 'Release the button'
  134.  
  135. delay(x); // Wait for USER-SPECIFIED amount of milliseconds
  136.  
  137. digitalWrite(RELAY, HIGH); // 'Push the button'
  138. delay(100); // Wait 1/10 second
  139. digitalWrite(RELAY, LOW); // 'Release the button'
  140.  
  141. } else if (myLittleBuffer.equals(disconnect)) {
  142. Serial.println("Disconnect Block:");
  143. Serial.println(myLittleBuffer);
  144. // TODO - Respond to Disconnect
  145. myLittleBuffer = ""; // Reset this for the next connection
  146. client.stop();
  147. break; // This is to get us out of the loop so we don't try and send one last garage door status
  148. }
  149. // *** THIS IS THE MAIN BLOCK OF CODE THAT ACTUALLY DOES STUFF HERE ***
  150.  
  151. // Reset our 'buffer' back to empty because we received the special '*' character to denote end-of-command
  152. myLittleBuffer = "";
  153.  
  154. } else {
  155. // We want to ignore '*', '@', and CR/LF
  156. if (thisChar != '*' && thisChar != (char)-1 && thisChar != '@' && thisChar != (char)13 && thisChar != (char)10) {
  157. myLittleBuffer = String(myLittleBuffer + thisChar);
  158. }
  159. }
  160.  
  161.  
  162. // Send the current status of the garage door, send the status to the client
  163. garage_door_state = digitalRead(BUTTON);
  164. if (garage_door_state == HIGH) {
  165. server.println("status:closed");
  166. Serial.println("Sent: status:closed");
  167. } else {
  168. server.println("status:open");
  169. Serial.println("Sent: status:open");
  170. }
  171.  
  172. // This is good for watching the commands come/go
  173. // Comment out when in production
  174. //delay(1000); // Wait some time so that we can read what's in the serial monitor
  175.  
  176. } while (client); // while the client is connected
  177.  
  178. } else {
  179. Serial.print(myLittleBuffer);
  180. Serial.print("incorrect password");
  181. server.println("incorrect password"); // Send an incorrect password message if the password doesn't match
  182. myLittleBuffer = ""; // Reset myLittleBuffer for next time
  183. client.flush(); // Flush out any remaining characters
  184. client.stop(); // disconnect client
  185. }
  186.  
  187. // Reset our 'buffer' back to empty because we received the special '*' character to denote end-of-command
  188. //myLittleBuffer = "";
  189.  
  190. } else {
  191.  
  192. myLittleBuffer = String(myLittleBuffer + thisChar);
  193.  
  194. }
  195.  
  196. }
  197.  
  198. }
  199.  
  200.  
  201. long stringToLong(String s)
  202. {
  203. char arr[12];
  204. s.toCharArray(arr, sizeof(arr));
  205. return atol(arr);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement