Skip to content

Commit 837d173

Browse files
committed
initial commit
0 parents  commit 837d173

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
webrepl
2+
data.txt

boot.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file is executed on every boot (including wake-boot from deepsleep)
2+
#import esp
3+
#esp.osdebug(None)
4+
import gc
5+
import webrepl
6+
gc.collect()
7+
8+
print('\nbooting...')
9+
10+
def do_connect():
11+
import network
12+
13+
file = open('data.txt')
14+
pw = file.read().strip()
15+
file.close()
16+
17+
ap_if = network.WLAN(network.AP_IF)
18+
ap_if.config(essid='Polly', password=pw)
19+
print('access point Polly open')
20+
21+
sta_if = network.WLAN(network.STA_IF)
22+
if not sta_if.isconnected():
23+
print('connecting to network...')
24+
sta_if.active(True)
25+
sta_if.connect('Bam-net', pw)
26+
while not sta_if.isconnected():
27+
pass
28+
print('connected at:', sta_if.ifconfig())
29+
30+
31+
do_connect()
32+
33+
webrepl.start()

main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sds011
2+
import machine
3+
import time
4+
import esp
5+
6+
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
7+
print('Woke from a deep sleep')
8+
9+
READ_SECONDS = 55
10+
SLEEP_SECONDS = 5
11+
12+
sds011.wake()
13+
sds011.read(READ_SECONDS * 1000);
14+
sds011.sleep();
15+
# esp.deepsleep(SLEEP_SECONDS * 1000000)

mqtt.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from umqtt.robust import MQTTClient
2+
import ubinascii as binascii
3+
import machine
4+
import ujson as json
5+
6+
MQTT_CONFIG = { 'broker': '192.168.0.3',
7+
'client_id': b'polly_' + binascii.hexlify(machine.unique_id()) }
8+
9+
MQTT_CONFIG.update({'topic': b'polly/' + MQTT_CONFIG['client_id']})
10+
11+
client = MQTTClient(MQTT_CONFIG['client_id'], MQTT_CONFIG['broker'])
12+
client.connect()
13+
14+
print('MQTT client connected to broker', MQTT_CONFIG['broker'])
15+
16+
def publish(msg):
17+
global client
18+
msg.update({
19+
'client_id': MQTT_CONFIG['client_id']
20+
})
21+
client.publish(MQTT_CONFIG['topic'], bytes(json.dumps(msg), 'utf8'))
22+
print('Message sent to', MQTT_CONFIG['broker'], 'on', MQTT_CONFIG['topic'])

sds011.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Reply format. See http://cl.ly/ekot
3+
4+
0 Header '\xaa'
5+
1 Command '\xc0'
6+
2 DATA1 PM2.5 Low byte
7+
3 DATA2 PM2.5 High byte
8+
4 DATA3 PM10 Low byte
9+
5 DATA4 PM10 High byte
10+
6 DATA5 ID byte 1
11+
7 DATA6 ID byte 2
12+
8 Checksum Low byte of sum of DATA bytes
13+
9 Tail '\xab'
14+
15+
"""
16+
17+
import machine
18+
import ustruct as struct
19+
import mqtt
20+
import sys
21+
import utime as time
22+
23+
uart = machine.UART(0, 9600)
24+
uart.init(9600, bits=8, parity=None, stop=1)
25+
26+
def wake():
27+
global uart
28+
while uart.read(1) == None:
29+
print('Sending wake command to sds011')
30+
cmd = b'\xaa\xb4\x06\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x06\xab'
31+
uart.write(cmd)
32+
print('sds011 woke up!')
33+
34+
def sleep():
35+
global uart
36+
AWAKE = True
37+
while AWAKE:
38+
print('Sending sleep command to sds011')
39+
cmd = b'\xaa\xb4\x06\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x05\xab'
40+
uart.write(cmd)
41+
header = uart.read(1)
42+
if header == b'\xaa':
43+
command = uart.read(1)
44+
if command == b'\xc5':
45+
AWAKE = False
46+
print('sds011 successfully put to sleep')
47+
48+
def process_packet(packet):
49+
try:
50+
print('\nPacket:', packet)
51+
*data, checksum, tail = struct.unpack('<HHBBBs', packet)
52+
pm25 = data[0]/10.0
53+
pm10 = data[1]/10.0
54+
# device_id = str(data[2]) + str(data[3])
55+
checksum_OK = checksum == (sum(data) % 256)
56+
tail_OK = tail == b'\xab'
57+
packet_status = 'OK' if (checksum_OK and tail_OK) else 'NOK'
58+
print('PM 2.5:', pm25, '\nPM 10:', pm10, '\nStatus:', packet_status)
59+
mqtt.publish({
60+
'pm25': pm25,
61+
'pm10': pm10,
62+
'status': packet_status
63+
})
64+
except Exception as e:
65+
print('Problem decoding packet:', e)
66+
sys.print_exception(e)
67+
68+
def read(allowed_time):
69+
global uart
70+
print('Reading from sds011 for', (allowed_time / 1000), 'secs')
71+
start_time = time.ticks_ms()
72+
SHOULD_READ = True
73+
while SHOULD_READ:
74+
try:
75+
delta_time = time.ticks_diff(time.ticks_ms(), start_time)
76+
if (delta_time > allowed_time): SHOULD_READ = False
77+
header = uart.read(1)
78+
if header == b'\xaa':
79+
command = uart.read(1)
80+
if command == b'\xc0':
81+
packet = uart.read(8)
82+
process_packet(packet)
83+
except Exception as e:
84+
print('Problem attempting to read:', e)
85+
sys.print_exception(e)

0 commit comments

Comments
 (0)