-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.py
144 lines (132 loc) · 4.22 KB
/
boot.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
133
134
135
136
137
138
139
140
141
142
143
144
# This file is executed on every boot (including wake-boot from deepsleep)
import esp
esp.osdebug(None)
import os
import time
import _thread
from neo6m import Neo6mGPS
import machine
from machine import Pin, I2C, UART
from wifi_manager import WiFiManager
from ssd1306 import SSD1306_I2C
from lora import lora
led = Pin(4, Pin.OUT)
pir = Pin(33, Pin.IN)
l = Pin(32, Pin.IN)
SSID = "WiFi_SSID"
PSK = "*******"
WIFI_CONNECT = False
WM = WiFiManager()
if WIFI_CONNECT:
WM.connect(SSID,PSK)
import webrepl
webrepl.start()
i2c = I2C(sda=Pin(21), scl=Pin(22))
display = SSD1306_I2C(128, 64, i2c)
uart = UART(2, 9600)
uart.init(9600, bits=8, parity=None, stop=1)
POS = {"Type":"loc","ID":str(machine.unique_id())}
RX = False
TX = False
def show_ip():
global WIFI_CONNECT
global WM
if WIFI_CONNECT:
rssi = WM.get_rssi()
display.fill(0)
display.text(SSID, 0, 0, 1)
display.text('RSSI: '+str(rssi), 0, 10, 1)
display.text(WM.get_ip(), 0, 20, 1)
else:
display.fill(0)
display.text("Not Connected", 0, 0, 1)
def handle_interrupt(pin):
transmit_loc()
def req_loc(pin):
try:
global TX
TX = True
payload = {"Type":"req_loc"}
lora.println(str(payload))
TX = False
except Exception as e:
print(e)
def transmit_loc():
try:
global TX
TX = True
lora.println(str(POS))
display.fill(0)
display.text("LORA TX",0,0,1)
display.show()
TX = False
except Exception as e:
print(e)
def GPSThread():
while True:
global RX
global TX
if TX == False and RX == False:
data = uart.read()
if data != None:
try:
show_ip()
gps = Neo6mGPS(data)
global POS
if gps.valid_loc:
lat = gps.latitude()
lng = gps.longitude()
if lat != None and lng != None:
display.text("Lat : "+gps.latitude(), 0, 30, 1)
display.text("Lng : "+gps.longitude(), 0, 40, 1)
POS["Lat"] = gps.latitude()
POS["Lng"] = gps.longitude()
if gps.valid_time:
hour = gps.hour()
minute = gps.minute()
sec = gps.sec()
t = hour+":"+minute+":"+sec
if hour != None and minute != None and sec != None:
display.text(t, 0, 50, 1)
POS["Time"] = hour+":"+minute+":"+sec
display.show()
except Exception as err:
print(err)
time.sleep(1)
def receive():
while True:
if lora.receivedPacket():
try:
global RX
RX = True
payload = lora.readPayload().decode()
lora.collectGarbage()
rssi = lora.packetRssi()
display.fill(0)
display.text("LORA RX", 0, 0, 1)
res = eval(payload)
if "Type" in res:
if res["Type"] == "loc":
record = open("record.txt", "a")
record.write(str(res)+"\r\n")
record.close()
if "Time" in res:
display.text(res["Time"], 0, 10, 1)
if "Lat" in res:
display.text(res["Lat"], 0, 20, 1)
if "Lng" in res:
display.text(res["Lng"], 0, 30, 1)
display.text("RSSI: "+str(rssi), 0, 40, 1)
display.show()
time.sleep(1)
elif res["Type"] == "req_loc":
transmit_loc()
RX = False
except Exception as e:
print(e)
show_ip()
display.show()
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
l.irq(trigger=Pin.IRQ_RISING, handler=req_loc)
_thread.start_new_thread(GPSThread, ())
_thread.start_new_thread(receive, ())