LM75 Temperature Sensor Library for NodeMCU

In all my excitement about the ESP8266 modules, I almost forgot that I had some boards made to create nice standalone temperature sensors with the ESP8266 using LM75 i2c temperature sensors.

Before my boards arrived, I had already made the central heating temperature logger and forgot about them. Today I soldered one of these boards as a test, the result is in the picture to the right. The board is mounted on a simple breakout that I made for my ESP8266 experiments. The sensor itself is not visible, as it is on the bottom side of the black PCB, so that it can easily be mounted to a surface and make proper thermal contact.

Compared to the DS1820 one-wire sensors, the temperature resolution is not that great, only 0.5 °C resolution, but interfacing these is a lot easier. To make interfacing even easier, I created a small Lua library to read an LM75 sensor from NodeMCU, it’s available in the public domain on Github.

Usage is easy:

1
2
3
4
5
require('lm75')

-- GPIO indexes of i2c signals
sda, scl = 4, 3
lm75:init(sda, scl)

The temperatures can then be read as either a sting repreenting a floating point value or scaled to an integer (temperature in °C is value/10.)

For example:

1
2
3
4
> =lm75:strTemp()
22.5
> =lm75:intTemp()
225

Using this library, a sensor node that emits temperatures in mrtg external script format to a TCP client can fit in this tiny snippet of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require('lm75')

sda, scl = 4, 3
port = 27315

lm75:init(sda, scl)

srv=net.createServer(net.TCP)
srv:listen(port,
        function(conn)
                local temp
                temp = lm75:intTemp()
                conn:send(
                        temp.."\n"
                        ..temp.."\n"
                        ..tmr.now().." ticks\n"
                        .."node-"..node.chipid())
                conn:on("sent",
                        function(conn) conn:close() end
                )
        end
)

See my previous post about how to easily integrate this into mrtg using netcat.

Enjoy!

Comments