So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By rob0to
#79615 Hi,
I have a problem with the reading value from esp8266 using the deep-sleep function. Problem is that real value after waking up is still returning more than maximum value (standard maximum value = 1024, but in described scenario is sensor returning the value around 65000 . I tried several sensors with the same result. In case that I disable Deep Sleep mode, everything is working fine.

Code snippet for measurement is following:
Code: Select all--------------------
--DO MOISTURE MEASSURE AS PERCENTAGE
--------------------
getMoistureLevel = function()
    adc.force_init_mode(adc.INIT_ADC)
    local moisture_sensor_val = tonumber(adc.read(0))
    local moisture_multiply =  moisture_sensor_val * 100
    local moisture_percentage =  moisture_multiply / MOISTURE_FULL
    --print("moisture_sensor_val:", moisture_sensor_val)
    --print("moisture_multiply", moisture_multiply)
    print("-------------------------")
    print("moisture sensor level:", moisture_sensor_val)
    print("moisture percentage level:", moisture_percentage)
    print("-------------------------")
    return moisture_percentage
end


Connection Scheme is the following:

Thanks for any help.

Image


Whole script:

Code: Select all---------------------------
-- COMMON METHODS
---------------------------
function startup()
    print('startup')
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
    else
        file.close("init.lua")
        initall()
        -- the actual application is stored in 'application.lua'
        -- dofile("application.lua")
    end
end

--------------------
--GET MEASUREMENT CYCLE COUNT
--------------------
getMeasurementCycleCount = function()
    local cycleCount = getRtcMemoryValue(KEY_SLOT_MEASUREMENT_CYCLE_COUNT)
    print("getMeasurementCycleCount: "..cycleCount)
    return cycleCount;
end

--------------------
--WRITE VALUE TO RTC MEMORY
--------------------
setRtcMemoryValue = function(slot, value)
    print("setRtcMemoryValue: "..value)
    rtcmem.write32(slot, value)
end

--------------------
--READ VALUE TO RTC MEMORY
--------------------
getRtcMemoryValue = function(slot)
    print("getRtcMemoryValue from slot:"..slot)
    local memVal = rtcmem.read32(slot)
    if (memVal == nil or memVal < 0) then
        memVal = 0
    end
    print("Value is :"..memVal)
    return memVal
end

--------------------
--DO MOISTURE MEASSURE AS PERCENTAGE
--------------------
getMoistureLevel = function()
    adc.force_init_mode(adc.INIT_ADC)
    local moisture_sensor_val = tonumber(adc.read(0))
    local moisture_multiply =  moisture_sensor_val * 100
    local moisture_percentage =  moisture_multiply / MOISTURE_FULL
    --print("moisture_sensor_val:", moisture_sensor_val)
    --print("moisture_multiply", moisture_multiply)
    print("-------------------------")
    print("moisture sensor level:", moisture_sensor_val)
    print("moisture percentage level:", moisture_percentage)
    print("-------------------------")
    return moisture_percentage
end

--------------------
--DO VOLTAGE MEASSURE
--------------------
getVoltage = function()
    adc.force_init_mode(adc.INIT_VDD33)
    local voltage = adc.readvdd33()
    --local voltage = adc.readvdd33(0)
    --local voltage = adc.read(0)
    print("System voltage s(mV):", voltage)
    return voltage
end

--------------------
--DO VOLTAGE MEASSURE AS PERCENTAGE
--------------------
getVoltagePercentage = function()
    local actualVoltage = getVoltage()
    local battery_remaining =  BATTERY_VOLTAGE_FULL - actualVoltage
    local battery_empty_full_diff =  BATTERY_VOLTAGE_FULL - BATTERY_VOLTAGE_EMPTY
    local battery_percentage = (battery_remaining / battery_empty_full_diff) * 100
    print("Battery percentage:", battery_percentage)
    return battery_percentage
end

--------------------
--GET DEEP SLEEP INTERVAL IN SECONDS
--------------------
getDeepSleepInterval = function()
    local deepSleepInterval = DEEP_SLEEEP / 1000000
    return deepSleepInterval
end

--------------------
--DO POST REQUEST
--------------------
doRequest = function(requestType)
    print("Do request type: "..string.format(requestType))
    --------------------
    --LOW BATTERY
    --------------------
    if (requestType == REQUEST_REPORT_ALARM_BATTERY) then
        print("BATTERY TRESHOLD EXCEEDED " .. string.format(getVoltagePercentage()))
        http.post(REPORTING_API_URL,
            DATA_KEY_CONTENT_TYPE,
            '{"'..DATA_KEY_BATTERY_LEVEL..'":"'..getVoltagePercentage()..'", "'..DATA_KEY_UUID..'":"'.. DEVICE_UUID ..'"}',
            function(code, data)
                if (code < 0) then
                    print("HTTP request failed")
                else
                    print(code, data)
                end
                goToDeepSleep()
            end)
    end
    --------------------
    --MOISTURE ALERT
    --------------------
    if (requestType == REQUEST_REPORT_ALARM_WATER) then
        print("MOISTURE TRESHOLD EXCEEDED " .. string.format(getMoistureLevel()))
        http.post(MEASUREMENT_API_URL,
            DATA_KEY_CONTENT_TYPE,
            '{"'..DATA_KEY_MOISTURE_LEVEL..'":"'..getMoistureLevel()..'", "'..DATA_KEY_UUID..'":"'.. DEVICE_UUID ..'"}',
            function(code, data)
                if (code < 0) then
                    print("HTTP request failed")
                    doRequest(requestType) --TODO: IN CASE OF REQUEST ERROR, SEND MESSAGE VIA GSM OR BT
                else
                    print(code, data)
                end
            end)
    end
    --------------------
    --HEALTH CHECK
    --------------------
    if (requestType == REQUEST_REPORT_HEALTHCHECK) then
        print("REPORT_HEALTHCHECK ")
        http.post(REPORTING_API_URL,
            DATA_KEY_CONTENT_TYPE,
            '{"'..DATA_KEY_BATTERY_LEVEL..'":"'..getVoltagePercentage()..'", "'..DATA_KEY_UUID..'":"'.. DEVICE_UUID ..'",  "'..DATA_KEY_MOISTURE_LEVEL..'":"'..getMoistureLevel()..'"}',
            function(code, data)
                if (code < 0) then
                    print("HTTP request failed")
                    doRequest(requestType) --TODO: IN CASE OF REQUEST ERROR, SEND MESSAGE VIA GSM OR BT
                else
                    print(code, data)
                    setRtcMemoryValue(KEY_SLOT_MEASUREMENT_CYCLE_COUNT, 0)
                end
                --goToDeepSleep();
            end)
    end
end

--------------------
--GO TO DEEP SLEEP (TO RESET DEVICE AFTER TIMEOUT IS NEED TO WIRE D0<->RST PIN)
--------------------
goToDeepSleep = function()
    print("Going into deep sleep for "..getDeepSleepInterval().." seconds")
    node.dsleep(DEEP_SLEEEP,0)
    ----------------
    -- 0, init data byte 108 is valuable
    -- >0, init data byte 108 is valueless
    -- 0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108
    -- 1, RF_CAL after deep-sleep wake up, there will be large current
    -- 2, no RF_CAL after deep-sleep wake up, there will only be small current
    -- 4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest
    -----------------
end

--------------------
--CONNECT TO WI-FI
--------------------
connectToWifi = function(requestType)
    print("connectToWifii")
    wifi.setmode(wifi.STATION)
    station_cfg={}
    station_cfg.ssid=CUSTOM_SSID
    station_cfg.pwd=CUSTOM_PASS
    station_cfg.save=false
    wifi.sta.config(station_cfg)
    --wifi.sta.autoconnect()
    tmr.alarm(1, 1000, 1, function()
        if wifi.sta.getip() == nil then
            print("Připojuji k "..CUSTOM_SSID)
        else
            tmr.stop(1)
            print("Připojeno, IP je " .. wifi.sta.getip())
            doRequest(requestType)
        end
    end)
end

--------------------
--TOOGLE DIODE
--------------------
switchDiode = function(state)
    local pin = 4
    local duration = 1000    -- 1 second duration for timer
    gpio.mode(pin, gpio.OUTPUT)
    if state == 1 then
        gpio.write(pin,gpio.LOW)
        print("Diode switched ON PIN " .. string.format(pin))
    else
        gpio.write(pin,gpio.HIGH)
        print("Diode switched OFF")
    end
end

--------------------
--DO MEASSURE
--------------------
doMeassure = function()
    print("doMeassure")
    local currentBatteryLevel = getVoltagePercentage()
    local currentMoistureLevel = getMoistureLevel()
    local measurementCycleCount = getMeasurementCycleCount()

    if (measurementCycleCount >= HEALTHCHECK_MEASURE_COUNT_TRESHOLD) then
        print("HEALTHCHECK_MEASURE_COUNT_TRESHOLD ")
        connectToWifiAndDoRequest(REQUEST_REPORT_HEALTHCHECK)
    end
    if (currentBatteryLevel < BATTERY_ALERT_TRESHOLD) then
        print("BATTERY TRESHOLD EXCEEDED " .. string.format(currentBatteryLevel))
        connectToWifiAndDoRequest(REQUEST_REPORT_ALARM_BATTERY)
    end
    if (currentMoistureLevel < MOISTURE_ALERT_TRESHOLD) then
        print("MOISTURE TRESHOLD EXCEEDED " .. string.format(currentMoistureLevel))
        connectToWifiAndDoRequest(REQUEST_REPORT_ALARM_WATER)
    end
    if currentMoistureLevel >= MOISTURE_ALERT_TRESHOLD
            and currentBatteryLevel >= BATTERY_ALERT_TRESHOLD
            and measurementCycleCount < HEALTHCHECK_MEASURE_COUNT_TRESHOLD then
        print("ALL MEASUREMENTS FINE, INCREASING COUNT OF MESUREMENT & GO TO SLEEP")
        goToDeepSleep();
    end
    --INCREASE MEASUREMENT CYCLES COUNT
    setRtcMemoryValue(KEY_SLOT_MEASUREMENT_CYCLE_COUNT, measurementCycleCount+1)
end

--------------------
--CONNECT TO WIFI AND DO REQUEST
--------------------
connectToWifiAndDoRequest = function(requestType)
    print("connectToWifiAndDoRequest")
    connectToWifi(requestType)
end


--------------------
--INIT FUNCTION
--------------------
initall = function()
    print("initall")
    switchDiode(1)
    tmr.alarm(1, CHECK_PERIOD, 1, function()
        doMeassure()
        --goToDeepSleep()
    end)
end

startup()
Last edited by rob0to on Sun Dec 30, 2018 7:40 am, edited 1 time in total.