-1

I have a waterproof DS18B20 sensor that I have connected to a WeMos D1 Mini microcontroller. This controller connects to Wifi and an MQTT broker, sending the temperature from the sensor and goes to deep sleep.

Everything works nicely, except for the sensor reading. It returns -127, which means the sensor is disconnected. I have checked cables, measured voltage and everything should be just fine.

I have data cable for the sensor connected to D2, so in my sketch I have tried the following pins without success: oneWire(D2) - oneWire(2) - oneWire(GPIO4) - oneWire(4). No success.

Can anyone please help me with the following sketch?

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// WiFi settings
const char* ssid     = "[REDACTED]";
const char* password = "[REDACTED]";

// MQTT settings
const char* mqttServer   = "[REDACTED]";
const char* mqttUser     = "[REDACTED]";
const char* mqttPassword = "[REDACTED]";
const int   mqttPort     = 1883;
const char* mqttClient   = "[REDACTED]";
const char* mqttTopic    = "[REDACTED]";

#define durationSleep   15 // deep sleep length in minutes
#define NB_TRYWIFI      20 // number of wifi connection tries
#define ONE_WIRE_BUS     4 // temperature sensor connection

WiFiClient        espClient;
PubSubClient      client(espClient);
OneWire           oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);

float celsius = 0;

void setup() {
  Serial.begin(115200);

  // Connect to sensors
  sensor.begin();

  // Setup WiFi
  WiFi.begin(ssid, password);
  int _try = 0;

  // Connect to WiFi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    _try++;
    if (_try >= NB_TRYWIFI) {
      ESP.deepSleep(durationSleep * 1000000);
    }
  }

  // Setup MQTT
  client.setServer(mqttServer, mqttPort);

  // Get temperature in celsius
  sensor.setResolution(10);
  sensor.requestTemperatures();
  celsius = sensor.getTempCByIndex(0);

  // Connect to MQTT broker
  while (!client.connected()) {
    if (client.connect(mqttClient, mqttUser, mqttPassword)) {
      client.publish("mspa/temperature", String(celsius).c_str(), true);
      delay(1000);
    } else {
      delay(2000);
    }
  }

  // Go to sleep
  ESP.deepSleep((durationSleep * 60) * 1000000);
}

void loop() {
}
  • Did you add a pull-up resistor to the DS18B20 data pin? – StarCat Mar 26 at 21:39
0

//Call this function after sensor begin in setup.. share output

uint8_t findDevices(int DSPIN)
{
  OneWire ow(pin);

  uint8_t address[8];
  uint8_t count = 0;


  if (ow.search(address))
  {
    Serial.print("\nuint8_t pin");
    Serial.print(pin, DEC);
    Serial.println("[][8] = {");
    do {
      count++;
      Serial.println("  {");
      for (uint8_t i = 0; i < 8; i++)
      {
        Serial.print("0x");
        if (address[i] < 0x10) Serial.print("0");
        Serial.print(address[i], HEX);
        if (i < 7) Serial.print(", ");
      }
      Serial.println("  },");
    } while (ow.search(address));

    Serial.println("};");
    Serial.print("// nr devices found: ");
    Serial.println(count);
  }

  return count;
}
| improve this answer | |
  • Thanks for your answer! Unfortunately this doesn't return anything: ets Jan 8 2013,rst cause:2, boot mode:(3,6) load 0x4010f000, len 1392, room 16 tail 0 chksum 0xd0 csum 0xd0 v3d128e5c ~ld – rebellion Mar 26 at 19:50
  • Btw, I also tried multiple DS18B20 sensors, with no luck. Not sure what I am doing wrong. I also tested with other D1s, no luck. – rebellion Mar 26 at 21:26
  • Did you add a pull-up resistor to the DS18B20 data pin? – StarCat Mar 27 at 14:44
0

And for the stability (Read the comments) of your solution (Change anyway!) instead of

// IMPORTANT: This leads to crashes due to heap fracturing and imho does not work stable on micro controllers
 client.publish("mspa/temperature", String(celsius).c_str(), true); 

 // Do this - define fixed chars before Setup - the compiler puts themto the stack
 char mqttBuffer[16] = '\0';
 char topic[] = "mspa/temperature";
 .....
 dtostrf(celsius,6,2,mqttBuffer); // convert a float to char
 client.publish(topic, mqttBuffer, true); // We use the fixed chars instead of heap fracturing Strings

Read more on evil Strings: https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/
Hope this helps

| improve this answer | |

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.