Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import library to do http requests:
- import urllib.request
- #import pyserial Library
- import serial
- #import time library for delays
- import time
- #import xml parser called minidom:
- from xml.dom.minidom import parseString
- #Initialize the Serial connection in COM3 or whatever port your arduino uses at 9600 baud rate
- ser = serial.Serial("COM11", 9600)
- i = 1
- #delay for stability while connection is achieved
- time.sleep(5)
- while i == 1:
- #download the rss file feel free to put your own rss url in here
- file = urllib.request.urlopen('http://rss.cbc.ca/lineup/canada.xml')
- #convert to string
- data = file.read()
- #close the file
- file.close()
- #parse the xml from the string
- dom = parseString(data)
- #retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName change tags to get different data
- xmlTag = dom.getElementsByTagName('title')[2].toxml()
- # the [2] indicates the 3rd title tag it finds will be parsed, counting starts at 0
- #strip off the tag (<tag>data</tag> ---> data)
- xmlData=xmlTag.replace('<title>','').replace('</title>','')
- #write the marker ~ to serial
- #ser.write(bytes('~', encoding = 'ascii'))
- time.sleep(5)
- #split the string into individual words
- nums = xmlData.split(' ')
- #loop until all words in string have been printed
- for num in nums:
- #write 1 word
- ser.write(bytes(num.upper(), encoding='ascii'))
- # write 1 space
- ser.write(bytes(' ',encoding='ascii'))
- # THE DELAY IS NECESSARY. It prevents overflow of the arduino buffer.
- time.sleep(2)
- # write ~ to close the string and tell arduino information sending is finished
- #ser.write(bytes('~', encoding = 'ascii'))
- # wait 5 minutes before rechecking RSS and resending data to Arduino
- time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement