使用 HC05 蓝牙模块将 pH 数据从 Arduino 发送到使用 flutter_bluetooth_serial 库的 Flutter 应用

问题描述 投票:0回答:0

我正在使用 PH-4502C pH 传感器和 HC05 蓝牙模块创建一个 pH 监测系统。我正在将传感器的数据发送到 Flutter 移动应用程序。我试图从 Flutter https://github.com/edufolly/flutter_bluetooth_serial 的文档中运行这段代码,但我在后台收集中遇到了问题。该应用程序无法接收来自 Arduino 的数据。它显示错误“Bad state”谁能帮我解决这个问题,或者你能提供一些最新的教程,告诉我如何使用蓝牙模块将数据从传感器发送到 Flutter 应用程序或直接发送到 Firebase 数据库吗?

我一直在互联网上寻找解决方案,但找不到解决我的问题的教程或解决方案。我编辑了 Arduino 的代码以仅包含 pH 传感器。非常感谢您的帮助。

`/// 温度和 ph 水平监视器示例 /// /// Patryk (PsychoX) Ludwikowski https://gitlab.com/PsychoXIVI/aquariumcontroller 的部分代码 /// 为 https://github.com/edufolly/flutter_bluetooth_serial/pull/35 切割成简单的监视器 //////////////////////////////////////////////// ///////////////////////////// #包括

#define 调试 1

#include "phSensor.hpp"

// 普通引脚上的虚拟串口库 #包括

软件串口蓝牙(10, 11); //10-RX, 11-TX

bool doUpdateStatus = false;

/////////////////////////////////////////////// ////////////////////////////// 无效循环(无效) { 延迟(1);

updatephSensorsSamplings(); 更新 phSensorsValues();

// 命令:“开始”、“停止” while (bluetooth.available() >= 4) { 开关(bluetooth.read()){ 案例's': 蓝牙.读(); // 忽略(可能)'t' 开关(bluetooth.read()){ case 'a': // “开始” doUpdateStatus = true; 数字写入(13,高);

        bluetooth.read(); // Ignore 'r'
        while (bluetooth.available() == 0);
        bluetooth.read(); // Ignore 't'
        break;
        
      case 'o': // "stop"
        doUpdateStatus = false;
        digitalWrite(13, LOW);
        
        bluetooth.read(); // Ignore 'p'
    }
    break;
}

}

static unsigned long lastRefreshTime = 0;
if (millis() - lastRefreshTime >= 1000) {
lastRefreshTime += 1000;

if (doUpdateStatus) {
  // Every update there are temperatures and water pH level sent coded into binary form of:
  // 't', 
  // integer part of value in Celcius of first termometer, 
  // fractional part of value in Celcius of first termometer,
  // integer part of value in Celcius of second termometer, 
  // fractional part of value in Celcius of second termometer,
  // 'w'
  // integer value of water pH level, 
  // fractional part of water pH level.
  
  // bluetooth.write('t');
  // for (byte i = 0; i < 2; i++) {
  //   bluetooth.write(static_cast<byte>(static_cast<int>(DS18B20_value[i])));
  //   bluetooth.write(static_cast<byte>(static_cast<int>((DS18B20_value[i] - static_cast<int>(DS18B20_value[i])) * 100)));
  // }
  
  bluetooth.write('p');
  bluetooth.write(static_cast<byte>(static_cast<int>(phSensors[0].value)));
  bluetooth.write(static_cast<byte>(static_cast<int>((phSensors[0].value - static_cast<int>(phSensors[0].value)) * 100)));
}
}

}

/////////////////////////////////////////////// ////////////////////////////// 无效设置(无效) { 序列号.begin(9600);

bluetooth.begin(9600);

setupphSensors();

pinMode(13,输出); 数字写入(13,低); }`

flutter arduino arduino-uno android-bluetooth hc-05
© www.soinside.com 2019 - 2024. All rights reserved.