介绍用本板卡最终实现了什么功能及各功能对应的主要代码片段
通过蓝牙读取开发板上的温度传感器数值,超过一定门槛后,再通过蓝牙控制开发板上的LED灯点亮做报警。
python代码:
import sys
import binascii
import time
from bluepy.btle import UUID, Peripheral
class BleDevice:
def __init__(self, mac):
self.mac = mac
def connect(self):
self.p = Peripheral(self.mac, "public")
def getTemperature(self):
services = self.p.getServiceByUUID("181A")
chlist = services.getCharacteristics()
ch = chlist[0]
temphex = binascii.b2a_hex(ch.read()).decode()
temphex = temphex[2:4]+temphex[0:2]
temp = int(temphex, 16)
return temp / 100
def setLedOn(self):
services = self.p.getServiceByUUID("1815")
chlist = services.getCharacteristics()
led = chlist[1]
led.write(b'\x01', True)
def setLedOff(self):
services = self.p.getServiceByUUID("1815")
chlist = services.getCharacteristics()
led = chlist[1]
led.write(b'\x00', True)
if __name__ == '__main__':
BG22 = BleDevice("84:2e:14:31:ba:12")
BG22.connect()
while(True):
temp = BG22.getTemperature()
print(temp)
if temp > 27:
BG22.setLedOn()
else:
BG22.setLedOff()
time.sleep(1)
Android APP:修改读到温度的片段com/silabs/thunderboard/ble/GattManager.java中
else if (ThunderBoardUuids.UUID_CHARACTERISTIC_TEMPERATURE.equals(uuid)) {
int temperature = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 0);
Timber.d("temperature: %d", temperature);
if (temperature >= 2700){
bleManager.ledAction(1);
}else{
bleManager.ledAction(0);
}
ThunderBoardSensorEnvironment sensor = device.getSensorEnvironment();
sensor.setTemperature(temperature);
bleManager.selectedDeviceMonitor.onNext(device);
bleManager.environmentReadMonitor.onNext(new EnvironmentEvent(device, uuid));}
对本活动的心得体会
这次Funpack活动,让我们不再局限于单片机的开发,转而去尝试上位机的开发。这次活动,也让我成功出圈,尝试了安卓程序的开发。虽然只是对APP的一个小小的改动,但也还是翻了好半天的程序,大致了解了下这个APP所定义的类和函数是做什么的。感谢硬禾本次的选题,让我成功不再局限于嵌入式编程,出圈尝试了安卓开发。
再次感谢硬禾学堂和得捷电子,同时也感谢在群里互帮互助的小伙伴们!