Relay On Time

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
sprinkfitter
Posts: 36
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Relay On Time

Post by sprinkfitter » Tue Apr 09, 2019 12:03 pm

I want to turn a relay on for a period of time. Can I just call millis

Code: Select all

if ds.DateTime() == (OnMin4, OnHour4, OnDay4, OnMonth4, OnYear4):
    relay1.value = 0  # HIGH
    millis(2000)
    print('Relay 1 ON')
    print(ds.DateTime)
    print(OnMin1, OnHour1, OnDay1, OnMonth1, OnYear1)
    
else:
    relay1.value = 1  # LOW
    print('Relay 1 OFF')

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Relay On Time

Post by Roberthh » Tue Apr 09, 2019 12:21 pm

You can use utime.sleep_ms(sleep_time).

sprinkfitter
Posts: 36
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Relay On Time

Post by sprinkfitter » Tue Apr 09, 2019 12:24 pm

Seems a little odd sleep time to be use to keep a relay or pin on. So you can use the sleep for high and low pins??

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Relay On Time

Post by ThomasChr » Tue Apr 09, 2019 1:31 pm

???

You turn the relay on. You let the uc sleep for a period of time. You turn the relay off.
What is odd here?

Yes, you can also use timers which have the benefit of letting your main code running and not sleeping, but sometimes you don't need to do anything in the sleep phase and so you can sleep...

sprinkfitter
Posts: 36
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Relay On Time

Post by sprinkfitter » Tue Apr 09, 2019 1:32 pm

Thanks that does work!!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Relay On Time

Post by pythoncoder » Tue Apr 09, 2019 4:59 pm

It seems to me that the above code will turn the relay on for one minute regardless of the delay. It turns on when minutes, day, month and year match. It turns off when they fail to match. If the code runs many times per minute, that will happen one minute later.

Or am I missing something?
Peter Hinch
Index to my micropython libraries.

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Relay On Time

Post by ThomasChr » Tue Apr 09, 2019 5:26 pm

I don‘t think the code makes much sense at all...

sprinkfitter
Posts: 36
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Relay On Time

Post by sprinkfitter » Wed Apr 10, 2019 1:14 pm

Under construction... Trying to turn relay on and off a certain times of day! Very new to Micro-python.

Code: Select all

OnMin1 = arr.array([10, 20])  # Array Setup for triggering relay 1
OnHour1 = arr.array([8, 16])
OnDay1 = arr.array([1, 3, 5, 7, ])
OnMonth1 = arr.array([6, 7, 8, 9])
OnYear1 = arr.array([2019, 2020, 2021, 2022])

OnMin2 = arr.array([15, 30])  # Array Setup for triggering relay 2
OnHour2 = arr.array([9, 17])
OnDay2 = arr.array([17, 18, 19, 20])
OnMonth2 = arr.array([6, 7, 8, 9])
OnYear2 = arr.array([2019, 2020, 2021, 2022])

OnMin3 = arr.array([17, 18, 19, 20])  # Array Setup for triggering relay 3
OnHour3 = arr.array([17, 18, 19, 20])
OnDay3 = arr.array([17, 18, 19, 20])
OnMonth3 = arr.array([6, 7, 8, 9])
OnYear3 = arr.array([2019, 2020, 2021, 2022])

OnMin4 = arr.array([17, 18, 19, 20])  # Array Setup for triggering relay 4
OnHour4 = arr.array([17, 18, 19, 20])
OnDay4 = arr.array([17, 18, 19, 20])
OnMonth4 = arr.array([6, 7, 8, 9])
OnYear4 = arr.array([2019, 2020, 2021, 2022])

if ds.DateTime() == (OnMin4, OnHour4, OnDay4, OnMonth4, OnYear4):
    relay1.value = 0  # HIGH
    utime.sleep_ms(10000)
    print('Relay 1 ON')
    print(ds.DateTime)
    print(OnMin1, OnHour1, OnDay1, OnMonth1, OnYear1)

else:
    relay1.value = 1  # LOW
    print('Relay 1 OFF')

sprinkfitter
Posts: 36
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Relay On Time

Post by sprinkfitter » Wed Apr 10, 2019 1:35 pm

Mr.pythoncoder now I do not want to do that. What I want to do is turn a relay on at a certain time let it stay on for the delay and then turn off?

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Relay On Time

Post by ThomasChr » Wed Apr 10, 2019 1:48 pm

What about:

Code: Select all

while True:
    relay1.on()
    time.sleep(10000)
    relay1.off()
    time.sleep(10000)
This will turn the relay on for 10 seconds and off for 10 seconds endlessly...

Post Reply