Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Run on Agent
- // Thresholds to adjust for better accuracy
- dataThreshold <- 270; // Minimum accelerometer value to count as ON
- onThreshold <- 20; // 20 Number of ON samples before machine enters RUNNING state
- offThreshold <- 30; // Number of OFF samples before machine enters OFF state
- // State variable
- running <- false;
- // Keep track of counts
- onCount <- 0;
- offCount <- 0;
- // Twilo code for sending SMS
- TWILIO_ACCOUNT_SID <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- TWILIO_AUTH_TOKEN <- "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
- TWILIO_FROM_NUMBER <- "+1600000000" // your twilo phone no goes here
- //TWILIO_TO_NUMBER <- "+6100000000" // destination phone no
- // Function for sending SMS vis Twilo
- function send_sms (number, message) {
- //manually enetred in "ID" for ease of coding in url
- //local twilio_url = format("https://api.twilio.com/2010-04-01/Accounts/AC3918b7ed6d5df4ba9793736e9c3448c2/SMS/Messages.json"");
- local twilio_url = format("https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json"", TWILIO_ACCOUNT_SID);
- local auth = "Basic " + http.base64encode(TWILIO_ACCOUNT_SID+":"+TWILIO_AUTH_TOKEN);
- local body = http.urlencode({From=TWILIO_FROM_NUMBER, To=number, Body=message});
- local req = http.post(twilio_url, {Authorization=auth}, body);
- local res = req.sendsync();
- //server.log(auth);
- if(res.statuscode != 201) {
- server.log("error sending message: "+res.body);
- }
- }
- // Array of phone numbers to be contacted with the laundry is done
- //phoneNumbers <- ["+61000000000", "+61000000000"];
- phoneNumbers <- ["+61000000000"];
- // Firebase
- logToFirebase <- false;
- firebaseURL <- "https://FIREBASE_URL.firebaseIO.com/data.json";
- firebaseHeaders <- { "Content-Type": "application/json" };
- // Called every time the imp emits data
- device.on("data", function(data) {
- // Is there enough accelerometer activity for the device to be considered ON?
- if (data >= dataThreshold) {
- onCount = onCount + 1;
- // Prevent overflow errors by resetting onCount when it gets close to limit
- if (onCount >= 65500) {
- onCount = onThreshold;
- }
- // If the device has been ON for long enough, set running state to be true
- if (onCount >= onThreshold) {
- running = true;
- // Running, so reset offCount
- offCount = 0;
- }
- // debug / logs
- if (running == true) {
- server.log(format("ON - RUNNING (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
- } else {
- server.log(format("ON (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
- }
- // Imp is not recording much accelerometer movement, so device seems to be OFF
- } else {
- offCount = offCount + 1;
- // Prevent overflow errors by resetting offCount when it gets close to limit
- if (offCount >= 65500) {
- offCount = offThreshold;
- }
- // Has the device been off for long enough to be "done"?
- if (offCount >= offThreshold) {
- // Was the device previously running?
- if (running == true) {
- // This means that the laundry had been running, and is now done.
- // Send an SMS to each phone number in the phoneNumbers array.
- foreach (number in phoneNumbers) {
- send_sms(number, "Candice your washing has finished!")
- }
- // debug / logs
- server.log("!!!! Emitting OFF event !!!!");
- }
- // Reset on count
- onCount = 0;
- // Machine is no longer running
- running = false;
- }
- // debug / logs
- if (running == true) {
- server.log(format("OFF - WAS RUNNING (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
- } else {
- server.log(format("OFF (%f), onCount (%d), offCount (%d)", data, onCount, offCount));
- }
- }
- if (logToFirebase == true) {
- // Build a post request to Firebase to log the data
- local body = format("{\"amount\": %f, \"running\": %s, \".priority\": %d}", data, running ? "true" : "false", time());
- local request = http.post(firebaseURL, firebaseHeaders, body);
- // Send the data to Firebase async
- local response = request.sendasync(function(done) {});
- }
- });
- //Webpage code
- washingmachinestate <- "Waiting for device to send state"
- connectedString <- "Device Disconnected";
- //Test if device is running
- function setPage(){
- if (running) {
- washingmachinestate = "Currently Running";
- } else {
- washingmachinestate = "Not Running";
- }
- //Test if device is connected
- if (device.isconnected())
- {
- connectedString = "Device Connected";
- }
- //server.log(connectedString);
- local page = @"
- <html>
- <head>
- <title>Candices Washing Machine Page</title>
- </head>
- <body>
- <h1>Washing Machine Status</h1>
- <h2>" + connectedString + @"</h2>
- <h2>" + washingmachinestate + @"</h2>
- <p>
- <script>
- document.write(Date());
- </script>
- </p>
- </body>
- </html>";
- return page;
- }
- http.onrequest(function(req, resp) {
- resp.send(200, setPage());
- //server.log(setPage());
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement