Interesting Use-Case Scenario for Livecode Server
Posted: Fri Apr 08, 2016 4:17 pm
I live in Northern California, which is a major agricultural area. Recently, an almond farmer approached me with a simple problem: At certain times each year almond farmers are allotted water. Each almond-field has a pump ( that runs on a generator) which distributes that water throughout the field.
Since water is allotted, and because of the drought, it is important that the farmer knows immediately if the pump turns off...which happens occasionally for various reasons...runs out of diesel, loses water pressure, malfunction, etc...
The farmer wanted to know if I could create a system that would alert him as soon as the pump turned off. Apparently there are services that do such things, but are more geared for industrial farming and cost prohibitive.
There is a house on the farm that has internet, and the almond fields sort of spread out from around the house.
Are your idea-wheels turning? I spent last month working out the mechanics of this project.
Here is what I have so far:
PING FUNCTION:
PHP TO DISPLAY STATUS
The above is a good start, but for various reasons I want this to be in Livecode rather than PHP. Also, I need to change a few things:
I should also mention that this is a free and open-source project...meaning I am not making any money off this, and will gladly share my code if anyone wants it. The farmer paid me with two truckloads of almond wood and a 25 pound bag of raw shelled almonds.
Any thoughts or ideas are appreciated.
Since water is allotted, and because of the drought, it is important that the farmer knows immediately if the pump turns off...which happens occasionally for various reasons...runs out of diesel, loses water pressure, malfunction, etc...
The farmer wanted to know if I could create a system that would alert him as soon as the pump turned off. Apparently there are services that do such things, but are more geared for industrial farming and cost prohibitive.
There is a house on the farm that has internet, and the almond fields sort of spread out from around the house.
Are your idea-wheels turning? I spent last month working out the mechanics of this project.
Here is what I have so far:
- a series of dd-wrt routers in bridge-mode allowed me to get internet all the way to the pump.
- The final dd-wrt router at the pump is connected to a solenoid so that if the pump stops, the router turns off.
- This router has openvpn in client-mode, which connects to a server on DigitalOcean that has Livecode server installed.
- From that server, I can ping the router that is connected to the pump (when the pump is running)
PING FUNCTION:
Code: Select all
<?php
/* a simple php ping function */
function ping($host)
{
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $rval === 0;
}
?>
PHP TO DISPLAY STATUS
Code: Select all
<?php /* check if the host is up */
$host = '10.8.0.13';
$up = ping($host);
$status = "Uh OH!!";
if($up){ //if event is up
$status = "Things are good!";
//read the file to see if it has an up or down date
$myFile = "ping";
$fr = fopen($myFile, 'r');
$event_name = fread($fr, 4);
fclose($fr);
//mark the current up time so we can calculate
$up_time = "up_time";
$ut = fopen($up_time, 'w') or die("can't open file");
$time = time();
fwrite($ut, $time);
fclose($ut);
if( $event_name == "Down" ) { //if event is down get an up date
$pretty_date = date(" g:ia D, F d" );
$myFile = "ping";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Up since ".$pretty_date;
fwrite($fh, $stringData);
fclose($fh);
}
}
if(!$up ) { //if down
//read the file to see what the last date/event was
$myFile = "ping";
$fr = fopen($myFile, 'r');
$event_name = fread($fr, 2);
fclose($fr);
//read the file to see the last uptime for calculating
$up_time = "up_time";
$ut = fopen($up_time, 'r');
$up_time = fread($ut, 10);
fclose($ut);
//if current time minus last up_time is greater than 5 minutes
if( date() - $up_time >= 300){ echo "HELLO IT HAS BEEN 5 MINUTES";
}
if( $event_name == "Up" ) { //if event is up get a down date
$pretty_date = date(" g:ia D, F d" );
$myFile = "ping";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Down at";
fwrite($fh, $stringData);
$stringData = $pretty_date;
fwrite($fh, $stringData);
fclose($fh);
}
} //end if down
$myFile = "ping";
$fr = fopen($myFile, 'r');
$event_date = fread($fr, 100);
fclose($fr);
/* give the div a class of up or down and echo status */
echo '<div class="'.($up ? 'up' : 'down').'" >'; ?>
<span class="status">
<?php echo $status; ?>
</span>
<span class="event-date">
<?php echo $event_date; ?>
</span>
</div>
The above is a good start, but for various reasons I want this to be in Livecode rather than PHP. Also, I need to change a few things:
- It should send an email (or text if that is possible) when things do go down
- It should wait five minutes if it cannot ping the router BEFORE sending out the email..I have noticed that sometimes there are very short down-times which are probably just brief wireless interruptions ...so I want to ignore those
- Currently it saves down-times in a simple text-file, but I think it would be good to make a database for it so there is a record of down-times
I should also mention that this is a free and open-source project...meaning I am not making any money off this, and will gladly share my code if anyone wants it. The farmer paid me with two truckloads of almond wood and a 25 pound bag of raw shelled almonds.
Any thoughts or ideas are appreciated.