DS18B20 Measure value

I’m using a NodeMCU ESP8266 12-E to control a heater. I have the app showing the correct temperature but I want to use the measured temperature to take decisions based on the value. The problem I’m having is the value shown when I try to print it on serial port is always -127 and every time I open the serial screen the value on the app also goes to -127. So I’m not sure what is going on.
Do I need to convert the value I read from the sensor to use it?

#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h> 
#include <DallasTemperature.h>

SimpleTimer timer;

WidgetLCD lcd(V1);

#define Pin D3//  pin DATA ds18b20
#define relay D4

char auth[] = "auth";

OneWire ourWire(Pin); 
DallasTemperature sensors(&ourWire); 

int userSet;
int maxTemp = 75;
float tempMeasured;
int aux;

void setup()
{
 // Set console baud rate
 Serial.begin(9600);
 delay(10);
 Blynk.begin(auth, "ssid", "pass");
 delay(10);
 sensors.begin();
 delay(10);
 timer.setInterval(1000L, ReadTemp);
 pinMode(relay, OUTPUT);
 }

  bool isFirstConnect = true;

BLYNK_CONNECTED() 
{
  if (isFirstConnect) 
  {
    Blynk.syncAll();
    lcd.clear();
   // isFirstConnect = false;
  }
}

void ReadTemp() //Read temperature from sensor and activate relay
{
  sensors.requestTemperatures();
  //Blynk.virtualWrite(0, sensors.getTempCByIndex(0)); 
  lcd.print(0, 0, "Temp:");
  lcd.print(5, 0, sensors.getTempCByIndex(0));
  lcd.print(11, 0,"°C"); 

  if(tempMeasured>maxTemp&&userSet!=1)
  {
    digitalWrite(relay, LOW);
    lcd.print(0, 1, "DESLIGADO");
  }
  if(tempMeasured<65&&userSet!=0)
  {
    digitalWrite(relay, HIGH);
    lcd.print(0, 1, "LIGADO   ");
  }
  if(tempMeasured>=90)
  {
    digitalWrite(relay, LOW);
    lcd.print(0, 1, "DESLIGADO");
   }
}


BLYNK_WRITE(V7) //Check user selection (On/Off)
  {
    if(param.asInt()==0)
    {
      userSet = 0;
      digitalWrite(relay, LOW);
      lcd.print(0, 1, "DESLIGADO");
    }
    else
    {
      userSet = 1;
      digitalWrite(relay, HIGH);
      lcd.print(0, 1, "LIGADO   ");
    }
  }

BLYNK_WRITE(V8) //Check user max temperature setting
  {
    maxTemp = param.asInt();
  }

void loop()
{
  Blynk.run();
  timer.run();
  lcd.print(10, 1, "MAX:");
  lcd.print(14, 1, maxTemp);
}

Could it be the Auto-reset everytime you access to the serial? Not sure…

Do you have any idea if the value I get from sensors.getTempCByIndex(0) needs to be converted? I’m not sure about it and I can’t use the serial monitor to confirm…

You don’t need to make any conversion with sensors.getTempCByIndex(0).

Does lcd.print(5, 0, sensors.getTempCByIndex(0)) show the correct value or -127?

It shows the correct value as long as I don’t open the serial monitor.

Does the value change in the LCD if you touch the DS18B20 (ignoring Serial Monitor)?

Why do you have lcd.print in your loop and do you know what this is doing?

Yes, the value is changing when I touch it. And I just forgot to move that lcd.print to somewhere else lol

Using a WeMoS D1 Mini and your sketch If I add Serial.println(sensors.getTempCByIndex(0)); in ReadTemp() Serial Monitor shows:

[5001] Connecting to blynk-cloud.com:8442
[5160] Ready (ping: 0ms).
31.25
31.25
31.25
31.25
31.50
31.50
31.50
31.50

I think it might be something related to the board I’m using. It’s working fine but if I open the serial monitor it just jump to -127.

I thought from your earlier posts it was just Serial Monitor showing -127 and that the LCD was OK.
Probably not a problem with your board.

Let me check something here and get back to you.

I see. The LCD also change the display when I open the serial monitor. If I close the monitor the LCD will work fine after a few seconds. Maybe is something related to the port I’m using the sensor?

D3 is GPIO 0, you could perhaps try D1 or D2 but I can’t figure out why Serial Monitor is creating problems for you.

I think you have a timing problem .
You’re using
Timer.setInterval ( 1000L , ReadTemp ) ;
You need 750 ms to complete reading in 12 bits.

When you start using the serial port, serial port ISR is your highest priority ISR and so the time Dallas will be compromised . And when 1-Wire sequence is compromised, you get -127 <img

@Surbyte I suspected it might relate to the resolution but yesterday when I reduced the 1000L to 600L for 12bit resolution I still couldn’t get the DS18B20 to fail (with a WeMos) and Serial Monitor.

@AndreSepulveda we generally work with 10 bit resolution with sensors.setResolution(10); // resolution 9 to 12 available after sensors.begin in setup(). You could try changing the resolution and also increasing the 1000L to 2000L to see if that helps.

Failing that it must be something specific to the NodeMCU USB port.

Tried everything suggested. @Costas I tried another port and changed the resolution to 10 bits as you said and I changed the timer.serInterval to 2000L as @Surbyte but it’s still weird if I open the serial monitor.
I can use the project since it’s working fine but I’m intrigued by this weird behaviour

Have you tried DS18B20 without Blynk but including Serial Monitor?

If that fails maybe your hardware is faulty.

I was first trying without Blynk and all I was getting was the -127 on the serial monitor. When I tried using the Blynk app and it gave me the correct reading I thought the value from sensors.getTempCByIndex(0) needed some kind of conversion and the app did that. But when I opened the serial monitor everything went bad again.

The project is up and running already. I just wanted to know why this happens when I open the serial monitor. Maybe someday when I get more familiar with the hardware I’ll find it out.

Probably worth you asking NodeMCU why their hardware is giving you problems and have it replaced if it is faulty. All you would need to do is send them the most basic sketch and then they can easily confirm if your hardware is faulty.

1 Like

@AndreSepulveda,
I have tested your code with my NodeMCU V3 and the serial is working fine:

¶Eô|vAMtè4C8ÿ[256] Connecting to xxxxx
[4257] Connected to WiFi
[4257] IP: xxx.xxx.x.x
[4257] Blynk v0.3.7 on NodeMCU
[5001] Connecting to xxx.xxx.x.xx
[5021] Ready (ping: 10ms).
26.62
26.62
26.62
26.62

I’ve done small changes to your code:

#define Pin D3//  pin DATA ds18b20
#define relay D5

Delete these lines inside the main loop

// lcd.print(10, 1, "MAX:");
//  lcd.print(14, 1, maxTemp);

Add Serial.println(sensors.getTempCByIndex(0)); in ReadTemp() as @Costas said.

Tested at 2 NodeMCU’s