Serial monitor breaks 1-wire protocol #1467
Description
The following code reads two DS18B20 digital temperature sensors and reports their values to the serial monitor.
// Program to read 1-wire temperatures and upload to ThingSpeak
#include "OneWire.h"
#include "DallasTemperature.h"
#define ONE_WIRE_BUS D3
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(115200);
sensors.begin();
delay(1000); //time for serial to connect
Serial.print(sensors.getDeviceCount());
Serial.println(" sensors found");
Serial.print(sensors.getDS18Count());
Serial.println(" DS18 sensors found");
// first temp reading seems bogus, so skip one
sensors.requestTemperatures(); // Send the command to get temperatures
float sensor0 = sensors.getTempFByIndex(0);
while ( sensor0 > 150)
{
#ifdef DEBUG
Serial.print("Discarding value ");
Serial.println(sensor0);
#endif
sensors.requestTemperatures(); // Send the command to get temperatures
sensor0 = sensors.getTempFByIndex(0);
delay(2000);
}
}
void loop()
{
#ifdef DEBUG
Serial.println("Requesting temperatures...");
#endif
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperature for Device 1 is: ");
Serial.println(sensors.getTempFByIndex(0));
Serial.print("Temperature for Device 2 is: ");
Serial.println(sensors.getTempFByIndex(1));
delay(5000);
}
I am testing this code on a Wemos D1 mini. When this code is compiled and uploaded from the Arduino IDE and observed via the Arduino IDE serial monitor, I get the expected results, showing the observed temperatures. If I compile and upload in VSCode and observe the output via the VSCode serial monitor, I get a result that indicates the devices aren't being read. If I compile/upload from VSCode but use the Arduino IDE serial monitor, I get the correct results, leading me to conclude that it is the serial monitor that is causing the problem. I have a more complex program that displays the temperature on an OLED display. The display shows the correct temperatures when there is no serial monitor or when connected to the Arduino IDE serial monitor. It displays the "no reading" value when connected to the VSCode serial monitor, providing additional evidence that the serial monitor is the root of the problem. I've tested with port speeds of 115200 and 19200 and get the same results.
Here's what "About" reports from VSCode
Version: 1.65.0 (user setup)
Commit: b5205cc8eb4fbaa726835538cd82372cc0222d43
Date: 2022-03-02T11:12:08.962Z
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19044
Arduino extension V0.4.11
Hardware: Wemos D1 mini (clone)