J
@johncaipa I have quite a lot of stuff going on in my code, but the main differences from yours are:
I always to the initial join manually, so I don't try to detect whether that already happened or not.
I use OTAA joins
I keep the socket blocking
I do wake on pin using the Pysense
Here's a slimmed down version. Important! Don't use this code as is! This code requires a Pysense firmware which supports INT wake-up.
from network import LoRa
from network import WLAN
from network import Server
import socket
import time
import binascii
from pysense import Pysense
import pycom
import machine
app_version = '2017-10-31-01'
SLEEP = 150
py = Pysense()
def read_pin():
return 1 if py.peek_memory(0xE) & 0x02 else 0
pycom.heartbeat(0)
app_eui = binascii.unhexlify('***')
app_key = binascii.unhexlify('***')
# Whatever happens, init the LoRa stack and (try to) restore its state
lora = LoRa(mode=LoRa.LORAWAN, adr=True)
lora.nvram_restore()
# This part is not called from the code, only manually
def doJoin(lora):
print(time.localtime(),"LoRa join")
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
while not lora.has_joined():
time.sleep(1)
print('Not yet joined...')
lora.nvram_save()
print(time.localtime(),"done LoRa join")
# This is the debug mode, entered when the button is pressed
def debugMode():
# set led to orange
pycom.rgbled(0x402000)
wlan = WLAN()
wlan.init(mode=WLAN.AP, ssid='LoPy-'+binascii.hexlify(lora.mac()).decode('ascii'), auth=(WLAN.WPA2,'***'), channel=7, antenna=WLAN.INT_ANT)
server = Server()
# set led to green once we're ready
pycom.rgbled(0x00ff00)
# This is a modified version of the Pysense go_to_sleep which keeps INT working
def go_to_sleep2():
PORTC_ADDR = const(0x00E)
ANSELA_ADDR = const(0x18C)
ANSELB_ADDR = const(0x18D)
ANSELC_ADDR = const(0x18E)
ADCON0_ADDR = const(0x9D)
CMD_GO_SLEEP = const(0x21)
# disable back-up power to the pressure sensor
py.mask_bits_in_memory(PORTC_ADDR, ~(1 << 7))
py.poke_memory(ADCON0_ADDR, 0) # disable the ADC
py.poke_memory(ANSELA_ADDR, ~(1 << 3)) # Don't touch RA3 so that button wake up works
py.poke_memory(ANSELB_ADDR, 0xFF)
py.poke_memory(ANSELC_ADDR, ~((1 << 7)|(1 << 1)))
py._write(bytes([CMD_GO_SLEEP]), wait=False)
# kill the run pin
Pin('P3', mode=Pin.OUT, value=0)
def sendData(val):
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, 0)
s.send(bytes([val]));
# This is the main function
def doTheWork():
if py.button_pressed():
debugMode()
return
p = read_pin()
sendData(p)
# send is blocking and should have waited long enough, but I'm a belt-and-suspenders kind of guy
time.sleep(2)
lora.nvram_save()
py.setup_sleep(SLEEP)
# Set edge
py.poke_memory(0x95, py.peek_memory(0x95) & ~0x40 if p else py.peek_memory(0x95) | 0x40)
# Enable INTE
# If your Pysense is not running a version of the firmware that supports it, this will probably lock it up
py.poke_memory(0x0b, py.peek_memory(0x0b) | 0x10)
go_to_sleep2()
doTheWork()
Note: I haven't tried running this version of the code, so I may have cut a bit too much.