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

2
  • Possible duplicate of stackoverflow.com/questions/10105666/…
    – v7d8dpo4
    Oct 18 '16 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. Oct 19 '16 at 9:31
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");
4
  • You could disable reset, by adding a capacitor between GND and RESET. Though this make uploading new code less convenient.
    – Gerben
    Oct 13 '16 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.
    – Majenko
    Oct 13 '16 at 12:28
  • Good point. Didn't think of that.
    – Gerben
    Oct 13 '16 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.? Oct 19 '16 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
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.