Noob problem with NodeMCU and OneWire

  • Ciao to all,
    I'm moving to MySensors network in my home to be used with Domoticz... after a first phase made by a gateway on a raspberry PI2, a rain (on/off), I'm now approaching to reorganize the entire network and move to a nodeMCU (12e) gateway which include the rain sensor and I would like to add a DS18B20 too (to gather external temp)
    While the gateway and the on/off switch works pretty fine, I'm having a bad problem with OneWire: even if the sketch is compiled with no error, I see no device, nr presented, nor updated, no device at all

    Wirings are ok: using D1 pin (no luck with other pins), with a 4k7 resistor... 3.3v are provided from an external 5v (2A) power supply and a AMS1117 3.3
    I checked the same sketch on a ATMega328p nano and it works... seems to be a NodeMCU device issue
    does anyone experienced the same?
    Is there a solution for this (which does not include to move to DHT22 😛 😛 )

  • Mod

    @emme welcome to the MySensors community. Are you still stuck on this? If so, could you post your sketch and describe all your wiring?

  • still unsolved, but workaround by replacing the onewire with a DHT22 😛 😛

    this is my sketch:

    // Gateway MySensors on NodeMCU with some Devices 
    // OneWire for DS18B20 
    // Bounce for S_DOOR (Rain Sensor)
    #define MY_DEBUG
    #define MY_BAUD_RATE 115200
    #define MY_RADIO_NRF24
    // #define MY_GATEWAY_SERIAL
    #define MY_GATEWAY_ESP8266
    #define MY_ESP8266_SSID "myWiFi"
    #define MY_ESP8266_PASSWORD "******"
    #define MY_ESP8266_HOSTNAME "sensor-gateway"
    #define MY_PORT 5003
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS D1 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 5
    unsigned long temp_Sleep = 30000; // Sleep time between reads (in milliseconds)
    unsigned long temp_nTime;
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    bool metric = true;
    // Initialize temperature message
    MyMessage msgTemp(0,V_TEMP);
    #include <Bounce2.h>
    // RAIN Sensor Definitions and settings
    #define CHILD_ID 10
    #define BUTTON_PIN D4  // Arduino Digital I/O pin for button/reed switch
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    MyMessage msgRain(CHILD_ID,V_TRIPPED);
    unsigned long cTime;  // Current Time
    void before()
      // Startup up the OneWire library
    void setup()  
      // requestTemperatures() will not block current thread
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temperature Sensor", "1.1");
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      present(CHILD_ID, S_DOOR);  
    void loop()     
      cTime = millis();  // Get current Timing
      if ((cTime - temp_nTime) >= temp_Sleep) {     // this is supposed to replace the sleep function 
        for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
          float temperature = static_cast<float>(static_cast<int>(sensors.getTempCByIndex(i)));
          #if COMPARE_TEMP == 1
          if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
          if (temperature != -127.00 && temperature != 85.00) {
        temp_nTime = millis();
      int value =;
      if (value != oldValue) {
         // Send in the new value
         send(msgRain.set(value==HIGH ? 1 : 0));
         oldValue = value;

    if I enter a

    Serial.print("Sensors Count over 1W bus: ");

    I got 0 😞

    wirings are ok... I checked with the tester and I get correct voltage (3,3) and impedence (4,7k)
    DHT works fine at first try on the same PIN

    thanks for the help

  • Hello,
    I have exactly the same issue with ESP8266 nodemcu as Gateway + sensor DHT22.
    I tested with 2 different libraries DHT22.
    And I also tested with mySensors 2.1.0 and 2.1.1

    Console serial write out: Failed reading from DHT

    Otherwise the gateway works well without sensor (so alone) and with the DHT22 sensor on a node (arduino nano)

  • Mod

    I'm moving to I2C sensors to avoid these kinds of problems 😀

  • Hardware Contributor

    I tried with two different Dallas Onewire and checked out the library at github. Have build these sensors before but got -127 all the time. Just adding this here if it pops up alof of people having issues...