Left here for archival purposes.

User avatar
By Andeersg
#12265 Hi,

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:
Code: Select allwifi.sleeptype(wifi.MODEM_SLEEP)


Complete init.lua:
Code: Select all--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?
User avatar
By laurynas
#12302 deepsleep.
you need to connect a couple of pins for that, but it will then use 1000000x less energy :)

my dht22 sensor works on battery. it takes <2s to measure, connect and post to thingspeak. after that it goes to sleep for 10 minutes and resets after it.
User avatar
By aldepas
#12468 Hello, I saw that in your code using the wifi.MODEM_SLEEP mode, which is already used by default. It appears that the best mode wifi.LIGHT_SLEEP results.

http://bbs.espressif.com/viewtopic.php?f=6&t=133&p=485&hilit=sleep+modem#p485

http://bbs.espressif.com/viewtopic.php?f=7&t=171&p=617

http://www.esp8266.com/viewtopic.php?f=9&t=1719