Is my Imp within my WiFi area? Let's test it out

I was wondering what would happen if a running imp (on a battery pack) could detect whether or not it was in it’s blinked-up WiFi area. Especially if it left the area and came back, would it pick up where it left off? So I used my existing website, with a simple PHP script.

  1. The imp waits for 10 seconds and wakes up.
  2. The imp sets pin 7 to ‘0’ (which lights an LED), it also does a POST to my website PHP script.
  3. The PHP script sends back a ‘1’, waits one second and sends back a ‘0’.
  4. Whenever an HTTP input is detected by the imp, if the value is a ‘1’, it resets pin 7 (sets it to a one, turning off the LED).
  5. If pin 7 is not reset within the 10 second watchdog, it lights pin 8 (RED LED), otherwise if pin was reset, all is well and it lights pin 9 (GREEN LED).

I have to make the PHP script send a 1 then a 0 because the HTTP IN node retains the last value it received. I only want the value of ‘1’ to be a pulse, in order to reset pin 7.

So I had a GREEN LED (pin 8). I walked about 200 feet from my house and the RED LED (pin 9) came on. When I returned back to the house, the GREEN LED came back on. Success.

First, here is the PHP script:

`<?php
// Send back a 1 then a 0

$v = 1;

$rURL = “https://api.electricimp.com/v1/73j9e/98h0f/?value=”.$v;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rURL);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$out = curl_exec($ch);
curl_close($ch);	

sleep(1);

$v=0;
$rURL = “https://api.electricimp.com/v1/73j9e/98h0f/?value=”.$v;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rURL);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$out = curl_exec($ch);
curl_close($ch);	

?>
`

This is the imp script:

`// Check WiFi Status

server.show("");

// Output structure for sending ping to server
local outputHTTP = OutputPort(“status”, “number”);

// set pin 7 (LED ON) and POST to PHP script.
function ping() {
local stat = 1;
outputHTTP.set(stat);
server.show(stat);
hardware.pin7.write(0);
}

// input class for checking connection to server
class inputHTTP extends InputPort {
name = "status"
type = "number"
function set(httpVal) {
local s7 = hardware.pin7.read();
server.log("Received val: "+httpVal);
if(httpVal == 1){
hardware.pin7.write(1);
}
}
}

function watchdog() {
imp.wakeup(10,watchdog);
local s7 = hardware.pin7.read();
if(s7==1){
hardware.pin9.write(0);
hardware.pin8.write(1);
server.log(“WiFi OK”);
} else {
hardware.pin9.write(1);
hardware.pin8.write(0);
server.log(“WiFi LOST”);
}
ping();
}

// start watchdog write every 10 seconds
watchdog();

// Configure pins as pull-up.
hardware.pin9.configure(DIGITAL_OUT);
hardware.pin8.configure(DIGITAL_OUT);
hardware.pin7.configure(DIGITAL_OUT);
hardware.pin9.write(1);
hardware.pin8.write(1);
hardware.pin7.write(1);

// Register with the server
imp.configure(“ping_wifi”, [inputHTTP()], [outputHTTP]);`

So, the idea could be used in a few different ways …

  1. The PHP script could signal something (SMS message) if it was never POSTed by the imp in a certain amount of time.

  2. The imp would affect a piece of hardware only if it was within the WiFi area. In this case the imp is on the piece of equipment that is being moved.

  3. A 2nd imp could be used. The PHP script would tie the two imps together and if they were ever separated by a distance, the imp that stays within the WiFi area could trigger some kind of action.

Sounds great, do you have any videos or pictures of it in action?

Good idea … I’ll try to record something to show.