ESP8266 + ds18b20 + thingspeak+ nodemcu

Update: 2016-02-06.

New greatly updated tutorial in new post with NodeMCU dev board and ArduinoIDE. A lot simpler approach.

Update: 2015-04-11.

Update with deep sleep now in new post. Testing the battery operation time in progress.

Update: 2015-03-18.

Updated the connection drawing with updated Fritzing part of ds18b20 with correct pin order also in the breadboard view. In the schematics the part was wired properly all the time. Note: don’t trust computers.

Simple temperature logger for your wifi surroundings is actually quite simple.

  • ESP8266, wifi-enabled microcontroller board with gpio
  • DS18B20, maxim 1-wire temperature sensor
  • 4k7 ohm resistor, some wire
  • 3v power source, for short term testing 2*1,5V AA batteries

Nodemcu firmware for ESP8266 is quite easy to work with. There is a module to interact with 1-wire temperature sensor readily available.

To set things up you also need a TTL converter, for example USB To RS232 TTL PL2303HX Auto Converter Module.

What you first need to do is connect ESP8266 to the ttl converter, 3.3v to VCC, GND to GND, RX to TX, TX to RX. And to enable the esp8266 you also need to connect CH_PD to VCC. After this you should be in a state where you can communicate with esp8266 using a serial console program and AT commands. After successful start the first thing is to get NodeMCU running on the ESP8266.

Intro to AT command phase

To flash new firmware to ESP8266 you need to boot the ESP8266 in upload mode and write new firmware, NodeMCU. Just connect GPIO0 to GND for upload mode and start flashing. More on flashing.

After flashing rebooting the ESP8266 without GPIO0 connecter should bring up some version strings to the serial console stating NodeMCU. This is good news as then all needed to do is to wire up the ds18b20 and upload init.lua and dallas.lua scripts. You also need to upload lua 1-wire -module (ds18b20.lua) to ESP8266.


Updated connection drawing from with updated – pin order fixed – ds18b20

Good tool to deal with ESP8266 and lua-scripts is ESPlorer. It lets you run scripts, upload files, run snippets and do bunch of other useful things with your ESP8266.

esp_ds_schemUpload to ESP8266

Before uploading init.lua set up your wireless network credentials.



And before uploading dallas.lua set up your API-key for



port = 80

-- ESP-01 GPIO Mapping
gpio0 = 3


function sendData()
print("Temp:"..t1.." C\n")

-- conection to
print("Sending data to")
conn=net.createConnection(net.TCP, 0) 
conn:on("receive", function(conn, payload) print(payload) end)
conn:send("GET /update?key=Q67WTIRSOWGXJEO&field1="..t1.." HTTP/1.1\r\n") 
conn:send("Accept: */*\r\n") 
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
                      print("Closing connection")
conn:on("disconnection", function(conn)
          print("Got disconnection...")

-- send data every X ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )

Run and enjoy.

The lua-scripts are in github.

You may also like...

