首页 > 解决方案 > Flutter 与 ESP32 蓝牙串口流通信

问题描述

几天来,我一直在尝试使用 flutter_bluetooth_serial 读取和存储来自 ESP32 的流数据。我必须使用以下方式读取以串行方式写入的那些数据:

void blue(){

  //SerialBT.print(",");
  SerialBT.print(String(Latitude,6));
  SerialBT.print(",");
  SerialBT.print(String(Longitude,6));
  SerialBT.print(",");
  SerialBT.print(s_temperature);
  SerialBT.print(",");
  SerialBT.print(bar_temperature);
  SerialBT.print(",");
  SerialBT.print(bar_pressure);
  SerialBT.print(",");
  SerialBT.print(bar_altitude);
  SerialBT.print(",");
  SerialBT.print(bar_humidity);
  SerialBT.print(",");
  SerialBT.print(accX);
  SerialBT.print(",");
  SerialBT.print(accY);
  SerialBT.print(",");
  SerialBT.print(accZ);
  SerialBT.print(",");
  SerialBT.print(gyroX);
  SerialBT.print(",");
  SerialBT.print(gyroY);
  SerialBT.print(",");
  SerialBT.print(gyroZ);
  SerialBT.print(",");
  /*
  SerialBT.print(accAngleX);
  SerialBT.print(",");
  SerialBT.print(accAngleY);
  SerialBT.print(",");
  SerialBT.print(gyroAngleX);
  SerialBT.print(" ");
  SerialBT.print(gyroAngleY);
  SerialBT.print(" ");
  SerialBT.print(gyroAngleZ);
  SerialBT.print(" ");
  */
  SerialBT.print(int(angleY)); //roll (or pitch) depends by axis direction
  SerialBT.print(",");
  SerialBT.print(int(angleX)); //pitch (or roll)
  SerialBT.print(",");
  SerialBT.print(int(angleZ)); //yaw
  SerialBT.print("\n");
  
  if (SerialBT.available()){
    char incomingChar = SerialBT.read();
  if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    
    Serial.write(incomingChar);  
    Serial.print(message);  
  }
  //Serial.println(message);  
  // Check received message and control output accordingly
  if (message =="1"){
    digitalWrite(green, HIGH);
    Serial.println("entra nell if log_on");  
  }
  else if (message =="0"){
    digitalWrite(green, LOW);
     Serial.println("entra nell if log_off");  
  }
  delay(20);
}

这是用于启动读取流的日志的代码:

    void _logOn() async {
        connection.output.add(utf8.encode("1"+ "\n"));
        await connection.output.allSent;
        connection.input.listen((Uint8List data) {
    _temp = ascii.decode(data);
  });
  setState(() {
          _deviceState = 1; // device on
          _logState = 1;
          _prova = _temp;
        });
  }

我知道在 _prova 上存储数据是一种不好的方法,因为我想存储数据直到 _logstate 等于 1。如何创建一个线程,在后台也连续存储数据,最后将数据保存在 txt 文件中??

标签: androidflutterdartesp32

解决方案


推荐阅读