ADT7410 I2C temp sensor works only after a power cycle (ESP8266)

esp8266i2csensor

I have an ADT7410 temperature sensor connected to an ESP8266 running nodemcu.

The issue I am having is that I can only get good changing data after power cycling the ADT7410. Until that I get either an reasonable fixed temp reading which doesn't change even if I heat/cool down the sensor or 0x0000 (a disconnected sensor results in 0xFFFF). I tried resetting the IC over I2C but it doesn't change anything.

The system consists of a mainboard/daughterboard. This schematic shows the connections between the boards. Unrelated things on the mainboard have been removed.
enter image description here

How could I solve this so that the device starts working after I plug in power without adding a fet to power cycle the daughter board?

NodeMCU lua code:

---------------------
-- Wifi connection --
---------------------

function is_connected()
    print(wifi.sta.status())
    return wifi.sta.status() == 5
end

function block_connected()
    while not is_connected() do
        print("Waiting for connection")
        tmr.delay(1000000)
    end
    print("WIFI connected!")
end

------------------
-- I2C & Sensor --
------------------

I2C_ID = 0
I2C_PIN_SDA = 2
I2C_PIN_SCL = 1

I2C_ADDRESS = 0x48
I2C_TEMP_REG_MSB = 0x00
I2C_TEMP_REG_LSB = 0x01
I2C_STATUS_REG = 0x02
I2C_CONF_REG = 0x03

function adt_reset()
    i2c.start(I2C_ID)
    i2c.address(I2C_ID, 0x00, i2c.TRANSMITTER)
    i2c.write(I2C_ID, 0x06)    
    i2c.stop(I2C_ID)
end

function adt_setup()    
    i2c.start(I2C_ID)
    i2c.address(I2C_ID, I2C_ADDRESS, i2c.TRANSMITTER)
    i2c.write(I2C_ID, I2C_CONF_REG)
    i2c.write(I2C_ID, 127)
    i2c.stop(I2C_ID)
end

function read_reg(id, dev_addr, reg_addr)
  i2c.start(id)
  i2c.address(id, dev_addr ,i2c.TRANSMITTER)
  i2c.write(id,reg_addr)
  i2c.start(id)
  i2c.address(id, dev_addr,i2c.RECEIVER)
  c=i2c.read(id,1)
  i2c.stop(id)
  return string.byte(c)
end

function get_temp()
    msb = read_reg(I2C_ID, I2C_ADDRESS, I2C_TEMP_REG_MSB)
    print(msb)
    lsb = read_reg(I2C_ID, I2C_ADDRESS, I2C_TEMP_REG_LSB)
    print(lsb)

    val = bit.lshift(msb, 8) + lsb
    if bit.isset(msb, 7) then
        return (val - 65536)/128
    else
        return val/128
    end
end

---------------------
-- MQTT Connection --
---------------------

MQTT_HOST = "rabbitmq-sysd.containers.ikioma"
MQTT_PORT = 1883

MQTT_CLIENTID = "temp-" .. node.chipid()
MQTT_KEEPALIVE = 120
MQTT_USER = "guest"
MQTT_PASS = "guest"

MQTT_QOS = 1
MQTT_RETAIN = 0


mqtt_connected = false

m = mqtt.Client(MQTT_CLIENTID, MQTT_KEEPALIVE, MQTT_USER, MQTT_PASS)

function mqtt_connected()
    print("MQTT connected")
    mqtt_connected = true
    m:publish("/connections", MQTT_CLIENTID, MQTT_QOS, MQTT_RETAIN)
end

function mqtt_disconnected()
    print("MQTT disconnected")
    mqtt_connected = false
end

function mqtt_get_connected()
    return mqtt_connected
end

m:lwt("/lwt", MQTT_CLIENTID, MQTT_QOS, MQTT_RETAIN)
m:on("connect", function(con) print ("connected?") end)
m:on("offline", mqtt_disconnected)

mqtt_ip = ""

function mqtt_connect()
    m:connect(mqtt_ip, MQTT_PORT, 0, 0, mqtt_connected)
end

function mqtt_connect_with_ip(ip)
    mqtt_ip = ip
    mqtt_connect()
end

function main_loop()
    if mqtt_get_connected() then
        m:publish("/" .. MQTT_CLIENTID, get_temp(), MQTT_QOS, MQTT_RETAIN, function(conn) print("sent") end)
        if to_advertisement == 0 then
            m:publish("/devices", MQTT_CLIENTID, MQTT_QOS, MQTT_RETAIN)
            to_advertisement = ADVERTISE_INTERVAL
        else
            to_advertisement = to_advertisement - 1
        end
    else
        mqtt_connect()
    end
end

----------
-- MAIN --
----------

block_connected()
print(wifi.sta.getmac())
print(wifi.sta.getip())


i2c.setup(I2C_ID, I2C_PIN_SDA, I2C_PIN_SCL, i2c.SLOW)
adt_reset()
adt_setup()

sk = net.createConnection(net.TCP, 0)
sk:dns(MQTT_HOST, function(conn, ip) mqtt_connect_with_ip(ip) end)
sk = nil

ADVERTISE_INTERVAL = 10
to_advertisement = 0


-- run "loop" every 10s
tmr.alarm(0, 5000, 1, main_loop)

Best Answer

I'd suggest to try with another fw, just to check if it is not NodeMCU problem. Like this: https://github.com/cesanta/smart.js

It has sample for another temp sensor (https://github.com/cesanta/smart.js/blob/master/platforms/esp8266/fs/MCP9808.js), I tried - and it works for me.

Also, with smart.js you can try C-code (not JS and not Lua).

Related Topic