-
-
Save awaemmanuel/bc4b3e450a39476c1d1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Adapted from generic web server example as part of IDE created by David Mellis and Tom Igoe. | |
#include "Arduino.h" | |
#include <Ethernet.h> | |
#include <SPI.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define DEBUG false | |
// <1> | |
// Enter a MAC address and IP address for your controller below. | |
// The IP address will be dependent on your local network: | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAE }; | |
byte ip[] = { <PUT YOUR IP HERE AS COMMA BYTES> }; //eg 192,168,0,100 | |
byte gateway[] = { <PUT YOUR GW HERE AS COMMA BYTES}; // eg 192,168,0,1 | |
byte subnet[] = { <PUT YOUR SUBNET HERE>}; //eg 255,255,255,0 | |
// Initialize the Ethernet server library | |
// with the IP address and port you want to use (in this case telnet) | |
EthernetServer server(23); | |
#define BUFFERLENGTH 255 | |
// these are the pins you wire each LED to. | |
#define RED 5 | |
#define GREEN 6 | |
#define BLUE 9 | |
#define GND true // change this to false if 5V type, true if GND type light disc | |
void setup() { | |
Ethernet.begin(mac, ip, gateway, subnet); | |
server.begin(); | |
#ifdef DEBUG | |
Serial.begin(9600); | |
Serial.println("Awaiting connection"); | |
#endif | |
} | |
void loop() { | |
char buffer[BUFFERLENGTH]; | |
int index = 0; | |
// Listen | |
EthernetClient client = server.available(); | |
if (client) { | |
#ifdef DEBUG | |
Serial.println("Got a client"); | |
#endif | |
// reset the input buffer | |
index = 0; | |
while (client.connected()) { | |
if (client.available()){ | |
char c = client.read(); | |
// if it's not a new line then add it to the buffer <2> | |
if (c != '\n' && c != '\r') { | |
buffer[index] = c; | |
index++; | |
if (index > BUFFERLENGTH) index = BUFFERLENGTH -1; | |
continue; | |
} else { | |
buffer[index] = '\0'; | |
} | |
// get the message string for processing | |
String msgstr = String(buffer); | |
// get just the bits we want between the {} | |
msgstr = msgstr.substring(msgstr.lastIndexOf('{')+1, msgstr.indexOf('}', msgstr.lastIndexOf('{'))); | |
msgstr.replace(" ", ""); | |
msgstr.replace("\'", ""); | |
#ifdef DEBUG | |
Serial.println("Message:"); | |
Serial.println(msgstr); | |
#endif | |
// rebuild the buffer with just the URL | |
msgstr.toCharArray(buffer, BUFFERLENGTH); | |
// iterate over the tokens of the message - assumed flat. <3> | |
char *p = buffer; | |
char *str; | |
while ((str = strtok_r(p, ",", &p)) != NULL) { | |
#ifdef DEBUG | |
Serial.println(str); | |
#endif | |
char *tp = str; | |
char *key; char *val; | |
// get the key | |
key = strtok_r(tp, ":", &tp); | |
val = strtok_r(NULL, ":", &tp); | |
#ifdef DEBUG | |
Serial.print("Key: "); | |
Serial.println(key); | |
Serial.print("val: "); | |
Serial.println(val); | |
#endif | |
// <4> | |
if (GND) { | |
if (*key == 'r') analogWrite(RED, atoi(val)); | |
if (*key == 'g') analogWrite(GREEN, atoi(val)); | |
if (*key == 'b') analogWrite(BLUE, atoi(val)); | |
} else { | |
if (*key == 'r') analogWrite(RED, 255-atoi(val)); | |
if (*key == 'g') analogWrite(GREEN, 255-atoi(val)); | |
if (*key == 'b') analogWrite(BLUE, 255-atoi(val)); | |
} | |
} | |
break; | |
} | |
} | |
delay(10); // give client time to send any data back | |
client.stop(); | |
} | |
} |
This file contains hidden or 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
#define RED 5 | |
#define GREEN 6 | |
#define BLUE 9 | |
#define MAX_COLOURS 8 | |
#define GND true // change this to false if 5V type | |
char* colours[] = {"Off", "red", "green", "yellow", "blue", "magenta", "cyan", "white"}; | |
uint8_t current_colour = 0; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Testing lights"); | |
pinMode(RED, OUTPUT); | |
pinMode(GREEN, OUTPUT); | |
pinMode(BLUE, OUTPUT); | |
if (GND) { | |
digitalWrite(RED, LOW); | |
digitalWrite(GREEN, LOW); | |
digitalWrite(BLUE, LOW); | |
} else { | |
digitalWrite(RED, HIGH); | |
digitalWrite(GREEN, HIGH); | |
digitalWrite(BLUE, HIGH); | |
} | |
} | |
void loop () { | |
Serial.print("Current colour: "); | |
Serial.println(colours[current_colour]); | |
if (GND) { | |
digitalWrite(RED, current_colour & 1); | |
digitalWrite(GREEN, current_colour & 2); | |
digitalWrite(BLUE, current_colour & 4); | |
} else { | |
digitalWrite(RED, !(bool)(current_colour & 1)); | |
digitalWrite(GREEN, !(bool)(current_colour & 2)); | |
digitalWrite(BLUE, !(bool)(current_colour & 4)); | |
} | |
if ((++current_colour) >= MAX_COLOURS) current_colour=0; | |
delay(1000); | |
} |
This file contains hidden or 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
#!/usr/bin/python | |
# This script will periodically go and check the weather for a given | |
# location and return the max and min forecasted temperature for the next | |
# 24 hours. | |
# Once this is retrieved it sends a message to a networked arduino in the form | |
# of an RGB colour map in order to control a light showing expected temps. | |
# | |
# Author: Andrew Fisher <ajfisher> | |
# Version: 0.1 | |
from datetime import datetime | |
from telnetlib import Telnet | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
# This is specific to melbourne, change it to your location | |
weather_url = "http://www.accuweather.com/en/au/melbourne/26216/weather-forecast/26216" | |
# details of your arduino on you network. Change to yours | |
arduino_ip = "10.0.1.91" | |
response = urllib2.urlopen(weather_url) | |
# load this up into beautiful soup | |
soup = BeautifulSoup(response.read()) | |
# these IDs were discovered by reading the html and gettng to the | |
# point where the forecast exists. Use the "next" item | |
forecast = soup.find('li', {'id':'feed-sml-1'}) | |
# grab the temps and remove the cruft | |
temp_val = int(forecast.find('strong', {'class':'temp'}).text.replace("°", "")) | |
print("Forecast temp is %d" % temp_val) | |
# convert to colour range | |
if temp_val <= 10: | |
red = 0 | |
blue = 255 | |
green = 20 | |
elif temp_val > 10 and temp_val <=20: | |
red = 128 | |
blue = 0 | |
green = 255 | |
elif temp_val >20 and temp_val <= 30: | |
red = 128 | |
blue = 0 | |
green = 128 | |
elif temp_val > 30 and temp_val <= 40: | |
red = 255 | |
blue = 0 | |
green = 128 | |
else: | |
red = 255 | |
blue = 0 | |
green = 0 | |
colour = { "r": red, "b": blue,"g": green } | |
# Send message to arduino | |
tn = Telnet() | |
tn.open(arduino_ip) | |
tn.write(str(colour)) | |
tn.write("\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment