Ok Brainiacs.......

Hi,

Yet another newbie. Programming an esp8266 01. I have hacked together code from several different sources to set up a temp probe that sends to a data.sparkfun.com account and also send me emails. Looks a little like a bad horror movie but it works...kinda. I have a couple of issues.

I call the getTemp() function from within 2 functions, postToPhant() and sendEmail(). It seems these return the correct temp the first time and 0.00 the next time. I think the getTemp() function is failing every second time, but not sure why.

Here's the code. There's lots of stuff commented out because I am still fooling around trying to get it all to work. I have found the ercv() function doesn't work right, so I've commented it out for now. I'm trying to figure out how I can print to the serial port what info is received when I am posting and emailing.

Any help would be great. I generally understand all the programming but I'm pretty rusty as I haven't played with programming for several years and never really played with c.

sendemailandPhant.txt (13.2 KB)

I have been noticing that every second call to the getTemp() function gets a "no more addresses" reply when the temp is 0.00 so it sounds like it can't find the ds18b20 probe? It's funny how it's every second time.

I tried moving the following code to the beginning of the getTemp() function just after the variables but it still gave me the same result so I'm not sure what the ds.reset does other then reset the onewire, the ds.search should work properly......I think?

ds.reset(); //??initializes the onewire?
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not.

I've only used the OneWire library with the DallasTemperature lib.

My guess is that search() is returning your device's address and zero when it next looks.

You could try the plain vanilla OneWire demo in the IDE and see if that returns similarly (I didn't compare line-for-line), then try the DallasTemperature library.

Also, I'd set the I2C address because I can and I needn't search for it every time I want to get a temp.

Bear in mind that this sensor is slow, as the author of the library points out..

What your code is doing is searching for the DS18B20 address and finding it on the first pass and successfully reading a temperature. Next time through it searches but has no more addresses, resets the search and exits without trying to read the DS18B20. Next time through it succeeds again, etc.

You should search for the address once (usually in setup) which then populates the addr array. Then in getTemp you start with the code you have at:

 ds.reset();
 ds.select(addr);

and proceed from there.

Pete

That's it. I simplified the sketch to just the getTemp function and had the same results. Once I moved the setup info to the setup area (doh!) it gives me correct info every time.

Here's what I did. I am not sure if I need the esp8266.h file to just read the temp. Probably not. The temp is a bit off but I suspect it could be that I'm using a 5K resistor as I didn't have a 4.7k handy. I tried moving everything up to the ds.write(0x44, 1); in the loop() section into the setup section but then it only showed 1 temperature and I think it wasn't refreshing.

here's the output, and I just read and figured out how to post the code properly. Thanks!

Data = 1 71 1 4B 46 7F FF C 10 3 CRC=3
Temperature = 23.06 Celsius, 73.51 Fahrenheit
Data = 1 71 1 4B 46 7F FF C 10 3 CRC=3
Temperature = 23.06 Celsius, 73.51 Fahrenheit
Data = 1 71 1 4B 46 7F FF C 10 3 CRC=3
Temperature = 23.06 Celsius, 73.51 Fahrenheit
Data = 1 71 1 4B 46 7F FF C 10 3 CRC=3
Temperature = 23.06 Celsius, 73.51 Fahrenheit

// Include the ESP8266 WiFi library. (Works a lot like the
// Arduino WiFi library.)
#include <ESP8266WiFi.h>
#include <OneWire.h>
OneWire  ds(2);
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;


 
void setup(void) {
  Serial.begin(115200);
   //ds.reset();
  //  Serial.println(addr);
  //  ds.select(addr);
  //  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

    if ( !ds.search(addr)) {
      Serial.println("No more addresses.");
      Serial.println();
      ds.reset_search();
      delay(250);
      return;
   }

  Serial.print("ROM =");
  for ( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
    Serial.println("CRC is not valid!");
    return;
  }
  Serial.println();

 

}

void loop(void) {
 // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      //      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      //     Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      //     Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(2000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE);         // Read Scratchpad

   Serial.print("  Data = ");
   Serial.print(present, HEX);
   Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
      Serial.print(data[i], HEX);
        Serial.print(" ");
  }
    Serial.print(" CRC=");
    Serial.print(OneWire::crc8(data, 8), HEX);
   Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    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
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
  return;
}

tempSensorDS18B20.ino (3.12 KB)

Read How to post code properly and then edit your message #4 to add code tags around your code. Then it won't mysteriously turn into italics part way through.

Pete