ESP8266 with DS18B20 temperature sensor

I am fairly new to arduino, (but an ex programmer). I purchased a few temperature sensors ( DS18B20) and a few ESP8266 boards and signed up for an arduino cloud account.
I added a thing, a device and some variables. However when I try to run my code, i have a few issues. 1) on the serial monitor, i see this:

Connection to "motor2" failed
Retrying in "500" milliseconds
-127.00ºC
-196.60ºF

and the sketch I have is this:

#include <OneWire.h>
#include <DallasTemperature.h>
#include "thingProperties.h"
// GPIO where the DS18B20 is connected to
const int oneWireBus = 2;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

void setup() {
  
 // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Start the DS18B20 sensor
  sensors.begin();

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");

  tempSensor1 = temperatureF; 
  delay(5000);
}

I connected the sensor wire from the digital pin 2 of the ESP8266 board.

Can someone point out what the problem(s) are and what I can do?

Try with only the sensor and serial monitor.

What I miss is the search to the sensors:

  deviceCount = sensors.getDeviceCount();
  for (int i = 0;  i < deviceCount;  i++)
  {
    sensors.getAddress(Adres, i);
    printAddress(Adres);
    //_delay_ms(9999); //10 seconden wachten voor de volgende
  }

void printAddress(DeviceAddress deviceAddress)
{
  
  for (uint8_t i = 0; i < 8; i++)
  {
    Serial.print("0x");
    if (deviceAddress[i] < 0x10) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) Serial.print(" ");    
  }
}

Thank you. I did your recommendation and i see I got zero devices.
I took out my multimeter and checked to see I am getting 3.3 volts, which are coming from the + and Gnd of the ESP8266. And the third wire (yellow) to the DS18B20 going to the D2 input. Cant understand why showing zero sensors. Also second issue is that my serial monitor says "Connection to "blue" failed" , "blue" being my home wifi network.

3rd question, is this: Also sometimes I see the pin defined as D2 and sometimes as 2 (digital pin input for the ESP8266), not sure which is correct. I have tried both.
Thanks for any and all help.

D2 = GPIO4

2 examples to wire your DS18B20

There must be a resistor of 4k7 between 3V3 (3,3V in schematic draw) and data to pull the open collector to high.

3V3 is short on the absolute minimum voltage 3V described in the datasheet. With longer wires there can be problems.

Most program stop if the WiFi isn't connected. Try without the WiFi.

Thank you for the the help. I did not know the pull up resistor was such an important thing. I was thinking i would get some value at least for testing, but I just addded the 4.7k and now am able to see the address of my sensor. Also am getting the temperature reading. Now I wish i could figure out why my device is not seen via IOT Cloud. next to my device, I see "Offline" and the serial monitor keeps stating " Connection Failed". Thanks so much for helping.

1 Like
// D0 - GPIO6
// D1 - GPIO5  // SDA I2C
// D2 - GPIO4  // SCL I2C
// D3 - GPIO0
// D4 - GPIO2
void wificonect() { // процедура подключения к Wifi.
  if (DHCP)  WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
  WiFi.begin(ssid, password, chanal, macAP, true);

  while (WiFi.status() != WL_CONNECTED) {
    int i;
    if (debug) if (!(i % 100))Serial.print(".");
    delay(1);
    i++; if (i > 3000)  ESP.deepSleep(60e6);//СПИМ 1 МИНУТу
  }
  if (debug) Serial.println("connected...");
  Hostname = "ESP" + WiFi.macAddress();
  Hostname.replace(":", "");// удаляем из названия двоеточия
  // WiFi.hostname(Hostname); // Название станции внутри локальной вайфай сети

  if (debug){ Serial.println(); Serial.print (millis()); Serial.println (" Подключено к wifi");}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.