I have set up my ESP to do the following:
1. read temperature from ds18b20
2. if the temperature is different from last reading connect to wifi and transmit temperature to server
3. disconnect from wifi and wait 30 min.
All of this is working, but i noticed that it uses 69-70mA all the time. Today i tried to add:
wifi.sleeptype(wifi.MODEM_SLEEP)
Complete init.lua:
--init.lua
require('ds18b20')
port = 80
gpio0 = 3
key = 'livingroom'
temp = 0
ds18b20.setup(gpio0)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
realtype = wifi.sleeptype(wifi.MODEM_SLEEP)
print("Sleepmode "..realtype)
function readTemp()
local current_temp = ds18b20.read()
current_temp = ds18b20.read()
current_temp = string.format("%8.1f", current_temp)
print("Read temp " ..current_temp)
if current_temp ~= temp then
sendData(current_temp)
temp = current_temp
end
end
function sendData(up_temp)
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
--print("IP unavaiable, Waiting...")
else
tmr.stop(1)
-- Send value to server.
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80,'00.00.00.00')
conn:send("GET /temptest.php?key="..key.."&field1="..up_temp.." HTTP/1.1\r\n")
conn:send("Host: example.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
conn:close()
end)
conn:on("disconnection", function(conn)
-- Disconnect from network and save energy.
wifi.sta.disconnect()
end)
end
end)
end
-- run at startup.
readTemp()
-- send data every X ms, 1800000 == 30 minutes
tmr.alarm(0, 1800000, 1, function() readTemp() end )
But i don't see any change in power consumption. My code is also here: https://github.com/andeersg/wifi-temperature-logger
Am i doing something wrong? Or are there some other tricks i can use to reduce idle power consumption?