-
Notifications
You must be signed in to change notification settings - Fork 1
/
ulp_reed.py
295 lines (258 loc) · 8.2 KB
/
ulp_reed.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import machine, time
import onewire, ds18x20
import reed_setting as setting
import esp32
import uio, ure, utime, urequests
import network, socket
import sys
from esp32 import ULP
from machine import Pin, mem32, ADC
from esp32_ulp import src_to_binary
import json
#the ulp source code is ULP pulse counter working on pin 0, improved copy of code from here https://esp32.com/viewtopic.php?t=13638
voltage1 = ""
source = """\
#define DR_REG_RTCIO_BASE 0x3ff48400
#define RTC_IO_TOUCH_PAD0_REG (DR_REG_RTCIO_BASE + 0x94)
#define RTC_IO_TOUCH_PAD0_MUX_SEL_M (BIT(19))
#define RTC_IO_TOUCH_PAD0_FUN_IE_M (BIT(13))
#define RTC_GPIO_IN_REG (DR_REG_RTCIO_BASE + 0x24)
#define RTC_GPIO_IN_NEXT_S 14
/* Define variables, which go into .bss section (zero-initialized data)
.bss*/
/* Next input signal edge expected: 0 (negative) or 1 (positive) */
.global next_edge
next_edge:
.long 0
/* Total number of signal edges acquired */
.global edge_count
edge_count:
.long 0
/* RTC IO number used to sample the input signal. Set by main program. */
.global io_number
io_number:
.long 10
/* Code goes into .text section */
.text
.global entry
entry:
# connect GPIO to the RTC subsystem so the ULP can read it
WRITE_RTC_REG(RTC_IO_TOUCH_PAD0_REG, RTC_IO_TOUCH_PAD0_MUX_SEL_M, 1, 1)
# switch the GPIO into input mode
WRITE_RTC_REG(RTC_IO_TOUCH_PAD0_REG, RTC_IO_TOUCH_PAD0_FUN_IE_M, 1, 1)
/* Load io_number */
move r3, io_number
ld r3, r3, 0
/* Lower 16 IOs and higher need to be handled separately,
* because r0-r3 registers are 16 bit wide.
* Check which IO this is.
*/
move r0, r3
jumpr read_io_high, 16, ge
/* Read the value of lower 16 RTC IOs into R0 */
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S, 16)
rsh r0, r0, r3
jump read_done
/* Read the value of RTC IOs 16-17, into R0 */
read_io_high:
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + 16, 2)
sub r3, r3, 16
rsh r0, r0, r3
read_done:
and r0, r0, 1
/* State of input changed? */
move r3, next_edge
ld r3, r3, 0
add r3, r0, r3
and r3, r3, 1
jump edge_detected, eq
/* Not changed */
jump entry
.global edge_detected
edge_detected:
/* Flip next_edge */
move r3, next_edge
ld r2, r3, 0
add r2, r2, 1
and r2, r2, 1
st r2, r3, 0
/* Increment edge_count */
move r3, edge_count
ld r2, r3, 0
add r2, r2, 1
st r2, r3, 0
halt
"""
logfile = uio.open("log.txt","a")
load_addr, entry_addr = 0, 3*4
ULP_MEM_BASE = 0x50000000
ULP_DATA_MASK = 0xffff # ULP data is only in lower 16 bits
led = machine.Pin(5, machine.Pin.OUT)
led.value(1)
def blink(value=1):
"""
The led blink is used to understand whats is happening with board
1 blink - ulp initialized
2 blinks - wifi init success
3 blinks - data sent to server
It can be eliminated to save batery live
"""
if setting.log_level==0:
return
for i in range(0, value):
led.value(0)
time.sleep(0.3)
led.value(1)
time.sleep(0.3)
def log(msg="None", level=1):
"""
This is log level which helps debuging of board connected via rhell or standalone one
"""
timeNow= time.localtime()
dateTime = "{:0>4d}-{:0>2d}-{:0>2d} {:0>2d}:{:0>2d}:{:0>2d}".format(timeNow[0], timeNow[1], timeNow[2], timeNow[3], timeNow[4], timeNow[5])
message = "{} : {}".format(dateTime, msg)
print(message)
if level >=3:
logfile.write(message+"\n")
logfile.flush()
def value(start=0):
"""
Function to read variable from ULP memory
"""
val = (int(hex(mem32[ULP_MEM_BASE + start*4] & ULP_DATA_MASK),16))
log("Reading value: " + str(val))
return val
def init_ulp():
binary = src_to_binary(source)
ulp = ULP()
ulp.set_wakeup_period(0, 50000) # use timer0, wakeup after 50.000 cycles
ulp.load_binary(load_addr, binary)
ulp.run(entry_addr)
log("ULP Started")
def setval(start=1, value=0x0):
"""
Function to set variable in ULP memory
"""
mem32[ULP_MEM_BASE + start*4] = value
def getTemp():
data = {}
if setting.temp_sensor == "DHT11" or setting.temp_sensor=="DHT22":
import dht
sensor = None
if setting.temp_sensor == "DHT11":
sensor = dht.DHT11(machine.Pin(setting.tempPin))
elif setting.temp_sensor=="DHT22":
sensor = dht.DHT22(machine.Pin(setting.tempPin))
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
if t > setting.threshold:
log("Temperature too high - return")
return
data["Temperature"] = "{:3.1f}".format(t)
data["Humidity"] = "{:3.1f}".format(h)
elif setting.temp_sensor == "DS18X20":
import onewire, ds18x20
ds_pin = machine.Pin(setting.tempPin)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
ds_sensor.convert_temp()
t = ds_sensor.read_temp(roms[0])
if t > setting.threshold:
log("Temperature too high {} - return".format(t),3)
return
data["Temperature"] = "{:3.1f}".format(t)
elif setting.temp_sensor == "dummy":
data["Temperature"] = "{:3.1f}".format(33.3)
data["Humidity"] = "{:3.1f}".format(33.3)
return data
def setupWifi():
ap_if = network.WLAN(network.AP_IF)
ap_if.active(0)
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
log("Disconnected")
sta_if.active(1)
sta_if.connect(setting.wifiSSID,setting.wifiPWD)
while not sta_if.isconnected():
pass
log(sta_if.ifconfig())
def parseTime( response):
#Parse time actualy calculates sleep time base on time from server HTTP response
search = ure.compile("(\d\d):(\d\d):(\d\d)")
log(response)
match = ure.search(search, response)
if match:
mins = int(match.group(2) )
secs = int(match.group(3) )
log(str(mins)+" " +str(secs))
return ( (setting.LPlen*60) - (( mins * 60 + secs ) % (setting.LPlen*60) ) ) * 1000
else:
#Sleep 30 seconds in case of failure
return 30000
def client():
message = {}
if setting.bat_measure:
message["Voltage"] = voltage1
log("Client start", 3)
pulses = value(1)
message[setting.channel] = pulses/2*setting.multiplier
setval(1,0x0)
temp = getTemp()
message.update(temp)
log(str(message), 3)
js = json.dumps(message)
js = str(js).replace('"','').replace("'","")
log(str(js), 3)
try:
s = socket.socket()
#15 seconds timeout
s.settimeout(15.0)
s.connect((setting.server, setting.port))
post = 'POST /api/v1/'+setting.device_id+'/telemetry HTTP/1.1\r\nHost: '+setting.server+':'+str(setting.port)+'\r\nUser-Agent: ESP32\r\nAccept:*/*\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: '+str(len(str(js)))+'\r\n\r\n'+str(js)+'\r\n\r\n'
log(post)
s.sendall(bytes(post,'utf8'))
response = ""
log("Response\n")
while True:
resp = s.readline()
if resp:
response += str(resp.decode("utf-8"))
else:
break
if resp:
log(str(resp, 'utf8'))
s.close()
except Exception as e:
log("Exception:\n")
sys.print_exception(e, logfile)
logfile.flush()
setval(1, pulses + value(1))
machine.deepsleep(15*60*1000)
return parseTime(response)
try:
# The code logic itself
if setting.bat_measure:
p = ADC(Pin(34))
p.atten(ADC.ATTN_11DB)
voltage1 = "{:3.1f}".format(p.read()*1.7)
log(machine.reset_cause(),3)
#Init UlP
if machine.reset_cause()==machine.PWRON_RESET or machine.reset_cause()==machine.HARD_RESET or machine.reset_cause()==machine.SOFT_RESET:
init_ulp()
setval(1,0x0)
blink(1)
#Start wifi
setupWifi()
blink(2)
#run thingboard client, send data and sleep
sleepTime = client()
blink(3)
log(sleepTime)
machine.deepsleep(sleepTime)
except Exception as e:
logfile = uio.open("log.txt","a")
sys.print_exception(e)
sys.print_exception(e, logfile)
logfile.flush()
machine.reset()