Advertisement
Guest User

Untitled

a guest
Aug 25th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. #import library to do http requests:
  2. import urllib.request
  3. #import pyserial Library
  4. import serial
  5. #import time library for delays
  6. import time
  7.  
  8. #import xml parser called minidom:
  9. from xml.dom.minidom import parseString
  10.  
  11. #Initialize the Serial connection in COM3 or whatever port your arduino uses at 9600 baud rate
  12. ser = serial.Serial("COM11", 9600)
  13. i = 1
  14. #delay for stability while connection is achieved
  15. time.sleep(5)
  16. while i == 1:
  17.      #download the rss file feel free to put your own rss url in here
  18.      file = urllib.request.urlopen('http://rss.cbc.ca/lineup/canada.xml')
  19.      #convert to string
  20.      data = file.read()
  21.      #close the file
  22.      file.close()
  23.      #parse the xml from the string
  24.      dom = parseString(data)
  25.      #retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName change tags to get different data
  26.      xmlTag = dom.getElementsByTagName('title')[2].toxml()
  27.      # the [2] indicates the 3rd title tag it finds will be parsed, counting starts at 0
  28.      #strip off the tag (<tag>data</tag>  --->   data)
  29.      xmlData=xmlTag.replace('<title>','').replace('</title>','')
  30.      #write the marker ~ to serial
  31.      #ser.write(bytes('~', encoding = 'ascii'))
  32.      time.sleep(5)
  33.      #split the string into individual words
  34.      nums = xmlData.split(' ')
  35.      #loop until all words in string have been printed
  36.      for num in nums:
  37.           #write 1 word
  38.           ser.write(bytes(num.upper(), encoding='ascii'))
  39.           # write 1 space
  40.           ser.write(bytes(' ',encoding='ascii'))
  41.           # THE DELAY IS NECESSARY. It prevents overflow of the arduino buffer.
  42.           time.sleep(2)
  43.          
  44.      # write ~ to close the string and tell arduino information sending is finished
  45.      #ser.write(bytes('~', encoding = 'ascii'))
  46.  
  47.      # wait 5 minutes before rechecking RSS and resending data to Arduino
  48.      time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement