当从Arduino传输RF时,python收到列表中的额外零

时间:2017-02-24 01:44:52

标签: python list arduino raspberry-pi wireless

我正在尝试通过NRF24L01模块从Arduino发送一个数组,以便通过python Raspberry Pi程序接收。下面的脚本是我的Arduino程序中的循环。

void loop() {

    int chk = DHT.read11(DHT11_PIN);
    MoistureLevel=analogRead(moistureA0);
    int temp = DHT.temperature;
    int humid = DHT.humidity;

    int data[]={MoistureLevel, temp, humid};

    delay(1000)

    radio.write(data, sizeof(data));
}

以下代码是为RPi编写的代码。

while(1):
    #ackPL = [1]
    while not radio.available(0):
        time.sleep(1 / 100)
    receivedMessage = []
    radio.read(receivedMessage,radio.getDynamicPayloadSize())

    print(receivedMessage)

当它打印消息时,我得到以下内容:

[6, 0, 21, 0, 26, 0]
[6, 0, 21, 0, 26, 0]
[7, 0, 21, 0, 26, 0]
[6, 0, 21, 0, 28, 0]

似乎是在添加另一个零。另外,如果第一个位置的值增加到100以上,它有时会用第一个和第三行的另一个数字填充第二个位置。对于第二行,它保留了正确的数字,但仍然有额外的零。

[17, 1, 21, 0, 23, 0]
[255, 0, 21, 0, 23, 0]
[26, 1, 21, 0, 23, 0]

我已经获得了一个Python脚本来单独接受数字。这可以在下面看到。如果我对来自Arduino的radio.write()每个数字,他们都将在Pi上正确显示。但是,我想将所有三个数字放在一个列表中,这样我就可以得到每个读数的列表。

while(1):
    while not radio.available(0):
        time.sleep(1 /100)
    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received:  {}".format(receivedMessage))

    print("Translating into Unicode char")
    string = ""
    for n in receivedMessage:
        if (n >= 32 and n <= 126):
            string += chr(n)
    print("Message decodes to:  {}".format(string))

是否有人能够为如何在Python端获得正确的列表提供指导?

0 个答案:

没有答案