-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiot_am7020.py
More file actions
86 lines (66 loc) · 1.88 KB
/
iot_am7020.py
File metadata and controls
86 lines (66 loc) · 1.88 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# iot_project.py
# @Author : Zack Huang ()
# @Link : zack@atticedu.com
# @Date : 2020/11/11 下午1:39:34
from time import time, sleep
from sim7020.sim7020_nb import SIM7020NB
from sim7020.sim7020_mqtt import SIM7020MQTT
from tsl2561 import TSL2561
tsl = TSL2561(debug=True)
apn = "twm.nbiot"
band = 28
MQTT_BROKER = "io.adafruit.com"
PORT = 1883
MQTT_USERNAME = "Zack_Huang"
MQTT_PASSWORD = "XXXXXXXXXXXXXXXXXXXX"
# topics
LUX = "Zack_Huang/feeds/pi3.lux"
UPLOAD_INTERVAL = 60
nb = SIM7020NB("/dev/ttyS0", 115200, 18)
mqtt = SIM7020MQTT(nb)
def nbConnect():
print("Initializing modem...")
while((not nb.init() or (not nb.nbiotConnect(apn, band)))):
print(".")
print("Waiting for network...")
while(not nb.waitForNetwork()):
print(".")
sleep(5)
print("success")
def reConnBroker():
if(not mqtt.chkConnBroker()):
print("Connecting to", MQTT_BROKER, "...")
if(mqtt.connBroker(MQTT_BROKER, PORT, username=MQTT_USERNAME, password=MQTT_PASSWORD, mqtt_id="MY_AM7020_TEST_MQTTID")):
print("success")
else:
print("fail")
def pubAdafruitIO(topic, msg):
print("publish {} to {}".format(msg, topic))
if(mqtt.publish(topic, msg)):
print("success")
else:
print("Fail")
def main():
nbConnect()
reConnBroker()
chk_net_timer = 0
pub_data_timer = 0
get_lux_timer = 0
lux = 0
while(True):
if(time() > get_lux_timer):
get_lux_timer = time() + 5
lux = tsl.lux()
if(time() > chk_net_timer):
chk_net_timer = time() + 10
if(not nb.chkNet()):
nbConnect()
reConnBroker()
if(time() > pub_data_timer):
pub_data_timer = time() + UPLOAD_INTERVAL
pubAdafruitIO(LUX, lux)
mqtt.procSubs()
main()