Door sensor with ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
vektra112
Posts: 2
Joined: Thu Oct 10, 2019 5:42 pm

Door sensor with ESP8266

Post by vektra112 » Thu Oct 10, 2019 5:49 pm

Hello!
I'm new to programming esp boards but I'dont know if problem is with my wiring or the code.
I've connected sensor to the GND and D02 and when the doors are closed it should open link and go to deep sleep. Instead it stays open all the time.
Here is the code:

Code: Select all

import os
import urequests
import machine
import time
from time import sleep
from machine import Pin
def connect():
    import network
 
    ssid = ""
    password =  ""
 
    station = network.WLAN(network.STA_IF)
 
    if station.isconnected() == True:
        print("Already connected")
        return
 
    station.active(True)
    station.connect(ssid, password)
 
    while station.isconnected() == False:
        pass
 
    print("Connection successful")
    print(station.ifconfig())

connect()
time.sleep(1)
p2 = Pin(2, Pin.IN)
while p2.value()==1:
  response=urequests.post("http://somer_link")
  time.sleep(10)  
else:
    response=urequests.post("http://other_link")
    time.sleep(5)
    machine.deepsleep() 
Thank you!

vektra112
Posts: 2
Joined: Thu Oct 10, 2019 5:42 pm

Re: Door sensor with ESP8266

Post by vektra112 » Thu Oct 10, 2019 7:01 pm

I found the problem. It was code and the wiring. I've changed it to Pin5 which is GPIO1 and the code is this.

Code: Select all

import os
import urequests
import machine
import time
from time import sleep
from machine import Pin
def connect():
    import network
 
    ssid = ""
    password =  ""
 
    station = network.WLAN(network.STA_IF)
 
    if station.isconnected() == True:
        print("Already connected")
        return
 
    station.active(True)
    station.connect(ssid, password)
 
    while station.isconnected() == False:
        pass
 
    print("Connection successful")
    print(station.ifconfig())

connect()
time.sleep(1)
p5 = Pin(5, Pin.IN, Pin.PULL_UP)
while p5.value()==1:
  response=urequests.post("http://")
  time.sleep(10)  
else:
    response=urequests.post("http://")
    time.sleep(5)
    machine.deepsleep() 

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

Re: Door sensor with ESP8266

Post by kevinkk525 » Thu Oct 10, 2019 9:15 pm

Glad you found the solution.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: Door sensor with ESP8266

Post by ghayne » Sun Oct 13, 2019 8:56 am

You may have problems with GPIO1 as it is the RX pin. It is high at boot and boot will fail if it's held low at boot time.

This is a good reference for which GPIO's you should use on the ESP8266
https://randomnerdtutorials.com/esp8266 ... nce-gpios/

Post Reply