@@ -8,22 +8,30 @@ aliases:
8
8
9
9
BLE Mesh module enables many-to-many device connections, based on Bluetooth module.
10
10
11
- ## Server Example
12
-
11
+ # Generic OnOff Example
12
+ Generic OnOff model is simplest model in BLE Mesh, makes it possible for one device to switch other devices on or off.
13
+ ## OnOff Server
14
+ OnOff Server has one bool State. Server can Get, Set or send Status abour this State to Client(s). In the example below, during Provisioning, ` Output OOB ` can be selected, LED is yellow in case of Unprovisioned, and green in case of Provisioned state. Changing the State of Server, LED's light is green or red.
13
15
``` python
14
16
from network import Bluetooth
15
17
import pycom
16
18
import time
17
19
18
20
def blink_led (n ):
19
21
for x in range (n):
20
- pycom.rgbled(0x 000000 ) # off
22
+ pycom.rgbled(0x ffff00 ) # yellow on
21
23
time.sleep(0.3 )
22
- pycom.rgbled(0x 555500 ) # yellow on
24
+ pycom.rgbled(0x 000000 ) # off
23
25
time.sleep(0.3 )
24
26
25
27
def server_cb (new_state , event , recv_op ):
26
- print (" Set State: " , new_state)
28
+ print (" SERVER | State: " , new_state)
29
+
30
+ # Turn on LED on board based on State
31
+ if new_state == True :
32
+ pycom.rgbled(0x 007f00 ) # green
33
+ else :
34
+ pycom.rgbled(0x 7f0000 ) # red
27
35
28
36
def prov_callback (event , oob_pass ):
29
37
if (event == BLE_Mesh .PROV_REGISTER_EVT or event == BLE_Mesh .PROV_RESET_EVT ):
@@ -33,7 +41,6 @@ def prov_callback(event, oob_pass):
33
41
# Green if Provisioned
34
42
pycom.rgbled(0x 007f00 )
35
43
if (event == BLE_Mesh .PROV_OUTPUT_OOB_REQ_EVT ):
36
- # Blink LED 'oob_pass' times, to see visually OOB pass
37
44
blink_led(oob_pass)
38
45
39
46
# BLE Mesh module
@@ -47,31 +54,125 @@ bluetooth = Bluetooth()
47
54
48
55
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
49
56
element = BLE_Mesh .create_element(primary = True , feature = BLE_Mesh .GATT_PROXY )
50
- model_server = element.add_model(BLE_Mesh .SENSOR , BLE_Mesh .SERVER , callback = server_cb, sen_min = - 1 , sen_max = 1 , sen_res = 0.1 )
57
+ model_server = element.add_model(BLE_Mesh .GEN_ONOFF , BLE_Mesh .SERVER , callback = server_cb)
51
58
52
59
# Initialize BLE_Mesh
53
- BLE_Mesh .init(" Pycom Server" , auth = BLE_Mesh .OOB_OUTPUT , callback = prov_callback)
60
+ BLE_Mesh .init(" Pycom OnOff Server" , auth = BLE_Mesh .OOB_OUTPUT , callback = prov_callback)
54
61
55
62
# Turn on Provisioning Advertisement
56
63
BLE_Mesh .set_node_prov(BLE_Mesh .PROV_ADV | BLE_Mesh .PROV_GATT )
57
64
```
58
65
59
- ## Client Example
66
+ ## OnOff Client
67
+ Client can Get or Set State of Server. In case of Get, or Server Status call, Client Gets the Status through the Model's callback.
68
+ ``` python
69
+ from network import Bluetooth
70
+ import pycom
71
+ import time
72
+
73
+ def client_cb (new_state , event , recv_op ):
74
+ print (" CLIENT | State: " , new_state)
75
+
76
+ def prov_callback (event , oob_pass ):
77
+ if (event == BLE_Mesh .PROV_REGISTER_EVT or event == BLE_Mesh .PROV_RESET_EVT ):
78
+ # Yellow if not Provision yet or Reseted
79
+ pycom.rgbled(0x 555500 )
80
+ if (event == BLE_Mesh .PROV_COMPLETE_EVT ):
81
+ # Green if Provisioned
82
+ pycom.rgbled(0x 007f00 )
60
83
84
+ # BLE Mesh module
85
+ BLE_Mesh = Bluetooth.BLE_Mesh
86
+
87
+ # Turn off the heartbeat behavior of the LED
88
+ pycom.heartbeat(False )
89
+
90
+ # Need to turn ON Bluetooth before using BLE Mesh
91
+ bluetooth = Bluetooth()
92
+
93
+ # Create a Primary Element with GATT Proxy feature and add a Server model to the Element
94
+ element = BLE_Mesh .create_element(primary = True , feature = BLE_Mesh .GATT_PROXY )
95
+ model_client = element.add_model(BLE_Mesh .GEN_ONOFF , BLE_Mesh .CLIENT , callback = client_cb)
96
+
97
+ # Initialize BLE_Mesh
98
+ BLE_Mesh .init(" Pycom OnOff Client" , callback = prov_callback)
99
+
100
+ # Turn on Provisioning Advertisement
101
+ BLE_Mesh .set_node_prov(BLE_Mesh .PROV_ADV | BLE_Mesh .PROV_GATT )
102
+ ```
103
+ # Sensor Example
104
+ In case of Sensor Models, State of Server can be modified only by Server itself, Client can only Get the State by calling Client's Get, or by Servers Status call, but cannot modify the Server's State.
105
+ ## Sensor Server
106
+ In this example Server takes a time measurement every 1 seconds, and send a Status message every 5 seconds, after became Provisioned.
61
107
``` python
62
108
from network import Bluetooth
63
109
import pycom
110
+ import time
111
+ from machine import Timer
112
+
113
+ def read_sensor (alarm ):
114
+ # In this example sensor reads local seconds
115
+ if (device_provisioned):
116
+ model_server.set_state(time.localtime()[5 ])
117
+ print (" SENSOR | State: " , model_server.get_state())
118
+
119
+ def status_sensor (alarm ):
120
+ if (device_provisioned):
121
+ model_server.status_state()
64
122
65
123
def prov_callback (event , oob_pass ):
124
+ global device_provisioned
66
125
if (event == BLE_Mesh .PROV_REGISTER_EVT or event == BLE_Mesh .PROV_RESET_EVT ):
67
126
# Yellow if not Provision yet or Reseted
68
127
pycom.rgbled(0x 555500 )
128
+ device_provisioned = False
69
129
if (event == BLE_Mesh .PROV_COMPLETE_EVT ):
70
130
# Green if Provisioned
71
131
pycom.rgbled(0x 007f00 )
132
+ device_provisioned = True
133
+
134
+ # BLE Mesh module
135
+ BLE_Mesh = Bluetooth.BLE_Mesh
136
+
137
+ # Turn off the heartbeat behavior of the LED
138
+ pycom.heartbeat(False )
139
+
140
+ # Need to turn ON Bluetooth before using BLE Mesh
141
+ bluetooth = Bluetooth()
142
+
143
+ # Create a Primary Element with GATT Proxy feature and add a Server model to the Element
144
+ element = BLE_Mesh .create_element(primary = True , feature = BLE_Mesh .GATT_PROXY )
145
+ model_server = element.add_model(BLE_Mesh .SENSOR , BLE_Mesh .SERVER , sen_min = 0 , sen_max = 59 , sen_res = 1 )
146
+
147
+ # Initialize BLE_Mesh
148
+ BLE_Mesh .init(" Pycom Sensor Server" , callback = prov_callback)
149
+
150
+ # Turn on Provisioning Advertisement
151
+ BLE_Mesh .set_node_prov(BLE_Mesh .PROV_ADV | BLE_Mesh .PROV_GATT )
152
+
153
+ # Sensor takes measurement every 1 second
154
+ Timer.Alarm(read_sensor, 1 , periodic = True )
155
+
156
+ # Sensor send status every 5 seconds
157
+ Timer.Alarm(status_sensor, 5 , periodic = True )
158
+ ```
159
+
160
+ ## Sensor Client
161
+ Sensor Client is looking for measurements, as Server sends Status every 5 seconds. Between these calls, Client can Get message any time.
162
+ ``` python
163
+ from network import Bluetooth
164
+ import pycom
72
165
73
166
def client_cb (new_state , event , recv_op ):
74
- print (" Set State: " , new_state)
167
+ print (" CLIENT | State: " , new_state)
168
+
169
+ def prov_callback (event , oob_pass ):
170
+ if (event == BLE_Mesh .PROV_REGISTER_EVT or event == BLE_Mesh .PROV_RESET_EVT ):
171
+ # Yellow if not Provision yet or Reseted
172
+ pycom.rgbled(0x 555500 )
173
+ if (event == BLE_Mesh .PROV_COMPLETE_EVT ):
174
+ # Green if Provisioned
175
+ pycom.rgbled(0x 007f00 )
75
176
76
177
# BLE Mesh module
77
178
BLE_Mesh = Bluetooth.BLE_Mesh
@@ -84,10 +185,10 @@ bluetooth = Bluetooth()
84
185
85
186
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
86
187
element = BLE_Mesh .create_element(primary = True , feature = BLE_Mesh .GATT_PROXY )
87
- model_client = element.add_model(BLE_Mesh .SENSOR , BLE_Mesh .CLIENT , callback = client_cb, sen_min = - 1 , sen_max = 1 , sen_res = 0. 1 )
188
+ model_client = element.add_model(BLE_Mesh .SENSOR , BLE_Mesh .CLIENT , callback = client_cb, sen_min = 0 , sen_max = 59 , sen_res = 1 )
88
189
89
190
# Initialize BLE_Mesh
90
- BLE_Mesh .init(" Pycom Client" , callback = prov_callback)
191
+ BLE_Mesh .init(" Pycom Sensor Client" , callback = prov_callback)
91
192
92
193
# Turn on Provisioning Advertisement
93
194
BLE_Mesh .set_node_prov(BLE_Mesh .PROV_ADV | BLE_Mesh .PROV_GATT )
0 commit comments