def main(): """ メイン関数 """ # 接続ピン PIN_LD = 23 PIN_PR = [10, 9, 11, 8] # フォトリフレクタラベル(フィールド追加) Button.label = 0 # 赤色lED設定 red = LED(PIN_LD) # フォトリフレクタ(複数)設定(ボタンとして) photorefs = [ Button(PIN_PR[idx], active_state=True, pull_up=None) \ for idx in range(len(PIN_PR)) ] # フォトリフレクタラベル設定 for idx in range(len(photorefs)): photorefs[idx].label = idx + 1 # フォトリフレクタ検出結果の論理和をLED入力に接続 red.source = any_values(photorefs[0],photorefs[1], \ photorefs[2],photorefs[3]) # コールバック設定 for pr in photorefs: # 白色検出時 pr.when_pressed = white # 黒色検出時 pr.when_released = black # 停止(Ctrl+c)まで待機 pause()
def main(): try: # logging configuration logging.basicConfig( filename='/opt/rpi-button-box/button-box.log', level=logging.INFO, format= '%(asctime)s.%(msecs)03d %(levelname)s %(module)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S') logging.info('Started the button box controller') buttons = config_buttons() # wait for a button labelled 'power' to be turned ON before continuing logging.info('Trying to find a power switch...') for button in buttons: if button.label == 'power': logging.info('Power switch found at {}'.format(button.pin)) if not button.is_active: print( 'Waiting for the power button ({}) to be turned ON...'. format(button.pin)) button.wait_for_active() logging.info('Power switch was turned ON by user'.format( button.pin)) sleep(button.hold_time ) # wait for the power button to enter is_held state break # when_* properties will pass the device that activated it to a function that takes a single parameter # use the device's attributes (e.g., pin, type, label) to determine what to do push_buttons, switches = [], [] for button in buttons: if button.type == 'switch': switches.append(button) button.when_held, button.when_released = event_held, event_released logging.info( 'Configured the switch button ({0}) at {1}'.format( button.label, button.pin)) else: # assumes only 'push' and 'switch' types push_buttons.append(button) button.when_pressed, button.when_released = event_pressed, event_released logging.info('Configured the push button ({0}) at {1}'.format( button.label, button.pin)) if args['buzzer']: # buzzer is activated by any push button buzzer, buzzer.source = Buzzer( args['buzzer']), any_values(*push_buttons) logging.info('Configured a buzzer at {}'.format(buzzer.pin)) print( 'The button box is now turned ON. To close it, release the power button or press Ctrl+C.' ) logging.info('The button box is ON and waiting for user input') pause() except KeyboardInterrupt: end(msg='Received a signal to stop.', status=1) except GPIOZeroError as err: end(msg='GPIOZero error: {}'.format(err), status=1)
def door_status_close(door_id): redis_db.set(str(door_id), 'close') led.source = any_values(door_sensor_1, door_sensor_2) verbose = program_remote_control() if verbose is 'yes': print("The " + str(door_id) + " is closed!") if zabbix_sender is 'yes': zabbix_sender_cmd = '/home/pi/scripts/RPiMS/zabbix_sender.sh info_when_door_is_closed' + " " + str( door_id) subprocess.call(zabbix_sender_cmd, shell=True) if use_picamera is 'yes': av_stream('start')
def door_action_closed(door_id): redis_db.set(str(door_id), 'close') led.source = any_values(door_sensor_1, door_sensor_2) verbose = program_remote_control() if verbose is 'yes': print("The " + str(door_id) + " has been closed!") if zabbix_sender is 'yes': zabbix_sender_cmd = '/home/pi/scripts/RPiMS/zabbix_sender.sh info_when_door_has_been_closed' + " " + str( door_id) subprocess.call(zabbix_sender_cmd, shell=True) if use_picamera is 'yes': sleep(0.2) av_stream('stop') sleep(0.2) subprocess.call("/home/pi/scripts/RPiMS/videorecorder.sh", shell=True) sleep(0.2) av_stream('start')
def main(): # from picamera import PiCamera from gpiozero import LED, Button, MotionSensor from gpiozero.tools import all_values, any_values from signal import pause # import logging import json import sys print('') print('# RPiMS is running #') print('') global redis_db redis_db = db_connect('localhost', 0) config_yaml = config_load('/var/www/html/conf/rpims.yaml') config = config_yaml['setup'] zabbix_agent = config_yaml['zabbix_agent'] gpio = config_yaml.get("gpio") redis_db.flushdb() redis_db.set('gpio', json.dumps(gpio)) redis_db.set('config', json.dumps(config)) redis_db.set('zabbix_agent', json.dumps(zabbix_agent)) get_hostip() hostnamectl_sh(**zabbix_agent) if bool(config['verbose']) is True: for k, v in config.items(): print(f'{k} = {v}') for k, v in zabbix_agent.items(): print(f'{k} = {v}') print('') if bool(config['use_door_sensor']) is True: global door_sensors_list door_sensors_list = {} for item in gpio: if (gpio[item]['type'] == 'DoorSensor'): door_sensors_list[item] = Button(gpio[item]['gpio_pin'], hold_time=int( gpio[item]['hold_time'])) if bool(config['use_motion_sensor']) is True: global motion_sensors_list motion_sensors_list = {} for item in gpio: if (gpio[item]['type'] == 'MotionSensor'): motion_sensors_list[item] = MotionSensor( gpio[item]['gpio_pin']) if bool(config['use_system_buttons']) is True: global system_buttons_list system_buttons_list = {} for item in gpio: if (gpio[item]['type'] == 'ShutdownButton'): system_buttons_list['shutdown_button'] = Button( gpio[item]['gpio_pin'], hold_time=int(gpio[item]['hold_time'])) global led_indicators_list led_indicators_list = {} for item in gpio: if (gpio[item]['type'] == 'door_led'): led_indicators_list['door_led'] = LED(gpio[item]['gpio_pin']) if (gpio[item]['type'] == 'motion_led'): led_indicators_list['motion_led'] = LED(gpio[item]['gpio_pin']) if (gpio[item]['type'] == 'led'): led_indicators_list['led'] = LED(gpio[item]['gpio_pin']) if bool(config['use_door_sensor']) is True: for k, v in door_sensors_list.items(): if v.value == 0: door_status_open(k, **config) else: door_status_close(k, **config) for k, v in door_sensors_list.items(): v.when_held = lambda s=k: door_action_closed(s, **config) v.when_released = lambda s=k: door_action_opened(s, **config) if bool(config['use_door_led_indicator']) is True: led_indicators_list['door_led'].source = all_values( *door_sensors_list.values()) if bool(config['use_motion_sensor']) is True: for k, v in motion_sensors_list.items(): if v.value == 0: motion_sensor_when_no_motion(k, **config) else: motion_sensor_when_motion(k, **config) for k, v in motion_sensors_list.items(): v.when_motion = lambda s=k: motion_sensor_when_motion(k, **config) v.when_no_motion = lambda s=k: motion_sensor_when_no_motion( k, **config) if bool(config['use_motion_led_indicator']) is True: led_indicators_list['motion_led'].source = any_values( *motion_sensors_list.values()) if bool(config['use_system_buttons']) is True: system_buttons_list['shutdown_button'].when_held = shutdown if bool(config['use_CPU_sensor']) is True: threading_function(get_cputemp_data, **config) if bool(config['use_BME280_sensor']) is True: threading_function(get_bme280_data, **config) if bool(config['use_DS18B20_sensor']) is True: threading_function(get_ds18b20_data, **config) if bool(config['use_DHT_sensor']) is True: threading_function(get_dht_data, **config) if bool(config['use_weather_station']) is True: threading_function(rainfall, **config) threading_function(wind_speed, **config) threading_function(wind_direction, **config) if bool(config['use_serial_display']) is True: threading_function(serial_displays, **config) if bool(config['use_picamera']) is True and bool( config['use_picamera_recording']) is False and bool( config['use_door_sensor']) is False and bool( config['use_motion_sensor']) is False: av_stream('start') pause()
from gpiozero import LED, Button from gpiozero.tools import all_values, any_values, negated from signal import pause in_1 = Button(23) in_2 = Button(15) out_1 = LED(24) out_2 = LED(18) out_3 = LED(14) out_1.source = all_values(in_1.values, in_2.values) out_2.source = any_values(in_1.values, in_2.values) out_3.source = negated(out_1.values) pause()
def main(): #from picamera import PiCamera from gpiozero import LED, Button, MotionSensor from gpiozero.tools import all_values, any_values from signal import pause #import logging import sys print('# RPiMS is running #') try: db_connect('localhost', 0) except Exception as err : print("Blad połączenia") sys.exit(1) #redis_db.flushdb() for key in redis_db.scan_iter("motion_sensor_*"): redis_db.delete(key) for key in redis_db.scan_iter("door_sensor_*"): redis_db.delete(key) config_yaml=config_load('/var/www/html/conf/rpims.yaml') config = config_yaml['setup'] zabbix_agent = config_yaml['zabbix_agent'] hostnamectl_sh(**zabbix_agent) if bool(config['use_door_sensor']) is True: global door_sensors_list door_sensors_list = {} redis_db.delete("door_sensors") for item in config_yaml.get("door_sensors"): door_sensors_list[item] = Button(config_yaml['door_sensors'][item]['gpio_pin'], hold_time=config_yaml['door_sensors'][item]['hold_time']) redis_db.sadd("door_sensors", item) if bool(config['use_motion_sensor']) is True: global motion_sensors_list motion_sensors_list = {} redis_db.delete("motion_sensors") for item in config_yaml.get("motion_sensors"): motion_sensors_list[item] = MotionSensor(config_yaml['motion_sensors'][item]['gpio_pin']) redis_db.sadd("motion_sensors", item) if bool(config['use_system_buttons']) is True: global system_buttons_list system_buttons_list = {} for item in config_yaml.get("system_buttons"): system_buttons_list[item] = Button(config_yaml['system_buttons'][item]['gpio_pin'], hold_time=config_yaml['system_buttons'][item]['hold_time']) if bool(config['use_led_indicators']) is True: global led_indicators_list led_indicators_list = {} for item in config_yaml.get("led_indicators"): led_indicators_list[item] = LED(config_yaml['led_indicators'][item]['gpio_pin']) if bool(config['verbose']) is True : print('') for s in config : redis_db.set(s, str(config[s])) if bool(config['verbose']) is True : print(s + ' = ' + str(config[s])) if bool(config['verbose']) is True : print('') for s in zabbix_agent : redis_db.set(s, str(zabbix_agent[s])) if bool(config['verbose']) is True : print(s + ' = ' + str(zabbix_agent[s])) if bool(config['verbose']) is True : print('') if bool(config['use_door_sensor']) is True : for s in door_sensors_list: if door_sensors_list[s].value == 0: door_status_open(s,**config) else: door_status_close(s,**config) for s in door_sensors_list: door_sensors_list[s].when_held = lambda s=s : door_action_closed(s,**config) door_sensors_list[s].when_released = lambda s=s : door_action_opened(s,**config) if bool(config['use_led_indicators']) is True : led_indicators_list['door_led'].source = all_values(*door_sensors_list.values()) if bool(config['use_motion_sensor']) is True : for s in motion_sensors_list: if motion_sensors_list[s].value == 0: motion_sensor_when_no_motion(s,**config) else: motion_sensor_when_motion(s,**config) for s in motion_sensors_list: motion_sensors_list[s].when_motion = lambda s=s : motion_sensor_when_motion(s,**config) motion_sensors_list[s].when_no_motion = lambda s=s : motion_sensor_when_no_motion(s,**config) if bool(config['use_led_indicators']) is True : led_indicators_list['motion_led'].source = any_values(*motion_sensors_list.values()) if bool(config['use_system_buttons']) is True: system_buttons_list['shutdown_button'].when_held = shutdown if bool(config['use_CPU_sensor']) is True: threading_function(get_cputemp_data, **config) if bool(config['use_BME280_sensor']) is True: threading_function(get_bme280_data, **config) if bool(config['use_DS18B20_sensor']) is True: threading_function(get_ds18b20_data, **config) if bool(config['use_DHT_sensor']) is True: threading_function(get_dht_data, **config) if bool(config['use_weather_station']) is True: threading_function(rainfall, **config) threading_function(wind_speed, **config) threading_function(wind_direction, **config) if bool(config['use_serial_display']) is True: threading_function(serial_displays, **config) if bool(config['use_picamera']) is True and bool(config['use_picamera_recording']) is False and bool(config['use_door_sensor']) is False and bool(config['use_motion_sensor']) is False : av_stream('start') pause()