【问题标题】:Arduino transmit data to HC-05Arduino 将数据传输到 HC-05
【发布时间】:2014-11-14 15:58:00
【问题描述】:

我正在从事涉及 Arduino、蓝牙和 Android 的项目。我的 Arduino 硬件将从传感器收集数据并通过蓝牙将它们发送到 Android 平板电脑。当我使用 BlueChat 测试我的 Android 应用程序时,它似乎运行良好;它成功地从 BlueChat 接收数据。以下是我的 Arduino 硬件代码。我很确定我正确启动了 HC-05。如果我的想法是从模拟引脚 0 的温度传感器收集读数,然后将它们传输到数字引脚 11,这是 Arduino 上的 Tx 引脚,连接到 Hc-05 的 Rx 引脚,任何人都可以查看我的代码并建议它是否有效?

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); 
int tempPin=0;

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}

void loop()
{ 
  float reading = analogRead(tempPin); // reading on tempPin        
  float voltage = reading*5.0/1024.0; // the resolution of a pin is 10 bit,
  float tempC = voltage/0.01; // 10mV = 1 Celcius 
  mySerial.write(tempC);

  delay(3000);
}

我应该提一下,我使用 9V 电池从外部为我的 Arduino Uno 供电。

【问题讨论】:

    标签: arduino android-bluetooth


    【解决方案1】:

    在这种情况下尝试的步骤: - 通过 HC-05 (hello world) 发送任何内容 -> 这将排除连接问题(将 HC-05 放在“真实”串行上,将调试消息放在“软”串行上可能是个好主意)

    • 通过串口监视器测试模拟读取部分代码:你可以看看你是否得到了合理的数据

    • 通过 HC-05 测试传感器读取和发送的组合

    【讨论】:

      【解决方案2】:

      我认为SoftwareSerial 没有write( float ) 方法。我建议您报告原始数据并让您的应用程序进行转换。不要忘记分隔符,这样您就知道一个数字何时结束,下一个数字何时开始:

      void loop()
      { 
        int reading = analogRead(tempPin); // reading on tempPin        
        mySerial.println( tempC, DEC );
      
        delay(3000);
      }
      

      【讨论】:

      • 谢谢,但它没有用,出现错误“类“SoftwareSerial”没有名为“writeln”的成员。
      • 是println,我打错了。
      • Tks 兄弟,我已经尝试了 println 和 print 但没有任何效果。
      • 这很奇怪。你配对了吗?我有一个远程传感器,可以在软件串行上完美运行。检查波特率... HC-05 很挑剔。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多