我有一些 Pycom Wipy 3 板,我想通过使用 Pymesh BLE 创建蓝牙网格。我已经开始关注此链接中的传感器示例。
这是我创建 Pymesh BLE 服务器的代码:
from network import Bluetooth
from hardware.rgb_led import RGBLed
import time
from machine import Timer
class BluetoothMeshServer:
bluetooth = None
model_server = None
ble_mesh = None
def __init__(self):
pass
def start(self):
BLE_Mesh = Bluetooth.BLE_Mesh
# BLE Mesh module
# self.ble_mesh = Bluetooth.BLE_Mesh.init(name="PYCOM-ESP-BLE-MESH", *, auth=0, callback=None)
self.ble_mesh = Bluetooth.BLE_Mesh.init(name="xxx", auth=0, callback=None)
# Need to turn ON Bluetooth before using BLE Mesh
self.bluetooth = Bluetooth()
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
element = self.ble_mesh.create_element(primary=True, feature=self.ble_mesh.GATT_PROXY)
self.model_server = element.add_model(self.ble_mesh.SENSOR, self.ble_mesh.SERVER, sen_min = 0, sen_max = 59, sen_res = 1)
# Initialize self.ble_mesh
self.ble_mesh.init("Pycom Sensor Server", callback=self.prov_callback)
# Turn on Provisioning Advertisement
self.ble_mesh.set_node_prov(self.ble_mesh.PROV_ADV|self.ble_mesh.PROV_GATT)
# Sensor takes measurement every 1 second
Timer.Alarm(self.read_sensor, 1, periodic=True)
# Sensor send status every 5 seconds
Timer.Alarm(self.status_sensor, 5, periodic=True)
def read_sensor(self, alarm):
# In this example sensor reads local seconds
if(device_provisioned):
self.model_server.set_state(time.localtime()[5])
print("SENSOR | State: ", self.model_server.get_state())
def status_sensor(self, alarm):
if (device_provisioned):
self.model_server.status_state()
def prov_callback(self, event, oob_pass):
global device_provisioned
if(event == self.ble_mesh.PROV_REGISTER_EVT or event == self.ble_mesh.PROV_RESET_EVT):
# Yellow if not Provision yet or Reset
RGBLed.turn_yellow()
device_provisioned = False
if(event == self.ble_mesh.PROV_COMPLETE_EVT):
# Green if Provisioned
RGBLed.turn_green
device_provisioned = True
我遇到的麻烦是这条线:
BLE_Mesh = Bluetooth.BLE_Mesh
在终端中引发异常。这是终端的错误输出:
Traceback (most recent call last):
File "main.py", line 11, in <module>
File "lib/bluetooth_mesh_server.py", line 16, in start
AttributeError: type object 'Bluetooth' has no attribute 'BLE_Mesh'
Pycom MicroPython 1.20.2.r6 [v1.11-c5a0a97] on 2021-10-28; WiPy with ESP32
Pybytes Version: 1.7.1
Type "help()" for more information.
如您所见,错误显示“AttributeError:类型对象'Bluetooth'没有属性'BLE_Mesh'”。为什么?如何将 BLE_Mesh 与 Wipy 3 板一起使用来创建蓝牙网格?
谢谢!