connecting arduino to yahoo weather using python as an middle ware

17
2011 Author: Manoj.j Reviewer: Ram Revision: 1.2 Tenet Technetronics 09/08/2011 Application Note: AN102 Connecting Arduino to yahoo weather using python as an middle ware

Upload: manoj-ram

Post on 07-Mar-2015

1.018 views

Category:

Documents


2 download

DESCRIPTION

Connecting Arduino to yahoo weather using python as an middle warehttp://tenettech.com/blogspot/

TRANSCRIPT

Page 1: Connecting Arduino to yahoo weather using python as an middle ware

2011

Author: Manoj.j

Reviewer: Ram

Revision: 1.2

Tenet Technetronics

09/08/2011

Application Note: AN102 Connecting Arduino to yahoo weather using python as an middle ware

Page 2: Connecting Arduino to yahoo weather using python as an middle ware

i

Contents

Introduction

Introduction to Connecting Arduino to yahoo weather

Hardware Overview

Schematic

Software Overview

Flow Chart

Pseudo code

Code

Demo Application

References and further reading

Page 3: Connecting Arduino to yahoo weather using python as an middle ware

1

Introduction

Here the Arduino is connected to yahoo weather through python and python is communicating

to Arduino through Pyserial. Pyserial supports two way communications with file handles. You

can read and write strait to serial.

The rough animations are created for different weather condition. The weather

condition is fetched from yahoo weather and data is sent serially, in the other end Arduino

reads the serially available data and plays the animation according to it.

Hardware required

Arduino/Comet board

16x16 led matrix

USB cable

connecting wires

You can buy the products and other electronic components at

www.tenettech.com

Software required

Arduino IDE

Python Emac editor

You can download the Arduino software, Emac editor, python, and pyserial package through

the following links

www.arduino.cc

http://www.gnu.org/software/emacs/

http://python.org/download/

http://linux.softpedia.com/get/Programming/Libraries/pySerial-51222.shtml

Page 4: Connecting Arduino to yahoo weather using python as an middle ware

2

Hardware Overview

Schematic

Fig1: schematic

NOTE: The pin out for shift register

Fig2: shift register pin out

Page 5: Connecting Arduino to yahoo weather using python as an middle ware

3

Software overview

Flowchart

For python: For Arduino:

NO

YES

Send the respective

character serially

Serial

communication and

baud rate

initialization

Fetching the data

from the yahoo

weather

Start

End

While

true

If data

matches

the array

Previous value

Void setup

Serial communication

and the matrix data(hex

array) initialization

Void loop

Read serially

available data

Start

End

Page 6: Connecting Arduino to yahoo weather using python as an middle ware

4

Pseudo code

For python (2):

Include header files for python weather API (PYWAPI), serial and string

Global declaration: For Serial port selection

For setting baud rate.

Loop : While

Description : Infinite loop

Body : Fetching yahoo weather condition continuously

Condition : If

Description : If condition true control enters the body

Body : If there is newly available serial data then compare with the array of

condition data and send the character (a or b or c) serially

For Arduino:

Include header files program space Global declaration : For Hex array to display weather animations Function : Setup

Return type : Void

Description : Initialization

Body : Data, latch, clock pin initialization

Serial communication initialization

Function : Loop

Return type : Void

Description : Infinite loop

Body : Read serially available data

: Frame rate

Page 7: Connecting Arduino to yahoo weather using python as an middle ware

5

Code

PYTHON CODE (1):

This code is to fetch weather report of newyork and print the result of the same --------------------------------------------------------------------------------------------------------------------------

import pywapi // importing python weather API import string yahoo_result = pywapi.get_weather_from_yahoo('10001') // function which fetches weather

report of newyork /* prints the newyork weather condition and temperature */ print "yahoo says: it is " + string.lower(yahoo_result['condition']['text']) + "and" + yahoo_result['condition']['temp'] + "c now in newyork.\n\n" -----------------------------------------------------------------------------------------------------------------------------

PYTHON CODE (2):

This code is to fetch yahoo weather report from Bangalore and compare the result with condition codes provided in yahoo and send the respective character(a or b or c) serially as per the compared result. ----------------------------------------------------------------------------------------------------------------------------- import pywapi // importing python weather API import string // importing string library import serial // importing serial library ser = serial.Serial('/dev/ttyACM0') // serial communication with com port “/dev/ttyACM0'” ser.baudrate = 9600 // serial communication baud rate of 9600 while 1: // infinite loop /* function which fetches weather report from Bangalore */ yahoo_result = pywapi.get_weather_from_yahoo('INXX0012' , 'metric') value = yahoo_result['condition']['text'] print value // print the weather condition

