Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import urllib2
- #import urllib.parse
- import json
- import sys
- import os
- import RPi.GPIO as GPIO
- red_light = 13 ##GPIO 13 connect to IN3
- yellow_light = 16 ##GPIO 16 connect to IN2
- green_light = 7 ##GPIO 7 connect to IN1
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(green_light, GPIO.OUT)
- GPIO.setup(red_light, GPIO.OUT)
- GPIO.setup(yellow_light, GPIO.OUT)
- GPIO.output(red_light,True)
- GPIO.output(yellow_light,True)
- GPIO.output(green_light,True)
- def LightOn(light,duration):
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(green_light, GPIO.OUT)
- GPIO.setup(red_light, GPIO.OUT)
- GPIO.setup(yellow_light, GPIO.OUT)
- GPIO.output(red_light,True)
- GPIO.output(yellow_light,True)
- GPIO.output(green_light,True)
- GPIO.output(light,False)
- time.sleep(duration)
- GPIO.output(light,True)
- GPIO.cleanup()
- def FlashSingleRapid(light, numTimes, duration):
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(13, GPIO.OUT)
- GPIO.setup(16, GPIO.OUT)
- GPIO.setup(7, GPIO.OUT)
- GPIO.output(13,True)
- GPIO.output(16,True)
- GPIO.output(7,True)
- for i in range(0,numTimes):## Run loop numTimes
- print("Flash Single Iteration " + str(i+1))## Print current loop
- GPIO.output(light,False)## Switch on
- time.sleep(duration)## Wait
- GPIO.output(light,True)## Switch off
- time.sleep(duration)
- GPIO.cleanup()
- print("Done")## When loop is complete, print "Done"
- def FlashLights(numTimes, speed):
- for i in range(0,numTimes):## Run loop numTimes
- print("Iteration " + str(i+1))## Print current loop
- GPIO.output(green_light,False)## Switch on pin 7
- time.sleep(speed)## Wait
- GPIO.output(green_light,True)## Switch off pin 7
- GPIO.output(yellow_light,False)## Switch on pin 15
- time.sleep(speed)## Wait
- GPIO.output(yellow_light,True)## Switch off pin 15
- GPIO.output(red_light,False)## Switch on pin 13
- time.sleep(speed)## Wait
- GPIO.output(red_light,True)## Switch off pin 13
- print("Done") ## When loop is complete, print "Done"
- GPIO.cleanup()
- def AllOnOff(numTimes, speed):
- for i in range(0,numTimes):## Run loop numTimes
- print("Iteration " + str(i+1))## Print current loop
- GPIO.output(green_light,False)## Switch on pin 7
- GPIO.output(yellow_light,False)## Switch on pin 15
- GPIO.output(red_light,False)## Switch on pin 13
- time.sleep(speed)## Wait
- GPIO.output(yellow_light,True)## Switch off pin 15
- GPIO.output(green_light,True)## Switch off pin 7
- GPIO.output(red_light,True)## Switch off pin 13
- print("Done") ## When loop is complete, print "Done"
- GPIO.cleanup()
- FlashLights(10,.1)
- period=0
- try:
- #json_data = requests.get('https://api.darksky.net/forecast/3597b17574df32c91c85e0952556e7f6/27.5299,-82.6205').json()
- f= urllib2.urlopen('https://api.darksky.net/forecast/3597b17574df32c91c85e0952556e7f6/27.5299,-82.6205')
- json_string=f.read()
- weather_data=json.loads(json_string)
- f.close()
- except IOError as e:
- os.system("sudo python readyflash.py")
- except Exception:
- os.system("sudo python readyflash.py")
- else:
- LowTemp=weather_data['daily']['data'][0]['temperatureLow']
- HighTemp=weather_data['daily']['data'][0]['temperatureHigh']
- POP=weather_data['currently']['precipProbability']
- POP=POP*100
- Conditions=weather_data['currently']['summary']
- LowTempstr = "Low Temp = " + str(LowTemp) +"F"
- HiTempstr = "High Temp = " + str(HighTemp) +"F"
- POPstr = "Chance of Rain is " + str(POP) + "%"
- print(LowTempstr)
- print(HiTempstr)
- print(POPstr)
- print("Today will be " + Conditions)
- light_colorstr= "Yellow Light"
- light_color=yellow_light
- if int(POP) >=90:
- light2 = red_light
- quick = .1
- NumX = int(POP)/10 + 1
- print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
- FlashSingleRapid(light2, NumX, quick)
- elif int(POP) >= 20 and int(POP) < 60:
- light2 = yellow_light
- quick = .1
- NumX = int(POP)/10 + 1
- print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
- FlashSingleRapid(light2, NumX, quick)
- time.sleep(.5)
- FlashSingleRapid(light2, NumX, quick)
- else:
- light2 = green_light
- quick = .1
- NumX = int(POP)/10 + 1
- print("POP = " + str(POP) + " so I'm flashing " + str(light2) + " " + str(NumX) + " times")
- FlashSingleRapid(light2, NumX, quick)
- time.sleep(.5)
- FlashSingleRapid(light2, NumX, quick)
- time.sleep(.5)
- FlashSingleRapid(light2, NumX, quick)
- time.sleep(.5)
- AllOnOff(3,.2)
- if int(LowTemp) >= 70:
- light_colorstr = "Green Light"
- light_color=green_light
- elif int(LowTemp) <= 50:
- light_colorstr = "Red Light"
- light_color=red_light
- else:
- light_colorstr= "Yellow Light"
- light_color=yellow_light
- print(light_colorstr)
- print(light_color)
- numTimes = int(lowTemp)/10 +1
- for i in range(0,numTimes):## Run loop numTimes
- LightOn(light_color,1)
- time.sleep(.2)
- ##GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement