ESP8266 and DS18B20 temperature with deep-sleep

This project is not done with the extra small and commonly available ESP01 version. Instead a ESP12 version is used. The ESP01 does not expose the GPIO16 needed for a proper deepsleep. However the chip still has the GPIO pin and if you have some good soldering skills, should be able to deepsleep the ESP01
For this development I used the NodeMCU custom build website to create the build I required. The modules used are the following: file, gpio, http, i2c, net, node, ow, sjson, tmr, uart
and wifi
. NodeMCU is a firmware which allows you to run Lua scripts. The specific build used has the function node.dsleep
which can be used to sleep up to ~71 minutes.
The node.dsleep
function requires the GPIO16 pin to be connected to the RST pin. This is because the internal clock is used to wake up the module. The internal clock controls GPIO16 and sends a short signal. When connected to the RST pin, the module will become active on the falling edge of the signal. This means that it’s not required to use the internal clock to wake the module. It is possible to connect an external source to the RST pin which gives a falling edge on the required moment. Some ideas are an other external clock with a longer timer, a PIR sensor, a button, etc. Deepsleep is explained nicely on the following site
Now connecting the DS18B20 temperature sensor. The diagram i’ve used is simple. It requires a 4.7K resistor between the 3.3V power and the wire of the sensor. The sensor itself is connected to GPIO1.
The next problem is the time. The module itself doesn’t know the current time, it only knowns how long its running in microseconds. There are multiple solutions. First of all it is possible to install a RTC module in the NodeMCU firmware. However I decided to do something less accurate, but which perfectly fine for the precision i require my temperature to be recorded. The picked solution is to do a HEAD
request to my database (CouchDB), which responds with the current time in its headers. Thus relying on the server having the correct time this should be rather correct. Next I take the measurement and send the data directly to the database, including the previously retrieved timestamp.
A different approach which I tried is to set CouchDB to use the current time in its generated ID’s. In the configuration I added uuids:algorithm:utc_random
.The next step is to get a set of id’s of size 1, which has the time in microsecond precision in it. However the ESP8266 couldn’t handle the microsecond precision and returned -1
. I’ve used someNumber = tonumber(someHexString, 16)
in combination with the 14hex precision timestamp retrieved from the database. Another solution if the exact time isn’t required is to send only the temperature data and sensor id to a backend which enriches the data with the timestamp received.
The code of my first attempt is available. The code of my second attempt is not yet available.
esp8266 ds18b20 hardware