Esp8266 Code problem

Joined
Mar 10, 2023
Messages
1
Reaction score
0
Hi im new to programming, ive done this code for a circuit that detects gas and sends a notification to your smartphone through Wi-Fi, problem is that if the gas alarm is not connected to Wi-Fi, the alarm doesnt work, how can i do that the gas alarm and the notification code work on their own and are non dependant?

Here are my codes:

main.py:
import network
import time
import json
import urequests
from wlan import do_connect
#indicadores y actuadores
from machine import Pin
from time import sleep

print('Inicialización')

led = Pin(14, Pin.OUT) # dato 5 salida
sensor_gas = Pin(13, Pin.IN) # dato 7 entrada
buzzer= Pin(12, Pin.OUT) # dato 6 salida
electrovalvula= Pin(15, Pin.OUT) # dato 8 salida
# estados iniciales network
do_connect()
start = time.ticks_ms()
apiKey = 'o.8vv6DQ82G3Ok2Ka4R9HydJoZDysiXkcc' #API key
url = 'https://api.pushbullet.com/v2/pushes'
headers = {'Access-Token': apiKey, 'Content-Type': 'application/json'}
data = {'type':'note','body':'Fuga de gas detectada','titulo':'Notificacion proyecto'}
dataJSON = json.dumps(data)

while True:

logic_state = sensor_gas.value()
if logic_state == False: # Si no esta presionado el push button
led.value(1) # led will turn ON
buzzer.value(1) # buzzer se enciende
electrovalvula.value(1) # electrovalvula impide flujo de gas
print('Gas detectado')
sleep(0.5)
#para notif
delta = time.ticks_diff(time.ticks_ms(), start)
if(delta> 8000):
print("Enviando mensaje")
r = urequests.post(url, headers=headers,data=dataJSON)
#meter el r.json con un if
print(r.json()['receiver_email'])
start = time.ticks_ms()
else: # Si push button está presionado
led.value(0) # led will turn OFF
buzzer.value(0) # buzzer se apaga
electrovalvula.value(0) #electrovalvula deja pasar gas
print('No hay presencia de gas')
sleep(0.5)

And here is the wlan header that I use on the main for the Wi-Fi connection:
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('Redmi Note 8T', '2889c467cef0')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top