Skip to content

Commit 4f90055

Browse files
authored
Added Sensor + OnOff examples
1 parent 8f4dce0 commit 4f90055

File tree

1 file changed

+113
-12
lines changed

1 file changed

+113
-12
lines changed

content/tutorials/all/ble_mesh.md

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,30 @@ aliases:
88

99
BLE Mesh module enables many-to-many device connections, based on Bluetooth module.
1010

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.
1315
```python
1416
from network import Bluetooth
1517
import pycom
1618
import time
1719

1820
def blink_led(n):
1921
for x in range(n):
20-
pycom.rgbled(0x000000) # off
22+
pycom.rgbled(0xffff00) # yellow on
2123
time.sleep(0.3)
22-
pycom.rgbled(0x555500) # yellow on
24+
pycom.rgbled(0x000000) # off
2325
time.sleep(0.3)
2426

2527
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(0x007f00) # green
33+
else:
34+
pycom.rgbled(0x7f0000) # red
2735

2836
def prov_callback(event, oob_pass):
2937
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
@@ -33,7 +41,6 @@ def prov_callback(event, oob_pass):
3341
# Green if Provisioned
3442
pycom.rgbled(0x007f00)
3543
if(event == BLE_Mesh.PROV_OUTPUT_OOB_REQ_EVT):
36-
# Blink LED 'oob_pass' times, to see visually OOB pass
3744
blink_led(oob_pass)
3845

3946
# BLE Mesh module
@@ -47,31 +54,125 @@ bluetooth = Bluetooth()
4754

4855
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
4956
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)
5158

5259
# 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)
5461

5562
# Turn on Provisioning Advertisement
5663
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)
5764
```
5865

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(0x555500)
80+
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
81+
# Green if Provisioned
82+
pycom.rgbled(0x007f00)
6083

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.
61107
```python
62108
from network import Bluetooth
63109
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()
64122

65123
def prov_callback(event, oob_pass):
124+
global device_provisioned
66125
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
67126
# Yellow if not Provision yet or Reseted
68127
pycom.rgbled(0x555500)
128+
device_provisioned = False
69129
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
70130
# Green if Provisioned
71131
pycom.rgbled(0x007f00)
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
72165

73166
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(0x555500)
173+
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
174+
# Green if Provisioned
175+
pycom.rgbled(0x007f00)
75176

76177
# BLE Mesh module
77178
BLE_Mesh = Bluetooth.BLE_Mesh
@@ -84,10 +185,10 @@ bluetooth = Bluetooth()
84185

85186
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
86187
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)
88189

89190
# Initialize BLE_Mesh
90-
BLE_Mesh.init("Pycom Client", callback=prov_callback)
191+
BLE_Mesh.init("Pycom Sensor Client", callback=prov_callback)
91192

92193
# Turn on Provisioning Advertisement
93194
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)

0 commit comments

Comments
 (0)