Advertisement
Guest User

twilo-imp-agentcode-washingmachine

a guest
Feb 18th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. // Run on Agent
  2.  
  3. // Thresholds to adjust for better accuracy
  4. dataThreshold <- 270; // Minimum accelerometer value to count as ON
  5. onThreshold <- 20; // 20 Number of ON samples before machine enters RUNNING state
  6. offThreshold <- 30; // Number of OFF samples before machine enters OFF state
  7.  
  8. // State variable
  9. running <- false;
  10.  
  11. // Keep track of counts
  12. onCount <- 0;
  13. offCount <- 0;
  14.  
  15. // Twilo code for sending SMS
  16. TWILIO_ACCOUNT_SID <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  17. TWILIO_AUTH_TOKEN <- "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  18. TWILIO_FROM_NUMBER <- "+1600000000" // your twilo phone no goes here
  19. //TWILIO_TO_NUMBER <- "+6100000000" // destination phone no
  20.  
  21. // Function for sending SMS vis Twilo
  22. function send_sms (number, message) {
  23. //manually enetred in "ID" for ease of coding in url
  24. //local twilio_url = format("https://api.twilio.com/2010-04-01/Accounts/AC3918b7ed6d5df4ba9793736e9c3448c2/SMS/Messages.json&quot");
  25. local twilio_url = format("https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json&quot", TWILIO_ACCOUNT_SID);
  26. local auth = "Basic " + http.base64encode(TWILIO_ACCOUNT_SID+":"+TWILIO_AUTH_TOKEN);
  27. local body = http.urlencode({From=TWILIO_FROM_NUMBER, To=number, Body=message});
  28. local req = http.post(twilio_url, {Authorization=auth}, body);
  29. local res = req.sendsync();
  30. //server.log(auth);
  31. if(res.statuscode != 201) {
  32. server.log("error sending message: "+res.body);
  33. }
  34. }
  35.  
  36. // Array of phone numbers to be contacted with the laundry is done
  37. //phoneNumbers <- ["+61000000000", "+61000000000"];
  38. phoneNumbers <- ["+61000000000"];
  39.  
  40. // Firebase
  41. logToFirebase <- false;
  42. firebaseURL <- "https://FIREBASE_URL.firebaseIO.com/data.json";
  43. firebaseHeaders <- { "Content-Type": "application/json" };
  44.  
  45. // Called every time the imp emits data
  46. device.on("data", function(data) {
  47.  
  48. // Is there enough accelerometer activity for the device to be considered ON?
  49. if (data >= dataThreshold) {
  50. onCount = onCount + 1;
  51.  
  52. // Prevent overflow errors by resetting onCount when it gets close to limit
  53. if (onCount >= 65500) {
  54. onCount = onThreshold;
  55. }
  56.  
  57. // If the device has been ON for long enough, set running state to be true
  58. if (onCount >= onThreshold) {
  59. running = true;
  60.  
  61. // Running, so reset offCount
  62. offCount = 0;
  63. }
  64.  
  65. // debug / logs
  66. if (running == true) {
  67. server.log(format("ON - RUNNING (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
  68. } else {
  69. server.log(format("ON (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
  70. }
  71.  
  72. // Imp is not recording much accelerometer movement, so device seems to be OFF
  73. } else {
  74. offCount = offCount + 1;
  75.  
  76. // Prevent overflow errors by resetting offCount when it gets close to limit
  77. if (offCount >= 65500) {
  78. offCount = offThreshold;
  79. }
  80.  
  81. // Has the device been off for long enough to be "done"?
  82. if (offCount >= offThreshold) {
  83.  
  84. // Was the device previously running?
  85. if (running == true) {
  86.  
  87. // This means that the laundry had been running, and is now done.
  88.  
  89. // Send an SMS to each phone number in the phoneNumbers array.
  90. foreach (number in phoneNumbers) {
  91. send_sms(number, "Candice your washing has finished!")
  92. }
  93.  
  94. // debug / logs
  95. server.log("!!!! Emitting OFF event !!!!");
  96. }
  97.  
  98. // Reset on count
  99. onCount = 0;
  100.  
  101. // Machine is no longer running
  102. running = false;
  103. }
  104.  
  105. // debug / logs
  106. if (running == true) {
  107. server.log(format("OFF - WAS RUNNING (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
  108. } else {
  109. server.log(format("OFF (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
  110. }
  111. }
  112.  
  113. if (logToFirebase == true) {
  114. // Build a post request to Firebase to log the data
  115. local body = format("{\"amount\": %f, \"running\": %s, \".priority\": %d}", data, running ? "true" : "false", time());
  116. local request = http.post(firebaseURL, firebaseHeaders, body);
  117.  
  118. // Send the data to Firebase async
  119. local response = request.sendasync(function(done) {});
  120. }
  121. });
  122.  
  123. //Webpage code
  124. washingmachinestate <- "Waiting for device to send state"
  125. connectedString <- "Device Disconnected";
  126.  
  127. //Test if device is running
  128. function setPage(){
  129. if (running) {
  130. washingmachinestate = "Currently Running";
  131. } else {
  132. washingmachinestate = "Not Running";
  133. }
  134. //Test if device is connected
  135. if (device.isconnected())
  136. {
  137. connectedString = "Device Connected";
  138. }
  139. //server.log(connectedString);
  140.  
  141. local page = @"
  142. <html>
  143. <head>
  144. <title>Candices Washing Machine Page</title>
  145. </head>
  146. <body>
  147. <h1>Washing Machine Status</h1>
  148. <h2>" + connectedString + @"</h2>
  149. <h2>" + washingmachinestate + @"</h2>
  150. <p>
  151. <script>
  152. document.write(Date());
  153. </script>
  154. </p>
  155. </body>
  156. </html>";
  157.  
  158. return page;
  159. }
  160.  
  161. http.onrequest(function(req, resp) {
  162. resp.send(200, setPage());
  163. //server.log(setPage());
  164. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement