Page 3 of 6

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Thu May 23, 2019 4:49 pm
by RajaRamesh
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()
pir1.png
pir1.png (11 KiB) Viewed 331 times
pir0.jpg
pir0.jpg (19.26 KiB) Viewed 331 times

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Thu May 23, 2019 5:33 pm
by Frida
You have to put "a=pir.value()" inside the while loop.
It has to ask the pin in every loop

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Fri May 24, 2019 10:16 am
by RajaRamesh
Frida wrote:
Thu May 23, 2019 5:33 pm
You have to put "a=pir.value()" inside the while loop.
It has to ask the pin in every loop
Thank you it's working as expected.

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Tue May 28, 2019 11:57 am
by RajaRamesh
kevinkk525 wrote:
Mon May 20, 2019 4:51 pm

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])
Creates this output: 2019-05-20 14:56:03 (YYYY-MM-DD HH:mm:SS)
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.

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?

Posted: Tue May 28, 2019 12:02 pm
by jimmo
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?

Posted: Tue May 28, 2019 12:45 pm
by Roberthh
if t[4]==5 and t[4]<=24:
That statements does not look reasonable. t[4] == 5 is sufficient.

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Tue May 28, 2019 1:47 pm
by kevinkk525
time.sleep( for 1 hour)
is certainly not right :D

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Wed May 29, 2019 8:33 am
by pythoncoder
Assuming you have the LED working as per the advice of @jimmo, you need something like

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)
The RTC on the ESP8266 is notoriously inaccurate. You might want to use similar logic to call settime() once at (say) 4:00.

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Wed Jun 05, 2019 7:07 am
by RajaRamesh
pythoncoder wrote:
Wed May 29, 2019 8:33 am
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.
Thank you for sharing the details pythoncoder. i will try and get back to you if i face any issues.

Re: How to switch on a electric bulb with ESP8266 board?

Posted: Sat Jun 15, 2019 7:00 am
by RajaRamesh
jimmo wrote:
Tue May 28, 2019 12:02 pm
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()
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.

>>> pin=machine.Pin(14,machine.Pin.OUT)
>>> pin.off()
>>> pin.on()