Page 1 of 1

Door sensor with ESP8266

Posted: Thu Oct 10, 2019 5:49 pm
by vektra112
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!

Re: Door sensor with ESP8266

Posted: Thu Oct 10, 2019 7:01 pm
by vektra112
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() 

Re: Door sensor with ESP8266

Posted: Thu Oct 10, 2019 9:15 pm
by kevinkk525
Glad you found the solution.

Re: Door sensor with ESP8266

Posted: Sun Oct 13, 2019 8:56 am
by ghayne
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/