Skip to content

Instantly share code, notes, and snippets.

@leumund
Created January 4, 2012 06:38
Show Gist options
  • Save leumund/1558813 to your computer and use it in GitHub Desktop.
Save leumund/1558813 to your computer and use it in GitHub Desktop.
#busstop arduino
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment