Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By hakha4
#37612 Happy New Year everybody!
I have a working sketch for Ds1820 powered external but can't get it work in parasitic mode with ESP01. I'll try powering with a MOSFET but how to manage pin powering MOSFET ? Do I need to change in DallasTemperature lib ? Any help appreciated
User avatar
By MikeKulls
#85304 I spent a lot of time on this and found a solution. First was that it only worked with some pins. I found it wouldn't work with D0, D3 or D4 but it would work with D5 and D6. Second problem was that nice wrapper Dallas module didn't work but the OneWire library worked fine. I made some changes to the sample code so that it loops through multiple sensors on the same pin in the one call. I also changed it to remove the 1000ms delay. Normally you need to request the sensor to take a reading, wait 1000ms and then read the data. I changed it so that it reads the data and then requests the sensor to take a reading. This means the reading will be there for the next time you call it. This means that you are good as long as you don't request a temperature more than once per second. The downside to that is that the reading is going be outdated if you don't take readings very often.

Code: Select all#include <OneWire.h>
 
OneWire  ds(D5);  // on pin D5 (a 4.7K resistor is necessary)
 
void setup()
{
  Serial.begin(9600);
}

void loop() {
  delay(1000);
  readTemps();
}

void readTemps()
{
  ds.reset_search();
 
  byte addr[8];
  while(ds.search(addr))
  {
    if (OneWire::crc8(addr, 7) != addr[7])
    {
        Serial.println("CRC is not valid!");
        break;
    }
 
    // the first ROM byte indicates which chip
    byte type_s;
    switch (addr[0])
    {
      case 0x10://DS18S20
        type_s = 1;
        break;
      case 0x28://DS18B20
        type_s = 0;
        break;
      case 0x22://DS1822
        type_s = 0;
        break;
      default:
        Serial.println("Device is not a DS18x20 family device.");
        break;
    }
   
    ds.reset();
    ds.select(addr);   
    ds.write(0xBE);         // Read Scratchpad
 
    byte data[12];
    for (byte i = 0; i < 9; i++)
    {           
      data[i] = ds.read();
    }
 
    // Convert the data to actual temperature
    int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
    }
    else
    {
      byte cfg = (data[4] & 0x60);
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    }
    float celsius = (float)raw / 16.0;
    Serial.print(celsius);
    Serial.print("C    ");
   
    ds.reset();
    ds.select(addr);   
    ds.write(0x44, 1);         // Read temp
  }
  Serial.println();
}