Commit 720d36c8 authored by Marco Dallagiacoma's avatar Marco Dallagiacoma

Refactor client

parent 48315147
import _thread
import time
import pycom
import struct
......@@ -6,6 +7,8 @@ import ubinascii
from network import LoRa
from network import Bluetooth
SYNC_INTERVAL_IN_SECONDS = 6
bluetooth = Bluetooth()
bluetooth.start_scan(-1) # start scanning with no timeout
......@@ -34,27 +37,35 @@ 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)
""" Your own code can be written below! """
def to_ascii(hex_string):
return ubinascii.hexlify(hex_string)
# uplink sending and downlink receiving
i = 0;
while(True):
def extract_device_info(raw_info):
# print(raw_info)
return {
'mac': to_ascii(raw_info.mac),
'rssi': raw_info.rssi,
'data': to_ascii(raw_info.data),
'addr_type': raw_info.addr_type,
'adv_type': raw_info.adv_type,
'ts': time.time()
}
def get_nearby_devices_info():
bluetooth_devices = bluetooth.get_advertisements()
return [extract_device_info(device)
for device in bluetooth_devices]
def upload_to_lora(pkt):
print("UPLOADING...")
pycom.rgbled(0x000033)
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) # create a LoRa socket
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5) # set the LoRaWAN data rate
s.setblocking(False) # make the socket non-blocking
i +=1
# uplink sending
pkt = b'PKT #' + str(i)
# print("address"+ '||'.join(bluetooth.get_adv()))
print("address: {}".format(bluetooth.get_adv()))
s.send(pkt)
print(pkt + " sent")
time.sleep(6) # this dead-time is needed by the network to gather to your LoRa packets
# LED blinking
pycom.rgbled(0x000033)
time.sleep(1)
pycom.rgbled(0x000000)
......@@ -63,4 +74,30 @@ while(True):
if rx:
print('Received: {}, on port: {}'.format(rx, port))
s.close() # close the LoRa socket
\ No newline at end of file
s.close() # close the LoRa socket
# uplink sending and downlink receiving
i = 0;
last_upload_to_server = time.time()
to_be_uploaded = {}
def append_for_mac(mac_addr, data):
if mac_addr not in to_be_uploaded:
to_be_uploaded[mac_addr] = []
to_be_uploaded[mac_addr].append(data)
while(True):
i += 1
# uplink sending
pkt = b'PKT #' + str(i)
devices_info = get_nearby_devices_info()
for device_info in devices_info:
append_for_mac(device_info['mac'], device_info)
should_upload = (time.time() - last_upload_to_server) >= SYNC_INTERVAL_IN_SECONDS
if should_upload:
_thread.start_new_thread(upload_to_lora, [pkt])
last_upload_to_server = time.time()
......@@ -99,7 +99,7 @@ password = "&p="+password
first_loop = True
old_value = ""
if(current_jwt!=-1):
if current_jwt != -1:
while (True):
......@@ -115,10 +115,10 @@ if(current_jwt!=-1):
value = jsonresult['values'][0][2]
print("VALUE_STORED " + value)
if(first_loop):
if first_loop:
old_value = value
first_loop = False
if(value != old_value):
if value != old_value:
downlink_msg = str('{"message":"'+value+'"}')
print(downlink_msg)
sendDownlink(current_jwt, downlink_msg)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment