Talk:OpenDuino
From syn2cat - HackerSpace.lu
temporary storage for source code
arduino part
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 33 };
byte gateway[] = { 192 ,168 ,1 ,254 };
byte subnet[] = { 255, 255, 255, 0 };
boolean open = false;
//byte server_ip[] = {74, 125, 127, 100}; //google
byte server_ip[] = { 80, 90, 47, 170 }; //hackerspace.lu
//byte server_ip[] = { 192, 168, 1, 65 };
char request[300] = "GET /OpenDuino.php?status=0 HTTP/1.0";
//char request[300] = "GET /index.html HTTP/1.0";
unsigned long time = 0;
unsigned long oldtime = 0;
unsigned long trigger_oldtime = 0;
//Server server(1337);
void setup()
{
oldtime = millis();
trigger_oldtime = oldtime;
Ethernet.begin(mac, ip, gateway, subnet);
//server.begin();
Serial.begin(9600);
lights();
// connect once on launch
dialup();
}
void dialup()
{
Client client(server_ip,80);
Serial.println("Connecting...");
Serial.println(request);
if( client.connect() ){
client.println(request);
client.println();
} else {
Serial.println("Connection failed.");
} //*/
if(client.available()) {
char c = client.read();
Serial.println(c);
} //*/
client.stop();
}
void lights() {
if( open ) {
digitalWrite(7,HIGH);
digitalWrite(5,LOW);
} else {
digitalWrite(7,LOW);
digitalWrite(5,HIGH);
}
}
void loop()
{
time = millis();
// read the switch state
if(digitalRead(0) == 0) {
if( time - trigger_oldtime > 1000 || time - trigger_oldtime < 0 ) {
trigger_oldtime = time;
Serial.println("Changing state.");
open = !open;
request[26] = (int)open+48;
lights();
dialup();
}
}
// send regular updates
if( time-oldtime > 300000 || time - oldtime < 0 ) {
oldtime = time;
dialup();
}
}
Server part I
<?php
try{
checkHostname();
setStatus();
respond();
} catch (Exception $e) {
mail('','OpenDuino failure',$e->getMessage());
writeToLog("Encountered an exception on ".date("r").": ".$e->getMessage());
}
/* functions */
function checkHostname(){
$hostname = 'openduino.dyndns.org';
$ip = $_SERVER['REMOTE_ADDR'];
if($ip != gethostbyname($hostname))
throw new Exception("Sorry dude, wrong ip! ($ip)");
}
function setStatus(){
$status = $_GET['status'];
if($status != "0" && $status != "1")
throw new Exception('No valid status given!');
$file = '/var/www/hackerspace.lu/open/status';
if(file_exists($file) && is_writeable($file)){
$current = file_get_contents($file);
if($status != $current){ // only write on change
file_put_contents($file,$status);
writeToLog("Wrote $status to statusfile on ".date("r"));
}
} else throw new Exception("Status file not writeable!");
}
function respond(){
print('ok');
}
function writeToLog($text){
$logfile = '/var/www/hackerspace.lu/open/log';
if(file_exists($logfile) && is_writeable($logfile))
file_put_contents($logfile,$text."\n",FILE_APPEND);
else throw new Exception("Logfile not writeable!");
}
?>
Server part II (Media Wiki extension)
<?php
/**
* Todo: Refactor
*/
$wgExtensionFunctions[] = "OpenDuino";
// Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
'name' => 'OpenDuino',
'author' => '[http://david.raison.lu David Raison]' ,
'url' => 'http://www.hackerspace.lu/wiki/OpenDuino',
'description' => 'OpenDuino is a hard- and software solution to notify site visitors of the hackerspace occupation status'
);
/* global vars */
$projectLink = '<a href="OpenDuino">*</a>';
function OpenDuino(){
global $wgParser;
$wgParser->setHook('openduino', 'ShowOpenDuinoStatus');
}
function ShowOpenDuinoStatus(){
global $wgOpenDuinoFile;
$status = readStatusFile($wgOpenDuinoFile);
if($status == 1)
return displayLightOn();
elseif($status == 0)
return displayLightOff();
else
return displayError();
return true;
}
function readStatusFile($file){
if(file_exists($file) && is_readable($file))
return file_get_contents($file);
}
function displayLightOn(){
global $wgParser, $projectLink;
$output = '<div style="width:90%;height:40px;border:1px dashed navy;padding: 20px 5px 0px 40px;'
.'background:#ffffff url(/w/thumb.php?f=LightOn.svg&width=32) left no-repeat;">'
.'The hackerspace is currently open. Come on in!'
.' '.$projectLink
.'</div>';
return $output;
}
function displayLightOff(){
global $wgParser, $projectLink;
//$link = "[[OpenDuino|*]]";
//$projectLink = $wgParser->parse($link,new Title(), new ParserOptions(),true,true)->getText();
$output = '<div style="width:90%;height:40px;border:1px dashed navy;padding: 20px 5px 0px 40px;'
.'background:#ffffff url(/w/thumb.php?f=LightOff.svg&width=32) left no-repeat;">'
.'The hackerspace is currently unoccupied. Check back later!'
.' '.$projectLink
.'</div>';
return $output;
}
function displayError(){
global $wgParser, $projectLink;
$output = '<div style="width:90%;height:50px;border:1px dashed navy;padding: 20px 5px 0px 40px;'
.'background:#ffffff url(/w/thumb.php?f=Attention.png&width=32) left no-repeat;">'
.'There was an error reading the Hackerspace status.<br/>You should probably contact an office member.'
.' '.$projectLink
.'</div>';
return $output;
}