print("WATER STORAGE TUB LEVEL SENSOR")
# Import necessary module/libraries
import hcsr04 #ultrasonic library
import ssd1306 #oled library
import urequests #favoriot
from utime import sleep
from machine import SoftI2C,Pin, PWM #Pulse Width Mode
#Define for Favoriot
url = 'https://apiv2.favoriot.com/v2/streams'
Device_Developer_Id= 'SOCIALDISTANCE_1_device@SYAKIRAH_'
Access_Token = 'lt8pYKyYGTxtKQCEZ4tgyM7szYi2j95K'
#Create an object called ultrasonic_sensor using OOP (Object Oriented Programming)
#OOP format is ---> library name.class name(.....)
ultrasonic_sensor = hcsr04.HCSR04(trigger_pin=13, echo_pin=27, echo_timeout_us=500*2*30) #echotimeout kiraan jarak 400cm
# Declare pin on OLED Display
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128 #based on spec
oled_height = 64 #based on spec
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)
#Declare buzzer connection
buzzer = PWM(Pin(23),Pin.OUT)
#Declare LED pin
led_red = Pin(25,Pin.OUT) #Built-in LED is connected to Pin 12
led_yellow = Pin(33,Pin.OUT)
led_green = Pin(32,Pin.OUT)
#Declare push button is connected to pin 4
#push_button = Pin(4, Pin.IN)
while True:
#Let's test our ultrasonic sensor
level_in_cm = ultrasonic_sensor.distance_cm() #use when u need decimal point number
level_in_mm = ultrasonic_sensor.distance_mm() #use when u want whole number
#Display the water level values on Serial Monitor
print('The water level in the water tank is :',level_in_cm,'cm')
print('The water level in the water tank is :',level_in_mm,'mm')
print('---------------------------------------------')
print('\n')
if level_in_cm < 50: # HIGH WATER LEVEL
for i in range (5):
#Display water level value on OLED screen
oled.fill(0) #To make screen becoming black
oled.text('Water Level', 0, 0, 1) #1st 0 is X-axis, 2nd 0 is Y-axis, 1 for white,0 for black)
oled.text('almost FULL!', 0, 10, 1)
oled.text(str(level_in_cm), 0, 30, 1)
oled.text('cm',70,30,1)
oled.text(str(level_in_mm), 0, 40, 1)
oled.text('mm',40,40,1)
oled.show()
buzzer.init(freq=800,duty=272) #ubah value bunyi berlainan
sleep(0.3)
buzzer.init(freq=1,duty=0)
sleep(0.3)
led_green.on()
sleep(0.2)
led_green.off()
sleep(0.1)
led_red.off()
led_yellow.off()
elif 50 <= level_in_cm < 300: # NORMAL WATER LEVEL
buzzer.init(freq=1,duty=0)
#Display water level value on OLED screen
oled.fill(0) #To make screen becoming black
oled.text('Water Level', 0, 0, 1) #1st 0 is X-axis, 2nd 0 is Y-axis, 1 for white,0 for black)
oled.text('in range!', 0, 10, 1)
oled.text(str(level_in_cm), 0, 30, 1)
oled.text('cm',70,30,1)
oled.text(str(level_in_mm), 0, 40, 1)
oled.text('mm',40,40,1)
oled.show()
led_yellow.on()
sleep(1)
led_red.off()
else: # LOW WATER LEVEL
for i in range (5):
#Display water level value on OLED screen
oled.fill(0) #To make screen becoming black
oled.text('Water Level', 0, 0, 1) #1st 0 is X-axis, 2nd 0 is Y-axis, 1 for white,0 for black)
oled.text('is LOW!', 0, 10, 1)
oled.text(str(level_in_cm), 0, 30, 1)
oled.text('cm',70,30,1)
oled.text(str(level_in_mm), 0, 40, 1)
oled.text('mm',40,40,1)
oled.show()
buzzer.init(freq=1200,duty=272)
sleep(0.2)
buzzer.init(freq=1,duty=0)
sleep(0.2)
led_red.on()
sleep(0.2)
led_red.off()
sleep(0.1)
led_green.off()
led_yellow.off()
#sleep(3)
# To Transfer Data at cloud Favoriot (outside the conditions)
sensor_data = {
"device_developer_id": Device_Developer_Id,
"data": {
'field1': level_in_cm,
#'field2': jauh_in_cm
}
}
http_header = {
'Content-Type': 'application/json',
'Apikey': Access_Token
}
data_submission = urequests.post(url, json=sensor_data, headers=http_header)
print(data_submission.status_code)
data_submission.close()
sleep(5)