import Rpi.GPIO as gpio # RPI gpio library import time #module for time import random # module for randomised number gpio.setmode(gpio,BCM) #used to set the RPI mode gpio.setup(21,gpio.OUT) #used to set pin as output while(1): a = input("ON or OFF\n") if a == 'ON': gpio.output(21,1) print('buzzer ON') time.sleep(2) gpio.output(21,0) elif a == 'OFF': gpio.output(21,0) else: print("Invalid value") gpio.output(21,0) gpio.cleanup() # clear existing states of GPIO
import Rpi.GPIO as GPIO #Importar librerías import time #Importar librería de tiempo GPIO.setmode (GPIO.BCM) #Configuración GPIO.setwarnings (False) #Anular Advertencias BUZZER = 18 #Asignación de Pin GPIO.setup (BUZZER, GPIO.OUT) #Se configura la salida if __name__ == "__main__": while True: #Ciclo While GPIO.output (BUZZER, GPIO.HIGH) #Ponemos en alto el pin del Buzzer time.sleep (1) #Esperamos un segundo antes de ejecutar la siguiente línea GPIO.output (BUZZER, GPIO.LOW) #Ponemos en bajo el pin del Buzzer time.sleep (4) #Esperamos cuatro segundos antes de ejecutar la siguiente linea
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ''' ''' Import relevant libraries ''' import Rpi.GPIO as GPIO import sys import os from subprocess import Popen ''' Setup GPIO pins on the Raspberry Pi ''' GPIO.setmode(GPIO.BCM) GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) ''' Set up movie locations and names ''' movie1 = ("/home/pi/Videos/movie1.mp4") movie2 = ("/home/pi/Videos/movie2.mp4") movie3 = ("/home/pi/Videos/movie3.mp4") movie4 = ("/home/pi/Videos/movie4.mp4") ''' Set up state variables for the videos and GPIO pins ''' last_state1 = True last_state2 = True
import Rpi.GPIO as GPIO import time GPIO.cleanup() GPIO.setmode(GPIO.Board) n = 22 a = 38 b = 40 l = 16 r = 14 u = 12 GPIO.setup(r,GPIO.OUT) #Right Drive GPIO.setup(l,GPIO.OUT) #Left Drive GPIO.setup(a,GPIO.OUT) #A GPIO.setup(b,GPIO.OUT) #B #Drive Motors GPIO.output(l, False) GPIO.outout(r, False) #Rudder Motor GPIO.output(a, False) GPIO.output(b, False) #Vertical Motors GPIO.output(u, False) #Emergency Button GPIO.setup(n,GPIO.IN) GPIO.output(u,True) for s in range(1,10) sleep(1) if(GPIO.input(n)) GPIO.cleanup() sys.exit("Emergency shutdown")
from ubidots import ApiClient import Rpi.GPIO as GPIO import time import picamera from time import sleep GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.IN) api=ApiClient("2355dc90c5e93fc697295b33f9a6ed4e5003c2e1") #create a "Variable" object test_variable=api.get_variable("566993407625420e4e82690a") while True: i=GPIO.input(11) i=0 if i==1 test_variable.save_value({'value':1}) camera=picamera.PiCamera() camera.capture('suyash.jpg') camera.start_preview() camera.vflip=True camera.hflip=True camera.brightness=60 sleep(10)
#!/usr/bin/python3 # A simple Raspberry pi 3 GPIO test # Written by # (except lines 5-13, these are from the RPi site raspberrypi.org/documentation/usage/python/more.md) # plscks import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # set board mode to Broadcom GPIO.setup(17, GPIO.OUT) # set up pin 17 GPIO.setup(18, GPIO.OUT) # set up pin 18 GPIO.output(17, 1) # turn on pin 17 GPIO.output(18, 1) # turn on pin 18
def SetTriggerPin(): gpio.setmode(gpio.BOARD) gpio.setup(TRIGGER, gpio.IN)
import Rpi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 12 ECHO = 24 GPIO.setup(TRIG, GPIO.OUT) GPIO.output(TRIG, 0) GPIO.setup(ECHO, GPIO.IN) time.sleep(0.1) print("Starting Measurement....") GPIO.output(TRIG, 1) time.sleep(0.00001) GPIO.output(TRIG, 0) while GPIO.input(ECHO) == 0: pass start = time.time() while GPIO.input(ECHO) == 1: pass stop = time.time() print(stop - start) * 17000
if ((datetime.now()-timedelta(milliseconds=500))<TrancaEvento):{ #A tranca estava aberta a tempo e a porta foi aberta. Não fazer nada. print "\nA tranca estava aberta a tempo e a porta foi aberta. Não fazer nada." } else { #A tranca foi aberta e logo depois a porta foi aberta. Acionar bom dia. print "\nA tranca foi aberta e logo depois a porta foi aberta. Acionar bom dia." } #A porta foi aberta } } } """ Configurando GPIO """ # Configurando o modo do GPIO como BCM gpio.setmode(gpio.BCM) # Configurando PIN's como INPUT e modo pull-down interno gpio.setup(Porta, gpio.IN, pull_up_down = gpio.PUD_DOWN) gpio.setup(Tranca, gpio.IN, pull_up_down = gpio.PUD_DOWN) # Adicionando um evento ao GPIO 23 na mudança RISING 0V[LOW] -> 3.3V[HIGH] gpio.add_event_detect(Porta, gpio.BOTH, callback=action_event_registrar) gpio.add_event_detect(Tranca, gpio.BOTH, callback=action_event_registrar) while True: try: if gpio.event_detected(Porta): action_event_button(Porta) #gpio.remove_event_detect(PIN)
import time import os import Rpi.GPIO as GPIO import sys import signal GPIO.setmode(GPIO>BCM) DEBUG = 1 def signal_handler(signal, frame): sys.exit(0) def readadc(adcnum, clockpin, mosipin, misopin, cspin): if((adcnum > 1) or (adcnum < 0)): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) GPIO.output(cspin, False) commandout = adcnum commandout |= 0x18 commandout <<=3 for i in range(5): if(commandout & 0x80): GPIO.output(mosipin, True) else: GPIO.output(mosipin, False) commandout <<=1 GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout = 0
GPIO.output(13,GPIO.HIGH) GPIO.output(19,GPIO.LOW) GPIO.output(26,GPIO.LOW) def yellowLight(): GPIO.output(13,GPIO.LOW) GPIO.output(19,GPIO.HIGH) GPIO.output(26,GPIO.LOW) time.sleep(5) def greenLight(): GPIO.output(13,GPIO.LOW) GPIO.output(19,GPIO.LOW) GPIO.output(26,GPIO.HIGH) GPIO.setmode(BCM) car_cascade_src = 'cars.xml' twowheeler_cascade_src='two_wheeler.xml' video_src = 'dataset/b.avi' cap = cv2.VideoCapture(video_src) car_cascade = cv2.CascadeClassifier(cascade_src) two_wheeler_cascade = cv2.CascadeClassifier(twowheeler_cascade_src) redcount=0 greencount=0 while True: ret, img = cap.read() if (type(img) == type(None)): break
# light cycle (12 hour schedule) using apscheduler let's specific sections of the code run at set time intervals, useful for different cycles sched = BlockingScheduler() @sched.scheduled_job('cron', day_of_week='mon-sun', hour=12) def scheduled_job(): return sched.configure(options_from_ini_file) sched.start() # pin selction for the pump io.setmode(ioBCM) io.setup(4, io.OUT) while True: io.output(4, 0) time.sleep(0, 30) io.output(4, 1) time.sleep(0, 30) # pin selction for the lights io.setmode(ioBCM) io.setup(5, io.OUT) while True: io.output(5, 0) time.sleep(0, 30) io.output(5, 1)
def init(): gpio.setmode(gpio.BOARD) gpio.setup(7, gpio.OUT) gpio.setup(11, gpio.OUT)
def setup(): GPIO.setmode(GPIO.BOARD) #Numbers GPIO's by physical location GPIO.setup(fart, GPIO.OUT) #Set fart's mode as output GPIO.output(fart, GPIO.HIGH) #Set fart high (3.3v) to turn on led
def setupPi(): GPIO.setmode(GPIO.BOARD) # define use of Board mode. GPIO.setup(11, GPIO.OUT) # Set pin 11(gpio 17) to be an output. pinsetup = GPIO.PWM(11, 50) # Pin and frequency of PWM. return pinsetup
import Rpi.GPIO as GPIO import time GPIO.setmode(GPIO.board) echo = 3 trig = 5 GPIO.setup(trig, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) # clear trig pin GPIO.output(trig, False) time.sleep(1) def distance(): GPIO.output(trig, True) time.sleep(0.00001) GPIO.output(trig, False) while GPIO.input(echo) == 0: pulse_start = time.time() while GPIO.input(echo) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start d = pulse_duration * 17150 d = round(distance, 2) return d
def setupPi(frequency): GPIO.setmode(GPIO.BOARD) #uses board pins GPIO.setup(12, GPIO.OUT) #sets pin 18 as the output (rudder motor) pinsetup = GPIO.PWM(12, frequency) #rudder is set to straight pin.start(1) #sets initial position of rudder return pinsetup
import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(6, GPIO.OUT) GPIO.output(7, 0) try: while True: if (GPIO.output(11) == 1): GPIO.output(7, 1) else: GPIO.output(7, 0) except KeyboardInterrupt: GPIO.cleanup()
#SIMPLE PROGRAM TO BLINK A LED USING BREAD-BOARD AND RASPBERRY-PI #Import libraries(to Import GPIO) import Rpi.GPIO as GPIO import time #wait for time GPIO.setmode( GPIO.BCM ) #Broadcom Chip Specific for GPIO pins number not the board given numbers GPIO.Setwarnings(False) #to remove all the warnings #Defining pins modes LEDpin = 21 GPIO.setup(LEDpin, GPIO.OUT) #pin mode as output #blinking logic GPIO.output(LEDpin, GPIO.HIGH) #GPIO high means : actiavted / 1 / true #GPIO low means : deactivated / 0 / false time.sleep(2) GPIO.output(LEDpin, GPIN.LOW)
def press_button(): log('Debug: press_button') import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(SERVER_POWER, GPIO.OUT) GPIO.setup(SERVER_RESET, GPIO.OUT) GPIO.setup(DESKTOP_POWER, GPIO.OUT) GPIO.setup(DESKTOP_RESET, GPIO.OUT) log('Debug: set up GPIO') try: if machine_to_control == "S": if button_to_press == "P": # Server Power GPIO.output(SERVER_POWER, True) log('Info: pressing server power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') elif button_to_press == "R": # Server Reset GPIO.output(SERVER_RESET, True) log('Info: pressing server reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_RESET, False) log('Info: button released') elif button_to_press == "PH": # Server Power Hold GPIO.output(SERVER_POWER, True) log('Info: holding server power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') else: log('Error: machine variables not set correctly') GPIO.cleanup() return if machine_to_control == "D": if button_to_press == "P": # Desktop Power GPIO.output(DESKTOP_POWER, True) log('Info: pressing desktop power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') elif button_to_press == "R": # Desktop Reset GPIO.output(DESKTOP_RESET, True) log('Info: pressing desktop reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_RESET, False) log('Info: button released') elif button_to_press == "PH": # Desktop Power Hold GPIO.output(DESKTOP_POWER, True) log('Info: holding desktop power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') else: log('Error: desktop variables not set correctly') GPIO.cleanup() return # TODO: catch and log the error. finally: GPIO.cleanup()
#timer for garden automaic water valve #importing the GPIO module import Rpi.GPIO as GPIO # set up GPIO pin as out put GPIO.setmode(GPIO.BMC) GPIO.SETUP(17, GPIO.OUT) # pin used to turn relay one with 3.3v state = 0 #import date and time module import datetime import time # setting day and time water_valve will turn on and off MonAmOn = datetime.time(hour = 6) #time on MonAmOff = datatime.time(hour = 7) #time off MonPMOn = datetime.time(hour= 18) MonPMOff = datetime.time(hour= 19) TueAMOn = datetime.time(hour = 6) TueAMOff = datetime.time(hour = 7) TuePMOn = datetime.time(hour = 18) TuePMOff = datetime.time(hour = 19) WedAMOn = datetime.time(hour = 6) WedAMOff = datetime.time(hour = 7) WedPMOn = datetime.time(hour = 18) WedPMOff = datetime.time(hour = 19) ThuAMOn = datetime.time(hour = 6) ThuAMOff = datetime.time(hour = 7 ) ThuPMOn = datetime.time(hour = 18) ThuPMOff = datetime.time(hour = 19)
def SetTriggerPin(): gpio.setmode(gpio.BOARD) gpio.setup(TRIGGER, gpio.IN)
import Rpi.GPIO as gpio import time gpio.setmode(gpio, BCM) trig = 21 #initialized trigger & echo pins echo = 20 gpio.setup(trig, gpio.OUT) #set trig pin as output gpio.setup(echo, gpio.IN) # set echo pin as Input gpio.output(trig, False) # clearing off the trigger pin print("Waiting for sensor to settle") time.sleep(2) while (True): gpio.output(trig, True) # line 16 to 18 creates a pulse time.sleep(0.0001) gpio.output(trig, False) while gpio.input(echo) == 0: #used to mark start time pulse_start = time.time() while gpio.input(echo) == 1: # used to mark end time pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) print("Distance is" + distance + "cm")
# this python file use for insert sensor data in database #import MyDB connection module import MyDB db = MyDB.DB() import Rpi.GPIO as GPIO import time sensorPin1 = 18 #Broadcom pin 18 #pin setup: GPIO.setmode(GPIO.BCM) #Broadcom pin numbering scheem GPIO.setup(sensorPin1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # sensor pin set as input user_id = 1023 data = [user_id] try: while 1: if GPIO.input(sensorPin1): db.execute("INSERT INTO Zones (user_id,zone1) \ VALUES (%s, NOW() )", [data]); db.commit() print "Records created successfully"; db.close() except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPIO
def press_button(): log('Debug: press_button') import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(SERVER_POWER, GPIO.OUT) GPIO.setup(SERVER_RESET, GPIO.OUT) GPIO.setup(DESKTOP_POWER, GPIO.OUT) GPIO.setup(DESKTOP_RESET, GPIO.OUT) log('Debug: set up GPIO') try: if machine_to_control == "S": if button_to_press == "P": # Server Power GPIO.output(SERVER_POWER, True) log('Info: pressing server power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') elif button_to_press == "R": # Server Reset GPIO.output(SERVER_RESET, True) log('Info: pressing server reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_RESET, False) log('Info: button released') elif button_to_press == "PH": # Server Power Hold GPIO.output(SERVER_POWER, True) log('Info: holding server power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') else: log('Error: machine variables not set correctly') GPIO.cleanup() return if machine_to_control == "D": if button_to_press == "P": # Desktop Power GPIO.output(DESKTOP_POWER, True) log('Info: pressing desktop power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') elif button_to_press == "R": # Desktop Reset GPIO.output(DESKTOP_RESET, True) log('Info: pressing desktop reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_RESET, False) log('Info: button released') elif button_to_press == "PH": # Desktop Power Hold GPIO.output(DESKTOP_POWER, True) log('Info: holding desktop power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') else: log('Error: desktop variables not set correctly') GPIO.cleanup() return # TODO: catch and log the error. finally: GPIO.cleanup()