Message
Message
BLYNK_AUTH_TOKEN = "gIwMgfU5sNPWDLFH0OAk-EYUX57i-JCa"
led1 = 18
led2 = 19
water_sensor_pin = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(water_sensor_pin, GPIO.IN)
# Initialize HX711
hx = HX711(dout_pin=5, pd_sck_pin=6)
hx.zero() # Tare the HX711
def calibrate_scale():
global calibration_factor, ratio
print("Calibration process:")
input('1. Place a known weight on the scale and press Enter.')
reading = hx.get_data_mean()
if reading:
print('2. Mean value from HX711 subtracted by offset:', reading)
known_weight_grams = input('3. Enter the weight in grams: ')
try:
value = float(known_weight_grams)
print(f"{value} grams")
except ValueError:
print('Invalid input. Calibration aborted.')
return
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
@blynk.on("V0")
def v0_write_handler(value):
global valuem
if int(value[0]) != 0:
GPIO.output(led1, GPIO.HIGH)
print("LED1 HIGH - Turning on Stepper Motor")
# Assuming you want to turn the stepper motor for 200 steps (adjust as
needed)
stepper_motor.enable(True)
stepper_motor.run(30000, True)
stepper_motor.enable(False)
print(value)
else:
GPIO.output(led1, GPIO.LOW)
print("LED1 LOW")
@blynk.on("V3")
def v3_write_handler(value):
global valuem
if int(value[0]) != 0:
blynk.virtual_write(5, 100)
valuem = 100
print(valuem)
print("Gauge on V2 reset to 100")
#@blynk.on("V1")
#def v1_write_handler(value):
# if int(value[0]) != 0:
# GPIO.output(led2, GPIO.HIGH)
# print("LED2 HIGH")
# else:
# GPIO.output(led2, GPIO.LOW)
# print("LED2 LOW")
@blynk.on("V1")
def v1_write_handler(value):
global servo
if int(value[0]) != 0:
print("Starting servo movement sequence...")
servo.mid()
time.sleep(5)
servo.min()
time.sleep(5)
servo.max()
time.sleep(5)
servo.mid()
time.sleep(5)
print("Servo movement sequence completed.")
else:
print("No action required for virtual pin V1.")
@blynk.on("connected")
def blynk_connected():
global weight_grams, adjusted_weight
print("RPI is connected to Blynk")
calibrate_scale() # Initiate calibration after Blynk connection
try:
while True:
# Read water sensor
water_level = GPIO.input(water_sensor_pin)
blynk.virtual_write(2, water_level)
time.sleep(1)
blynk.run()
except KeyboardInterrupt:
print("Code stopped by user.")
finally:
GPIO.cleanup()
print("Clean up")