Advertisement
AverageMan

Average Man's Radio Code

Oct 1st, 2013
2,863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.23 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Radio code by Average Man (AverageManVsRaspberryPi.Blogspot.com)
  4. #
  5. # You'll need to map your buttons with the same GPIO pins as the code here
  6. # You'll also need to use 'mpc add' to add stations to your playlist
  7. # You need an IFTTT account and recipe to make the evernote/tweet buttons work (see my blog)
  8. # If you have different stations to me (Indie etc), you'll need to change the names in the code
  9. # Add a comment to my blog if you get stuck (AverageManVsRaspberryPi.Blogspot.com)
  10. #
  11. # Probably too much here to work out easily without a tutorial - once I complete my Plywood case I'll do a FULL tutorial.
  12.  
  13.  
  14.  
  15. # The wiring for the LCD is as follows:
  16. # 1 : GND
  17. # 2 : 5V
  18. # 3 : Contrast (0-5V)*
  19. # 4 : RS (Register Select)
  20. # 5 : R/W (Read Write)       - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
  21. # 6 : Enable or Strobe
  22. # 7 : Data Bit 0             - NOT USED
  23. # 8 : Data Bit 1             - NOT USED
  24. # 9 : Data Bit 2             - NOT USED
  25. # 10: Data Bit 3             - NOT USED
  26. # 11: Data Bit 4
  27. # 12: Data Bit 5
  28. # 13: Data Bit 6
  29. # 14: Data Bit 7
  30. # 15: LCD Backlight +5V
  31. # 16: LCD Backlight GND
  32.  
  33.  
  34. #import
  35. import RPi.GPIO as GPIO
  36. import time
  37. import os
  38. import subprocess
  39. import socket
  40. import fcntl
  41. import struct
  42. import datetime
  43. import smtplib
  44. from email.mime.text import MIMEText
  45.  
  46. # Define GPIO to LCD mapping
  47. LCD_RS = 17
  48. LCD_E  = 18
  49. LCD_D4 = 27
  50. LCD_D5 = 22
  51. LCD_D6 = 23
  52. LCD_D7 = 24
  53.  
  54. # Define GPIO for button Controls
  55.  
  56. OFF = 25
  57. VOLDWN = 4
  58. VOLUP = 10
  59. PREV = 9
  60. PAUSE =11
  61. PLAY = 8
  62. NEXT = 7
  63. IPSHOW = 14
  64.  
  65. # Define some device constants
  66. LCD_WIDTH = 16    # Maximum characters per line
  67. LCD_CHR = True
  68. LCD_CMD = False
  69.  
  70. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  71. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  72.  
  73. # Timing constants
  74. E_PULSE = 0.00005
  75. E_DELAY = 0.00005
  76.  
  77. def main():
  78.   GPIO.cleanup()
  79.   GPIO.setwarnings(False)
  80.   GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  81.   GPIO.setup(LCD_E, GPIO.OUT)  # E
  82.   GPIO.setup(LCD_RS, GPIO.OUT) # RS
  83.   GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  84.   GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  85.   GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  86.   GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  87.  
  88.   GPIO.setup(NEXT, GPIO.IN) # Next Channel button
  89.   GPIO.setup(PAUSE, GPIO.IN) # Stop button
  90.   GPIO.setup(PLAY, GPIO.IN) # Start button
  91.   GPIO.setup(PREV, GPIO.IN) # Previous Channel button
  92.   GPIO.setup(OFF, GPIO.IN) # OFF button
  93.   GPIO.setup(VOLDWN, GPIO.IN) # Volume down button
  94.   GPIO.setup(VOLUP, GPIO.IN) # Volume up button
  95.   GPIO.setup(IPSHOW, GPIO.IN) # Show IP button
  96.  
  97.   # Initialise display
  98.   lcd_init()
  99.  
  100.   # Send some test
  101.   lcd_byte(LCD_LINE_1, LCD_CMD)
  102.   lcd_string("Average Man's",2)
  103.   lcd_byte(LCD_LINE_2, LCD_CMD)
  104.   lcd_string("NERDBOX!",2)
  105.   time.sleep(1)
  106.   menu()
  107.  
  108.  
  109.  
  110. def menu():
  111.   timelastchecked = 0
  112.   time.sleep(0.5)
  113.   while(1):
  114.    if time.time() >= timelastchecked:
  115.     timelastchecked = time.time()+3
  116.     mystring = ""
  117.     mytime = ""
  118.     mytemp = ""
  119.     pretemp = "NBX ["
  120.     posttemp = "] "
  121.     f=os.popen("date")
  122.     for i in f.readlines():
  123.      mytime += i
  124.      mytime = mytime[11:-13]
  125.      f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  126.      for i in f.readlines():
  127.       mytemp += i
  128.       mytemp = mytemp[5:-3]
  129.       mystring = pretemp + mytemp + posttemp + mytime
  130.       lcd_byte(LCD_LINE_1, LCD_CMD)
  131.       lcd_string(mystring,1)
  132.       lcd_byte(LCD_LINE_2, LCD_CMD)
  133.       lcd_string("< Off     Menu >",2)
  134.    else:
  135.     if ( GPIO.input(NEXT) == False):
  136.      menu1()
  137.     if ( GPIO.input(PREV) == False):
  138.      off()
  139.    
  140. def menu1(): #iRadio
  141.   timelastchecked = 0
  142.   time.sleep(0.5)
  143.   while(1):
  144.    if time.time() >= timelastchecked:
  145.     timelastchecked = time.time()+3
  146.     mystring = ""
  147.     mytime = ""
  148.     mytemp = ""
  149.     pretemp = "NBX ["
  150.     posttemp = "] "
  151.     f=os.popen("date")
  152.     for i in f.readlines():
  153.      mytime += i
  154.      mytime = mytime[11:-13]
  155.      f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  156.      for i in f.readlines():
  157.       mytemp += i
  158.       mytemp = mytemp[5:-3]
  159.       mystring = pretemp + mytemp + posttemp + mytime
  160.       lcd_byte(LCD_LINE_1, LCD_CMD)
  161.       lcd_string(mystring,1)
  162.       lcd_byte(LCD_LINE_2, LCD_CMD)
  163.       lcd_string("[GO]  < iRadio >",2)
  164.    else:
  165.     if ( GPIO.input(PREV) == False):
  166.      chooseradio()
  167.     if ( GPIO.input(PLAY) == False):
  168.      menu()
  169.     if ( GPIO.input(NEXT) == False):
  170.      menu2()
  171.  
  172.  
  173. def menu2(): #IP
  174.   timelastchecked = 0
  175.   time.sleep(0.5)
  176.   while(1):
  177.    if time.time() >= timelastchecked:
  178.     timelastchecked = time.time()+3
  179.     mystring = ""
  180.     mytime = ""
  181.     mytemp = ""
  182.     pretemp = "NBX ["
  183.     posttemp = "] "
  184.     f=os.popen("date")
  185.     for i in f.readlines():
  186.      mytime += i
  187.      mytime = mytime[11:-13]
  188.      f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  189.      for i in f.readlines():
  190.       mytemp += i
  191.       mytemp = mytemp[5:-3]
  192.       mystring = pretemp + mytemp + posttemp + mytime
  193.       lcd_byte(LCD_LINE_1, LCD_CMD)
  194.       lcd_string(mystring,1)
  195.       lcd_byte(LCD_LINE_2, LCD_CMD)
  196.       lcd_string("[GO]      < IP >",2)
  197.    else:
  198.     if ( GPIO.input(PREV) == False):
  199.      ip()
  200.     if ( GPIO.input(PLAY) == False):
  201.      menu1()
  202.     if ( GPIO.input(NEXT) == False):
  203.      menu3()
  204.      
  205. def menu3():#Tweet
  206.   timelastchecked = 0
  207.   time.sleep(0.5)
  208.   while(1):
  209.    if time.time() >= timelastchecked:
  210.     timelastchecked = time.time()+3
  211.     mystring = ""
  212.     mytime = ""
  213.     mytemp = ""
  214.     pretemp = "NBX ["
  215.     posttemp = "] "
  216.     f=os.popen("date")
  217.     for i in f.readlines():
  218.      mytime += i
  219.      mytime = mytime[11:-13]
  220.      f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  221.      for i in f.readlines():
  222.       mytemp += i
  223.       mytemp = mytemp[5:-3]
  224.       mystring = pretemp + mytemp + posttemp + mytime
  225.       lcd_byte(LCD_LINE_1, LCD_CMD)
  226.       lcd_string(mystring,1)
  227.       lcd_byte(LCD_LINE_2, LCD_CMD)
  228.       lcd_string("[GO]    < Tweet ",2)
  229.    else:
  230.     if ( GPIO.input(PREV) == False):
  231.      tweetemail()
  232.     if ( GPIO.input(PLAY) == False):
  233.      menu2()
  234.      
  235. def ip():
  236.   timelastchecked = 0
  237.   time.sleep(0.5)
  238.   while(1):
  239.    if time.time() >= timelastchecked:
  240.     timelastchecked = time.time()+3
  241.     mystring = ""
  242.     mytime = ""
  243.     mytemp = ""
  244.     pretemp = "NBX ["
  245.     posttemp = "] "
  246.     f=os.popen("date")
  247.     for i in f.readlines():
  248.      mytime += i
  249.      mytime = mytime[11:-13]
  250.      f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  251.      for i in f.readlines():
  252.       mytemp += i
  253.       mytemp = mytemp[5:-3]
  254.       mystring = pretemp + mytemp + posttemp + mytime
  255.       lcd_byte(LCD_LINE_1, LCD_CMD)
  256.       lcd_string(mystring,1)
  257.       preIP = "IP "
  258.       address = get_ip_address('wlan0')
  259.       address = preIP + address
  260.       lcd_byte(LCD_LINE_2, LCD_CMD)
  261.       lcd_string(address,1)
  262.       time.sleep(2.5)
  263.       menu2()
  264.  
  265. def station1():
  266.   os.system("mpc play 1")
  267.   time.sleep(0.4)
  268.   while(1):
  269.    f=os.popen("date")
  270.    for i in f.readlines():
  271.     mytime = ""
  272.     pretime = "<Country>  "
  273.     mytime += i
  274.     mytime = mytime[11:-13]
  275.     mytime = pretime + mytime
  276.     lcd_byte(LCD_LINE_1, LCD_CMD)
  277.     lcd_string(mytime,1)
  278.     f=os.popen("mpc current")
  279.     for i in f.readlines():
  280.      station = ""
  281.      station += i
  282.      str_pad = " " * 16
  283.      station = station[84:-1]
  284.      station = str_pad + station
  285.      for i in range (0, len(station)):  
  286.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  287.       lcd_text = station[i:(i+16)]
  288.       lcd_string(lcd_text,1)  
  289.       time.sleep(0.2)  
  290.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  291.       lcd_string(str_pad,1)
  292.       if ( GPIO.input(IPSHOW) == True):
  293.        evernote()
  294.       if ( GPIO.input(PREV) == False):
  295.        station12()
  296.       if ( GPIO.input(PAUSE) == False):
  297.        station2()
  298.       if ( GPIO.input(NEXT) == False):
  299.        os.system("mpc stop")
  300.        menu1()
  301.        
  302. def station2():
  303.   os.system("mpc play 2")
  304.   time.sleep(0.4)
  305.   while(1):
  306.    f=os.popen("date")
  307.    for i in f.readlines():
  308.     mytime = ""
  309.     pretime = "<Covers>   "
  310.     mytime += i
  311.     mytime = mytime[11:-13]
  312.     mytime = pretime + mytime
  313.     lcd_byte(LCD_LINE_1, LCD_CMD)
  314.     lcd_string(mytime,1)
  315.     f=os.popen("mpc current")
  316.     for i in f.readlines():
  317.      station = ""
  318.      station += i
  319.      str_pad = " " * 16
  320.      station = station[46:-1]
  321.      station = str_pad + station
  322.      for i in range (0, len(station)):  
  323.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  324.       lcd_text = station[i:(i+16)]
  325.       lcd_string(lcd_text,1)  
  326.       time.sleep(0.2)  
  327.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  328.       lcd_string(str_pad,1)
  329.       if ( GPIO.input(IPSHOW) == True):
  330.        evernote()
  331.       if ( GPIO.input(PREV) == False):
  332.        station1()
  333.       if ( GPIO.input(PAUSE) == False):
  334.        station3()
  335.       if ( GPIO.input(NEXT) == False):
  336.        os.system("mpc stop")
  337.        menu1()
  338.  
  339. def station3():
  340.   os.system("mpc play 3")
  341.   time.sleep(0.4)
  342.   while(1):
  343.    f=os.popen("date")
  344.    for i in f.readlines():
  345.     mytime = ""
  346.     pretime = "<Beat>     "
  347.     mytime += i
  348.     mytime = mytime[11:-13]
  349.     mytime = pretime + mytime
  350.     lcd_byte(LCD_LINE_1, LCD_CMD)
  351.     lcd_string(mytime,1)
  352.     f=os.popen("mpc current")
  353.     for i in f.readlines():
  354.      station = ""
  355.      station += i
  356.      str_pad = " " * 16
  357.      station = station[76:-1]
  358.      station = str_pad + station
  359.      for i in range (0, len(station)):  
  360.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  361.       lcd_text = station[i:(i+16)]
  362.       lcd_string(lcd_text,1)  
  363.       time.sleep(0.2)  
  364.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  365.       lcd_string(str_pad,1)
  366.       if ( GPIO.input(IPSHOW) == True):
  367.        evernote()
  368.       if ( GPIO.input(PREV) == False):
  369.        station2()
  370.       if ( GPIO.input(PAUSE) == False):
  371.        station4()
  372.       if ( GPIO.input(NEXT) == False):
  373.        os.system("mpc stop")
  374.        menu1()
  375.        
  376. def station4():
  377.   os.system("mpc play 4")
  378.   time.sleep(0.4)
  379.   while(1):
  380.    f=os.popen("date")
  381.    for i in f.readlines():
  382.     mytime = ""
  383.     pretime = "<Indie>    "
  384.     mytime += i
  385.     mytime = mytime[11:-13]
  386.     mytime = pretime + mytime
  387.     lcd_byte(LCD_LINE_1, LCD_CMD)
  388.     lcd_string(mytime,1)
  389.     f=os.popen("mpc current")
  390.     for i in f.readlines():
  391.      station = ""
  392.      station += i
  393.      str_pad = " " * 16
  394.      station = station[44:-1]
  395.      station = str_pad + station
  396.      for i in range (0, len(station)):  
  397.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  398.       lcd_text = station[i:(i+16)]
  399.       lcd_string(lcd_text,1)  
  400.       time.sleep(0.2)  
  401.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  402.       lcd_string(str_pad,1)
  403.       if ( GPIO.input(IPSHOW) == True):
  404.        evernote()
  405.       if ( GPIO.input(PREV) == False):
  406.        station3()
  407.       if ( GPIO.input(PAUSE) == False):
  408.        station5()
  409.       if ( GPIO.input(NEXT) == False):
  410.        os.system("mpc stop")
  411.        menu1()
  412.        
  413. def station5():
  414.   os.system("mpc play 5")
  415.   time.sleep(0.4)
  416.   while(1):
  417.    f=os.popen("date")
  418.    for i in f.readlines():
  419.     mytime = ""
  420.     pretime = "<80's>     "
  421.     mytime += i
  422.     mytime = mytime[11:-13]
  423.     mytime = pretime + mytime
  424.     lcd_byte(LCD_LINE_1, LCD_CMD)
  425.     lcd_string(mytime,1)
  426.     f=os.popen("mpc current")
  427.     for i in f.readlines():
  428.      station = ""
  429.      station += i
  430.      str_pad = " " * 16
  431.      station = station[66:-1]
  432.      station = str_pad + station
  433.      for i in range (0, len(station)):  
  434.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  435.       lcd_text = station[i:(i+16)]
  436.       lcd_string(lcd_text,1)  
  437.       time.sleep(0.2)  
  438.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  439.       lcd_string(str_pad,1)
  440.       if ( GPIO.input(IPSHOW) == True):
  441.        evernote()
  442.       if ( GPIO.input(PREV) == False):
  443.        station4()
  444.       if ( GPIO.input(PAUSE) == False):
  445.        station6()
  446.       if ( GPIO.input(NEXT) == False):
  447.        os.system("mpc stop")
  448.        menu1()
  449.        
  450. def station6():
  451.   os.system("mpc play 6")
  452.   time.sleep(0.4)
  453.   while(1):
  454.    f=os.popen("date")
  455.    for i in f.readlines():
  456.     mytime = ""
  457.     pretime = "<Groove>   "
  458.     mytime += i
  459.     mytime = mytime[11:-13]
  460.     mytime = pretime + mytime
  461.     lcd_byte(LCD_LINE_1, LCD_CMD)
  462.     lcd_string(mytime,1)
  463.     f=os.popen("mpc current")
  464.     for i in f.readlines():
  465.      station = ""
  466.      station += i
  467.      str_pad = " " * 16
  468.      station = station[77:-1]
  469.      station = str_pad + station
  470.      for i in range (0, len(station)):  
  471.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  472.       lcd_text = station[i:(i+16)]
  473.       lcd_string(lcd_text,1)  
  474.       time.sleep(0.2)  
  475.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  476.       lcd_string(str_pad,1)
  477.       if ( GPIO.input(IPSHOW) == True):
  478.        evernote()
  479.       if ( GPIO.input(PREV) == False):
  480.        station5()
  481.       if ( GPIO.input(PAUSE) == False):
  482.        station7()
  483.       if ( GPIO.input(NEXT) == False):
  484.        os.system("mpc stop")
  485.        menu1()
  486.        
  487. def station7():
  488.   os.system("mpc play 7")
  489.   time.sleep(0.4)
  490.   while(1):
  491.    f=os.popen("date")
  492.    for i in f.readlines():
  493.     mytime = ""
  494.     pretime = "<LushFM>   "
  495.     mytime += i
  496.     mytime = mytime[11:-13]
  497.     mytime = pretime + mytime
  498.     lcd_byte(LCD_LINE_1, LCD_CMD)
  499.     lcd_string(mytime,1)
  500.     f=os.popen("mpc current")
  501.     for i in f.readlines():
  502.      station = ""
  503.      station += i
  504.      str_pad = " " * 16
  505.      station = station[67:-1]
  506.      station = str_pad + station
  507.      for i in range (0, len(station)):  
  508.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  509.       lcd_text = station[i:(i+16)]
  510.       lcd_string(lcd_text,1)  
  511.       time.sleep(0.2)  
  512.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  513.       lcd_string(str_pad,1)
  514.       if ( GPIO.input(IPSHOW) == True):
  515.        evernote()
  516.       if ( GPIO.input(PREV) == False):
  517.        station6()
  518.       if ( GPIO.input(PAUSE) == False):
  519.        station8()
  520.       if ( GPIO.input(NEXT) == False):
  521.        os.system("mpc stop")
  522.        menu1()
  523.        
  524. def station8():
  525.   os.system("mpc play 8")
  526.   time.sleep(0.4)
  527.   while(1):
  528.    f=os.popen("date")
  529.    for i in f.readlines():
  530.     mytime = ""
  531.     pretime = "<DubStep>  "
  532.     mytime += i
  533.     mytime = mytime[11:-13]
  534.     mytime = pretime + mytime
  535.     lcd_byte(LCD_LINE_1, LCD_CMD)
  536.     lcd_string(mytime,1)
  537.     f=os.popen("mpc current")
  538.     for i in f.readlines():
  539.      station = ""
  540.      station += i
  541.      str_pad = " " * 16
  542.      station = station[91:-1]
  543.      station = str_pad + station
  544.      for i in range (0, len(station)):  
  545.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  546.       lcd_text = station[i:(i+16)]
  547.       lcd_string(lcd_text,1)  
  548.       time.sleep(0.2)  
  549.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  550.       lcd_string(str_pad,1)
  551.       if ( GPIO.input(IPSHOW) == True):
  552.        evernote()
  553.       if ( GPIO.input(PREV) == False):
  554.        station7()
  555.       if ( GPIO.input(PAUSE) == False):
  556.        station9()
  557.       if ( GPIO.input(NEXT) == False):
  558.        os.system("mpc stop")
  559.        menu1()
  560.        
  561. def station9():
  562.   os.system("mpc play 9")
  563.   time.sleep(0.4)
  564.   while(1):
  565.    f=os.popen("date")
  566.    for i in f.readlines():
  567.     mytime = ""
  568.     pretime = "<Jazz>     "
  569.     mytime += i
  570.     mytime = mytime[11:-13]
  571.     mytime = pretime + mytime
  572.     lcd_byte(LCD_LINE_1, LCD_CMD)
  573.     lcd_string(mytime,1)
  574.     f=os.popen("mpc current")
  575.     for i in f.readlines():
  576.      station = ""
  577.      station += i
  578.      str_pad = " " * 16
  579.      station = station[15:-1]
  580.      station = str_pad + station
  581.      for i in range (0, len(station)):  
  582.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  583.       lcd_text = station[i:(i+16)]
  584.       lcd_string(lcd_text,1)  
  585.       time.sleep(0.2)  
  586.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  587.       lcd_string(str_pad,1)
  588.       if ( GPIO.input(IPSHOW) == True):
  589.        evernote()
  590.       if ( GPIO.input(PREV) == False):
  591.        station8()
  592.       if ( GPIO.input(PAUSE) == False):
  593.        station10()
  594.       if ( GPIO.input(NEXT) == False):
  595.        os.system("mpc stop")
  596.        menu1()
  597.        
  598. def station10():
  599.   os.system("mpc play 10")
  600.   time.sleep(0.4)
  601.   while(1):
  602.    f=os.popen("date")
  603.    for i in f.readlines():
  604.     mytime = ""
  605.     pretime = "<BMarley>  "
  606.     mytime += i
  607.     mytime = mytime[11:-13]
  608.     mytime = pretime + mytime
  609.     lcd_byte(LCD_LINE_1, LCD_CMD)
  610.     lcd_string(mytime,1)
  611.     f=os.popen("mpc current")
  612.     for i in f.readlines():
  613.      station = ""
  614.      station += i
  615.      str_pad = " " * 16
  616.      station = station[11:-1]
  617.      station = str_pad + station
  618.      for i in range (0, len(station)):  
  619.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  620.       lcd_text = station[i:(i+16)]
  621.       lcd_string(lcd_text,1)  
  622.       time.sleep(0.2)  
  623.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  624.       lcd_string(str_pad,1)
  625.       if ( GPIO.input(IPSHOW) == True):
  626.        evernote()
  627.       if ( GPIO.input(PREV) == False):
  628.        station9()
  629.       if ( GPIO.input(PAUSE) == False):
  630.        station11()
  631.       if ( GPIO.input(NEXT) == False):
  632.        os.system("mpc stop")
  633.        menu1()
  634.        
  635. def station11():
  636.   os.system("mpc play 11")
  637.   time.sleep(0.4)
  638.   while(1):
  639.    f=os.popen("date")
  640.    for i in f.readlines():
  641.     mytime = ""
  642.     pretime = "<SlowJam>  "
  643.     mytime += i
  644.     mytime = mytime[11:-13]
  645.     mytime = pretime + mytime
  646.     lcd_byte(LCD_LINE_1, LCD_CMD)
  647.     lcd_string(mytime,1)
  648.     f=os.popen("mpc current")
  649.     for i in f.readlines():
  650.      station = ""
  651.      station += i
  652.      str_pad = " " * 16
  653.      station = station[9:-1]
  654.      station = str_pad + station
  655.      for i in range (0, len(station)):  
  656.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  657.       lcd_text = station[i:(i+16)]
  658.       lcd_string(lcd_text,1)  
  659.       time.sleep(0.2)  
  660.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  661.       lcd_string(str_pad,1)
  662.       if ( GPIO.input(IPSHOW) == True):
  663.        evernote()
  664.       if ( GPIO.input(PREV) == False):
  665.        station10()
  666.       if ( GPIO.input(PAUSE) == False):
  667.        station12()
  668.       if ( GPIO.input(NEXT) == False):
  669.        os.system("mpc stop")
  670.        menu1()
  671.        
  672. def station12():
  673.   os.system("mpc play 12")
  674.   time.sleep(0.4)
  675.   while(1):
  676.    f=os.popen("date")
  677.    for i in f.readlines():
  678.     mytime = ""
  679.     pretime = "<CDelMar>  "
  680.     mytime += i
  681.     mytime = mytime[11:-13]
  682.     mytime = pretime + mytime
  683.     lcd_byte(LCD_LINE_1, LCD_CMD)
  684.     lcd_string(mytime,1)
  685.     f=os.popen("mpc current")
  686.     for i in f.readlines():
  687.      station = ""
  688.      station += i
  689.      str_pad = " " * 16
  690.      station = station[13:-1]
  691.      station = str_pad + station
  692.      for i in range (0, len(station)):  
  693.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  694.       lcd_text = station[i:(i+16)]
  695.       lcd_string(lcd_text,1)  
  696.       time.sleep(0.2)  
  697.       lcd_byte(LCD_LINE_2, LCD_CMD)  
  698.       lcd_string(str_pad,1)
  699.       if ( GPIO.input(IPSHOW) == True):
  700.        evernote()
  701.       if ( GPIO.input(PREV) == False):
  702.        station11()
  703.       if ( GPIO.input(PAUSE) == False):
  704.        station1()
  705.       if ( GPIO.input(NEXT) == False):
  706.        os.system("mpc stop")
  707.        menu1()
  708.  
  709. def chooseradio():
  710.   time.sleep(0.5)
  711.   while(1):
  712.    if ( GPIO.input(PREV) == False):
  713.     station1()
  714.    if ( GPIO.input(PLAY) == False):
  715.     choose12()
  716.    if ( GPIO.input(NEXT) == False):
  717.     choose2()
  718.    else:
  719.     lcd_byte(LCD_LINE_1, LCD_CMD)
  720.     lcd_string(" Choose Station ",2)
  721.     lcd_byte(LCD_LINE_2, LCD_CMD)
  722.     lcd_string("[GO] < Country >",2)
  723.    
  724.    
  725.  
  726. def choose2():
  727.   time.sleep(0.5)
  728.   while(1):
  729.    if ( GPIO.input(PREV) == False):
  730.     station2()
  731.    if ( GPIO.input(PLAY) == False):
  732.     chooseradio()
  733.    if ( GPIO.input(NEXT) == False):
  734.     choose3()
  735.    else:
  736.     lcd_byte(LCD_LINE_1, LCD_CMD)
  737.     lcd_string(" Choose Station ",2)
  738.     lcd_byte(LCD_LINE_2, LCD_CMD)
  739.     lcd_string("[GO]  < Covers >",2)
  740.    
  741. def choose3():
  742.   time.sleep(0.5)
  743.   while(1):
  744.    if ( GPIO.input(PREV) == False):
  745.     station3()
  746.    if ( GPIO.input(PLAY) == False):
  747.     choose2()
  748.    if ( GPIO.input(NEXT) == False):
  749.     choose4()
  750.    else:
  751.     lcd_byte(LCD_LINE_1, LCD_CMD)
  752.     lcd_string(" Choose Station ",2)
  753.     lcd_byte(LCD_LINE_2, LCD_CMD)
  754.     lcd_string("[GO]    < Beat >",2)
  755.    
  756. def choose4():
  757.   time.sleep(0.5)
  758.   while(1):
  759.    if ( GPIO.input(PREV) == False):
  760.     station4()
  761.    if ( GPIO.input(PLAY) == False):
  762.     choose3()
  763.    if ( GPIO.input(NEXT) == False):
  764.     choose5()
  765.    else:
  766.     lcd_byte(LCD_LINE_1, LCD_CMD)
  767.     lcd_string(" Choose Station ",2)
  768.     lcd_byte(LCD_LINE_2, LCD_CMD)
  769.     lcd_string("[GO]   < Indie >",2)
  770.    
  771. def choose5():
  772.   time.sleep(0.5)
  773.   while(1):
  774.    if ( GPIO.input(PREV) == False):
  775.     station5()
  776.    if ( GPIO.input(PLAY) == False):
  777.     choose4()
  778.    if ( GPIO.input(NEXT) == False):
  779.     choose6()
  780.    else:
  781.     lcd_byte(LCD_LINE_1, LCD_CMD)
  782.     lcd_string(" Choose Station ",2)
  783.     lcd_byte(LCD_LINE_2, LCD_CMD)
  784.     lcd_string("[GO]    < 80's >",2)
  785.  
  786. def choose6():
  787.   time.sleep(0.5)
  788.   while(1):
  789.    if ( GPIO.input(PREV) == False):
  790.     station6()
  791.    if ( GPIO.input(PLAY) == False):
  792.     choose5()
  793.    if ( GPIO.input(NEXT) == False):
  794.     choose7()
  795.    else:
  796.     lcd_byte(LCD_LINE_1, LCD_CMD)
  797.     lcd_string(" Choose Station ",2)
  798.     lcd_byte(LCD_LINE_2, LCD_CMD)
  799.     lcd_string("[GO]  < Groove >",2)
  800.    
  801. def choose7():
  802.   time.sleep(0.5)
  803.   while(1):
  804.    if ( GPIO.input(PREV) == False):
  805.     station7()
  806.    if ( GPIO.input(PLAY) == False):
  807.     choose6()
  808.    if ( GPIO.input(NEXT) == False):
  809.     choose8()
  810.    else:
  811.     lcd_byte(LCD_LINE_1, LCD_CMD)
  812.     lcd_string(" Choose Station ",2)
  813.     lcd_byte(LCD_LINE_2, LCD_CMD)
  814.     lcd_string("[GO]  < LushFM >",2)
  815.        
  816. def choose8():
  817.   time.sleep(0.5)
  818.   while(1):
  819.    if ( GPIO.input(PREV) == False):
  820.     station8()
  821.    if ( GPIO.input(PLAY) == False):
  822.     choose7()
  823.    if ( GPIO.input(NEXT) == False):
  824.     choose9()
  825.    else:
  826.     lcd_byte(LCD_LINE_1, LCD_CMD)
  827.     lcd_string(" Choose Station ",2)
  828.     lcd_byte(LCD_LINE_2, LCD_CMD)
  829.     lcd_string("[GO] < DubStep >",2)
  830.    
  831. def choose9():
  832.   time.sleep(0.5)
  833.   while(1):
  834.    if ( GPIO.input(PREV) == False):
  835.     station9()
  836.    if ( GPIO.input(PLAY) == False):
  837.     choose8()
  838.    if ( GPIO.input(NEXT) == False):
  839.     choose10()
  840.    else:
  841.     lcd_byte(LCD_LINE_1, LCD_CMD)
  842.     lcd_string(" Choose Station ",2)
  843.     lcd_byte(LCD_LINE_2, LCD_CMD)
  844.     lcd_string("[GO]    < Jazz >",2)
  845.    
  846. def choose10():
  847.   time.sleep(0.5)
  848.   while(1):
  849.    if ( GPIO.input(PREV) == False):
  850.     station10()
  851.    if ( GPIO.input(PLAY) == False):
  852.     choose9()
  853.    if ( GPIO.input(NEXT) == False):
  854.     choose11()
  855.    else:
  856.     lcd_byte(LCD_LINE_1, LCD_CMD)
  857.     lcd_string(" Choose Station ",2)
  858.     lcd_byte(LCD_LINE_2, LCD_CMD)
  859.     lcd_string("[GO] < BMarley >",2)
  860.    
  861. def choose11():
  862.   time.sleep(0.5)
  863.   while(1):
  864.    if ( GPIO.input(PREV) == False):
  865.     station11()
  866.    if ( GPIO.input(PLAY) == False):
  867.     choose10()
  868.    if ( GPIO.input(NEXT) == False):
  869.     choose12()
  870.    else:
  871.     lcd_byte(LCD_LINE_1, LCD_CMD)
  872.     lcd_string(" Choose Station ",2)
  873.     lcd_byte(LCD_LINE_2, LCD_CMD)
  874.     lcd_string("[GO] < SlowJam >",2)
  875.    
  876. def choose12():
  877.   time.sleep(0.5)
  878.   while(1):
  879.    if ( GPIO.input(PREV) == False):
  880.     station12()
  881.    if ( GPIO.input(PLAY) == False):
  882.     choose11()
  883.    if ( GPIO.input(NEXT) == False):
  884.     chooseradio()
  885.    else:
  886.     lcd_byte(LCD_LINE_1, LCD_CMD)
  887.     lcd_string(" Choose Station ",2)
  888.     lcd_byte(LCD_LINE_2, LCD_CMD)
  889.     lcd_string("[GO] < CDelMar >",2)
  890.  
  891. def tweetemail():
  892.   temp1 = ""
  893.   f=os.popen("/opt/vc/bin/vcgencmd measure_temp")
  894.   for i in f.readlines():
  895.    temp1 += i
  896.    pretemp = " Pi temperature is "
  897.    temp1 = temp1[5:]
  898.    temp1 = pretemp + temp1
  899.    #Gmail account details
  900.    USERNAME = "YOURGMAILADDRESS@gmail.com" #Your gmail email address
  901.    PASSWORD = "YOURGMAILPASSWORD" # your gmail password
  902.    MAILTO  = "trigger@ifttt.com" # IFTTT standard email trigger address
  903.    #Email content
  904.    msg = MIMEText(temp1)
  905.    msg['Subject'] = "#RaspberryPi GPIO button tweeting from my Raspberry Pi Internet Radio -"
  906.    msg['From'] = USERNAME
  907.    msg['To'] = MAILTO
  908.    #Server stuff
  909.    server = smtplib.SMTP('smtp.gmail.com:587')
  910.    server.ehlo_or_helo_if_needed()
  911.    server.starttls()
  912.    server.ehlo_or_helo_if_needed()
  913.    server.login(USERNAME,PASSWORD)
  914.    server.sendmail(USERNAME, MAILTO, msg.as_string())
  915.    server.quit()
  916.    #LCD Message
  917.    time.sleep(0.5)
  918.    lcd_byte(LCD_LINE_1, LCD_CMD)
  919.    lcd_string("NERDBOX",2)
  920.    lcd_byte(LCD_LINE_2, LCD_CMD)
  921.    lcd_string("HAS TWEETED!",2)
  922.    time.sleep(1)
  923.    menu3()
  924.    
  925. def evernote():
  926.   f=os.popen("echo 'currentsong' | nc localhost 6600 | grep -e '^Title: '")
  927.   tracknow = ""
  928.   for i in f.readlines():
  929.    tracknow += i
  930.    tracknow = tracknow[7:]
  931.    #Gmail account details
  932.    USERNAME = "YOURGMAILADDRESS@gmail.com"
  933.    PASSWORD = "YOURGMAILPASSWORD"
  934.    MAILTO  = "trigger@ifttt.com"
  935.    #Email content
  936.    msg = MIMEText(tracknow)
  937.    msg['Subject'] = "#song New song to record"
  938.    msg['From'] = USERNAME
  939.    msg['To'] = MAILTO
  940.    #Server stuff
  941.    server = smtplib.SMTP('smtp.gmail.com:587')
  942.    server.ehlo_or_helo_if_needed()
  943.    server.starttls()
  944.    server.ehlo_or_helo_if_needed()
  945.    server.login(USERNAME,PASSWORD)
  946.    server.sendmail(USERNAME, MAILTO, msg.as_string())
  947.    server.quit()
  948.    #LCD Message
  949.    lcd_byte(LCD_LINE_2, LCD_CMD)
  950.    lcd_string("SENT",2)
  951.    time.sleep(0.3)
  952.    lcd_byte(LCD_LINE_2, LCD_CMD)
  953.    lcd_string("",2)
  954.    time.sleep(0.3)
  955.    lcd_byte(LCD_LINE_2, LCD_CMD)
  956.    lcd_string("TO",2)
  957.    time.sleep(0.3)
  958.    lcd_byte(LCD_LINE_2, LCD_CMD)
  959.    lcd_string("",2)
  960.    time.sleep(0.3)
  961.    lcd_byte(LCD_LINE_2, LCD_CMD)
  962.    lcd_string("EVERNOTE",2)
  963.    time.sleep(0.6)
  964.    
  965. def get_ip_address(ifname):
  966.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  967.     return socket.inet_ntoa(fcntl.ioctl(
  968.         s.fileno(),
  969.         0x8915,  
  970.         struct.pack('256s', ifname[:15])
  971.     )[20:24])
  972.    
  973. def off():
  974.   time.sleep(0.5)
  975.   while(1):
  976.    if ( GPIO.input(PREV) == False):
  977.     menu()
  978.    if ( GPIO.input(NEXT) == False):
  979.     lcd_byte(LCD_LINE_1, LCD_CMD)
  980.     lcd_string("Shutting Down   ",2)
  981.     lcd_byte(LCD_LINE_2, LCD_CMD)
  982.     lcd_string("                ",2)
  983.     time.sleep(0.5)
  984.     lcd_byte(LCD_LINE_1, LCD_CMD)
  985.     lcd_string("Shutting Down.  ",2)
  986.     lcd_byte(LCD_LINE_2, LCD_CMD)
  987.     lcd_string("                ",2)
  988.     time.sleep(0.5)
  989.     lcd_byte(LCD_LINE_1, LCD_CMD)
  990.     lcd_string("Shutting Down.. ",2)
  991.     lcd_byte(LCD_LINE_2, LCD_CMD)
  992.     lcd_string("                ",2)
  993.     time.sleep(0.5)
  994.     lcd_byte(LCD_LINE_1, LCD_CMD)
  995.     lcd_string("Shutting Down...",2)
  996.     lcd_byte(LCD_LINE_2, LCD_CMD)
  997.     lcd_string("                ",2)
  998.     time.sleep(0.5)
  999.     os.system("sudo halt")
  1000.     time.sleep(8)
  1001.    else:
  1002.     lcd_byte(LCD_LINE_1, LCD_CMD)
  1003.     lcd_string("   Shut down?   ",2)
  1004.     lcd_byte(LCD_LINE_2, LCD_CMD)
  1005.     lcd_string("< No       Yes >",2)
  1006.    
  1007.  
  1008. def lcd_init():
  1009.   # Initialise display
  1010.   lcd_byte(0x33,LCD_CMD)
  1011.   lcd_byte(0x32,LCD_CMD)
  1012.   lcd_byte(0x28,LCD_CMD)
  1013.   lcd_byte(0x0C,LCD_CMD)  
  1014.   lcd_byte(0x06,LCD_CMD)
  1015.   lcd_byte(0x01,LCD_CMD)  
  1016.  
  1017. def lcd_string(message,style):
  1018.   # Send string to display
  1019.   # style=1 Left justified
  1020.   # style=2 Centred
  1021.   # style=3 Right justified
  1022.  
  1023.   if style==1:
  1024.     message = message.ljust(LCD_WIDTH," ")  
  1025.   elif style==2:
  1026.     message = message.center(LCD_WIDTH," ")
  1027.   elif style==3:
  1028.     message = message.rjust(LCD_WIDTH," ")
  1029.  
  1030.   for i in range(LCD_WIDTH):
  1031.     lcd_byte(ord(message[i]),LCD_CHR)
  1032.  
  1033. def lcd_byte(bits, mode):
  1034.   # Send byte to data pins
  1035.   # bits = data
  1036.   # mode = True  for character
  1037.   #        False for command
  1038.  
  1039.   GPIO.output(LCD_RS, mode) # RS
  1040.  
  1041.   # High bits
  1042.   GPIO.output(LCD_D4, False)
  1043.   GPIO.output(LCD_D5, False)
  1044.   GPIO.output(LCD_D6, False)
  1045.   GPIO.output(LCD_D7, False)
  1046.   if bits&0x10==0x10:
  1047.     GPIO.output(LCD_D4, True)
  1048.   if bits&0x20==0x20:
  1049.     GPIO.output(LCD_D5, True)
  1050.   if bits&0x40==0x40:
  1051.     GPIO.output(LCD_D6, True)
  1052.   if bits&0x80==0x80:
  1053.     GPIO.output(LCD_D7, True)
  1054.  
  1055.   # Toggle 'Enable' pin
  1056.   time.sleep(E_DELAY)    
  1057.   GPIO.output(LCD_E, True)  
  1058.   time.sleep(E_PULSE)
  1059.   GPIO.output(LCD_E, False)  
  1060.   time.sleep(E_DELAY)      
  1061.  
  1062.   # Low bits
  1063.   GPIO.output(LCD_D4, False)
  1064.   GPIO.output(LCD_D5, False)
  1065.   GPIO.output(LCD_D6, False)
  1066.   GPIO.output(LCD_D7, False)
  1067.   if bits&0x01==0x01:
  1068.     GPIO.output(LCD_D4, True)
  1069.   if bits&0x02==0x02:
  1070.     GPIO.output(LCD_D5, True)
  1071.   if bits&0x04==0x04:
  1072.     GPIO.output(LCD_D6, True)
  1073.   if bits&0x08==0x08:
  1074.     GPIO.output(LCD_D7, True)
  1075.  
  1076.   # Toggle 'Enable' pin
  1077.   time.sleep(E_DELAY)    
  1078.   GPIO.output(LCD_E, True)  
  1079.   time.sleep(E_PULSE)
  1080.   GPIO.output(LCD_E, False)  
  1081.   time.sleep(E_DELAY)  
  1082.  
  1083. if __name__ == '__main__':
  1084.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement