#This programe will
#Upgrade: We wil send the number of blinking time to ThingSpeak
#using HTTP protocol
#Date :3/1/2024
from machine import Pin, SoftI2C
from utime import sleep
import urequests as requests
import random
import oled_library
led = Pin(2, Pin.OUT)
Pin_scl_sda = SoftI2C(scl = Pin(22), sda = Pin(21))
skrin = oled_library.SSD1306_I2C(width=128, height=64, i2c=Pin_scl_sda)
#paramiter declaration:
blinking =0
bill = 0
tax = 0
Consumption = 0
total_bill = 0
total_consumption = 0
#main programe
while True :
led.on()
sleep(1)
led.off()
sleep(1)
Current = float(random.randint(60,69))
Voltage = float(random.randint(1500,1500))
Power = Current*Voltage
kWh = Power/1000
tax = (6/100)
if (kWh >=1 and kWh <= 200):
bill = (kWh)*0.218
elif (kWh >200 and kWh <= 300):
bill = 200*0.218 + (kWh - 200)*0.334
elif (kWh >300 and kWh <= 600):
bill = 200*0.218 + 100*0.334 + (kWh - 300)*0.516
elif (kWh >600 and kWh <= 900):
bill = 200*0.218 + 100*0.334 + 300*0.516 + (kWh - 600)*0.546
else:
bill = 200*0.218 + 100*0.334 + 300*0.516 + 300*0.546 + (kWh - 900)*0.571
total_bill += bill
total_consumption += kWh
ICPT1 = (total_consumption*0.02)
ICPT2 = 60 + (total_consumption - 600)*0.1
ICPT3 = (total_consumption - 600)*0.1
plustax = ((total_consumption- 600)*0.546)*tax
plustax1 = (300*0.546)*tax + (total_consumption- 900)*0.571*tax
plustax3 = (300*0.546)*tax + ((total_consumption- 900)*0.571*tax) + (ICPT3*tax)
if (total_consumption >=1 and total_consumption <= 200):
total_bill = (total_consumption)*0.218 - ICPT1
elif (total_consumption >200 and total_consumption <= 300):
total_bill = 200*0.218 + (total_consumption - 200)*0.334 - ICPT1
elif (total_consumption >300 and total_consumption <= 600):
total_bill = 200*0.218 + 100*0.334 + (total_consumption - 300)*0.516 - ICPT1
elif (total_consumption >600 and total_consumption <= 900):
total_bill = 200*0.218 + 100*0.334 + 300*0.516 + (total_consumption - 600)*0.546 + plustax
elif (total_consumption >900 and total_consumption <= 1500):
total_bill = 200*0.218 + 100*0.334 + 300*0.516 + 300*0.546 + (total_consumption - 900)*0.571 + plustax1
elif (total_consumption > 1500):
total_bill = 200*0.218 + 100*0.334 + 300*0.516 + 300*0.546 + (total_consumption - 900)*0.571 +plustax3 + ICPT2
blinking =blinking + 1
#print ("My LED has blink for",blinking, "times")
print("Bacaan semasa :", kWh, "kWh")
print(f"Bill semasa: RM {bill:.2f}")
print(f"Electricity bill for {total_consumption:.2f} kWh is: RM {total_bill:.2f}\n")
skrin.fill(0) #0 - for BLACK, 1 for WHITE
skrin.text("Consumption = ", 10, 10, 1)
skrin.text(str(total_consumption), 25, 20, 1)
skrin.text("kWh ", 75, 20, 1)
skrin.text("Total Bill = ", 10, 35, 1)
skrin.text(str(f"{total_bill:.2f}"), 50, 45, 1)
skrin.text("RM", 25, 45, 1)
skrin.show() #a must!
sleep(60)