I want the pico to monitor these and display a webpage that is run of the pico.
I had someone on Fiverr help me with the code, but after 10 attempts at writing (without him testing his code first) and all failed, so I cancelled it and tried fixing it myself. After rewriting all the code, I now have the website part working, however, with all the IF statements, unless the the last statement is active, none of the others work.
I added a POWERON pin status as the last one, so this is enabled at startup and then enables all the other pins to function, however, they will only work in the order they are written from the last to the first . As both tanks readings need to be independent of each other, I can find away to separate the two tank codes so they are not dependent on the IF statement below them being active.
I only have basic skill in coding so dont really understand the If ELIF and ELSE functions so out of my depth as to how to get it to work
Code: Select all
import time
import network
import socket
from machine import Pin
from utime import localtime
dateTimeObj = localtime()
#FRESH TANK
led = Pin("LED",Pin. OUT)
freshmt = Pin(1, Pin.IN, Pin.PULL_DOWN) # tank empty red
fresh1 = Pin(2, Pin.IN ,Pin.PULL_DOWN) # tank 25% yellow
fresh2 = Pin(3, Pin.IN ,Pin.PULL_DOWN) # tank 50% green
fresh3 = Pin(4, Pin.IN ,Pin.PULL_DOWN) # tank 75% Green
freshfull = Pin(5, Pin.IN, Pin.PULL_DOWN) # tank full Blue
#WASTE TANK
wastemt = Pin(6, Pin.IN ,Pin.PULL_DOWN) #waste empty blue
waste1 = Pin(7, Pin.IN ,Pin.PULL_DOWN) #waste 25% full green
waste2 = Pin(8, Pin.IN ,Pin.PULL_DOWN) #waste 50% full green
waste3 = Pin(9, Pin.IN, Pin.PULL_DOWN) #waste 75%L full green
wastefull = Pin(10, Pin.IN ,Pin.PULL_DOWN) #waste FULL RED
poweron = Pin(14, Pin.IN ,Pin.PULL_DOWN)
buzzer = Pin(15, Pin.OUT) #alarm buzzer
# wifi code start
ssid = 'SSID' # Replace with your actual wifi name
password = 'password' # Replace with your actual password
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# wifi code end
# HTML conten start
# Wait for connection or fail
max_wait = 10
while max_wait > 0:
if wlan.isconnected():
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if not wlan.isconnected():
raise RuntimeError('network connection failed')
else:
print('Connected')
status = wlan.ifconfig()
print('IP = ' + status[0])
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(addr)
s.listen(1)
print('Listening on', addr)
html_response = html
# Listen for connections, serve client
while True:
try:
cl, addr = s.accept()
print('Client connected from', addr)
request = cl.recv(1024)
request = str(request)
#fresh tank inputs below
if freshfull.value() == 1: #tank full
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-1"></div>', '<div class="box box-1 box-blue"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if fresh3.value() == 1: #tank 75%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-3"></div>', '<div class="box box-3 box-green"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if fresh2.value() == 1: #tank 50%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-5"></div>', '<div class="box box-5 box-green"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if fresh1.value() == 1: #tank 25%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-7"></div>', '<div class="box box-7 box-yellow"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if freshmt.value() == 1: #Tank empty
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-9"></div>', '<div class="box box-9 box-red"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
# waste tank inputs below
if wastefull.value() == 1: #tank full
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-2"></div>', '<div class="box box-2 box-red"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if waste3.value() == 1: #tank 75%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-4"></div>', '<div class="box box-4 box-yellow"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if waste2.value() == 1: #tank 50%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-6"></div>', '<div class="box box-6 box-green"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if waste1.value() == 1: #tank 25%
buzzer.value(1)
led.value(1)
html_response = html_response.replace('<div class="box box-8"></div>', '<div class="box box-8 box-green"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if wastemt.value() == 1: #tank empty
buzzer.value(0)
led.value(1)
html_response = html_response.replace('<div class="box box-10"></div>', '<div class="box box-10 box-blue"></div>', 1)
else:
html_response = html
buzzer.value(0)
led.value(0)
if poweron.value() == 1: # this being the last and alwasy on only way to get the others to work
html_response = html_response.replace('<div class="box box-14"></div>', '<div class="box box-14 box-orange"></div>', 1)
buzzer.value(1)
led.value(0)
else:
html_response = html
buzzer.value(0)
led.value(0)
# Send the modified HTML response with or without the "box-red" class
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(html_response)
cl.close()
except OSError as e:
cl.close()
print('Connection closed')
Thank you
John