How to switch on a electric bulb with ESP8266 board?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

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

Post by RajaRamesh » Thu May 23, 2019 4:49 pm

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 312 times
pir0.jpg
pir0.jpg (19.26 KiB) Viewed 312 times

User avatar
Frida
Posts: 40
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

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

Post by Frida » 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
Yes Frida is my watchdog!

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

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

Post by RajaRamesh » Fri May 24, 2019 10:16 am

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.

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

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

Post by RajaRamesh » Tue May 28, 2019 11:57 am

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')

User avatar
jimmo
Posts: 401
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

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

Post by jimmo » 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()

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

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

Post by Roberthh » Tue May 28, 2019 12:45 pm

if t[4]==5 and t[4]<=24:
That statements does not look reasonable. t[4] == 5 is sufficient.

kevinkk525
Posts: 416
Joined: Sat Feb 03, 2018 7:02 pm

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

Post by kevinkk525 » Tue May 28, 2019 1:47 pm

time.sleep( for 1 hour)
is certainly not right :D

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

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

Post by pythoncoder » Wed May 29, 2019 8:33 am

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.
Peter Hinch

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

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

Post by RajaRamesh » Wed Jun 05, 2019 7:07 am

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.

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

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

Post by RajaRamesh » Sat Jun 15, 2019 7:00 am

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()

Post Reply