""" ABP Node example compatible with the LoPy Nano Gateway """ from network import LoRa import socket import binascii import struct import time from machine import Pin #p_Relay1 = Pin(Pin.module.P5, mode=Pin.OUT) """ p_Relay1 = Pin('P5', mode=Pin.OUT) p_Relay2 = Pin('P6', mode=Pin.OUT) p_Relay3 = Pin('P7', mode=Pin.OUT) """ p_Relay4 = Pin('P8', mode=Pin.OUT) comptador = 0 def pin_handler(arg): global comptador print("%d) got an interrupt in pin %s" % (comptador,arg.id())) s.send(b'PKT #' + bytes([comptador])) time.sleep(4) comptador = comptador + 1 rx = s.recv(256) if rx: print(rx) if rx == b'u': #p_Relay1.value(1) print("Relay 1 ON") if rx == b'U': #p_Relay1.value(0) print("Relay 1 OFF") if rx == b'd': #p_Relay2.value(1) print("Relay 2 ON") if rx == b'D': #p_Relay2.value(0) print("Relay 2 OFF") if rx == b't': #p_Relay3.value(1) print("Relay 3 ON") if rx == b'T': #p_Relay3.value(0) print("Relay 3 OFF") if rx == b'q': p_Relay4.value(1) print("Relay 4 ON") if rx == b'Q': p_Relay4.value(0) print("Relay 4 OFF") time.sleep(6) p_in = Pin('P13', mode=Pin.IN, pull=Pin.PULL_UP) p_in.callback(Pin.IRQ_FALLING , pin_handler) # create an OTA authentication params # dev_eui = binascii.unhexlify('70B3D5499A016D2F') # app_eui = binascii.unhexlify('70B3D57ED0009597') # app_key = binascii.unhexlify('3748F979ACC327EE740A3C6C7BD1B450') # Initialize LoRa in LORAWAN mode. lora = LoRa(mode=LoRa.LORAWAN) # create an ABP authentication params dev_addr = struct.unpack(">l", binascii.unhexlify('26 01 14 92'.replace(' ','')))[0] # these settings can be found from TTN nwk_swkey = binascii.unhexlify('C3 A9 7C 21 69 07 15 1E DC E2 0B B2 68 CE 3B 37'.replace(' ','')) # these settings can be found from TTN app_swkey = binascii.unhexlify('46 1D BD 77 DC F9 07 5F 8A 42 4C 5F 7E F8 B6 C2'.replace(' ','')) # these settings can be found from TTN # join a network using ABP (Activation By Personalization) lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey)) # remove all the non-default channels for i in range(3, 16): lora.remove_channel(i) # set the 3 default channels to the same frequency lora.add_channel(0, frequency=868100000, dr_min=0, dr_max=5) lora.add_channel(1, frequency=868100000, dr_min=0, dr_max=5) lora.add_channel(2, frequency=868100000, dr_min=0, dr_max=5) # create a LoRa socket s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) # set the LoRaWAN data rate s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5) # make the socket non-blocking s.setblocking(False) """ Your own code can be written below! """ for i in range (2): #while True: s.send(b'PKT #' + bytes([i])) time.sleep(4) rx = s.recv(256) if rx: print(rx) time.sleep(6)