-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·102 lines (83 loc) · 2.94 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
import socket
import time
import binascii
import pycom
from network import LoRa
from CayenneLPP import CayenneLPP
from pysense import Pysense
from LIS2HH12 import LIS2HH12
from SI7006A20 import SI7006A20
from LTR329ALS01 import LTR329ALS01
from MPL3115A2 import MPL3115A2, ALTITUDE, PRESSURE
from network import WLAN
wlan = WLAN()
wlan.deinit()
py = Pysense()
si = SI7006A20(py)
lt = LTR329ALS01(py)
li = LIS2HH12(py)
# Disable heartbeat LED
pycom.heartbeat(False)
# Initialize LoRa in LORAWAN mode.
lora = LoRa(mode=LoRa.LORAWAN)
lora.nvram_restore()
# create an OTAA authentication parameters
app_eui = binascii.unhexlify('')
app_key = binascii.unhexlify('')
print("DevEUI: %s" % (binascii.hexlify(lora.mac())))
print("AppEUI: %s" % (binascii.hexlify(app_eui)))
print("AppKey: %s" % (binascii.hexlify(app_key)))
# join a network using OTAA (Over the Air Activation)
if not lora.has_joined():
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=10)
# wait until the module has joined the network
while not lora.has_joined():
pycom.rgbled(0x140000)
time.sleep(2.5)
pycom.rgbled(0x000000)
time.sleep(1.0)
print('Not yet joined...')
print('OTAA joined')
if lora.has_joined():
# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.settimeout(180.0)
try:
pycom.rgbled(0x000014)
lpp = CayenneLPP()
print('\n\n** 3-Axis Accelerometer (LIS2HH12)')
print('Acceleration', li.acceleration())
print('Roll', li.roll())
print('Pitch', li.pitch())
lpp.add_accelerometer(1, li.acceleration()[0], li.acceleration()[1], li.acceleration()[2])
lpp.add_gryrometer(1, li.roll(), li.pitch(), 0)
print('\n\n** Digital Ambient Light Sensor (LTR-329ALS-01)')
print('Light', lt.light())
lpp.add_luminosity(1, lt.light()[0])
lpp.add_luminosity(2, lt.light()[1])
print('\n\n** Humidity and Temperature Sensor (SI7006A20)')
print('Humidity', si.humidity())
print('Temperature', si.temperature())
lpp.add_relative_humidity(1, si.humidity())
lpp.add_temperature(1, si.temperature())
mpPress = MPL3115A2(py,mode=PRESSURE)
print('\n\n** Barometric Pressure Sensor with Altimeter (MPL3115A2)')
print('Pressure (hPa)', mpPress.pressure()/100)
lpp.add_barometric_pressure(1, mpPress.pressure()/100)
mpAlt = MPL3115A2(py,mode=ALTITUDE)
print('Altitude', mpAlt.altitude())
print('Temperature', mpAlt.temperature())
lpp.add_gps(1, 0, 0, mpAlt.altitude())
lpp.add_temperature(2, mpAlt.temperature())
print('Sending data (uplink)...')
s.send(bytes(lpp.get_buffer()))
s.setblocking(False)
data = s.recv(64)
print('Received data (downlink)', data)
except:
1
lora.nvram_save()
py.setup_sleep(300)
py.go_to_sleep()