-->
Page 1 of 2

DeepSleep milage?

PostPosted: Sat Dec 13, 2014 2:25 pm
by sancho
Hello, all.

I made a short script for measuring DS18S20 temperature, sending it to TCP port 8888 of my laptop and entering deep sleep for 30 seconds.
Everything is fine, except...
I pluged the device to 3.4V power source throuch 2.5ohm resistor (4x10ohm in parallel, smallest I had at home).
I am measuring voltage on such resistors.
I have just a crappy multimeter (costed $17 2 years ago).
The smallest range I can measure is 200mV.
When running the device, the voltage is from 80mV to 170mV - which translates to 32 to 70mA.
However, when entering the deep sleep, the voltage is funny - sometimes it is just 0.1mV - translating to 40uA - just great.
But, sometimes, the voltage during deep sleep stays at 1.6mV - that means 640uA - 16 times the original value.
And sometimes, with no apparent reason, the voltage stays as 2.6mV - 1.04mA.

Of course, my code includes setting the 1-wire line to high impedance input, instead of output, to avoid powering the DS sensor during sleep.

It runs a cycle as described, last 5 measurements are:
1.7mV
1.7mV
3.7mV
0.1mV
2.1mV
I cannot explain the results.

Can anyone else share their experience?

Re: DeepSleep milage?

PostPosted: Sun Dec 14, 2014 6:05 am
by sancho
Btw, for anyone interested, the code is here:
Code: Select allpin = 9
ow.setup(pin)

counter=0
lasttemp=-999
sensor=""

function bxor(a,b)
   local r = 0
   for i = 0, 31 do
      if ( a % 2 + b % 2 == 1 ) then r = r + 2^i end
      a = a / 2
      b = b / 2
   end
   return r
end

function getTemp()
      addr = ow.reset_search(pin)
      repeat
          if (addr ~= nil) then
            crc = ow.crc8(string.sub(addr,1,7))
            if (crc == addr:byte(8)) then
              if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
               sensor = ""
               for j = 1,7 do sensor = sensor .. string.format("%02x", addr:byte(j)) end
                    ow.reset(pin)
                    ow.select(pin, addr)
                    ow.write(pin, 0x44, 1)
                    tmr.delay(1000*1000)
                    present = ow.reset(pin)
                    ow.select(pin, addr)
                    ow.write(pin,0xBE, 1)
                    data = nil
                    data = string.char(ow.read(pin))
                    for i = 1, 8 do
                      data = data .. string.char(ow.read(pin))
                    end
                    crc = ow.crc8(string.sub(data,1,8))
                    if (crc == data:byte(9)) then
                       t = (data:byte(1) + data:byte(2) * 256)
                       if (t > 32768) then
                         t = (bxor(t, 0xffff)) + 1
                         t = (-1) * t
                       end
                       t = t * 625
                       lasttemp = t
                    end                   
              end
            end
          end
          addr = ow.search(pin)
      until(addr == nil)
end

function sendData()
    srv=net.createConnection(net.TCP, 0)
    srv:connect(8888, "192.168.11.1")
    getTemp()
    t1 = lasttemp / 10000
    t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
    srv:send(wifi.sta.getmac() .. "\t" .. sensor .. "\t" .. t1 .. "." .. string.format("%04d", t2) .. "\n")
    print(wifi.sta.getmac() .. "\t" .. sensor .. "\t" .. t1 .. "." .. string.format("%04d", t2) .. "\n")
    srv:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
    srv:on("disconnection", function(conn)
                                print("Got disconnection, shutdown")
                                powerOff()
                            end
    )
end

function checkIP()
    if wifi.sta.getip() ~= nil then
        tmr.stop(0)
        tmr.wdclr()
        print("Got IP, transmitting data")
        print(wifi.sta.getip())
        sendData()
    end
end

function powerOff()
   print("Bye!")
        gpio.mode(pin, gpio.INPUT) --disable output to save power for 1wire
        node.dsleep(30*1000*1000)
end

tmr.alarm(0, 500, 1, checkIP)

Re: DeepSleep milage?

PostPosted: Mon Dec 15, 2014 12:25 pm
by Warthog
Maybe this line:

tmr.alarm(0, 500, 1, checkIP)

wakes up from deep.sleep?

Re: DeepSleep milage?

PostPosted: Mon Dec 15, 2014 12:40 pm
by sancho
Warthog wrote:Maybe this line:

tmr.alarm(0, 500, 1, checkIP)

wakes up from deep.sleep?

No.
That's the initial line to wait for connection to wifi.
The line with the code
Code: Select allnode.dsleep(30*1000*1000)

Causes the sleep. The wakeup is done by reset - according the documentation and my experience, too.