-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathZerynthMain.py
67 lines (49 loc) · 2.1 KB
/
ZerynthMain.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
# AWS IoT Controlled publish period
# Created at 2017-10-03 08:49:48.182639
import streams
import json
import timers
from wireless import wifi
# choose a wifi chip supporting secure sockets and client certificates
from espressif.esp32net import esp32wifi as wifi_driver
# import aws iot module
from aws.iot import iot
# import helpers functions to easily load keys, certificates and thing configuration
import helpers
buttonPin=BTN0
pinMode(buttonPin,INPUT_PULLUP)
# THING KEY AND CERTIFICATE FILE MUST BE PLACED INSIDE PROJECT FOLDER
new_resource('private.pem.key')
new_resource('certificate.pem.crt')
# SET THING CONFIGURATION INSIDE THE FOLLOWING JSON FILE
new_resource('thing.conf.json')
# define a callback for shadow updates
def shadow_callback(requested):
global publish_period
print('requested publish period:', requested['publish_period'])
publish_period = requested['publish_period']
return {'publish_period': publish_period}
streams.serial()
wifi_driver.auto_init()
print('connecting to wifi...')
# place here your wifi configuration
wifi.link("YOUR-WIFI-NETWORK-NAME-HERE",wifi.WIFI_WPA2,"YOUR-WIFI-PASSWORD")
pkey, clicert = helpers.load_key_cert('private.pem.key', 'certificate.pem.crt')
thing_conf = helpers.load_thing_conf()
publish_period = 1000 #decreased from 5 secs to one sec
# create aws iot thing instance, connect to mqtt broker, set shadow update callback and start mqtt reception loop
thing = iot.Thing(thing_conf['endpoint'], thing_conf['mqttid'], clicert, pkey, thingname=thing_conf['thingname'])
print('connecting to mqtt broker...')
thing.mqtt.connect()
thing.on_shadow_request(shadow_callback)
thing.mqtt.loop()
thing.update_shadow({'publish_period': publish_period})
#while True:
def pressed():
print('publish random sample...')
thing.mqtt.publish("dev/sample", json.dumps({ 'Device ID' : thing_conf['thingname'], 'Time': timers.now(), 'Humidity': random(0,50), 'Temperature': random(0,100) }))
#sleep(publish_period)
#onPinFall(buttonPin,pressed) #better not to use this or it now resets program
while True:
onPinFall(buttonPin,pressed)
sleep(publish_period)