ESP8266 digital read (ds18b20)

Digital read and digital devices
In last topic we saw how to read analogic sensors using the ADC pin with our esp transmissor. Now we will see how to read from digital devices that counts with an IC (integrated circuit) which provides a comunication bus and a standariced transmission protocol like One Wire Bus, I2c, SPI, etc. It means that we will need to build our code including the specific protocol for each device. Fortunately, there are some arduino libraries to make easy this job.

There are some advantages using digital transmision: like no accuracy losts by wire interferences, or the posibility to link a lot of devices and sensors using the same bus (in I2C or SPI).

Most popular digital sensors are: DHT11 and DHT22 temperature + humidity, Dallas ds18b20 only temperature, bmp180 (I2C) for atmospheric pressure, etc.

ESP8266 + DS18B20

  1. Wiring up dallas ds18b20 sensor with pull-up resistor:


    4.7K-10K ressistor between dallas pin2 and dallas3 pin (pull-up between output and vcc)

  2. In our arduino IDE we will add OneWire.h and DallasTemperature.h libraries, and copy this code:

#include < OneWire.h>
#include < DallasTemperature.h>
#include < SPI.h>
#include < ESP8266WiFi.h>
#include < ThingerWifi.h>

#define ONE_WIRE_BUS 12 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.

#define USERNAME “your_user_name”
#define DEVICE_ID “your_device_id”
#define DEVICE_CREDENTIAL “your_device_credential”

#define SSID “your_wifi_ssid”
#define SSID_PASSWORD “your_wifi_ssid_password”

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
pinMode(BUILTIN_LED, OUTPUT);

thing.add_wifi(SSID, SSID_PASSWORD);

thing[“temp”] >> (pson & out) {
sensors.requestTemperatures(); // Send the command to get temperatures
out[“TEMPERATURA”] = sensors.getTempCByIndex(0);
};

sensors.begin();
}

void loop(void) {
thing.handle();

}

Just a note. It is not necessary to request the temperature inside the loop function. It can be done directly inside the [“temp”] resource. So the microcontroller doesn’t need to read the temperature every time, just when it is requested. This will save power consumption and will improve the overall response time =)

1 Like

I am getting this error for this code “
xtensa-lx106-elf-g++: error: CreateProcess: No such file or directory” help me out

Hi @Ravi, what is your environment? This example is for the Arduino libraries with the Thinger.io libraries, Dallas sensor, etc.

I am using Arduino IDE, Dallas library, ESP8266 12E

Do you have all the libraries and boards updated? And the ESP board selected? This error seems to be from the ESP8266 board:

Hey there,

I’m quite new with all of this (meaning: Arduino, NodeMCU, Thinger.io and such) but have been tinkerer and overall electronic enthusiast for very long time. Now, I was able to pull some great stuff with NodeMCU and Thinger.io and this tutorial helped me a lot in making tiny weather station for my back yard using this very same sensor. However, I’ve noticed that since I’ve uploaded this sketch, NodeMCU became somewhat sluggish and it seems that it is always “working” something this it’s being a bit unresponsive. Now, since I’m using the same microcontroller for some other purposes (unlocking the strike lock, lights through the backyard) I’ve noticed that, when I have added this solution to my original sketch, controlling of the relays became pretty unreliable and often with quite a bit of latency.

Here’s my sketch:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define ONE_WIRE_BUS D4 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define USERNAME "xxxxxxx"
#define DEVICE_ID "xxxxxxx"
#define DEVICE_CREDENTIAL "xxxxxxxx"

#define SSID "xxxxxxx"
#define SSID_PASSWORD "xxxxxxxx"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  pinMode(D3, OUTPUT);
  pinMode(D2, OUTPUT);
  digitalWrite(D2, HIGH);

  thing.add_wifi(SSID, SSID_PASSWORD);

  thing["DoorUnlock"] << [](pson& in){ digitalWrite(D3, in ? HIGH : LOW); };
  thing["Lights"] << digitalPin(D2);
  thing["Lights"] << invertedDigitalPin(D2);
  thing["temp"] >> [](pson & out) {
sensors.requestTemperatures(); // Send the command to get temperatures
out["Temperature"] = sensors.getTempCByIndex(0);
};

sensors.begin();
}
  
void loop(void) {
  thing.handle();
}    

Any ideas what I might be doing wrong and how to improve the overall efficiency and get rid of the nasty lag?

Sorry about the lengthy post but this is my first one :slight_smile:
Any ideas are appreciated. However, since I’m not a programmer, I’d kindly ask if you can provide examples along with the explanation.

Thanks a lot in advance :slight_smile:

You sketch seems to be fine, however you have two resources duplicated (that could be a problem) and also the dallas reading could be improved a bit to avoid requesting all connected sensors (assuming you have just one sensor). Also, the thing type should be ThingerESP8266 if you are using such device, so you can get SSL connections by default. Something like:

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

#define ONE_WIRE_BUS D4 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define USERNAME "xxxxxxx"
#define DEVICE_ID "xxxxxxx"
#define DEVICE_CREDENTIAL "xxxxxxxx"

#define SSID "xxxxxxx"
#define SSID_PASSWORD "xxxxxxxx"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  pinMode(D3, OUTPUT);
  pinMode(D2, OUTPUT);
  digitalWrite(D2, HIGH);

  thing.add_wifi(SSID, SSID_PASSWORD);

  thing["DoorUnlock"] << digitalPin(D3);
  thing["Lights"] << digitalPin(D2);
  thing["Temperature"] >> [](pson & out) {
	sensors.requestTemperaturesByIndex(0);
	out = sensors.getTempCByIndex(0);
  };

  sensors.begin();
}
  
void loop() {
  thing.handle();
}

Thank you so much for the quick response.
I’ve ended up changing just thing [“Temperature”] part in my sketch and it worked like a charm.

Now, the other thing I’m struggling to make happen is to display temperature on the dashboard without decimals. Currently, if I’m using Text/Value widget the value has four decimals and if I’m using Dount/Chart (just for aesthetics) it displays two decimals. Ideally, I would like it without decimals but I understand that this is nitpicking and I don’t mind leaving it as-is for now. Then again, I believe that there must be some kind of easy solution for this but I just can’t seem to find it.

Once again, thank you for the great support and great platform.

Cheers!

If you want the temperature without decimals, you can just cast the data type of the temperature to an integer, that it will be also more efficient over the wire.

Something like:

thing["Temperature"] >> [](pson & out) {
	sensors.requestTemperaturesByIndex(0);
	out = (int) sensors.getTempCByIndex(0);
};

Bests!

Works like a charm :slight_smile:

Thanks a million!

Hola Alvaro!
una pregunta quiero probar este codigo en un arduino uno wifi oficial, tendria que cambiar algo del codigo para que fuera compatible con la uno wifi shield ?

gracias Alvaro
un saludo !