How to switch on a electric bulb with ESP8266 board?
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
Re: How to switch on a electric bulb with ESP8266 board?
Hi,
i m trying with PIR sensor and below is the code in main.py, when i run the code on esp8266 board i am getting as below screens and each time i need to reset the button to detect the motion. can someone suggest me where i do wrong.
main.py
pin=Pin(2,Pin.OUT)
pir=Pin(14,Pin.IN,Pin.PULL_UP)
a=pir.value()
print(a)
while True:
time.sleep(3)
if a==0:
print('motion not detected.')
if a==1:
print('motion detected.')
pin.off()
time.sleep(2)
pin.on()
i m trying with PIR sensor and below is the code in main.py, when i run the code on esp8266 board i am getting as below screens and each time i need to reset the button to detect the motion. can someone suggest me where i do wrong.
main.py
pin=Pin(2,Pin.OUT)
pir=Pin(14,Pin.IN,Pin.PULL_UP)
a=pir.value()
print(a)
while True:
time.sleep(3)
if a==0:
print('motion not detected.')
if a==1:
print('motion detected.')
pin.off()
time.sleep(2)
pin.on()
Re: How to switch on a electric bulb with ESP8266 board?
You have to put "a=pir.value()" inside the while loop.
It has to ask the pin in every loop
It has to ask the pin in every loop
Yes Frida is my watchdog!
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
Re: How to switch on a electric bulb with ESP8266 board?
Hi, i am trying to switch on LED every day for one hour and below is my code. i have connected one pin of led to GND and other to D5 on ESP8266. i am able to get print statement but the led is not switching on. can someone let me know where i do mistake....is it with connections or coding.kevinkk525 wrote: ↑Mon May 20, 2019 4:51 pmCreates this output: 2019-05-20 14:56:03 (YYYY-MM-DD HH:mm:SS)Code: Select all
t = time.localtime() t = t[0:3] + (0,) + (t[3] + TIMEZONE_OFFSET,) + t[4:6] + (0,) print("{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(t[0], t[1], t[2], t[3],t[4],t[5])
Code: Select all
from ntptime import settime
import time
import machine
settime()
rtc=machine.RTC()
t = rtc.datetime()
pin=machine.Pin(14,machine.Pin.OUT)
if t[4]==5 and t[4]<=24:
pin.off()
print('led on')
time.sleep( for 1 hour)
pin.on()
print('led off')
Re: How to switch on a electric bulb with ESP8266 board?
Does it work if you try it from the REPL? Try and just get the LED working on its own.
Code: Select all
>>> pin=machine.Pin(14,machine.Pin.OUT)
>>> pin.on()
>>> pin.off()
Re: How to switch on a electric bulb with ESP8266 board?
That statements does not look reasonable. t[4] == 5 is sufficient.if t[4]==5 and t[4]<=24:
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: How to switch on a electric bulb with ESP8266 board?
time.sleep( for 1 hour)
is certainly not right
is certainly not right
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to switch on a electric bulb with ESP8266 board?
Assuming you have the LED working as per the advice of @jimmo, you need something like
The RTC on the ESP8266 is notoriously inaccurate. You might want to use similar logic to call settime() once at (say) 4:00.
Code: Select all
from ntptime import settime
import time
import machine
settime()
rtc=machine.RTC()
pin=machine.Pin(14,machine.Pin.OUT)
led_on = False
while True:
t = rtc.datetime()
if t[4] == 5 and not led_on:
led_on = True
pin.on()
if t[4] == 6 and led_on:
led_on = False
pin.off()
time.sleep(1)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
Re: How to switch on a electric bulb with ESP8266 board?
Thank you for sharing the details pythoncoder. i will try and get back to you if i face any issues.pythoncoder wrote: ↑Wed May 29, 2019 8:33 amAssuming you have the LED working as per the advice of @jimmo, you need something like
The RTC on the ESP8266 is notoriously inaccurate. You might want to use similar logic to call settime() once at (say) 4:00.
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
Re: How to switch on a electric bulb with ESP8266 board?
Hi Jimmo, led working with below code and when pin.off() it is glowing and pin.on() it stop blinking. not sure why functionality is reverse.jimmo wrote: ↑Tue May 28, 2019 12:02 pmDoes it work if you try it from the REPL? Try and just get the LED working on its own.
Code: Select all
>>> pin=machine.Pin(14,machine.Pin.OUT) >>> pin.on() >>> pin.off()
>>> pin=machine.Pin(14,machine.Pin.OUT)
>>> pin.off()
>>> pin.on()