1180| 0
|
[求助] arduino使用蓝牙调试器进行曲线绘图 |
我想使用手机端的蓝牙调试器来绘制Arduino测量的温湿度数据,请问如何打包数据啊?我这里调制完之后,也不绘图。代码如下,请大佬指点一二 #include <DHT.h> #include <SoftwareSerial.h> SoftwareSerial BTSerial(0, 1); // RX, TX #define DHTPIN 8 // DHT传感器连接的Arduino引脚 #define DHTTYPE DHT11 // 使用DHT11或DHT22传感器 DHT dht(DHTPIN, DHTTYPE); #define USART_TX_LEN 11 uint8_t USART_TX_BUF[USART_TX_LEN]; void float_to_byte(float f, uint8_t *byte) { union { float a; uint8_t bytes[4]; } thing; thing.a = f; for (int i = 0; i < 4; i++) { byte[i] = thing.bytes[i]; } } uint8_t calculate_checksum(uint8_t *data, int len) { uint8_t sum = 0; for (int i = 0; i < len; i++) { sum += data[i]; } return sum; } void setup() { BTSerial.begin(9600); dht.begin(); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); USART_TX_BUF[0] = 0xA5; // 包头 float_to_byte(t, &USART_TX_BUF[1]); float_to_byte(h, &USART_TX_BUF[5]); USART_TX_BUF[9] = calculate_checksum(USART_TX_BUF, 9); // 计算校验和 USART_TX_BUF[10] = 0x5A; // 尾部 for (int i = 0; i < USART_TX_LEN; i++) { BTSerial.write(USART_TX_BUF[i]); } delay(1000); // 延时1秒 } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed