#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
from utime import sleep
import urequests as requests
import random
led = Pin(2, Pin.OUT)
#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")
sleep(1)