Problem regarding receiving string data for HC12

Hi guys i am newbie for Arduino programming. If i have 16 data (float) and generally i make it became a string send once per second using HC12. For another computer connected with another HC12, how can i receive that string data? I trying using readString function but it cant work even lol. Really appreciate for who can answers this question Thank you very much. Btw for easy your guys understand the code, i make it more simple.

This is my code for Transmitter

#include <SoftwareSerial.h>
SoftwareSerial HC12(5,6); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
}

void loop() {
  float sensors1 = 1.234;
  float sensors2 = 5.678;
  float sensors3 = 9.012;

  String String1 = String (sensors1,3);
  String String2 = String (sensors2,3);
  String String3 = String (sensors3,3);

  String allstring = String (String1+" "+String2+" "+String3);

  //Serial.println(allstring);
  HC12.print(allstring);
  delay(1000);
}

And this is the code for receiver

#include <SoftwareSerial.h>
SoftwareSerial HC12(5,6);                 // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
}

void loop() {   
  while (HC12.available()){               // If HC-12 has data
    Serial.print(HC12.readString());      // Read the string and send the data to Serial monitor
  } 
}

what defines a the string being received? a NUL last characters? some other character (e.g. \n)

have considered using sprintf() and sscanf()?

gcjr:
what defines a the string being received? a NUL last characters? some other character (e.g. \n)

have considered using sprintf() and sscanf()?

Erm..... actually i didn't receive any thing...just a blank for receiver side's Arduino serial monitor

I think the last characters is the float number that depends on the variable sensors3

or i need to change my code below? From this

String allstring = String (String1+" "+String2+" "+String3);

to this?

String allstring = String (String1+" "+String2+" "+String3+'\n');

so now i got the last character \n

Considered using that, but the question is i don really understand what the relation for softserial /serial between sprintf()/sscanf()

i think i really need the help, because i am totally newbie for wireless communication even read a lot of post but still cant find a solution

Start at the transmitter side and uncomment your Serial.print so you can see what allString contains.

You don’t have to concatenate before sending; below will send the same (and it will not use String).

HC12.print(sensors1,3);
HC12.print(" ");
HC12.print(sensors2,3);
HC12.print(" ");
HC12.print(sensors3,3);

in the receiver, when available() can you simple read() a single character and display it on the serial monitor?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Thank you your guys give optinion. The question have been solve. Below are the code for transceiver about how to solve.

For transmitter

#include <SoftwareSerial.h>
SoftwareSerial HC12(5,6); // HC-12 TX Pin, HC-12 RX Pin


void setup() {
  Serial.begin(9600);
  HC12.begin(9600); 

}
void loop() {  

    float sensors1 = 1.234; 
    float sensors2 = 5.678;
    float sensors3 = 9.012;
    float sensors4 = 0.123;
    float sensors5 = 1.567;
    float sensors6 = 3.142;
    float sensors7 = 9.814;
    float sensors8 = 2.878;
    float sensors9 = 1.414;
    float sensors10 = 0.577;
    float sensors11 = 1.618;
    float sensors12 = 1.324;
    float sensors13 = 4.669;
    float sensors14 = 2.503;
    float sensors15 = 0.915;
    float sensors16 = 1.451;
  
    String String1 = String (sensors1,3);
    String String2 = String (sensors2,3);
    String String3 = String (sensors3,3);
    String String4 = String (sensors4,3);
    String String5 = String (sensors5,3);
    String String6 = String (sensors6,3);
    String String7 = String (sensors7,3);
    String String8 = String (sensors8,3);
    String String9 = String (sensors9,3);
    String String10 = String (sensors10,3);
    String String11 = String (sensors11,3);
    String String12 = String (sensors12,3);
    String String13 = String (sensors13,3);
    String String14 = String (sensors14,3);
    String String15 = String (sensors15,3);
    String String16 = String (sensors16,3);
  
    String allstring = String (String1+" "+String2+" "+String3+" "+String4+" "+String5+" "+String6+" "+String7+" "+String8+" "+String9+" "+String10+" "+String11+" "+String12+" "+String13+" "+String14+" "+String15+" "+String16+'\n'); 

    Serial.print(allstring);
    HC12.print(allstring);
    delay(1000);
  
}

Receiver

#include <SoftwareSerial.h>
SoftwareSerial HC12(5,6);                 // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);
  HC12.begin(9600); 
}

void loop() {   
  if (HC12.available()){               // If HC-12 has data
    Serial.println(HC12.readStringUntil('\n'));      // Read the string and send the data to Serial monitor
  }  
}

Btw can i ask about how to send the data (example a interger value = 1 from my receiver to trasmitter?) based on my current code?

cyjian_99:
Thank you your guys give optinion. The question have been solve. Below are the code for transceiver about how to solve.

It does not seem as if you have taken much notice of the comments you have received.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

And there is no need to make a compound String or cstring in order to send the data.

...R

Robin2:
It does not seem as if you have taken much notice of the comments you have received.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

And there is no need to make a compound String or cstring in order to send the data.

...R

Ya it happen exactly what you said the memory corruption.Btw, sorry i don understand, will possible that the data will loss if no to make the compound String or cstring? or my transmitter can change the code like below

Serial.print(Sensors1);
Serial.print(" ");
Serial.print(Sensors2);
Serial.println('\n'); // end marker

and my receiver code like below

if (HC12.available()){               // If HC-12 has data
    Serial.println(HC12.readStringUntil('\n'));      // Read the string and send the data to Serial monitor
  }

Robin2:
It does not seem as if you have taken much notice of the comments you have received.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

And there is no need to make a compound String or cstring in order to send the data.

...R

Btw can i have any simply code just let me understand how cstring work in this type of situation? What i found is only this 2 website link is it usable? Sorry really don understand
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/
and

cyjian_99:
...
or my transmitter can change the code like below

Serial.print(Sensors1);

Serial.print(" ");
Serial.print(Sensors2);
Serial.println('\n'); // end marker

See reply #3 where that approach was suggested :wink:

cyjian_99:
Btw can i have any simply code just let me understand how cstring work in this type of situation? What i found is only this 2 website link is it usable? Sorry really don understand
c_str() - Arduino Reference
and
https://forum.arduino.cc/index.php?topic=537393.0

See Robin's link in reply #5

cyjian_99:
orry i don understand, will possible that the data will loss if no to make the compound String or cstring?

The receiving device cannot tell whether the transmitting device sends

Serial.println("Hello World");

or

Serial.print("Hello ");
Serial,println("World");

In Reply #5 I have given you examples of code that will reliably send and receive data - have you tried them?

...R