40 Responses

  1. Chris says:

    When I try this Lua reports:

    ds18b20-B.lua:1: module ‘ds18b20’ not found:
    no field package.preload[‘ds18b20’]
    no file ‘ds18b20.lua’
    no file ‘’

    I’d appreciate some guidance as to how to fix it.

  2. Sam says:

    Hi, I would like to first thank you about sharing this nice topic.
    Do you have any idea regarding hte battery life. ?


    • kengu says:

      The code as-is is not optimized in any way to preserve energy. Running it with a powerbank containing 2*2800mAh 3.7v lithium cells it did run for 5 days. With those AA-batteries it depends on the batteries but I would say something along the same lines. I never did prolonged testing with that code and AA-batteries.

  3. hanza says:

    code does notwork with negative temperatures below 0

    • kengu says:

      Might be true. Feel free to fix it and publish the outcome. The example code is used for indoor temperature logging so no testing with negative temperatures.

  4. Pee says:

    Pystyykö tuohon kytkemään useamman ds18b20 anturin?

  5. Luminaric says:

    Nice write-up. But I have a problem. When I run the above Lua program I am getting “attempt to index global ‘ds18b20’ (a nill value). Any idea why?

  6. Blaku says:

    If I’m not mistaken the DS18B20 is drawn as a mirror image of the actual package in both images. The pin names are correct though; the resistor is and should be a pullup. Note that it can also be run in parasitic mode by tying VDD to GND.

  7. Mikko says:

    Really nice article!

    I’m getting “Got disconnection…” every time it tries to connect to thingspeak.
    It is connected to my network and gets a ip but for some reason it does not upload any data.
    I have set up my api keys properly.

    Do you have any ideas what I could try?

    ———– serial log
    > Temp:26 C

    Sending data to
    Closing connection
    Got disconnection…

    • JR01 says:

      Hi, I have the same problem, it seams that the ds18b20 is read properly, if I hold the sensor, the temp reported goes up, but, as yours, it does not save to thingspeak. I tested with same API key in a browser from my computer, and that works. So I assume that the ESP-03 is not getting to send over internet to thingspeak.
      =====dallas.lua reports the following: ======
      Temp:33.5 C

      Sending data to
      Got disconnection…
      =====whilst the wifi.sta.getip() reports, thus I assume all okay with wifi settings, and I can ping it=====
      > =wifi.sta.getip()
      Does anybody have an idea on where I must look?
      I also tried another s1820.lua solution on git, and have the same issue, thus suspecting esp-03 not getting onto internet ???

  8. Frank says:

    In your diagram the DS18B20 is drawn the wrong way round!! My DS18B20 got really hot when i connected it like in your diagram.

    Please correct the image!

    • kengu says:

      The schematics was correct all the time. The part was just errorneous in the parts library of Fritzing and thus produced a faulty image with pin orders. Now fixed. Never trust computers.

  9. Chris says:

    Hey thanks for creating this guide! I followed your instructions and it works great. Like some have already mentioned the picture shows the DS18B20 pins reversed but that was easy to correct. Now that I have the temperature posting to thingspeak I was wondering if you know how to modify the code to display F instead of C. Again thanks for sharing your work!

    • Jason says:

      I used this little function to do that in my dht11 module.

      local function toFahrenheit(c)
      return c * 9 / 5 + 32

      just apply the math and bam!!! Fahrenheit 🙂

    • kengu says:

      Now fixed the image with a fixed part for Fritzing with right pin-order in the fancy looking breadboard picture also.

  10. George says:

    Just a question;
    You are doing this in your code:
    print(“Temp:”..t1..” C\n”)

    You read the temperature twice,
    is it because that you want to avoid the reset value that is 85°C?

    • kengu says:

      Now that you say that.. I have no idea why it is read twice. But maybe the reason is as you stated to skip the 85C. Or could it be just a typo on cleaning up the code and thus multiplication of a line there.

      • lee says:

        Nice write-up thanks. I got 85C result before it was called twice – then I got 20C correctly, so it appears it is needed.

  11. Pawel says:

    Why there is resistor between DQ and VDD ?

    • croscwa says:

      The resistor is a pull-up resistor for the data line. This insures that a reliable stream of 1’s and 0’s come out of the sensor.

      There are several examples on the Internet where there is no resistor. The data stream relies on a ‘parasitic’ voltage for the data stream, which usually works, but not always.

  12. Frank says:

    Can you explain the code:

    print(“Closing connection”)

    Is this checking to make sure the send has worked?

  13. Mike says:

    I suspect the 85C / 20C 2 reads is because of not enough delay after issuing a convert cmd to the ds18b20, if the DS18b20 is operating in parasite mode [ no +5Volts] then need to delay 750Ms+
    I’m not sure how much delay is needed if using non parasite- see data sheet

  14. Charles says:

    Have all 3 LUA files on the device…

    Am using the “fixed for negative values” of ds18b20.lua from here ( as Im wanting to use this as a freezer temp monitor.

    Running the files on the ESP8266 using ESplorer, I get the following:

    error loading module ‘ds18b20’ from file ‘ds18b20.lua’:
    ds18b20.lua:11: ‘=’ expected near ‘local’

    Line 11 is

    “local modname = …”

    direct from the original file.

    Is “…” correct, or am I supposed to change this to something?

    FWIW, this is running direct from the programmer usb doodah – I dont have the resister / temp chip on the board yet… just wanted to see if the script ran ok before I take it out of scripting mode for the final build.

    Thanks 🙂

  15. Alfredo says:

    Hi, thanks for your help in writing this code.
    I am trying to implement this example with en ESP8266 dev board, and I’m getting this message :

    Config done, IP is
    PANIC: unprotected error in call to Lua API (error loading module ‘ds18b20’ from file ‘ds18b20.lua’:
    ds18b20.lua:114: malformed number near ‘1.8’)

    Any idea on what’s going wrong ? Thanks in advance for your support !

  16. Alex says:

    Hi, I’m trying to use same code, but when execute ” t.setup(gpio0) ” i see:
    attempt to index global ‘t’ (a boolean value)
    I think my ” t = require(“ds18b20″) ” doesnt work, but ds18b20.lua and are exist…

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.