Random characters in serial output using integrated NodeMCU and ESP8266

I have a project using the NodeMCU-12E with ESP8266 and this reads temp and humidity from a DHT22 sensor.

The code works correctly, and when connected to the Mac I get serial output confirming the readings.

As I work towards reducing power consumption I have added a line to force the ESP8266 to deep sleep - the idea being that I only Tx the data once a minute.

It all works, but I do notice that if I look at the serial output, the first line of the two every minute has some garbage up front between the timestamp and the written output "Humidity" and then value.

So, I might get, for example, something like:

"14:20:03 ?.%???Humidity 45%
14:20:03 Temperature 20.0 C"

The second line with the Temperature reading has no garbage. Happens EVERY first line.

Am I right that this is unavoidable - due to booting up as a result of my deep sleep command?

It does not appear to affect the data sent to Blynk - but can I ask if it may well in fact do so?

This is my loop code. The garbage ONLY occurs when I include the ESP.deepSleep line.

My first project, so apologies if I have made any mistakes posting...

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates the timer
  sendSensor(); // Pulls data from DHT sensor
  // The following serial code allows you to ensure that you are getting valid readings which can be checked when connected via USB
  Serial.print("Humidity: ");
  Serial.print(String(h).c_str());
  Serial.println(" %");
  Serial.print("Temperature: ");
  Serial.print(String(t).c_str());
  Serial.println(" C"); 
  // REMEMBER to set any 'offline alerts' in Blynk Projects to EXCEED the read time delay  
  ESP.deepSleep(60e6); // DEEP SLEEP for 60 seconds   
}

Since all the data you are printing to the Serial monitor is present, the ESP2866 going to sleep is probably releasing the TX line which makes the receiver (your PC) believe there is incoming data. The only way around would be to have a start and end marker around your data so you can discard any garbage. Serial Input Basics[\url] but then your PC would have to parse this (if you care)

Try putting a several ms delay at the start of the loop to let things settle out a bit after becoming awake.

zoomkat:
Try putting a several ms delay at the start of the loop to let things settle out a bit after becoming awake.

Good point - I did try a 2000mS delay before sendSensor, but this did not solve the issue. Might it if I put it right at the start of the loop instead?

"Good point - I did try a 2000mS delay before sendSensor, but this did not solve the issue. Might it if I put it right at the start of the loop instead?"

I don't see all of your code, but I would put it before any sensor is read (analog inputs often need to be read twice to get good data). You might add an empty Serial.print() before the start of the data printing. Just looking the time may have the garbage added, so another place to look.

Super all, thanks for feedback. It's not a show-stopper as the Tx to Blynk each minute appears to work, but I'd prefer not to have loose ends.

I'll experiment with earlier delays and report back if I solve it conclusively for the info of all here.