-
Notifications
You must be signed in to change notification settings - Fork 6
/
lopy-receiver.py
132 lines (96 loc) · 2.98 KB
/
lopy-receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import _thread
import socket
import pycom
import utime
from machine import WDT
from machine import RTC
from network import LoRa
## Set RTC for timestamping (although it's not currently used)
rtc = machine.RTC()
rtc.ntp_sync("pool.ntp.org")
utime.sleep_ms(750)
print("\nRTC Set from NTP to UTC:", rtc.now())
utime.timezone(7200)
print("Adjusted from UTC to EST timezone", utime.localtime(), "\n")
## Set up the LoRa in longer range mode
lora = LoRa(mode=LoRa.LORA, frequency=868000000, tx_power=14, bandwidth=LoRa.BW_125KHZ, sf=7)
l = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
l.setblocking(False)
## Set up web socket
wdt = WDT(timeout=5000)
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.settimeout(1)
s.bind(addr)
s.listen(1)
print("listening on", addr)
## Threaded server
def _serve():
while True:
wdt.feed()
method, route, proto = None, None, None
ts = utime.time()
try:
cl, addr = s.accept()
except OSError:
continue
print("client connected from", addr)
while True:
try:
line = cl.readline()
print(line)
## Only handle GET requests
if line[0:3] == b"GET":
method, route, proto = line.split(b" ")
if not line or line == b'\r\n':
break
except MemoryError:
cl.send(" ")
cl.close()
continue
## Boilerplate JSON response
response = """HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/javascript\r\n\r\n{
"position": {
"latitude": %f,
"longitude": %f
},
"message": "%s",
"timestamp": %i
}"""
## Crude hack to handle URL routes
if route is not None:
route = route.split(b"/")
route.pop(0)
print(route)
## If position.json is requested then receive LoRa message from sender
if route[0] == b"position.json":
msg = l.recv(64)
## Decode and split out into latitude and longitude
if not msg == b"":
msg = msg.decode("utf-8")
lat, lon = [float(i) for i in msg.split(",")]
response = response % (lat, lon, "success", ts)
print(response)
## Otherwise, send 404
else:
response = """HTTP/1.1 404 Not Found
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Pragma: no-cache
"""
print(response)
## Handles missing routes by sending 404
else:
response = """HTTP/1.1 404 Not Found
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Pragma: no-cache
"""
cl.send(response)
cl.close()
## Start the thread
_thread.start_new_thread(_serve, ())