简体   繁体   English

在Arduino上使用RF24Network发送字符数组

[英]Sending a char array using RF24Network on Arduino

I am trying to use the RF24Network library to create a sensor network in which the root node connects to an MQTT broker using the PubSubCient library. 我正在尝试使用RF24Network库创建传感器网络,其中根节点使用PubSubCient库连接到MQTT代理。

I have done a basic test which relays the MQTT topic and message payload from MQTT to one other node in the network, it does this by using sprintf to create a delimited string of the current millis() time on the root node, the topic and message which is stored in a car array of size 200 (as the size of the RF payload must be predefined), I can see from serial output that this is being done correctly. 我已经完成了一项基本测试,该测试将MQTT主题和消息有效负载从MQTT中继到网络中的另一个节点,它通过使用sprintf在根节点,主题和当前节点上创建当前millis()时间的定界字符串来完成此任务。消息存储在大小为200的汽车阵列中(因为必须预先定义RF有效负载的大小),我可以从串行输出中看到这已正确完成。

However from the serial output of the receiving node I find that only the first 24 characters of the string are being received and I am out of ideas as to why this is the case. 但是,从接收节点的串行输出中,我发现仅接收到字符串的前24个字符,因此我不知道为什么会这样。

Code for sending node: 发送节点代码:

void ByteToChar(byte* bytes, char* chars, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
         chars[i] = (char)bytes[i];
    chars[count] = '\0';
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.println("Sending...");

  char buf[length + 1];
  ByteToChar(message, buf, length);

  char payload[200];
  sprintf(payload, "%lu:%s:%s", millis(), topic, buf);

  Serial.println(payload);
  Serial.println(sizeof(payload));

  RF24NetworkHeader header(other_node);
  bool ok = network.write(header, payload, sizeof(payload));
  if (ok)
    Serial.println("ok.");
  else
    Serial.println("failed.");

  free(payload);
}

And receiving node: 和接收节点:

while ( network.available() )
  {
    RF24NetworkHeader header;
    char payload[200];
    memset(payload, 0, 200);
    network.read(header, &payload, sizeof(payload));

    Serial.println(payload);
    Serial.println(sizeof(payload));
    free(payload);
  }

It's not impossible that I have just overlooked something very simple. 我只是忽略了一些非常简单的事情,这并非没有可能。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Edit: This would probably be helpful, this is the data I am sending from the root node: 4203:in_3:hello world again 编辑:这可能会有所帮助,这是我从根节点发送的数据: 4203:in_3:hello world again

and this is what I get: 4203:in_3:hello world ag 这就是我得到的: 4203:in_3:hello world ag

Looking into the source of size_t RF24Network::read 查看size_t RF24Network :: read的来源

bufsize = min(maxlen,frame_size-sizeof(RF24NetworkHeader));

That are at most 24 bytes. 最多为24个字节。

I have tried payloads larger than 24 bytes, it works. 我尝试过大于24字节的有效负载,它可以正常工作。 Somehow, the lib supports a FRAGMENTATION feature. lib某种程度上支持FRAGMENTATION功能。 72-byte payload works on UNO, but not bigger. 72字节有效负载可在UNO上使用,但不能更大。

https://tmrh20.github.io/RF24Network/AdvancedConfig.html https://tmrh20.github.io/RF24Network/AdvancedConfig.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM