-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (135 loc) · 5.03 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
176
import pycom
import time
from mqtt import MQTTClient
from network import WLAN
import machine
import time
from _pymesh_config import PymeshConfig
from _pymesh import Pymesh
import config
from machine import I2C
from sht30 import SHT30
import ubinascii
from network import LoRa
# 0 = Child
# 1 = Router
# 2 = Leader
NODE_TYPE = 1
SEN0562_LIGHT_SENSOR_I2C_ADDRESS = 0x23
SEN0562_VALUE_REGISTER = 0x10
SHT31_TEMP_HUMIDITY_SENSOR_I2C_ADDRESS = 0x44
SHT31_COMMAND_MEAS_HIGHREP_0 = 0x24 # The MSB of the wanted command
SHT31_COMMAND_MEAS_HIGHREP_1 = 0x00 # The LSB of the wanted command
def sub_cb(topic, msg):
print(msg)
def send_mqtt_message(message):
while not wlan.isconnected():
machine.idle()
print("Connected to WiFi\n")
client = MQTTClient(config.MQTT_ID, config.MQTT_BROKER,user= config.MQTT_USER, password= config.MQTT_PASSWORD, port= config.MQTT_PORT, keepalive=30, ssl=False, ssl_params={})
client.set_callback(sub_cb)
time.sleep(5)
client.connect()
client.subscribe(topic="yaman/feeds/test")
print("Recieved from mesh")
client.publish(topic="yaman/feeds/recieved", msg=message)
def new_message_cb(rcv_ip, rcv_port, rcv_data):
send_mqtt_message(rcv_data)
''' callback triggered when a new packet arrived '''
print('Incoming %d bytes from %s (port %d):' %
(len(rcv_data), rcv_ip, rcv_port))
print(rcv_data)
# user code to be inserted, to send packet to the designated Mesh-external interface
for _ in range(3):
pycom.rgbled(0x888888)
time.sleep(.2)
pycom.rgbled(0)
time.sleep(.1)
return
def ReadLightSensor():
i2c = I2C(0)
i2c = I2C(0, I2C.MASTER)
i2c.init(I2C.MASTER, baudrate=20000)
lightI2C = i2c.readfrom_mem(SEN0562_LIGHT_SENSOR_I2C_ADDRESS, SEN0562_VALUE_REGISTER, 2)
lightConverted = lightI2C[0] << 8 | lightI2C[1]
lightValue = lightConverted / 1.2 #according to documentation
i2c.deinit()
return lightValue
def ReadHumTempSensor():
tempHumiditySensor = SHT30()
readings = tempHumiditySensor.measure()
tempHumiditySensor.terminateI2C()
return readings
pycom.heartbeat(False)
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
#This is where nodes can identify each other and belong to a one network
masterkey = ubinascii.unhexlify("11bcc8573c9c70c12ee8323bd9f051d9")
pymesh = lora.Mesh(key=masterkey)
#initialize Pymesh
#mac = pymesh.mac()
#if mac > 10:
# pymesh.end_device(True)
#elif mac == 5:
# pymesh.leader_priority(255)
print("Waiting to be connected")
while not pymesh.is_connected():
print(pymesh.status_str())
time.sleep(3)
# send message to the Node having MAC address 5
pymesh.send_mess(5, "Hello World")
def new_br_message_cb(rcv_ip, rcv_port, rcv_data, dest_ip, dest_port):
''' callback triggered when a new packet arrived for the current Border Router,
having destination an IP which is external from Mesh '''
print('Incoming %d bytes from %s (port %d), to external IPv6 %s (port %d)' %
(len(rcv_data), rcv_ip, rcv_port, dest_ip, dest_port))
print(rcv_data)
send_mqtt_message(rcv_data)
# user code to be inserted, to send packet to the designated Mesh-external interface
# ...
return
#add current node as Border Router, with a priority and a message handler callback
pymesh.br_set(PymeshConfig.BR_PRIORITY_NORM, new_br_message_cb)
# remove Border Router function from current node
# pymesh.br_remove()
# send data for Mesh-external, basically to the BR
# ip = "1:2:3::4"
# port = 5555
# pymesh.send_mess_external(ip, port, "Hello World")
print("done Pymesh init, CLI is started, h - help/command list, stop - CLI will be stopped")
#pymesh.cli_start()
# while True:
# time.sleep(3)
wlan = WLAN(mode=WLAN.STA)
wlan.connect(config.WIFI_SSID, auth=(WLAN.WPA2, config.WIFI_PASSWORD), timeout=5000)
while not wlan.isconnected():
machine.idle()
print("Connected to WiFi\n")
client = MQTTClient(config.MQTT_ID, config.MQTT_BROKER,user= config.MQTT_USER, password= config.MQTT_PASSWORD, port= config.MQTT_PORT, keepalive=30, ssl=False, ssl_params={})
client.set_callback(sub_cb)
time.sleep(5)
client.connect()
client.subscribe(topic="yaman/feeds/control")
if NODE_TYPE == 0: # 0 = Child
time.sleep(1)
print("Light Sensor")
Light = ReadLightSensor()
print(Light)
print("HumTemp Sensor")
HumTemp = ReadHumTempSensor()
print(HumTemp)
client.publish(topic="loraMesh/Child1/Light", msg=str(Light))
client.publish(topic="loraMesh/Child1/Temperature", msg=str(HumTemp[0]))
client.publish(topic="loraMesh/Child1/Humidity", msg=str(HumTemp[1]))
client.check_msg()
pymesh.send_mess_external("1:2:3::4", 5555, Light)
pymesh.send_mess_external("1:2:3::4", 5555, HumTemp[0])
pymesh.send_mess_external("1:2:3::4", 5555, HumTemp[1])
time.sleep(1)
machine.deepsleep(600000)
elif NODE_TYPE == 1: # 1 = Router
while True:
print("Light Sensor")
print("HumTemp Sensor")
client.publish(topic="loraMesh/Router1/Test", msg= "Connected")
client.check_msg()
time.sleep(10)