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.
#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
}
}
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)
#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.
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