Introduction: ESP8266 - WIFI Temperature Data Logger - MCP9808

In this project we will build a simple, high precision, WIFI Temperature Data logger based on ESP8266 module and the MCP9808 chip, a ±0.5°C Maximum Accuracy Digital Temperature Sensor.


• Accuracy:
- ±0.25 (typical) from -40°C to +125°C

- ±0.5°C (maximum) from -20°C to 100°C

- ±1°C (maximum) from -40°C to +125°C

• User-Selectable Measurement Resolution:

- +0.5°C, +0.25°C, +0.125°C, +0.0625°C

• User-Programmable Temperature Limits:

- Temperature Window Limit

- Critical Temperature Limit

• User-Programmable Temperature Alert Output

• Operating Voltage Range: 2.7V to 5.5V

• Operating Current: 200 μA (typical)

• Shutdown Current: 0.1 μA (typical)

• 2-wire Interface: I2C/SMBus Compatible

• Available Packages: 2x3 DFN-8, MSOP-8

For more details, please see MCP9008 Datasheet

Step 1: MCP9808 - a Tiny MSOP 8 Package But Great Features

Don't be scared by the MSOP-8 package, with a small MSOP to DIP adapter will fit great in our CBDB extension slots or any other DevBoard with a compatible DIP slot.

Step 2: What We Will Need

What we will need:

  • CBDB Board or any other ESP8266 module
  • USB adapter (take a look on Part 1 for details how to connect them together)
  • MCP9808 Module from above
  • For programming we will use NodeMCU, a LUA language interpreter implementation for ESP8266 and for uploading the driver and the software we will use the LuaUploader.

Step 3: Driver Implementation

General considerations:

This is for Tambient > 0°C only. Take a look in the MCP9008 Datasheet .
If you need it also on the negative temperature scale then you need to do some extra transformations as the temperature data is stored in the 16-bit read-only Ambient Temperature register Ta as 13-bit data in two’s complement format.

As MCP9808 has a I2C compatible compatible interface, building a driver for it in NodeMCU it's a pretty straigh forward process:

1. Init I2C bus/interface

          dev_addr = 0x1F,
init = function (self, sda, scl) = 0 i2c.setup(, sda, scl, i2c.SLOW) end

2. Read / Write to/from the desired register location

  • READ Register function

read_reg = function(self, dev_addr, reg_addr)
i2c.start( i2c.address(, dev_addr ,i2c.TRANSMITTER) i2c.write(,reg_addr) i2c.stop( i2c.start( i2c.address(, dev_addr,i2c.RECEIVER),2) i2c.stop( return c end
  • READ Temperature function

readTemp = function(self)
h, l = string.byte(self:read_reg(0x1F, 0x05), 1, 2),0x1F) --check if Ta > 0C or Ta<0C Sgn =,0x10) -- transform - CLEAR Sing BIT if Ta < 0C h2 =,0x0F) tp = h2*16+l/16 --END calculate temperature for Ta > 0 return tp end

Step 4: Web Server - First RUN

Save the code on ESP as 'web_temp.lua', restart ESP and run:

=wifi.sta.getip() -- find the IP Address where your Web Server will be dofile("web_temp.lua") -- Start the Web Server

It is supposed that you have done already your ESP8266 module WIFI setup. If not, forst run:

      -- One time ESP Setup --
wifi.setmode(wifi.STATION) wifi.sta.config ( "WIFI_SSID" , "PASSWORD" ) print(wifi.sta.getip())

Open your favorite Web browser and type your new Web Server IP address. If all OK, should look something like in the attached image.

Step 5: Automatically Start Program and WebServer at Power On/reboot

If you want the Web Server to start automatically when your CBDB module starts or reboots, then you neet to create and add some lines in your 'init.lua' file:                     -- for debug only, you can skip it
wifi.sta.getmac()             -- for debug only, you can skip it        
wifi.sta.getip()              -- for debug only, you can skip itnode.heap()
dofile("web_temp.lua")        -- needed to start Web Server and  Temperature logger

Save the code on ESP as 'init.lua', restart ESP. It should reboot and restart the program and reinitialize the Web Server.

Step 6: Conclusion

With integrated WIFI Modules life can become easier than ever imagined only few years ago. To have a nice temperature WIFI sensor with Web interface, accessible thru Internet from any Worldwide location, for less than 10 USD, that was something more that a dream.

For more details and more exciting ESP8266 Projects please visit :

Step 7: First RUN

For testing, just save the code on ESP as 'mcp9808.lua', restart ESP and run:

require('mcp9808')                         --call for new created MCP9808 Module Driver
sda=2 --GPIO4 -- declare your I2C interface PIN's scl=1 --GPIO5 mcp9808:init(sda, scl) -- initialize I2C tp1 = mcp9808:readTemp() -- read temperature =print(tp1) -- print it

Step 8: Web Server Interface

If all ok, we can move to the next step, how to make the data available on the web.

For this one we will need to build our Web Server functions:

  • WEB Server
srv:listen(80, function(conn) conn:on("receive",function(conn,payload) print(payload) conn:send("HTTP/1.1 200 OK\n\n")
        conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
        conn:send("<html><title>MCP9808 - Temperature Log Server - ESP8266</title><body>")
        conn:send("<h1>ESP8266 Temperature Log Server - MCP9808</h1><BR>")           
        conn:send("Temperature   : <b>" .. tp1 .. "°C</b><BR><BR>")           
        conn:send("Node.HEAP : <b>" .. node.heap() .. "</b><BR><BR>")           
        conn:send("IP ADDR    : <b>".. wifi.sta.getip() .. "</b><BR>")           
        conn:send("Node MAC   : <b>" .. wifi.sta.getmac() .. "</b><br>")           
        conn:send("TMR.NOW    : <b>" .. .. "</b><BR<BR><BR>")           
        conn:on("sent",function(conn) conn:close() end)
end) end)
  • READ Temperature function
function readTMP()
mcp9808:init(sda, scl) tp1 = mcp9808:readTemp() print(tp1) end

  • TIMER - to establish how often we want the reading process to be done.
    -- read data every 1sec for direct web reading
tmr.alarm(0, 1000, 1, function() readTMP() end )