def action_light(payload, repeat): rfdevice = RFDevice(17) rfdevice.enable_tx() rfdevice.tx_repeat = repeat tx_code(rfdevice, payload, 'light1', 279889, 279892) tx_code(rfdevice, payload, 'light2', 282961, 282964) rfdevice.cleanup()
def RF_on(set_dic, switch_log): script = 'RF_on.py' msg = ("") msg += (" #############################################\n") msg += (" ## Turning the RF - ON ##\n") from rpi_rf import RFDevice if 'gpio_RF' in set_dic: if not str(set_dic['gpio_RF']).strip() == '': gpio_pin = int(set_dic['gpio_RF']) else: print(" - RF gpio pin not set") else: msg += (" !! NO RF SET !!\n") msg += (" !! run config program or edit config.txt !!\n") msg += (" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n") pigrow_defs.write_log(script, 'Failed - due to none set in config', switch_log) return msg rfdevice = RFDevice(gpio_pin) rfdevice.enable_tx() rfdevice.tx_code(15465751, 1, 432) rfdevice.tx_repeat = 5 msg += (" ## using GPIO " + str(gpio_pin) + " ##\n") msg += (" #############################################\n") pigrow_defs.set_condition(condition_name="RF", trig_direction="on", cooldown="none") pigrow_defs.write_log(script, 'RF turned on', switch_log) return msg
def data_tx(code: int, rfpin: int): rfdevice = RFDevice(rfpin) rfdevice.enable_tx() rfdevice.tx_repeat = 10 print('sent code:' + str(code)) rfdevice.tx_code(code, 1, 500, 24) rfdevice.cleanup()
def turnon_light(): try: rfdevice = RFDevice(17) rfdevice.enable_tx() rfdevice.tx_repeat = 10 rfdevice.tx_code(4478259, 1, 186, 24) rfdevice.cleanup() return Response('Light turned on!') except: return Response('Error with turning on light.')
def send(rfsocket_num, sig): rfd = RFDevice(_pin) rfd.enable_tx() rfd.tx_repeat = 10 if rfsocket_num in _codes: rfsocket_codes = _codes[rfsocket_num] if sig in rfsocket_codes: rfd.tx_code(rfsocket_codes[sig]) rfd.cleanup()
def send_decimal(code): logger.debug(f"'send_decimal' called. Sending code {code} using rpi_rf") try: rf_device = RFDevice(GPIO_PIN) rf_device.enable_tx() rf_device.tx_repeat = 20 rf_device.tx_code(code, tx_pulselength=500) except: logger.exception("Error while sending code to socket using rpi_rf") finally: rf_device.cleanup()
def send_code(socketnr, code): logger.debug("'send_code' called. Sending code %s to socket %d using rpi_rf", code, socketnr) try: rf_device = RFDevice(GPIO_PIN) rf_device.enable_tx() rf_device.tx_repeat = 20 rf_device.tx_code(CODES[socketnr][code], tx_pulselength=500) except: logger.exception("Error while sending code to socket using rpi_rf") finally: rf_device.cleanup()
def off(): rfdevice = RFDevice(17) rfdevice.tx_repeat = 1 rfdevice.enable_tx() rfdevice.tx_code(4265276, 1, 188) rfdevice.tx_code(4265276, 1, 191) rfdevice.tx_code(4265276, 1, 191) rfdevice.tx_code(4265276, 1, 191) rfdevice.cleanup() print(datetime.datetime.now().strftime("%H:%M:%S"), "Electric heater turned off.") logging.info("%s Electric heater turned off.", datetime.datetime.now().strftime("%H:%M:%S"))
def on(): rfdevice = RFDevice(17) rfdevice.tx_repeat = 1 rfdevice.enable_tx() rfdevice.tx_code(137276512, 3, 83) rfdevice.tx_code(17061062, 1, 190) rfdevice.tx_code(17061272, 3, 83) rfdevice.tx_code(4265267, 1, 190) rfdevice.cleanup() print(datetime.datetime.now().strftime("%H:%M:%S"), "Electric heater turned on.") logging.info("%s Electric heater turned on.", datetime.datetime.now().strftime("%H:%M:%S"))
def transmit(job_list=[]): trans = RFDevice(TRANSMIT_PIN) trans.enable_tx() trans.tx_repeat = 10 for job in job_list: trans.tx_code(job, PROTOCOL, TRANS_LENGTH, 24) if job == ON_MAIN: config.main_state = 1 elif job == OFF_MAIN: config.main_state = 0 elif job == ON_SEC: config.sec_state = 1 elif job == OFF_SEC: config.sec_state = 0 elif job == ON_LAMP: config.lamp_state = 1 elif job == OFF_LAMP: config.lamp_state = 0 trans.cleanup()
def __send_code(self, code): try: _logger.debug("Sending '{} ({:s})' to '{}'".format( code, 'on' if code == self._code_on else 'off', self._display_name)) rfdevice = RFDevice(self._gpio_pin) rfdevice.enable_tx() rfdevice.tx_repeat = self._send_repeat rfdevice.tx_code(code, self._protocol, self._pulse_length, self._code_length) rfdevice.cleanup() return True except: # pragma: no cover err_msg = traceback.format_exc(limit=3) _logger.error("Error while sending code: " + err_msg) if code == self._code_on: msg = _('Error while turning the LPD433 power sockets on:\n') else: msg = _('Error while turning the LPD433 power sockets off:\n') broadcast.error(msg + err_msg, source=broadcast.SRC_LPD433) return False
import random import pymysql import os import time import signal stamp = dict() realstamp = dict() mqttdevices = dict() tordevices = torturedevices() slaves = slaves() rfdevice = RFDevice( 27 ) rfsend = RFDevice( 17 ) rfdevice.enable_rx() rfsend.enable_tx() rfdevice.tx_repeat = 10 timestamp = None old_slave_count = -1 old_device_count = -1 logger = logging.getLogger( 'Punisher' ) logger.setLevel( logging.DEBUG ) log = logging.FileHandler('logs/punisher.log') log.setLevel( logging.DEBUG ) logger.addHandler( log ) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') log.setFormatter( formatter ) logger.addHandler( log ) config = configparser.ConfigParser() config.read('config/config.ini')
help="Protocol (Default: 1)") parser.add_argument('-l', dest='length', type=int, default=None, help="Codelength (Default: 24)") parser.add_argument('-r', dest='repeat', type=int, default=10, help="Repeat cycles (Default: 10)") args = parser.parse_args() rfdevice = RFDevice(args.gpio) rfdevice.enable_tx() rfdevice.tx_repeat = args.repeat if args.protocol: protocol = args.protocol else: protocol = "default" if args.pulselength: pulselength = args.pulselength else: pulselength = "default" if args.length: length = args.length else: length = "default" logging.info(
def handle(self, *args, **options): print help #path = os.path.join(PROJECT_PATH,'growmat','ramdisk', 'raspistill.jpg') #os.system('raspistill -v -w 640 -h 480 -vf -s -n -t 0 -o ' + path + ' &') #-tl 60000 #extIP = get_external_ip() #text = 'GROWMAT IP: ' + extIP text = 'GROWMAT' print text #call(['python', os.path.join(PROJECT_PATH, 'xsend.py') ,'*****@*****.**', text]) Config = ConfigParser.ConfigParser() try: Config.read(os.path.join(self.PROJECT_PATH, 'growmat.ini')) port = Config.get('modbus', 'port') debug = Config.get('modbus', 'debug') rctxpin = Config.get('rc', 'txpin') except: cfgfile = open(os.path.join(self.PROJECT_PATH, 'growmat.ini'), 'w+') Config.add_section('modbus') Config.set('modbus', 'port', '/dev/ttyUSB0') #Config.set('modbus','port','/dev/ttyAMA0') #Config.set('modbus','port','COM1') Config.set('modbus', 'debug', 'False') Config.add_section('rc') Config.set('rc', 'txpin', 18) Config.write(cfgfile) cfgfile.close() print 'Please check growmat.ini and try again!' return if MODBUS: station = minimalmodbus.Instrument(port, 1) station.serial.baudrate = 9600 # Baud station.serial.bytesize = 8 #station.serial.parity = serial.PARITY_NONE station.serial.stopbits = 2 station.serial.timeout = 0.1 print station.serial.port if debug == 'True': station.debug = True else: station = None if RC: rfdevice = RFDevice(int(rctxpin)) rfdevice.enable_tx() rfdevice.tx_repeat = 10 while 1: time_now = int(strftime('%H%M%S', gmtime())) #INPUTS print '> inputs' instruments = Instrument.objects.order_by('pk') for instrument in instruments: instrument = Instrument.objects.get(pk=instrument.pk) if instrument.manual == False and instrument.output == False: if instrument.address > 0 and instrument.type == 0: #instrument.INSTRUMENT_TYPE.MODBUS if debug == 'True': print 'read station {} index {}'.format( instrument.address, instrument.index) modbus_read(station, instrument, self) if instrument.address == 0 and instrument.type == 10: #instrument.INSTRUMENT_TYPE.SYSTEM: if instrument.index == 0: instrument.value = int(strftime( "%H%M%S", gmtime())) #gmtime())) instrument.save() #print instrument.INSTRUMENT_TYPE if instrument.type == 20: #instrument.INSTRUMENT_TYPE.SCRIPT: scriptexec(instrument, self) if instrument.type == 30: #instrument.INSTRUMENT_TYPE.BTSPP2FILE if debug == 'True': print 'read btspp2file address {} index {} type {}'.format( instrument.address, instrument.index, instrument.type) btspp2file_read(instrument, self) if instrument.type == 50: #instrument.INSTRUMENT_TYPE.GROWMATUTILS #if debug == 'True': # print 'read btspp2file address {} index {} type {}'.format(instrument.address, instrument.index, instrument.type) value = eval('growmatutils.' + instrument.name + '(' + str(instrument.address) + ')') instrument.value = value instrument.save() #RULES print '> rules' rules = Rule.objects.order_by('priority') for rule in rules: #print rule.description time_from = int(rule.period.time_from.strftime('%H%M%S')) time_to = int(rule.period.time_to.strftime('%H%M%S')) if time_from < time_now and time_now <= time_to and not rule.output.manual: if rule.input_attribute == 'VALUE': a = rule.input.value else: a = float(rule.input.status) b = rule.input_parameter op = rule.input_operation exp = 'True if {} {} {} else False'.format(a, op, b) r = eval(exp) rule.result = r if rule.output_attribute == 'VALUE': a = rule.output.value else: a = rule.output.status if rule.result: b = rule.output_parameter_true op = rule.output_action_true else: b = rule.output_parameter_false op = rule.output_action_false if op != 'None': if op == '=': r = b else: if op[0:1] == '&' or op == '|': a = int(a) b = int(b) exp = '{} {} {}'.format(a, op, b) r = eval(exp) if rule.output_attribute == 'VALUE': rule.output.value = r else: rule.output.status = int(r) rule.output.save() if (rule.once and (rule.result != rule.result0)) or ~rule.once: if rule.output.address == 0: if rule.output.index == 0: #cl.send(xmpp.Message( rule.output.name ,rule.description )) #execfile('python xsend.py "{}" "{}"'.format(rule.output.name ,rule.description)) scriptname = os.path.join( self.PROJECT_PATH, 'xsend.py') #call(['python', '/home/pi/growmat/xsend.py' ,'{}'.format(rule.output.name), '{}'.format(rule.description)]) call([ 'python', scriptname, '{}'.format(rule.output.name), '{}'.format(rule.description) ]) #print "Jabber: send" if rule.output.index == 1: i = rule.description.find(' ') if i > -1: scriptparam = ' ' + str( rule.result) + rule.description[i:] scriptname = os.path.join( self.PROJECT_PATH, 'growmat', 'scripts', rule.description[:i]) else: scriptparam = ' ' + str(rule.result) scriptname = os.path.join( self.PROJECT_PATH, 'growmat', 'scripts', rule.description) scriptname = scriptname + scriptparam + ' &' #print scriptname + ' start' print scriptname try: os.system(scriptname) #print scriptname + ' end' #os.spawnl(os.P_DETACH, scriptname) except: print sys.exc_info()[0] if rule.result != rule.result0: rule.result0 = rule.result rule.datetime = timezone.now() rule.output.status = rule.output.status & ~1 rule.save() #fn = '/home/pi/growmat/growmat/ramdisk/0.csv' fn = os.path.join(self.PROJECT_PATH, 'growmat', 'ramdisk', '0.csv') f = open(fn, 'a+') f.write(str(rule.id)) f.write(';') f.write(dateformat.format(rule.datetime, 'Y-m-d H:i:s')) f.write(';') f.write(str(rule.result)) f.write(';') f.write(rule.description) f.write(';') f.write('\n') f.close() #OUTPUTS print '> outputs' instruments = Instrument.objects.filter(output=True).order_by('pk') for instrument in instruments: instrument = Instrument.objects.get(pk=instrument.pk) if instrument.output == True: if instrument.address > 0 and instrument.type == 0: #instrument.INSTRUMENT_TYPE.MODBUS: if debug == 'True': print 'write station {} index {} value {}'.format( instrument.address, instrument.index, instrument.value) modbus_write(station, instrument, self) if instrument.type == 20: #instrument.INSTRUMENT_TYPE.SCRIPT: scriptexec(instrument, self) if instrument.type == 30: #instrument.INSTRUMENT_TYPE.BTSPP2FILE if debug == 'True': print 'write btspp2file address {} index {} type {}'.format( instrument.address, instrument.index, instrument.type) btspp2file_write(instrument, self) if instrument.type == 40: #instrument.INSTRUMENT_TYPE.RC if debug == 'True': print 'write rc address {} index {} type {}'.format( instrument.address, instrument.index, instrument.type) rc_write(rfdevice, instrument, self) time.sleep(0.5)
import argparse from rpi_rf import RFDevice TRANSMIT_PIN = 2 ON_MAIN = 70741 ON_SEC = 83029 OFF_MAIN = 70740 OFF_SEC = 83028 ON_LAMP = 86101 OFF_LAMP = 86100 TRANS_LENGTH = 350 PROTOCOL = 2 trans = RFDevice(TRANSMIT_PIN) trans.enable_tx() trans.tx_repeat = 10 parser = argparse.ArgumentParser() parser.add_argument("--on") parser.add_argument("--off") args = parser.parse_args() print(args) if args.on: if args.on == "main": trans.tx_code(ON_MAIN, PROTOCOL, TRANS_LENGTH, 24) trans.cleanup() elif args.on == "sec": trans.tx_code(ON_SEC, PROTOCOL, TRANS_LENGTH, 24)
logging.basicConfig( level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S', format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', ) with open('config.yml') as f: cfg = yaml.load(f, Loader=yaml.FullLoader) app = Flask(__name__) # fixme: move into class rfdevice = RFDevice(cfg['lift']['gpio']) rfdevice.enable_tx() rfdevice.tx_repeat = cfg['lift']['repeat'] # fixme: move into class # cec.init() # cec_device = cec.Device(0) def transmit_lift_code(code): logging.info( 'lift rf transmit: %s; protocol: %s; pulse length: %d, length: %d', code, cfg['lift']['protocol'], cfg['lift']['pulse'], cfg['lift']['length']) rfdevice.tx_code(code, cfg['lift']['protocol'], cfg['lift']['pulse'], cfg['lift']['length'])