Created
January 4, 2012 06:39
-
-
Save leumund/1558814 to your computer and use it in GitHub Desktop.
#busstop arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Circuit: | |
* Ethernet shield attached to pins 10, 11, 12, 13 | |
created 21 May 2011 | |
by Tom Igoe | |
This code is in the public domain. | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <Wire.h> | |
#include "BlinkM_funcs.h" | |
// Enter a MAC address and IP address for your controller below. | |
// The IP address will be dependent on your local network: | |
byte mac[] = { | |
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; | |
IPAddress ip(192,168,1,20); | |
#define blinkm_addr 0x00 | |
int delay_time = 200; // time betwen colors, 1000 milliseconds = 1 second | |
int num_colors = 7; // how many colors are there the list below | |
int i = 0; | |
byte r,g,b; | |
// initialize the library instance: | |
EthernetClient client; | |
const int requestInterval = 10000; // delay between requests | |
char serverName[] = "leumund.ch"; // twitter URL | |
boolean requested; // whether you've made a request since connecting | |
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds | |
String currentLine = ""; // string to hold the text from server | |
String tweet = ""; // string to hold the tweet | |
boolean readingTweet = false; // if you're currently reading the tweet | |
void setup() { | |
// reserve space for the strings: | |
currentLine.reserve(256); | |
tweet.reserve(150); | |
BlinkM_beginWithPower(); | |
BlinkM_stopScript( blinkm_addr ); // turn off startup script | |
BlinkM_fadeToRGB(blinkm_addr,0x00,0x00,0x00); | |
BlinkM_setFadeSpeed(blinkm_addr,20); | |
// initialize serial: | |
Serial.begin(9600); | |
// attempt a DHCP connection: | |
if (!Ethernet.begin(mac)) { | |
// if DHCP fails, start with a hard-coded address: | |
Ethernet.begin(mac, ip); | |
} | |
// connect to Twitter: | |
connectToServer(); | |
} | |
void loop() | |
{ | |
// delay(delay_time); // wait a bit because we don't need to go fast | |
// Serial.println(millis()-lastAttemptTime); | |
//Serial.println("start loop"); | |
if (client.connected()) { | |
//Serial.println("if client connected"); | |
if (client.available()) { | |
// Serial.println("if client avalaible"); | |
// read incoming bytes: | |
char inChar = client.read(); | |
// add incoming byte to end of line: | |
currentLine += inChar; | |
// if you get a newline, clear the line: | |
if (inChar == '\n') { | |
// Serial.println(currentLine); | |
if ( currentLine.startsWith("off",0)) { | |
Serial.println("OFF"); | |
BlinkM_fadeToRGB(blinkm_addr,0x00,0x00,0x00); | |
client.stop(); | |
Serial.println("stop"); | |
} | |
if ( currentLine.startsWith("green",0)) { | |
Serial.println("GREEN"); | |
BlinkM_fadeToRGB(blinkm_addr,0x00,0x99,0x00); | |
client.stop(); | |
Serial.println("stop"); | |
} | |
if ( currentLine.startsWith("red",0)) { | |
Serial.println("RED"); | |
BlinkM_fadeToRGB(blinkm_addr,0x99,0x00,0x00); | |
client.stop(); | |
Serial.println("stop"); | |
} | |
if ( currentLine.startsWith("error",0)) { | |
Serial.println("ERROR"); | |
BlinkM_playScript(blinkm_addr, 1,0,0); | |
client.stop(); | |
Serial.println("stop"); | |
} | |
currentLine = ""; | |
} | |
} | |
// client.stop(); | |
// Serial.println("stop"); | |
} | |
else if ((millis() - lastAttemptTime) >= requestInterval) { | |
// if you're not connected, and two minutes have passed since | |
// your last connection, then attempt to connect again: | |
Serial.println("restart connect"); | |
connectToServer(); | |
} | |
} | |
void connectToServer() { | |
// attempt to connect, and wait a millisecond: | |
Serial.println("connecting to server..."); | |
if (client.connect(serverName, 80)) { | |
Serial.println("making HTTP request..."); | |
// make HTTP GET request to twitter: | |
client.println("GET /busstop/index.php HTTP/1.1"); | |
client.println("HOST: leumund.ch"); | |
client.println(); | |
} | |
// note the time of this connect attempt: | |
lastAttemptTime = millis(); | |
// client.stop(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
#busstop php parser für Haltestellen Informationen | |
2012 by Christian Leu - leumund.ch | |
*/ | |
//parameter | |
// walking time in seconds | |
$walking = "180"; | |
//minimum threshold = white wenn Zeit noch länger wie 4 Minuten | |
$threshold ="60"; | |
//destination wie in fahrplan.search.ch angegeben, z.b. Guisanplatz | |
$destination ="Guisanplatz"; | |
//haltestelle, URL wie in fahrplan.search.ch verwendet. z.B. "http://fahrplan.search.ch/bern,sulgenau" | |
$haltestelle = "http://fahrplan.search.ch/bern,sulgenau"; | |
// Keine weiteren Informationen oder Anpassungen notwendig! (Bzw. nur wer auch in etwa begreift was er tut nach dieser Linie) | |
$html=file_get_contents($haltestelle); | |
/*** a new dom object ***/ | |
$dom = new domDocument; | |
/*** load the html into the object ***/ | |
$dom->loadHTML($html); | |
/*** discard white space ***/ | |
$dom->preserveWhiteSpace = false; | |
/*** the table by its tag name ***/ | |
$tables = $dom->getElementsByTagName('table'); | |
/*** get all rows from the table ***/ | |
$rows = $tables->item(4)->getElementsByTagName('tr'); | |
/*** loop over the table rows ***/ | |
foreach ($rows as $row) | |
{ | |
/*** get each column by tag name ***/ | |
$cols = $row->getElementsByTagName('td'); | |
if ($cols->item(3)->nodeValue==$destination){ | |
$zeiten = explode(",", $cols->item(2)->nodeValue); | |
$zeit = explode(":", $zeiten[0]); | |
$jetzt = date('H'); | |
if ($zeit[0]<$jetzt){ $tagesupdate ="+1 day"; } else { $tagesupdate ="now";} | |
$tag = date("d",strtotime($tagesupdate)); | |
$monat = date("m",strtotime($tagesupdate)); | |
$jahr = date("Y",strtotime($tagesupdate)); | |
$nexttrain = mktime($zeit[0], $zeit[1],0,$monat,$tag,$jahr,0); | |
$seconds = ($nexttrain - time()); | |
if ($seconds>($walking+$threshold)) { echo "off";$output = "off"; } | |
if ($seconds>=($walking) AND $seconds<=($walking+$threshold)) { echo "green";$output = "green"; } | |
if ($seconds<($walking)) { echo "red"; $output = "red"; } | |
$count++; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment