|
| 1 | +--- |
| 2 | +title: "BLE Mesh" |
| 3 | +aliases: |
| 4 | + - tutorials/all/ble_mesh.html |
| 5 | + - tutorials/all/ble_mesh.md |
| 6 | + - chapter/tutorials/all/ble_mesh |
| 7 | +--- |
| 8 | + |
| 9 | +BLE Mesh module enables many-to-many device connections, based on Bluetooth module. |
| 10 | + |
| 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. |
| 15 | +```python |
| 16 | +from network import Bluetooth |
| 17 | +import pycom |
| 18 | +import time |
| 19 | + |
| 20 | +def blink_led(n): |
| 21 | + for x in range(n): |
| 22 | + pycom.rgbled(0xffff00) # yellow on |
| 23 | + time.sleep(0.3) |
| 24 | + pycom.rgbled(0x000000) # off |
| 25 | + time.sleep(0.3) |
| 26 | + |
| 27 | +def server_cb(new_state, event, recv_op): |
| 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 |
| 35 | + |
| 36 | +def prov_callback(event, oob_pass): |
| 37 | + if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT): |
| 38 | + # Yellow if not Provision yet or Reseted |
| 39 | + pycom.rgbled(0x555500) |
| 40 | + if(event == BLE_Mesh.PROV_COMPLETE_EVT): |
| 41 | + # Green if Provisioned |
| 42 | + pycom.rgbled(0x007f00) |
| 43 | + if(event == BLE_Mesh.PROV_OUTPUT_OOB_REQ_EVT): |
| 44 | + blink_led(oob_pass) |
| 45 | + |
| 46 | +# BLE Mesh module |
| 47 | +BLE_Mesh = Bluetooth.BLE_Mesh |
| 48 | + |
| 49 | +# Turn off the heartbeat behavior of the LED |
| 50 | +pycom.heartbeat(False) |
| 51 | + |
| 52 | +# Need to turn ON Bluetooth before using BLE Mesh |
| 53 | +bluetooth = Bluetooth() |
| 54 | + |
| 55 | +# Create a Primary Element with GATT Proxy feature and add a Server model to the Element |
| 56 | +element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY) |
| 57 | +model_server = element.add_model(BLE_Mesh.GEN_ONOFF, BLE_Mesh.SERVER, callback=server_cb) |
| 58 | + |
| 59 | +# Initialize BLE_Mesh |
| 60 | +BLE_Mesh.init("Pycom OnOff Server", auth=BLE_Mesh.OOB_OUTPUT, callback=prov_callback) |
| 61 | + |
| 62 | +# Turn on Provisioning Advertisement |
| 63 | +BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT) |
| 64 | +``` |
| 65 | + |
| 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) |
| 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. |
| 107 | +```python |
| 108 | +from network import Bluetooth |
| 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() |
| 122 | + |
| 123 | +def prov_callback(event, oob_pass): |
| 124 | + global device_provisioned |
| 125 | + if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT): |
| 126 | + # Yellow if not Provision yet or Reseted |
| 127 | + pycom.rgbled(0x555500) |
| 128 | + device_provisioned = False |
| 129 | + if(event == BLE_Mesh.PROV_COMPLETE_EVT): |
| 130 | + # Green if Provisioned |
| 131 | + 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 |
| 165 | + |
| 166 | +def client_cb(new_state, event, recv_op): |
| 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) |
| 176 | + |
| 177 | +# BLE Mesh module |
| 178 | +BLE_Mesh = Bluetooth.BLE_Mesh |
| 179 | + |
| 180 | +# Turn off the heartbeat behavior of the LED |
| 181 | +pycom.heartbeat(False) |
| 182 | + |
| 183 | +# Need to turn ON Bluetooth before using BLE Mesh |
| 184 | +bluetooth = Bluetooth() |
| 185 | + |
| 186 | +# Create a Primary Element with GATT Proxy feature and add a Server model to the Element |
| 187 | +element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY) |
| 188 | +model_client = element.add_model(BLE_Mesh.SENSOR, BLE_Mesh.CLIENT, callback=client_cb, sen_min = 0, sen_max = 59, sen_res = 1) |
| 189 | + |
| 190 | +# Initialize BLE_Mesh |
| 191 | +BLE_Mesh.init("Pycom Sensor Client", callback=prov_callback) |
| 192 | + |
| 193 | +# Turn on Provisioning Advertisement |
| 194 | +BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT) |
| 195 | +``` |
0 commit comments