Skip to content

Instantly share code, notes, and snippets.

@biomood
Created September 12, 2011 19:44
Show Gist options
  • Select an option

  • Save biomood/1212185 to your computer and use it in GitHub Desktop.

Select an option

Save biomood/1212185 to your computer and use it in GitHub Desktop.
Client code for Meggy Jr to display a weather icon and temperature from host machine
char one[8][4] = {{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0}};
char two[8][4] = {{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 1}};
char three[8][4] = {{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{1, 1, 1, 1}};
char four[8][4] = {{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1}};
char five[8][4] = {{1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{1, 1, 1, 1}};
char six[8][4] = {{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}};
char seven[8][4] = {{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1}};
char eight[8][4] = {{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}};
char nine[8][4] = {{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1}};
char zero[8][4] = {{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}};
#include <stdlib.h>
#include <MeggyJrSimple.h>
#include "Font.h"
char char_temp[] = {'0', '0'};
int image[64];
int temp_text[64];
int icon_image[64];
int pos = 0;
int temp_pos = 0;
char msg = '0';
char read_mode= 'T';
void display_image();
void display_temp();
void setup() {
MeggyJrSimpleSetup();
Serial.begin(9600);
}
void loop() {
ClearSlate();
while (Serial.available() > 0) {
msg = Serial.read();
if (msg == 'N') {
read_mode = 'N';
pos = 0;
}
else if (msg == 'T') {
read_mode = 'T';
temp_pos = 0;
}
else {
if (read_mode == 'N') {
icon_image[pos] = msg - '0';
pos++;
}
if (read_mode == 'T' && temp_pos < 2) {
char_temp[temp_pos] = msg;
Serial.println(msg);
temp_pos++;
}
}
}
if (read_mode == 'N' && pos == 63) {
int i;
for (i=0; i<64; i++) {
image[i] = icon_image[i];
}
}
else if (read_mode == 'T') {
display_temp();
}
display_image();
DisplaySlate();
}
void display_image() {
int i;
for (i=0; i<8; i++) {
int j;
for (j=0; j<8; j++) {
DrawPx(j, 7-i, image[8*i+j]);
}
}
}
void display_temp() {
switch (char_temp[1]) {
case '0':
display_2nd(zero);
break;
case '1':
display_2nd(one);
break;
case '2':
display_2nd(two);
break;
case '3':
display_2nd(three);
break;
case '4':
display_2nd(four);
break;
case '5':
display_2nd(five);
break;
case '6':
display_2nd(six);
break;
case '7':
display_2nd(seven);
break;
case '8':
display_2nd(eight);
break;
case '9':
display_2nd(nine);
break;
}
switch (char_temp[0]) {
case '0':
display_1st(zero);
break;
case '1':
display_1st(one);
break;
case '2':
display_1st(two);
break;
case '3':
display_1st(three);
break;
case '4':
display_1st(four);
break;
case '5':
display_1st(five);
break;
case '6':
display_1st(six);
break;
case '7':
display_1st(seven);
break;
case '8':
display_1st(eight);
break;
case '9':
display_1st(nine);
break;
}
int i;
for (i=0; i<64; i++) {
image[i] = temp_text[i];
}
}
void display_1st(char figure[8][4]) {
for (int i=0; i<8; i++) {
for (int j=0; j<4; j++) {
temp_text[8*i+j] = figure[i][j];
}
}
}
void display_2nd(char figure[8][4]) {
for (int i=0; i<8; i++) {
for (int j=4; j<8; j++) {
temp_text[8*i+j] = figure[i][j-4];
}
}
}
from xml.dom import minidom
import urllib2
import sys
import time
import serial
send_image = True
ns = 'http://xml.weather.yahoo.com/ns/rss/1.0'
url = 'http://weather.yahooapis.com/forecastrss?w=11090&u=c'
arduino_connection ='/dev/tty.usbserial-FTF3JMMN'
mostlycloudy_night = ['0', '0', '0', '0', '0', '0', '0', '0',
'0', '7', '7', '7', '0', '0', '0', '0',
'7', '7', '7', '7', '7', '0', '0', '0',
'0', '7', '7', '7', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '7', '7', '7', '0',
'0', '0', '0', '7', '7', '7', '7', '7',
'0', '0', '0', '0', '7', '7', '7', '0']
mostlycloudy_day = ['2', '2', '2', '2', '2', '2', '2', '2',
'2', '7', '7', '7', '2', '2', '2', '2',
'7', '7', '7', '7', '7', '2', '2', '2',
'2', '7', '7', '7', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '7', '7', '7', '2',
'2', '2', '2', '7', '7', '7', '7', '7',
'2', '2', '2', '2', '7', '7', '7', '2']
showers = ['7', '5', '7', '7', '7', '5', '7', '7',
'5', '5', '7', '7', '5', '5', '7', '7',
'5', '7', '7', '7', '5', '7', '7', '7',
'7', '7', '5', '7', '7', '7', '5', '7',
'7', '5', '5', '7', '7', '5', '5', '7',
'7', '5', '7', '7', '7', '5', '7', '7',
'7', '7', '7', '5', '7', '7', '7', '5',
'7', '7', '5', '5', '7', '7', '7', '5']
snow = ['0', '7', '0', '0', '0', '0', '0', '0',
'7', '0', '7', '0', '0', '7', '0', '0',
'0', '7', '0', '0', '7', '0', '7', '0',
'0', '0', '0', '0', '0', '7', '0', '0',
'0', '0', '7', '0', '0', '0', '0', '0',
'0', '7', '0', '7', '0', '0', '7', '0',
'0', '0', '7', '0', '0', '7', '0', '7',
'0', '0', '0', '0', '0', '0', '7', '0']
sunny = ['0', '0', '2', '2', '2', '2', '2', '2',
'2', '0', '0', '2', '2', '2', '2', '2',
'0', '0', '0', '2', '2', '2', '2', '2',
'0', '2', '0', '0', '2', '2', '2', '2',
'2', '0', '0', '0', '0', '2', '2', '2',
'0', '0', '0', '2', '0', '0', '0', '2',
'0', '0', '2', '0', '0', '2', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0']
windy = ['0', '0', '4', '4', '4', '4', '4', '0',
'0', '4', '4', '4', '4', '4', '4', '0',
'4', '4', '4', '4', '4', '4', '4', '0',
'0', '0', '0', '0', '0', '8', '8', '0',
'0', '0', '0', '0', '0', '8', '8', '0',
'0', '0', '0', '0', '0', '8', '8', '0',
'0', '0', '0', '0', '0', '8', '8', '0',
'0', '0', '0', '0', '0', '8', '8', '0']
cold = ['0', '0', '0', '5', '5', '0', '0', '0',
'0', '0', '5', '5', '5', '5', '0', '0',
'0', '5', '0', '5', '5', '0', '5', '0',
'5', '5', '5', '5', '5', '5', '5', '5',
'5', '5', '5', '5', '5', '5', '5', '5',
'0', '5', '0', '5', '5', '0', '5', '0',
'0', '0', '5', '5', '5', '5', '0', '0',
'0', '0', '0', '5', '5', '0', '0', '0']
thunderstorm = ['3', '0', '0', '0', '0', '0', '0', '0',
'3', '3', '0', '3', '0', '0', '0', '0',
'0', '3', '0', '3', '0', '0', '0', '0',
'0', '0', '0', '3', '3', '0', '0', '0',
'0', '0', '0', '3', '3', '0', '0', '0',
'0', '0', '0', '0', '3', '0', '3', '0',
'0', '0', '0', '0', '3', '0', '3', '3',
'0', '0', '0', '0', '0', '0', '0', '3']
weather_dict = {}
weather_dict['4'] = thunderstorm
weather_dict['11'] = showers
weather_dict['16'] = snow
weather_dict['24'] = windy
weather_dict['25'] = cold
weather_dict['27'] = mostlycloudy_night
weather_dict['28'] = mostlycloudy_day
weather_dict['29'] = mostlycloudy_night
weather_dict['32'] = sunny
def read_rest_request():
""" returns xml response """
response = urllib2.urlopen(url)
dom = minidom.parse(response)
return dom
def write_image(code):
""" sends weather image to arduino """
image = weather_dict[code]
arduino.write('N')
for x in image:
arduino.write(x)
def write_temperature(temp):
""" sends temp value to arduino """
arduino.write('T')
for i in range(len(temp)):
arduino.write(temp[i])
# try and connect to the arduino
try:
arduino = serial.Serial(arduino_connection, 9600)
time.sleep(2) #give arduino time to reset
except:
sys.exit('Unable to connect to the Arduino')
# request weather every minute, send image to arduino
while True:
try:
dom = read_rest_request()
except urllib2.HTTPError, e:
print 'HTTP error: %d' % e.code
sys.exit('HTTP error')
except urllib2.URLError, e:
print 'Network error: %s' % e.reason.args[1]
sys.exit('Network error occurred')
else:
# retrieve the values we want from the xml response
weather_node = dom.getElementsByTagNameNS(ns, 'forecast')[0]
code = weather_node.getAttribute('code')
temperature = weather_node.getAttribute('high')
print code
print temperature
# send data to arduino
if send_image:
write_image(code)
else:
write_temperature(temperature)
send_image = not send_image
time.sleep(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment