One of the main benefits of using Micropython is its small size and low resource requirements. Because it is designed to run on small devices with limited processing power and memory, micro python is able to run efficiently and quickly even on devices with very limited hardware resources. This makes it an ideal choice for a wide range of applications, including Internet of Things (IoT) devices, sensors, and other types of embedded systems.
Home Assistant is a free platform where you can integrate your Devices and monitor the data and control the operations.The Home assistant can also be integrated with Alexa and Google Assistant.The Windows portable version of Home Assistant is suitable for PC and Laptops. It is a very lightweight version.This HAAS WP can be downloaded from this link and watch this video to know how to download and use it.
import network
import time
from umqtt.simple import MQTTClient
import machine
WiFi_SSID = "" #Wfif SSID
WiFi_PASS = "" #WIFI PASSWORD
SERVER = "broker.hivemq.com"//you can try for other brokers like test.mosquito.org
PORT = 1883
CLIENT_ID = "Esp32Mlc"
#create topic to publish the message about Tank Level
topicOutTankLevel = "WaterTank/level"
#create topic to publish the message about Pump Status
topicOutPump = "WaterTank/Pump/Status"
#define Port Pins for wire sensors
LOW_EVEL = 18
MID_EVEL = 5
HIGH_EVEL = 19
#Current Level of Tank
levelStatus ="Empty"
#Pump operation Flag
fpump = "OFF"
#Set Tank level Sensors pins as input
lowlevel = machine.Pin(LOW_EVEL, machine.Pin.IN, machine.Pin.PULL_UP)
midlevel = machine.Pin(MID_EVEL, machine.Pin.IN, machine.Pin.PULL_UP)
highlevel = machine.Pin(HIGH_EVEL, machine.Pin.IN, machine.Pin.PULL_UP)
#Set GPIO pin 2 as output
PUMP = machine.Pin(2, machine.Pin.OUT)
PUMP.value(0)
#check low level input
def senLowLevel():
#flag shwoing tank level status
ftanklevel = False
if(lowlevel.value() == 0):
time.sleep(1)
if(lowlevel.value() == 0):
ftanklevel = True
elif(lowlevel.value() == 1):
time.sleep(1)
if(lowlevel.value() == 1):
ftanklevel = False
return ftanklevel
#check mid level input
def senMidLevel():
ftanklevel = False
if(midlevel.value() == 0):
time.sleep(1)
if(midlevel.value() == 0):
ftanklevel = True
elif(midlevel.value() == 1):
time.sleep(1)
if(midlevel.value() == 1):
ftanklevel = False
return ftanklevel
#check High level input
def senHighLevel():
ftanklevel = False
if(highlevel.value() == 0):
time.sleep(1)
if(highlevel.value() == 0):
ftanklevel = True
elif(highlevel.value() == 1):
time.sleep(1)
if(highlevel.value() == 1):
ftanklevel = False
return ftanklevel
def wifi_connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(WiFi_SSID, WiFi_PASS)
while not wlan.isconnected():
pass
print("Connected to Wifi Router")
#connect esp32 to wifi
wifi_connect()
#create a client and connect to the mqtt broker
client = MQTTClient(CLIENT_ID, SERVER,PORT)
#Connect clint with Broker
client.connect()
print("Mqtt connected")
while True:
#check level Sensor inputs
if ((senHighLevel() == True)):
levelStatus = "Full"
elif ((senMidLevel() == True) ):
levelStatus = "MidLevel"
elif ((senLowLevel() == True) ):
levelStatus = "LowLevel"
else:
levelStatus = "Empty"
if ((levelStatus == "Empty") ):
#When Tank Empty Turn the PUMP on
fpump = "ON"
PUMP.value(1)
elif (levelStatus == "Full") :
#When Tank is full Trun PUMP Off
fpump = "OFF"
PUMP.value(0)
#Publish the level status and pump status to HAAS
client.publish(topicOutTankLevel, levelStatus )
client.publish(topicOutPump, fpump )
print(levelStatus)
print(fpump)
#Check any message arrived
client.check_msg()
time.sleep(2)