1

I have coded arduino for DHT11 which used to print humidity in first and temperature in second line. But some times when I open SerialPort I get absurd data.enter image description here

asked Oct 13, 2016 at 11:24
2
  • Possible duplicate of stackoverflow.com/questions/10105666/… Commented Oct 18, 2016 at 2:18
  • I am sending this data from arduino to pi serially and from pi to cloud. So some times I used to receive humidity as 6767.0 on the cloud. I am printing on console just for showing an example. Commented Oct 19, 2016 at 9:31

2 Answers 2

3

You can't.

The data you are seeing has already been sent by the previous execution of the sketch before the main MCU was reset. That data resides in a small buffer within the USB interface chip (or even within the USB CDC/ACM / USB UART driver itself, depending on your OS) to which you have no access.

You just have to ignore that data. The simplest way is to put a marker at the start of your program to indicate that whatever is receiving the data should start its receiving from this point on. Something like:

Serial.println();
Serial.println("START");
answered Oct 13, 2016 at 11:27
4
  • You could disable reset, by adding a capacitor between GND and RESET. Though this make uploading new code less convenient. Commented Oct 13, 2016 at 12:27
  • 1
    @Gerben It also means that you can start reading partway through a line of digits giving you similar garbage at the start of your reception. Doesn't really cure the problem. Better is to use a proper protocol rather than plain ASCII text. Commented Oct 13, 2016 at 12:28
  • Good point. Didn't think of that. Commented Oct 13, 2016 at 14:14
  • I am sending this data from arduino to pi serially and from pi to cloud. So some times I used to receive humidity as 6767.0 on cloud. I am printing on console just for showing an example. So is there no way to clean output buffer of arduino when ever it starts.? Commented Oct 19, 2016 at 9:34
0

I've circumvented this issue by sending a byte to the Arduino whenever I'm ready to receive data using this quick-and-dirty method:

if(Serial.available() > 0) { // At least one byte is available
 Serial.println(millis());
 Serial.read(); // Read the (one) character to empty the buffer
}
answered Dec 2, 2020 at 12:54
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.