This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbtest-webcomand.py
199 lines (169 loc) · 5.53 KB
/
dbtest-webcomand.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
import machine
import utime
import ustruct
import sys
import ujson
import network
import requests
ssid = 'WIFI SSID'
password = 'WIFI PASSWORD'
led0 = machine.Pin("LED", machine.Pin.OUT)
led0.off()
###############################################
# Constants
# I2C address
PCBARTISTS_DBM = 0x48
# Registers
I2C_REG_VERSION = 0x00
I2C_REG_ID3 = 0x01
I2C_REG_ID2 = 0x02
I2C_REG_ID1 = 0x03
I2C_REG_ID0 = 0x04
I2C_REG_SCRATCH = 0x05
I2C_REG_CONTROL = 0x06
I2C_REG_TAVG_HIGH = 0x07
I2C_REG_TAVG_LOW = 0x08
I2C_REG_RESET = 0x09
I2C_REG_DECIBEL = 0x0A
I2C_REG_MIN = 0x0B
I2C_REG_MAX = 0x0C
I2C_REG_THR_MIN = 0x0D
I2C_REG_THR_MAX = 0x0E
I2C_REG_HISTORY_0 = 0x14
I2C_REG_HISTORY_99 = 0x77
###############################################
# Settings
# Initialize I2C with pins
i2c = machine.I2C(0,
scl=machine.Pin(17),
sda=machine.Pin(16),
freq=100000)
###############################################
# Functions
def reg_write(i2c, addr, reg, data):
"""
Write bytes to the specified register.
"""
# Construct message
msg = bytearray()
msg.append(data)
# Write out message to register
i2c.writeto_mem(addr, reg, msg)
def reg_read(i2c, addr, reg, nbytes=1):
"""
Read byte(s) from specified register. If nbytes > 1, read from consecutive
registers.
"""
# Check to make sure caller is asking for 1 or more bytes
if nbytes < 1:
return bytearray()
# Request data from specified register(s) over I2C
data = i2c.readfrom_mem(addr, reg, nbytes)
return data
###############################################
# Main
# Read device ID to make sure that we can communicate with the ADXL343
data = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_VERSION)
print("dbMeter VERSION = 0x{:02x}".format(int.from_bytes(data, "big")))
data = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_ID3, 4)
print("Unique ID: 0x{:02x} ".format(int.from_bytes(data, "big")))
#1000ms
#reg_write(i2c, PCBARTISTS_DBM, I2C_REG_TAVG_HIGH, 0x03) # 1000ms
#reg_write(i2c, PCBARTISTS_DBM, I2C_REG_TAVG_LOW, 0xe8) # 1000ms
thi = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_TAVG_HIGH)
tlo = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_TAVG_LOW)
if tlo == b'\xe8' and thi == b'\x03':
print("T avg = 1000ms")
else:
print("Tavg high = 0x{:02x}".format(int.from_bytes(thi, "big")))
print("Tavg low = 0x{:02x}".format(int.from_bytes(tlo, "big")))
#data = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_CONTROL)
#print("Control = 0B{:08b}".format(int.from_bytes(data, "big")))
reg_write(i2c, PCBARTISTS_DBM, I2C_REG_RESET, 0B00000010) # resets min/max
#open logfile
file=open("log.log","a")
print("STARTUP")
file.write(str(utime.localtime()) + " STARTUP\n")
file.flush()
#now get the wifi going
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connection to establish
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
utime.sleep(1)
# Manage connection errors
if wlan.status() != 3:
file.write(str(utime.localtime()) + " could not connect to wifi\n")
file.flush()
raise RuntimeError('could not connect to wifi')
else:
print('connected')
led0.on()
utime.sleep(0.1)
led0.off()
token = "INSERT TOKEN HERE"
if token == "":
print("TOKEN IS EMPTY")
file.write(str(utime.localtime()) + " TOKEN IS EMPTY\n")
file.flush()
while True:
utime.sleep(59)
data = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_DECIBEL)
dbmin = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_MIN)
dbmax = reg_read(i2c, PCBARTISTS_DBM, I2C_REG_MAX)
print("Sound Level (dB SPL) = {:03d} / min {:03d} / max {:03d}".format(int.from_bytes(data, "big"),int.from_bytes(dbmin, "big"),int.from_bytes(dbmax, "big")))
#send data
headers = {
"Authorization": "Token " + token,
"Content-Type": "application/json"
}
datatosend = { "parent": "/Bases/nm1",
"data": {
"type": "comand",
"version": "1.0",
"contents":
[
{
"Type": "Noise",
"Min": int.from_bytes(dbmin, "big"),
"Max": int.from_bytes(dbmax, "big"),
"DeviceID": "gabe1"
}
]
}}
print(ujson.dumps(datatosend))
resp = None
jsonstr = ""
try:
resp=requests.post("https://noisemeter.webcomand.com/ws/put", headers=headers, data=ujson.dumps(datatosend))
#jsonstr = str(resp.text).replace("\n0\r","").replace("\n","").replace("\r","").replace("\n","").replace("\r","").replace("55{","{").replace("}0","}")
jsonstr = str(resp.text)
except:
if resp is None:
print("request error (no other info)")
jsonstr = "[EMPTY]"
else:
print("non-fatal request error: " + str(resp.reason) + " / " + str(resp.text))
print("jsonstr: " + jsonstr)
file.write(str(utime.localtime()) + " jsonstr: "+jsonstr+"\n")
file.flush()
if not '"status_code":201' in jsonstr:
if jsonstr is None:
print("unexpected status (no other info)")
file.write(str(utime.localtime()) + " unexpected status (no other info)\n")
file.flush()
else:
print("unexpected status: " + jsonstr)
file.write(str(utime.localtime()) + " unexpected status: " + jsonstr + "\n")
file.flush()
reg_write(i2c, PCBARTISTS_DBM, I2C_REG_RESET, 0B00000010) # resets min/max
led0.on()
utime.sleep(1)
led0.off()
sys.exit()