def setup_rfm98w(frequency=441.200, spi_device=0, shutdown=False, deviation = 71797): # Set this to 1 if your RFM98W is on CE1 hw = HardwareInterface(spi_device) # Start talking to the module... lora = LoRa(hw) # Set us into FSK mode, and set continuous mode on lora.set_register(0x01,0x00) # Standby Mode # If we have been asked to shutdown the RFM98W, then exit here. if shutdown: sys.exit(0) # Otherwise, proceed. lora.set_register(0x31,0x00) # Set Continuous Mode # Set TX Frequency lora.set_freq(frequency) # Set Deviation (~70 kHz). Signals ends up looking a bit wider than the RFM22B version. _dev_lsbs = int(deviation / 61.03) _dev_msb = _dev_lsbs >> 8 _dev_lsb = _dev_lsbs % 256 lora.set_register(0x04,_dev_msb) lora.set_register(0x05,_dev_lsb) # Set Transmit power to 50mW. # NOTE: If you're in another country you'll probably want to modify this value to something legal... lora.set_register(0x09,0x8F) # Go into TX mode. lora.set_register(0x01,0x02) # .. via FSTX mode (where the transmit frequency actually gets set) lora.set_register(0x01,0x03) # Now we're in TX mode...
def __init__(self): super(LoraServer,self).__init__() MyLog.debug('LoraServer in') BOARD.setup() self.lora = LoRa() self.LoraServer_init(self.lora) self.WaitCarComeTime = int(120) # 等待车子停进来的时间,2min不来就升锁 self.WaitCarLeaveTime = int(300) # 车子停进来前5min,依旧是2min升锁,超出时间立刻升锁 self.AfterCarLeaveTime = int(10) # 超出5min,认为车子是要走了,1min升锁 try: cf = configparser.ConfigParser() cf.read(path.expandvars('$HOME') + '/Downloads/WWTFrontServer_Lora/Configuration.ini', encoding="utf-8-sig") self.WaitCarComeTime = cf.getint("StartLoad", "WaitCarComeTime") self.WaitCarLeaveTime = cf.getint("StartLoad", "WaitCarLeaveTime") self.AfterCarLeaveTime = cf.getint("StartLoad", "AfterCarLeaveTime") except Exception as ex: MajorLog(ex + 'From openfile /waitcartime') MyLog.debug("WaitCarComeTime:" + str(self.WaitCarComeTime)) MyLog.debug("WaitCarLeaveTime:" + str(self.WaitCarLeaveTime)) MyLog.debug("AfterCarLeaveTime:" + str(self.AfterCarLeaveTime)) global stridList stridList = [] self.mtimer = QTimer() self.mtimer.timeout.connect(self.LockAutoDown) self.mtimer.start(1000) self.mtimer2 = QTimer() self.mtimer2.timeout.connect(self.WaitCarStatusDisable) self.mtimer2.start(1000) pass
def lorainit(): global lora BOARD.setup() lora = LoRa(verbose=False) lora.set_mode(MODE.STDBY) lora.set_freq(923.0) #set to 915MHz for SG use
self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b00000000) self.assertEqual(lora.get_dio_mapping(), dio_mapping) dio_mapping = [0, 1, 2, 0, 1, 2] lora.set_dio_mapping(dio_mapping) self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b00011000) self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b01100000) self.assertEqual(lora.get_dio_mapping(), dio_mapping) dio_mapping = [1, 2, 0, 1, 2, 0] lora.set_dio_mapping(dio_mapping) self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b01100001) self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b10000000) self.assertEqual(lora.get_dio_mapping(), dio_mapping) # def test_set_lna_gain(self): # bkup_lna_gain = lora.get_lna()['lna_gain'] # for target_gain in [GAIN.NOT_USED, GAIN.G1, GAIN.G2, GAIN.G6, GAIN.NOT_USED, bkup_lna_gain]: # print(target_gain) # lora.set_lna_gain(target_gain) # actual_gain = lora.get_lna()['lna_gain'] # self.assertEqual(GAIN.lookup[actual_gain], GAIN.lookup[target_gain]) if __name__ == '__main__': BOARD.setup() lora = LoRa(verbose=False) unittest.main() BOARD.teardown()
# own applications, or shipping pySX127x with a closed source product. # # You should have received a copy of the GNU General Public License along with pySX127. If not, see # <http://www.gnu.org/licenses/>. from SX127x.LoRa import * from SX127x.board_config import BOARD import argparse BOARD.setup() parser = argparse.ArgumentParser(description='LoRa utility functions') parser.add_argument('--dump', '-d', dest='dump', default=False, action="store_true", help="dump all registers") args = parser.parse_args() lora = LoRa(verbose=False) if args.dump: print("LoRa register dump:\n") print("%02s %18s %2s %8s" % ('i', 'reg_name', 'v', 'v')) print("-- ------------------ -- --------") for reg_i, reg_name, val in lora.dump_registers(): print("%02X %18s %02X %s" % (reg_i, reg_name, val, format(val, '#010b')[2:])) print("") else: print(lora) BOARD.teardown()
from SX127x.LoRa import * from SX127x.board_config import BOARD BOARD.setup() lora = LoRa() from pdb import set_trace set_trace() BOARD.teardown()
from SX127x.LoRa import * from SX127x.board_config import BOARD BOARD.setup() lora = LoRa() lora.set_mode(MODE.STDBY) from time import sleep sleep(2) print 'mode is', hex(lora.get_mode()) BOARD.teardown()
from SX127x.LoRa import * from SX127x.board_config import BOARD import time import RPi.GPIO as GPIO BOARD.setup() lora = LoRa() lora.set_mode(MODE.STDBY) print(lora.get_freq()) lora.set_freq(478.0) lora.set_coding_rate(CODING_RATE.CR4_6) lora.set_rx_crc(1) lora.set_register(0x1E, 0x94) print(hex(lora.get_register(0x1E))) lora.set_register(0x12, 0xff) lora.set_mode(MODE.RXCONT) lora.set_mode(MODE.RXCONT) lora.set_mode(MODE.RXCONT) value = lora.get_all_registers() print(value) print(len(value)) List = [] for i in range(0, len(value)): List.append(hex(i) + '--' + hex(value[i])) print(List)
from SX127x.LoRa import * from SX127x.board_config import BOARD import time import RPi.GPIO as GPIO BOARD.setup() lora = LoRa() lora.set_mode(MODE.SLEEP) lora.set_dio_mapping([1, 0, 0, 0, 0, 0]) lora.set_mode(MODE.STDBY) print(lora.get_freq()) lora.set_freq(478.0) lora.set_coding_rate(CODING_RATE.CR4_6) lora.set_rx_crc(1) lora.set_register(0x1E,0x94) print(hex(lora.get_register(0x1E))) lora.set_register(0x12,0xff) value = lora.get_all_registers() print(value) print(len(value)) List = [] for i in range(0,len(value)): List.append(hex(i)+'--'+hex(value[i])) print(List)
from SX127x.LoRa import * from SX127x.board_config import BOARD import argparse BOARD.setup() parser = argparse.ArgumentParser(description='LoRa utility functions') parser.add_argument('--dump', '-d', dest='dump', default=False, action="store_true", help="dump all registers") args = parser.parse_args() lora = LoRa(verbose=False) if args.dump: print("LoRa register dump:\n") print("%02s %18s %2s %8s" % ('i', 'reg_name', 'v', 'v')) print("-- ------------------ -- --------") for reg_i, reg_name, val in lora.dump_registers(): print("%02X %18s %02X %s" % (reg_i, reg_name, val, format(val, '#010b')[2:])) print("") else: print(lora) BOARD.teardown()
from SX127x.LoRa import * from SX127x.board_config import BOARD BOARD.setup() lora = LoRa() lora.set_mode(MODE.STDBY) lora.set_freq(433.0) # Set the frequency to 433 MHz print 'freq = %s' % lora.get_freq() BOARD.teardown()
def setup_rfm98w(frequency=434.650, spi_device=0, shutdown=False): # Set this to 1 if your RFM98W is on CE1 hw = HardwareInterface(spi_device) # Start talking to the module... lora = LoRa(hw) # Set us into FSK mode, and set continuous mode on lora.set_register(0x01, 0x00) # Standby Mode # If we have been asked to shutdown the RFM98W, then exit here. if shutdown: print("Transmitter shutdown.") sys.exit(0) # Otherwise, proceed. lora.set_register(0x31, 0x00) # Set Continuous Mode # Set TX Frequency lora.set_freq(frequency) # Set Deviation (~470 Hz) lora.set_register(0x04, 0x00) lora.set_register(0x05, 0x04) # Set Transmit power to 50mW. # NOTE: If you're in another country you'll probably want to modify this value to something legal... lora.set_register(0x09, 0x8F) # Go into TX mode. lora.set_register( 0x01, 0x02 ) # .. via FSTX mode (where the transmit frequency actually gets set) lora.set_register(0x01, 0x03) # Now we're in TX mode...
class LoraServer(QThread): signal = pyqtSignal(str) signal_newLock = pyqtSignal(MyLock) signal_Lock = pyqtSignal(MyLock) def __init__(self): super(LoraServer,self).__init__() MyLog.debug('LoraServer in') BOARD.setup() self.lora = LoRa() self.LoraServer_init(self.lora) self.WaitCarComeTime = int(120) # 等待车子停进来的时间,2min不来就升锁 self.WaitCarLeaveTime = int(300) # 车子停进来前5min,依旧是2min升锁,超出时间立刻升锁 self.AfterCarLeaveTime = int(10) # 超出5min,认为车子是要走了,1min升锁 try: cf = configparser.ConfigParser() cf.read(path.expandvars('$HOME') + '/Downloads/WWTFrontServer_Lora/Configuration.ini', encoding="utf-8-sig") self.WaitCarComeTime = cf.getint("StartLoad", "WaitCarComeTime") self.WaitCarLeaveTime = cf.getint("StartLoad", "WaitCarLeaveTime") self.AfterCarLeaveTime = cf.getint("StartLoad", "AfterCarLeaveTime") except Exception as ex: MajorLog(ex + 'From openfile /waitcartime') MyLog.debug("WaitCarComeTime:" + str(self.WaitCarComeTime)) MyLog.debug("WaitCarLeaveTime:" + str(self.WaitCarLeaveTime)) MyLog.debug("AfterCarLeaveTime:" + str(self.AfterCarLeaveTime)) global stridList stridList = [] self.mtimer = QTimer() self.mtimer.timeout.connect(self.LockAutoDown) self.mtimer.start(1000) self.mtimer2 = QTimer() self.mtimer2.timeout.connect(self.WaitCarStatusDisable) self.mtimer2.start(1000) pass def LoraServer_init(self,lora): print("LoraServer start init----") lora.set_mode(MODE.STDBY) lora.set_dio_mapping([0,0,0,0,0,0]) print(lora.get_freq()) lora.set_freq(478.0) lora.set_coding_rate(CODING_RATE.CR4_6) lora.set_rx_crc(1) lora.set_register(0x1E, 0x94) print(hex(lora.get_register(0x1E))) lora.set_register(0x12, 0xff) lora.set_mode(MODE.RXCONT) lora.set_mode(MODE.RXCONT) lora.set_mode(MODE.RXCONT) value = lora.get_all_registers() print(value) print(len(value)) List = [] for i in range(0, len(value)): List.append(hex(i) + '--' + hex(value[i])) print(List) for i in range(0, len(List)): print(List[i]) print('0x40--' + hex(lora.get_register(0x40))) print('0x41--' + hex(lora.get_register(0x41))) print('0x42--' + hex(lora.get_register(0x42))) print("LoraServer finish init----") def LoraServer_write(self,payload): self.lora.set_mode(MODE.STDBY) self.lora.set_dio_mapping([1, 0, 0, 0, 0, 0]) self.lora.set_pa_config(pa_select=1) self.lora.clear_irq_flags(TxDone=1) # payload1 = [0xEB, 0x90, 0x14, 0x0B, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x10, 0x02, 0xFF, 0x00, 0x29, 0xC1] # payload2 = [0xEB, 0x90, 0x14, 0x0B, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x10, 0x03, 0xFF, 0x00, 0x29, 0xC0] # payload3 = [0xEB, 0x90, 0x14, 0x0A, 0xFF, 0xFF, 0xFF, 0xFF, 0x04, 0x20, 0x02, 0x00, 0x03, 0x25] self.lora.write_payload(payload) self.lora.set_mode(MODE.TX) self.lora.set_mode(MODE.STDBY) self.lora.set_dio_mapping([0,0,0,0,0,0]) self.lora.set_mode(MODE.RXCONT) def LoraServer_read(self): self.lora.set_register(0x12, 0xff) # 将GPIO清0 flags = self.lora.get_irq_flags() rx_nb_bytes = self.lora.get_rx_nb_bytes() rx_addr = self.lora.get_fifo_rx_current_addr() payload = self.lora.read_payload() PrintList = [] for i in range(0, len(payload)): PrintList.append(hex(payload[i])) print(PrintList) return payload def LockAutoDown(self): # 定时器调用,检测无车满60s后自动发送升锁指令 for lock in SharedMemory.LockList: if lock.arm == '10': if lock.car == '00': lock.nocaron += 1 else: lock.nocaron = 0 if lock.nocaron >= self.WaitCarComeTime and lock.waitcar == False: # 降锁后等待车子来停 lock.nocaron = 0 self.sendToCan(lock.addr + '02') lock.carLeave = datetime.now() lock.reservd2 = datetime.strftime(lock.carLeave, '%Y-%m-%d %H:%M:%S') lock.carStayTime = (str(lock.carLeave - lock.carCome).split('.'))[0] lock.reservd3 = lock.carStayTime self.signal_Lock.emit(lock) t.sleep(0.05) if lock.nocaron >= self.AfterCarLeaveTime and lock.carFinallyLeave == True: # 车子离开等待60s就升锁 lock.carFinallyLeave = True lock.nocaron = 0 self.sendToCan(lock.addr + '02') t.sleep(5) if lock.arm == '10': self.LockUp(lock.addr) t.sleep(5) if lock.arm == '10': self.LockUp(lock.addr) t.sleep(5) if lock.arm == '01': lock.carLeave = datetime.now() lock.reservd2 = datetime.strftime(lock.carLeave, '%Y-%m-%d %H:%M:%S') lock.carStayTime = (str(lock.carLeave - lock.carCome).split('.'))[0] lock.reservd3 = lock.carStayTime self.signal_Lock.emit(lock) t.sleep(0.05) else: # 连续多次未判断到升锁到位,认为出现故障 lock.machine = '88' pass if lock.arm == '01' or lock.arm == '00': lock.nocaron = 0 pass def WaitCarStatusDisable(self): for lock in SharedMemory.LockList: if lock.waitcar == True: lock.waitcartime += 1 if lock.carFinallyLeave == False: lock.waitcartime2 += 1 if lock.waitcartime >= self.WaitCarComeTime: lock.waitcar = False lock.waitcartime = 0 if lock.waitcartime2 >= self.WaitCarLeaveTime: lock.carFinallyLeave = True lock.waitcartime2 = 0 pass def run(self): MyLog.debug("LoraServer run ") self.ThreadTag = True t = threading.Thread(target=ServerOn,args=(self,)) t.start() def LockUp(self, addr): buf = [0xEB, 0x90, 0x14, 0x0A, int(addr[0:2],16),int(addr[2:4],16),int(addr[4:6],16),int(addr[6:8],16), 0x05, 0x10, 0x02, 0xFF, 0x00, 0x00] print(buf) self.LoraServer_write(buf) MyLog.info("LockUp"+addr) def LockDown(self, addr): buf = [0xEB, 0x90, 0x14, 0x0A, int(addr[0:2],16),int(addr[2:4],16),int(addr[4:6],16),int(addr[6:8],16), 0x05, 0x10, 0x03, 0xFF, 0x00, 0x00] self.LoraServer_write(buf) MyLog.info('LockDown:' + addr) for lock in SharedMemory.LockList: if lock.addr == addr: lock.waitcar = True lock.waitcartime = 0 lock.waitcartime2 = 0 lock.carCome = datetime.now() lock.reservd1 = datetime.strftime(lock.carCome,'%Y-%m-%d %H:%M:%S') lock.reservd2 = '' lock.reservd3 = '' lock.carFinallyLeave = False self.signal_Lock.emit(lock) def LockCMDExcute(self, str): MyLog.debug("触发Lockcmdexcute") if len(str) == 10: addr = str[0:8] cmd = str[8:10] if cmd == '02': self.LockUp(addr) elif cmd == '03': self.LockDown(addr) else: # to do other things here pass else: MyLog.error("FrontServer-->Lock的控制指令长度不正确") pass def LockCMDExcute2(self, str): MyLog.debug("触发Lockcmdexcute2 本地点击"+str) if len(str) == 10: addr = str[2:10] cmd = str[0:2] if cmd == '02': self.LockUp(addr) MyLog.info("本地点击升锁!") elif cmd == '03': self.LockDown(addr) MyLog.info("本地点击降锁!") elif cmd == '04': buf = [int(addr[0:2], 16), int(addr[2:4], 16), int(addr[4:6], 16), int(addr[6:8], 16), 4] self.mcp2515_write(buf) MyLog.info("EnableAlarm" + addr) elif cmd == '05': buf = [int(addr[0:2], 16), int(addr[2:4], 16), int(addr[4:6], 16), int(addr[6:8], 16), 5] self.mcp2515_write(buf) MyLog.info("DisableAlarm" + addr) elif cmd == '06': buf = [int(addr[0:2], 16), int(addr[2:4], 16), int(addr[4:6], 16), int(addr[6:8], 16), 6] self.mcp2515_write(buf) MyLog.info("LockReset" + addr) else: # to do other things here pass else: MyLog.error("FrontServer-->Lock的控制指令长度不正确") pass
from SX127x.LoRa import * from SX127x.board_config import BOARD import array import hexdump def handle_input(input): ary = array.array('B', input) # print[(hexdump.dump(ary, sep=" , 0x"))] lora.write_payload(ary.tolist()) BOARD.setup() lora = LoRa() lora.set_mode(MODE.STDBY) lora.set_payload_length(100) input=raw_input('Enter a message >>\n') handle_input(input) lora.set_mode(MODE.TX) BOARD.teardown()