Page 8: Connecting Arduino to yahoo weather using python as an middle ware

6

/* comparing the condition codes with result, if it is true send “a” to serial */ if (("hot" == value) or ("sunny" == value)): ser.write("a") /* comparing the condition codes with result, if it is true send “b” to serial */ if (("windy" == value)) or (“air” == value)): ser.write("b") /* comparing the condition codes with result, if it is true send “c” to serial */ if (("rain" == value) or ("drizzle" == value) or ("freezing drizzle" == value) or ("freezing rain" == value) or ("showers" == value) or ("light rain" == value)) : ser.write("c") /* comparing the condition codes with result, if it is true send “d” to serial */ if (("cloudy" == value) or ("partly cloudy" == value) or ("cold"== value) or ("mostly cloudy" == value)): ser.write("d") /* comparing the condition codes with result, if it is true send “e” to serial */ if (("fair" == value) or ("normal" == value) or ("clear"== value)): ser.write("e") -----------------------------------------------------------------------------------------------------------------------------

ARDUINO CODE:

The below Arduino sketch reads the serially available data and displays the animation with respect

to the obtained data

--------------------------------------------------------------------------------------------------------------------------

#include <avr/pgmspace.h> const int sunny[2][16] PROGMEM = { {0xEBB7,0xD3DA,0xB7E4,0x07F0,0xCE73,0xE5A7,0xF3CF,0xF10F,0xF3CF,0xF1AF,0xE667,0x8FE3,0x0FC8,0xA7DA,0xD3B6,0xDB2F},// Code for {0x6DB7,0x51DB,0xB7E4,0x07F0,0xCE73,0xE5A7,0xF3CF,0xF08F,0xF34F,0xF1CF,0xE7A7,0x8B63,0x0CC8,0xA7DA,0xD3B6,0xDB2D},// Code for !}; }; const int rainy[2][16] PROGMEM = { {0xFFFF,0x9FFF,0x6FBF,0x6FD7,0x73EB,0x7BFD,0x7BD7,0x77EA,0xB3F4,0xB5FE,0xCDFE,0xFDFE,0x7BFF,0xA7FF,0x8FFF,0xFFFF},// Code for "

Page 9: Connecting Arduino to yahoo weather using python as an middle ware

7

{0xFFFF,0x9FDB,0x6FFF,0x6FD5,0x73EE,0x7BFB,0x7BAF,0x77FA,0xB3D6,0xB5FC,0xCDD6,0xFDFE,0x7BAB,0xA7FF,0x8FD7,0xFFFF},// Code for # }; const int windy[2][16] PROGMEM = { {0x3FFC,0xE1FC,0x67C5,0xABCE,0xBDD7,0xDED1,0xEFDD,0xF7FD,0x1886,0xDE9F,0xC7AF,0xF7E3,0xF8FB,0x7FF8,0xBFFF,0xDFFF},// Code for 0x {0xFFE0,0xFFE3,0xFFED,0x07EE,0xF8FF,0x7F8C,0xFF9C,0x7FAD,0x87B7,0xFBBB,0xFDFD,0x0FFE,0xF7C7,0xFBCF,0x1FD0,0xEFFF},// Code for % }; const int cloudy[2][16] PROGMEM = { {0x3FFC,0xE1FC,0x67C5,0xABCE,0xBDD7,0xDED1,0xEFDD,0xF7FD,0x1886,0xDE9F,0xC7AF,0xF7E3,0xF8FB,0x7FF8,0xBFFF,0xDFFF},// Code for 0x {0xFFE0,0xFFE3,0xFFED,0x07EE,0xF8FF,0x7F8C,0xFF9C,0x7FAD,0x87B7,0xFBBB,0xFDFD,0x0FFE,0xF7C7,0xFBCF,0x1FD0,0xEFFF},// Code for % }; const int fair[2][16] PROGMEM = { {0xB6FF,0xC5FF,0x1BF3,0xD9ED,0xA2ED,0x55EE,0x76EF,0x7FEF,0xFFCE,0x7FD6,0xBFD6,0xBFD9,0xBFDF,0x7FEF,0xFFF4,0xFFF1},// Code for ' {0x5BFF,0x86FF,0xD9F3,0x1BED,0xC1ED,0x2AEE,0x6BEF,0x7DEF,0xFFCE,0x7FD6,0xBFD6,0xBFD9,0xBFDF,0x7FEF,0xFFF4,0xFFF1},// Code for ( }; const int XXX[2][16] PROGMEM = { {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF},// Code for 0x {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},// Code for % }; int dataPin = 8; //Pin connected to DS of 74HC595 int latchPin = 9; //Pin connected to ST_CP of 74HC595 int clockPin = 10; //Pin connected to SH_CP of 74HC595 int coloum = 0; int row1 = 0; int dispa=0; // variable to switch the case for different weather condition char incomingByte = 0; // variable to read the serially available data int rdata1 = 0; long previousMillis = 0; // variable for frame rate long interval = 200;

