from blynk import Blynk
import time
from machine import Pin, ADC
# consts
RELAY_PIN_NUMBER = 22
LDR_PIN_NUMBER = 32
LUX_THRESHOLD = 500
GAMMA = 0.7
RL10 = 33
ON = 1
OFF = 0
# setup
relay = Pin(RELAY_PIN_NUMBER, Pin.OUT)
photoresistor = ADC(Pin(LDR_PIN_NUMBER))
client = Blynk(
"MbudBZULsXDgZwD-7-NCpbqtUNj44Chk",
wifi_ssid="Wokwi-GUEST",
wifi_password="",
verbose=True
)
def ldr_to_lux():
analog_value = photoresistor.read()
voltage = analog_value / 4096. * 3.3;
resistance = 2000 * voltage / (1 - voltage / 3.3);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux
def main():
print("Starting the Lighting Automation Circuit...")
while True:
try:
luminosity = ldr_to_lux()
if luminosity <= LUX_THRESHOLD:
relay.value(ON)
client.publish("ds/Led", str(ON))
else:
relay.value(OFF)
client.publish("ds/Led", str(OFF))
client.publish("ds/Luminosity", str(luminosity))
except OSError as e:
print("Hardware or network operation failure:", e)
print("*" * 20)
time.sleep(10)
if __name__ == "__main__":
main()
#define BLYNK_TEMPLATE_ID "TMPL2qAB1ks-B"
#define BLYNK_TEMPLATE_NAME "IoT Somativa 2"
#define BLYNK_AUTH_TOKEN "MbudBZULsXDgZwD-7-NCpbqtUNj44Chk"