Arduino Float Value to Python using NRF24L01
Arduino Float Value to Python using NRF24L01
我试图通过NRF24L01将温度传感器数据发送到Raspberry Pi,并使用python在Raspberry Pi中读取它。但是温度传感器数据以字母的形式来到树莓派,我发现这是 Ascii 值。我不确定如何显示从Arduino到Raspberry Pi的实际读数
这是Arduino代码:
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SPI.h>
#include <RF24.h>
#include "printf.h"
#define ONE_WIRE_BUS 2
OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RF24 radio(9, 10);
void setup(void) {
Serial.begin(9600);
sensors.begin();
radio.begin() ;
radio.setPALevel(RF24_PA_MAX) ;
radio.setChannel(0x76) ;
radio.openWritingPipe(0xF0F0F0F0E1LL) ;
radio.enableDynamicPayloads() ;
radio.powerUp() ;
}
void loop(void) {
sensors.requestTemperatures();
float temperature = sensors.getTempFByIndex(0);
radio.write(&temperature, sizeof(float));
delay(1000);
Serial.print(sensors.getTempFByIndex(0));
}
这是 Python 中 Raspberry Pi 的代码
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True:
while not radio.available(0):
time.sleep(1/100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))
print("Translating...")
string = ""
for n in receivedMessage:
if (n >= 32 and n <= 126):
string += chr(n)
print("Our received message decodes to: {}".format(string))
我想以数字而不是字母的形式获取温度值。而不是这样:
正在翻译。。。 我们收到的消息解码为:N
您应该收到 4 个字节(在大多数体系结构上,sizeof(float)
始终为 4),因此请检查您收到的数据:
if (len(receivedMessage) == 4)
四个字节表示一个浮点数,以便对其进行转换:
temperature = float.fromhex(''.join(format(x, '02x') for x in receivedMessage))
四个字节被转换为十六进制字符串并转换为浮点数。
编辑(未测试):
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
if (len(receivedMessage) == 4)
temperature = float.fromhex(''.join(format(x, '02x') for x in receivedMessage))
print '%.2f' % temperature
相关文章:
- "using namespace std;"在C++的作用是什么?
- vector<vector<double>> to mxArray using memcpy
- Using C++ CryptAPI:CNG Create CSR
- STLPort using C++11
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- Using getchar_unlocked()
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- 为什么"using System;"不被视为不良做法?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- 'using namespace'实现细节的便捷方法(仅标头库)?
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 如何区分C++中的"using declaration"和"using directive"?
- C++ "using tcp=x"到"namespace tcp=x"有什么区别
- 如何为流输出运算符提供重载<<模板'using'类型别名?
- Using PrimitiveBatch::DrawIndexed with ID3D11DeviceContext::
- 如何创建一个 while 循环,继续循环,直到在C++中输入'using streams'键?
- 是否可以使用 using 将基类中的公共成员重新声明为派生类中的私有成员?
- Arduino Float Value to Python using NRF24L01