-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
175 lines (152 loc) · 6.54 KB
/
main.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
"""
Copyright 2020 LeMaRiva|Tech (Mauro Riva) info@lemariva.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import ujson
import utime
import config
import machine
import gc
import esp32
import network
import neopixel
from letters import characters
from third_party import string
from third_party import rsa
from umqtt.simple import MQTTClient
from ubinascii import b2a_base64
from uPySensors.pmsa003 import PMSA003
from uPySensors.bme680 import BME680
epoch_offset = 946684800
device_uart = machine.UART(1, baudrate=9600)
device_i2c = -1
sta_if = network.WLAN(network.STA_IF)
np = neopixel.NeoPixel(machine.Pin(27), 25)
bme_sensor = BME680(device_i2c, config.device_config)
pms_sensor = PMSA003(device_uart, config.device_config)
def write_2leds(letter, color):
rgb = color
char_matrix = characters.get(letter)
led_counter = 0
for row in char_matrix:
for led in row:
if(led):
np[led_counter] = rgb
else:
np[led_counter] = (0, 0, 0)
led_counter += 1
np.write()
def on_message(topic, message):
print((topic,message))
def b42_urlsafe_encode(payload):
return string.translate(b2a_base64(payload)[:-1].decode('utf-8'),{ ord('+'):'-', ord('/'):'_' })
def create_jwt(project_id, private_key, algorithm, token_ttl):
print("Creating JWT...")
private_key = rsa.PrivateKey(*private_key)
# Epoch_offset is needed because micropython epoch is 2000-1-1 and unix is 1970-1-1. Adding 946684800 (30 years)
epoch_offset = 946684800
claims = {
# The time that the token was issued at
'iat': utime.time() + epoch_offset,
# The time the token expires.
'exp': utime.time() + epoch_offset + token_ttl,
# The audience field should always be set to the GCP project id.
'aud': project_id
}
#This only supports RS256 at this time.
header = { "alg": algorithm, "typ": "JWT" }
content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8'))
content = content + '.' + b42_urlsafe_encode(ujson.dumps(claims).encode('utf-8'))
signature = b42_urlsafe_encode(rsa.sign(content,private_key,'SHA-256'))
return content+ '.' + signature #signed JWT
def get_mqtt_client(project_id, cloud_region, registry_id, device_id, jwt):
"""Create our MQTT client. The client_id is a unique string that identifies
this device. For Google Cloud IoT Core, it must be in the format below."""
client_id = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project_id, cloud_region, registry_id, device_id)
print('Sending message with password {}'.format(jwt))
client = MQTTClient(client_id.encode('utf-8'),server=config.google_cloud_config['mqtt_bridge_hostname'],port=config.google_cloud_config['mqtt_bridge_port'],user=b'ignored',password=jwt.encode('utf-8'),ssl=True)
client.set_callback(on_message)
client.connect()
client.subscribe('/devices/{}/config'.format(device_id), 1)
client.subscribe('/devices/{}/commands/#'.format(device_id), 1)
return client
def main():
wdt.feed()
if config.app_config["deepsleep"]:
esp32.wake_on_ext0(pin = machine.Pin(config.device_config["btn"], machine.Pin.IN), level = esp32.WAKEUP_ALL_LOW)
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('esp32 has been woken from a deep sleep')
# cloud connection
write_2leds("+", (0, 5, 0))
jwt = create_jwt(config.google_cloud_config['project_id'], config.jwt_config['private_key'], config.jwt_config['algorithm'], config.jwt_config['token_ttl'])
client = get_mqtt_client(config.google_cloud_config['project_id'], config.google_cloud_config['cloud_region'], config.google_cloud_config['registry_id'], config.google_cloud_config['device_id'], jwt)
gc.collect()
# sensor connection
write_2leds("+", (0, 0, 5))
bme_sensor.set_gas_heater_profile(320, 120, 0)
pms_sensor.wake_up()
# acquiring and sending data
loop = 0
while True:
wdt.feed()
machine.freq(160000000)
# acquiring data
write_2leds("+", (5, 5, 5))
timestamp = utime.time() + epoch_offset
bme_data = bme_sensor.measurements
pms_data = pms_sensor.measurements[1]
message = {
"device_id": config.google_cloud_config['device_id'],
"timestamp": timestamp,
"temp": float(bme_data["temp"]),
"hum": float(bme_data["hum"]),
"press": float(bme_data["press"]),
"gas": float(bme_data["gas"]),
"cpm10": int(pms_data["cpm10"]),
"cpm25": int(pms_data["cpm25"]),
"cpm100": int(pms_data["cpm100"]),
"apm10": int(pms_data["apm10"]),
"apm25": int(pms_data["apm25"]),
"apm100": int(pms_data["apm100"])
}
#sending data
print("Publishing message "+str(ujson.dumps(message)))
write_2leds("+", (5, 0, 0))
mqtt_topic = '/devices/{}/{}'.format(config.google_cloud_config['device_id'], 'events')
client.publish(mqtt_topic.encode('utf-8'), ujson.dumps(message).encode('utf-8'))
client.check_msg() # Check for new messages on subscription
#wating and cleaning
gc.collect()
write_2leds("-", (0, 0, 5))
utime.sleep_ms(config.app_config["delay"]) # Delay for delay seconds.
loop += 1
if loop >= config.app_config["loops"]:
loop = 0
write_2leds("+", (2, 2, 2))
print("Going to sleep for about %s milliseconds!" % config.app_config["deepsleepms"])
pms_sensor.power_off()
bme_sensor.power_off()
if config.app_config["deepsleep"]:
utime.sleep_ms(1000)
machine.deepsleep(config.app_config["deepsleepms"])
else:
sta_if.active(False)
machine.freq(20000000)
utime.sleep_ms(config.app_config["deepsleepms"])
machine.reset()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
raise
except:
utime.sleep_ms(10000)
machine.reset()