// AGENT // //-------------------------------------------------- // POST PROX Sensor Data to Website API //-------------------------------------------------- device.on("senddata", function(data) { // Set URL to your web service // IMPORTANT: Make sure URL ends with / local url = "https://www.yourwebsite.com/yourphpscript/"; // Set Content-Type header to json local headers = { "Content-Type": "application/json" }; // encode data and log local body = http.jsonencode(data); server.log(body); // send data to your web service local request = http.post(url, headers, body); local response = request.sendsync(); // send the response from the PHP script, // which is the desired sleep time in seconds. device.send("data", response.body); }); // DEVICE // // Globals mail <- "no"; sleepTime <- 300; // Alias GPIO pins proxSensor <- hardware.pinS; LED <- hardware.pinC; ENABLE <- hardware.pinE; function poll(){ // Wake-up (enable) the prox sensor ENABLE.write(1); imp.sleep(1.0); local state = proxSensor.read(); // Read the pin and log its value ENABLE.write(0); if (state < 1) { mail="yes"; server.log("Mail Detected"); } if (state > 0){ mail="no"; server.log("No Mail Detected"); } local data = { "mail": mail, "apikey": "674iKm6-7orU-928E-61k2ha-bx8R2960ccBda" } agent.send("senddata", data); } // Here we already sent the sensor status to the website. // The website responds back with the sleeptime it requires. // During the night, we'll deep sleep for 15 hours to save battery. function gotosleep(sleepTime){ imp.sleep(2); server.log("Going to sleep for: " + sleepTime); imp.onidle(function(){ server.sleepfor(sleepTime); }); } // Receiving the sleep time from the PHP script. agent.on("data", function(value) { sleepTime = value.tointeger(); gotosleep(sleepTime); }); // Configure IO proxSensor.configure(DIGITAL_IN); LED.configure(DIGITAL_OUT,0); ENABLE.configure(DIGITAL_OUT,0); // Start the loop poll();