0

I send 3 set of data from 3 sensors from Arduino 1 (router) to another Arduino(coordinator) to with wireless technology (xbee): On coordinator, I receive wireless data from this 3 sensors(from the router) perfectly. The data stream is something like this(each sensor data on its line):

22.5624728451
944
8523

I want to have these 3 values as 3 variables that get updated constantly and then pass these values on to the rest of the program to make something like print on LCD or something else:

temperature=22. 5624728451
gas=944
smoke=8523

Initially, I had only 2 sensors and I send the data of these 2 sensors something like this:
22.5624728451944(22.5624728451 – temperature, 944 - gas) and I received both of them on the same line and divided everything into two variables(with readString.substring() ) with the code below. But now I have 3 sensors and I receive data on a separate line because I don't know which is the length of each data string … And I can't use the same technique (sending only one string that contain all sensor data on the same line and then divide them)

My old code:

    #include <LiquidCrystal.h>
    LiquidCrystal lcd(12,11,10,9,8,7);  
    String temperature;
    String gas;
    String readString;

    void setup() {
    Serial.begin(9600);          
    lcd.begin(16, 2);                              
    }
    void loop() {
      while (Serial.available() > 0)                             
    {
    char IncomingData = Serial.read();                   
    readString += IncomingData ;       
    temperature = readString.substring(0, 13); //get the first 13 characters
    gas = readString.substring(13, 16); //get the last 3 characters 

    Serial.print(IncomingData);  //here I have my string: 20.1324325452924 wichs is updating properly when I have sensor values changes 

    // Process message when new line character is DatePrimite
    if (IncomingData == '\n')
    {
      Serial.println(temperature);
      lcd.setCursor(0,0);                              
      lcd.write("T:");
      lcd.print(temperature);               
     delay(500);
     temperature = "";                               // Clear DatePrimite buffer

     Serial.println(gaz);
     lcd.begin(16, 2);
     lcd.setCursor(0,1);                              
     lcd.write("G:");
     lcd.print(gas);
     delay(500);
     gaz = "";                                       // Clear DatePrimite buffer
     readString = "";
  }
}

}

All I want to do now is to assign a variable for every sensor data (3 lines – 3 variables for each line) updated constantly and then pass these values on to the rest of the program. Does anyone have any idea how to modify the code tO work in this situation? Thank you in advance!

4

1 回答 1

1

string.split()如果您承诺使用字符串值,我建议您将值连接到发送端的同一行,并在接收端使用逗号等分隔符。编辑:看来Arduino没有这个string.split()功能。有关示例,请参见此对话。

另一种方法是设置标准字节长度并将数字作为二进制而不是表示数字的 ASCII 编码字符串发送。有关背景知识,请参阅Arudino 论坛上的这篇文章。我建议以原始字节表示法发送数字,而不是 ASCII 字符。当您在 arduino 上将变量定义为整数时,它默认为 16 位有符号整数值。浮点数是一个 32 位浮点数。例如,如果您发送一个浮点数和两个整数作为二进制值,浮点数将始终是前 4 个字节,第一个整数,下一个 2,最后一个整数是最后 2。字节的顺序(字节序,或最有效字节优先(Big Endian,摩托罗拉风格)/最低有效位优先(Little Endian,英特尔风格))。

于 2019-01-08T16:18:36.007 回答