ESP8266 Deepsleep/code problems

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Posts: 1
Joined: Tue Feb 12, 2019 4:31 am

ESP8266 Deepsleep/code problems

Post by Mustela125 » Tue Feb 12, 2019 5:02 am

Hello all,

I've had quite the headache trying to get this code to work. I do not come from a programming or ECE background, so I am requesting assistance in getting this code to work as intended as well as some programming pointers to improve the code. All critiques are welcome!

I really just want a sort of super basic alarm clock. I have hard coded the alarm time and when the time comes an LED turns on. All other times I just want it to deepsleep. Due to the lack of adequate clock on the esp8266 I am having it wake up periodically to re-check the time. After the alarm goes off, it's supposed to just go back into deepsleep and then begin waiting for the next day to "sound the alarm". After a hole bunch of research, trial and error and with an utter lack of debugging skills using hardware I have finally been able to get this thing to work two days in a row, but the third day the LED does not turn on. I'm not sure what's going on or how to approach debugging it further.

This code shows that every day I want the LED to turn on at 7am for 30 minutes, and then turn off again till the next morning. I have my LED hooked up to pin 5 (D1) and RST pin tied to pin 16 (D0). The following code is my on the board.

Thanks in advance!

Code: Select all

import machine
import utime
import ntptime
import network

def snooze(milliseconds):
    # To be able to use this feature
    # configure RTC.ALARM0 to be able to wake the device
    rtc = machine.RTC()
    rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
    # set RTC.ALARM0 to fire after some milliseconds
    rtc.alarm(rtc.ALARM0, milliseconds)
    # put the device to sleep

def do_connect():
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.connect('PinkWeasel', 'weslin 16 nicole home')
        while not sta_if.isconnected():
            print(".", end="")
    # print('network config:', sta_if.ifconfig())

def getAZtime():
    (year, month, mday, hour, minute, second, weekday, yearday) = utime.localtime()
    hour = hour - 7
    if hour < 0:
        hour += 24
        mday -= 1
        if mday < 0:
            mday += 30  # this is clearly not handled correctly
            month -= 1
            if month < 1:
                month += 12
                year -= 1
    azTime = (year, month, mday, hour, minute, second, weekday, yearday)
    return azTime

def getDuration(wakeHour, wakeMin):
    azTime = getAZtime()
    (year, month, mday, hour, minute, second, weekday, yearday) = azTime
    print('AZ Time:')
    hoursLeft = wakeHour - hour  # hours
    if hoursLeft < 0:
        hoursLeft += 24
    minLeft = wakeMin - minute  # minutes
    if minLeft < 0:
        minLeft += 60
        hoursLeft -= 1
        if hoursLeft < 0:
            hoursLeft += 24
    secLeft = 0 - second  # seconds
    if secLeft < 0:
        secLeft += 60
        minLeft -= 1
        if minLeft < 0:
            minLeft += 60
            hoursLeft -= 1
            if hoursLeft < 0:
                hoursLeft += 24
    duration = hoursLeft + (minLeft/60) + (secLeft/3600)  # hours
    print('Hours remaining: ' + str(hoursLeft))
    print('Minutes remaining: ' + str(minLeft))
    print('Seconds remaining: ' + str(secLeft))
    print('Total duration in hours: ' + str(duration))
    return duration

def flashLED(pinLED, flashTime):

wakeHour = 7  # hour
wakeMin = 0  # minute
onDuration = 30  # minutes

pin = machine.Pin(5, machine.Pin.OUT)

duration = getDuration(wakeHour, wakeMin)  # hour
print("Start Program")
# check if the device awoke from a deep sleep
if machine.reset_cause() != machine.DEEPSLEEP_RESET:
    flashLED(pin, .1)
    flashLED(pin, .1)
    flashLED(pin, .5)
    print("Start NOT from deepsleep. ")
    print(str(duration) + " hours remaining. ")
    print("Awoke from a deep sleep. ")
    print(str(duration) + " hours remaining. ")

if duration > 1:
    print("Time is greater than an hour, snooze.")
    print(str(duration) + " hours remaining. ")
elif duration > 10/60:
    print("Time is greater than 10 minutes. Snooze. ")
    print(str(duration) + " hours remaining. ")
    print("Time is less than 10 minutes. Wait. ")
    print(str(duration) + " hours remaining. ")
    utime.sleep(duration*3600)  # utime sleep is in seconds and can be decimal values
    flashLED(pin, onDuration*60)

Post Reply