Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import time
  2. import urllib2
  3. #import urllib.parse
  4. import json
  5. import sys
  6. import os
  7. import RPi.GPIO as GPIO
  8.  
  9. red_light = 13 ##GPIO 13 connect to IN3
  10. yellow_light = 16 ##GPIO 16 connect to IN2
  11. green_light = 7 ##GPIO 7 connect to IN1
  12.  
  13. GPIO.setmode(GPIO.BOARD)
  14. GPIO.setup(green_light, GPIO.OUT)
  15. GPIO.setup(red_light, GPIO.OUT)
  16. GPIO.setup(yellow_light, GPIO.OUT)
  17.  
  18. GPIO.output(red_light,True)
  19. GPIO.output(yellow_light,True)
  20. GPIO.output(green_light,True)
  21.  
  22. def LightOn(light,duration):
  23. GPIO.setmode(GPIO.BOARD)
  24. GPIO.setup(green_light, GPIO.OUT)
  25. GPIO.setup(red_light, GPIO.OUT)
  26. GPIO.setup(yellow_light, GPIO.OUT)
  27. GPIO.output(red_light,True)
  28. GPIO.output(yellow_light,True)
  29. GPIO.output(green_light,True)
  30. GPIO.output(light,False)
  31. time.sleep(duration)
  32. GPIO.output(light,True)
  33. GPIO.cleanup()
  34.  
  35. def FlashSingleRapid(light, numTimes, duration):
  36. GPIO.setmode(GPIO.BOARD)
  37. GPIO.setup(13, GPIO.OUT)
  38. GPIO.setup(16, GPIO.OUT)
  39. GPIO.setup(7, GPIO.OUT)
  40. GPIO.output(13,True)
  41. GPIO.output(16,True)
  42. GPIO.output(7,True)
  43. for i in range(0,numTimes):## Run loop numTimes
  44. print("Flash Single Iteration " + str(i+1))## Print current loop
  45. GPIO.output(light,False)## Switch on
  46. time.sleep(duration)## Wait
  47. GPIO.output(light,True)## Switch off
  48. time.sleep(duration)
  49. GPIO.cleanup()
  50. print("Done")## When loop is complete, print "Done"
  51.  
  52. def FlashLights(numTimes, speed):
  53. for i in range(0,numTimes):## Run loop numTimes
  54. print("Iteration " + str(i+1))## Print current loop
  55. GPIO.output(green_light,False)## Switch on pin 7
  56. time.sleep(speed)## Wait
  57. GPIO.output(green_light,True)## Switch off pin 7
  58. GPIO.output(yellow_light,False)## Switch on pin 15
  59. time.sleep(speed)## Wait
  60. GPIO.output(yellow_light,True)## Switch off pin 15
  61. GPIO.output(red_light,False)## Switch on pin 13
  62. time.sleep(speed)## Wait
  63. GPIO.output(red_light,True)## Switch off pin 13
  64. print("Done") ## When loop is complete, print "Done"
  65. GPIO.cleanup()
  66.  
  67. def AllOnOff(numTimes, speed):
  68. for i in range(0,numTimes):## Run loop numTimes
  69. print("Iteration " + str(i+1))## Print current loop
  70. GPIO.output(green_light,False)## Switch on pin 7
  71. GPIO.output(yellow_light,False)## Switch on pin 15
  72. GPIO.output(red_light,False)## Switch on pin 13
  73. time.sleep(speed)## Wait
  74. GPIO.output(yellow_light,True)## Switch off pin 15
  75. GPIO.output(green_light,True)## Switch off pin 7
  76. GPIO.output(red_light,True)## Switch off pin 13
  77. print("Done") ## When loop is complete, print "Done"
  78. GPIO.cleanup()
  79.  
  80. FlashLights(10,.1)
  81.  
  82.  
  83. period=0
  84. try:
  85. #json_data = requests.get('https://api.darksky.net/forecast/3597b17574df32c91c85e0952556e7f6/27.5299,-82.6205').json()
  86. f= urllib2.urlopen('https://api.darksky.net/forecast/3597b17574df32c91c85e0952556e7f6/27.5299,-82.6205')
  87. json_string=f.read()
  88. weather_data=json.loads(json_string)
  89. f.close()
  90. except IOError as e:
  91. os.system("sudo python readyflash.py")
  92. except Exception:
  93. os.system("sudo python readyflash.py")
  94. else:
  95. LowTemp=weather_data['daily']['data'][0]['temperatureLow']
  96. HighTemp=weather_data['daily']['data'][0]['temperatureHigh']
  97. POP=weather_data['currently']['precipProbability']
  98. POP=POP*100
  99. Conditions=weather_data['currently']['summary']
  100. LowTempstr = "Low Temp = " + str(LowTemp) +"F"
  101. HiTempstr = "High Temp = " + str(HighTemp) +"F"
  102. POPstr = "Chance of Rain is " + str(POP) + "%"
  103. print(LowTempstr)
  104. print(HiTempstr)
  105. print(POPstr)
  106. print("Today will be " + Conditions)
  107. light_colorstr= "Yellow Light"
  108. light_color=yellow_light
  109.  
  110. if int(POP) >=90:
  111. light2 = red_light
  112. quick = .1
  113. NumX = int(POP)/10 + 1
  114. print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
  115. FlashSingleRapid(light2, NumX, quick)
  116.  
  117. elif int(POP) >= 20 and int(POP) < 60:
  118. light2 = yellow_light
  119. quick = .1
  120. NumX = int(POP)/10 + 1
  121. print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
  122. FlashSingleRapid(light2, NumX, quick)
  123. time.sleep(.5)
  124. FlashSingleRapid(light2, NumX, quick)
  125.  
  126. else:
  127. light2 = green_light
  128. quick = .1
  129. NumX = int(POP)/10 + 1
  130. print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
  131. FlashSingleRapid(light2, NumX, quick)
  132. time.sleep(.5)
  133. FlashSingleRapid(light2, NumX, quick)
  134. time.sleep(.5)
  135. FlashSingleRapid(light2, NumX, quick)
  136. time.sleep(.5)
  137.  
  138. AllOnOff(3,.2)
  139.  
  140. if int(LowTemp) >= 70:
  141. light_colorstr = "Green Light"
  142. light_color=green_light
  143.  
  144. elif int(LowTemp) <= 50:
  145. light_colorstr = "Red Light"
  146. light_color=red_light
  147. else:
  148. light_colorstr= "Yellow Light"
  149. light_color=yellow_light
  150.  
  151.  
  152.  
  153. print(light_colorstr)
  154. print(light_color)
  155. numTimes = int(lowTemp)/10 +1
  156.  
  157. for i in range(0,numTimes):## Run loop numTimes
  158. LightOn(light_color,1)
  159. time.sleep(.2)
  160.  
  161. ##GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement