R
@ambropete
import binascii
import pycom
import socket
import time
import ujson # put that into the import section
from network import LoRa
from machine import Pin, Timer
echo = Pin(Pin.exp_board.G7, mode=Pin.IN)
trigger = Pin(Pin.exp_board.G8, mode=Pin.OUT)
# Colors
off = 0x000000
red = 0xff0000
green = 0x00ff00
blue = 0x0000ff
# Turn off hearbeat LED
pycom.heartbeat(False)
# Initialize LoRaWAN radio
lora = LoRa(mode=LoRa.LORAWAN)
# Set network keys
app_key = binascii.unhexlify('11 22 33 44 55 66 77 88 99 00 11 22 33 44 55 65'.replace(' ',''))
app_eui = binascii.unhexlify('00 00 00 00 00 00 00 00'.replace(' ',''))
# Join the network
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
pycom.rgbled(red)
# Loop until joined
while not lora.has_joined():
print('Not joined yet...')
pycom.rgbled(off)
time.sleep(0.1)
pycom.rgbled(red)
time.sleep(2)
print('Joined')
pycom.rgbled(blue)
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.setblocking(True)
trigger(0)
chrono = Timer.Chrono()
while True:
chrono.reset()
trigger(1)
time.sleep_us(10)
trigger(0)
while echo() == 0:
pass
chrono.start()
while echo() == 1:
pass
chrono.stop()
distance = chrono.read_us() / 58.0
s.send(str(distance).encode())
pycom.rgbled(green)
time.sleep(0.5)
pycom.rgbled(blue)
time.sleep(0.5)
print("Distance {:.0f} cm".format(distance))
time.sleep(5)