c++ - Arduino .read() 函数干扰 BLE 连接
问题描述
首先,我要道歉,因为我是 BLE 连接和大部分 Arduino 编程的新手。我正忙于一个项目,该项目涉及制作一个智能咖啡秤,它可以通过 BLE 连接将数据输出到智能手机。我正在使用 Arduino nano 33 IoT 和 hx711 称重传感器放大器。
我需要创建一个程序,我可以在其中向 Arduino 和智能手机应用程序发送和接收数据。我使用了标准的 ArduinoBLE 外围库,例如“BatteryMonitor”草图和“ButtonLED”草图。通过将这两个示例草图结合在一起,我设法建立了一个可以发送和接收数据的连接。
当我尝试使用 HX711 库中的函数(例如 scale.read(); 检索从 hx711 放大器输出的值。当我使用诸如 scale.read() 之类的串行读取功能时,蓝牙连接在正确建立之前失败。我想这是由于 scale.read() 函数干扰了 Arduino 传输和接收的串行数据,但我不知道如何解决这个问题。
我基本上想将电池监视器输出更改为从 hx711 称重传感器放大器读取的值的输出,但我正在努力让它工作。
#include "HX711.h"
#include <ArduinoBLE.h>
HX711 scale;
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
int oldBatteryLevel = 0; // last battery level reading from analog input
long previousMillis = 0; // last time the battery level was checked, in ms
const int ledPin = LED_BUILTIN; // pin to use for the LED
double val;
void setup() {
Serial.begin(9600);
scale.begin(A1, A0); //Initialized scale on these pins
while (!Serial);
scale.set_scale(432.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("COFFEE");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
ledService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected())
{
// Battery Monitor
// scale.read();
long currentMillis = millis();
// if 200ms have passed, check the battery level:
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
// scale.read(); // This function alone will prevent the BLE connection from establishing properly.
updateBatteryLevel();
// outputScale();
}
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
void updateBatteryLevel()
{
/* Read the current voltage level on the A0 analog input pin.
This is used here to simulate the charge level of a battery.
*/
int battery = analogRead(A0);
int batteryLevel = map(battery, 0, 1023, 0, 100);
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
// Serial.print("Battery Level % is now: "); // print it
Serial.println(batteryLevel);
batteryLevelChar.writeValue(batteryLevel); // and update the battery level characteristic
oldBatteryLevel = batteryLevel; // save the level for next comparison
}
}
void outputScale(){
int t, i, n, T;
double val, sum, sumsq, mean;
float stddev;
n = 20;
t = millis();
i = sum = sumsq = 0;
while (i<n) {
val = ((scale.read() - scale.get_offset()) / scale.get_scale());
sum += val;
sumsq += val * val;
i++;
}
t = millis() - t;
mean = sum / n;
stddev = sqrt(sumsq / n - mean * mean);
// Serial.print("Mean, Std Dev of "); Serial.print(i); Serial.print(" readings:\t");
Serial.print(sum / n, 3); Serial.print("\n"); // Serial.print(stddev, 3);
// Note: 2 sigma is 95% confidence, 3 sigma is 99.7%
//Serial.print("\nTime taken:\t"); Serial.print(float(t)/1000, 3); Serial.println("Secs\n");
/*
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
*/
}
解决方案
您正在初始化,就像scale.begin(A1, A0)
尝试从同一个A0
引脚读取一样。
推荐阅读
- python - Eager Execution 与 tf.function 的关系
- python - Numpy数组索引意外行为
- sql - sql server中的表行到列
- c# - 将数据与实体框架绑定时如何处理不存在的列?
- angular - 带有枚举和复选框的 Angular 8 反应式表单
- python - 如何使用 tensorflow 从亚马逊下载数据集
- macos - 获取运行进程的路径,当路径包含空格时
- android - postDelayed 为运行代码执行两次
- selenium - org.openqa.selenium.NoSuchElementException 没有这样的元素:无法找到元素
- windows - Ansible 的 Windows 更新