Page 10: Connecting Arduino to yahoo weather using python as an middle ware

8

void setup() { pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); Serial.begin(9600); // baud rate for serial communication } void loop() {

/* to set the frame rate*/ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; // save the last time you blinked the LED rdata1 = rdata1 + 1; if(rdata1 == 2) rdata1 = 0; }

if (Serial.available() > 0) { incomingByte = Serial.read(); // read the incoming byte: if(incomingByte == 'a') dispa = 1; if(incomingByte == 'b') dispa = 2; if(incomingByte == 'c') dispa = 3;

if(incomingByte == 'd') dispa = 4; if(incomingByte == 'e') dispa = 5; } disp(); } // end of void loop void disp() { coloum = 1; for (int i = 0; i < 16; i++) {

Page 11: Connecting Arduino to yahoo weather using python as an middle ware

9

switch (dispa) // switching of case statements for different weather condition { case 1: row1 = pgm_read_word_near(&sunny[rdata1][i]); break; case 2: row1 = pgm_read_word_near(&windy[rdata1][i]); break; case 3: row1 = pgm_read_word_near(&rainy[rdata1][i]); break;

case 4: row1 = pgm_read_word_near(&cloudy[rdata1][i]); break;

case 5: row1 = pgm_read_word_near(&fair[rdata1][i]); break; default: row1 = pgm_read_word_near(&XXX[rdata1][i]); //it’s a blink break; }

digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, lowByte(row1)); shiftOut(dataPin, clockPin, MSBFIRST, highByte(row1)); shiftOut(dataPin, clockPin, MSBFIRST, highByte(coloum)); shiftOut(dataPin, clockPin, MSBFIRST, lowByte(coloum)); digitalWrite(latchPin, HIGH);

coloum = coloum << 1; } } -------------------------------------------------------------------------------------------------------------------------

Page 12: Connecting Arduino to yahoo weather using python as an middle ware

10

Demo Application

16x16 LEDs matrix PCB with four shift register to shift the data

Fig3: 16x16 LEDs matrix

16x16 LED matrix is interfaced to Arduino

Fig4: Arduino with 16x16 LEDs matrix

Page 13: Connecting Arduino to yahoo weather using python as an middle ware

11

Arduino sketch which tells to display animation as per the result obtained serially

Fig5: Arduino sketch

Python code to fetch yahoo weather report and send the result serially

Fig6: python sketch

Page 14: Connecting Arduino to yahoo weather using python as an middle ware

12

Condition codes available from yahoo weather

Fig7: condition codes

Terminal window showing the conditions after fetching the data from yahoo weather

Fig8: result on terminal

Here the terminal showing the atmosphere condition (for this code is not included above)

Page 15: Connecting Arduino to yahoo weather using python as an middle ware

13

Fig9: atmosphere result on terminal

Here the terminal showing the location condition of Bangalore (for this code is not included above)

Fig10: location result on terminal

Page 16: Connecting Arduino to yahoo weather using python as an middle ware

14

Result showing it’s windy

Fig10: result

The results showing sunny, rainy for other places

Fig11: result for other places

Page 17: Connecting Arduino to yahoo weather using python as an middle ware

15

References and Further Reading

http://developer.yahoo.com/weather/ http://pyserial.sourceforge.net/ http://www.arduino.cc/playground/Interfacing/Python http://code.google.com/p/python-weather-api/#Examples http://code.google.com/p/python-weather-api/wiki/Examples http://developer.yahoo.com/weather/#channel http://docs.python.org/tutorial/ http://tenettech.com/blogspot/ http://www.youtube.com/tenetworld

Contact details

Tenet Technetronics,

No 8/14, Third Floor, MN Chambers, P.T.Street, Opp to Pai Vista Convention Hall, Basavangudi, Bangalore -560004, India Ph: 080-42103124 Email: [email protected]