import machine
import ssd1306
import urandom
import utime
# OLED display settings
WIDTH = 128
HEIGHT = 64
I2C_SDA_PIN = 21 # Replace with your SDA pin number
I2C_SCL_PIN = 22 # Replace with your SCL pin number
I2C_ADDRESS = 0x3C # Replace with your OLED display's I2C address
# Initialize I2C
i2c = machine.I2C(-1, machine.Pin(I2C_SDA_PIN), machine.Pin(I2C_SCL_PIN))
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=I2C_ADDRESS)
# Relay pins
relay_1 = machine.Pin(12, machine.Pin.OUT) # Replace with the actual pins
relay_2 = machine.Pin(13, machine.Pin.OUT)
relay_3 = machine.Pin(14, machine.Pin.OUT)
# Thresholds
nitrogenThreshold = 100.0 # Adjust as needed
phosphorousThreshold = 100.0 # Adjust as needed
potassiumThreshold = 100.0 # Adjust as needed
def main():
# Clear the display
oled.fill(0)
oled.show()
while True:
# Your nutrient calculations here
calculatedNitrogen = calculateNitrogen()
calculatedPhosphorous = calculatePhosphorous()
calculatedPotassium = calculatePotassium()
# Check nitrogen threshold
if calculatedNitrogen > nitrogenThreshold:
relay_1.on()
print("Relay_1 turned ON") # For debugging
utime.sleep_ms(urandom.randint(5000, 10000))
else:
relay_1.off()
print("Relay_1 turned OFF") # For debugging
utime.sleep_ms(urandom.randint(3000, 7000))
# Check phosphorous threshold
if calculatedPhosphorous > phosphorousThreshold:
relay_2.on()
print("Relay_2 turned ON") # For debugging
utime.sleep_ms(urandom.randint(5000, 10000))
else:
relay_2.off()
print("Relay_2 turned OFF") # For debugging
utime.sleep_ms(urandom.randint(3000, 7000))
# Check potassium threshold
if calculatedPotassium > potassiumThreshold:
relay_3.on()
print("Relay_3 turned ON") # For debugging
utime.sleep_ms(urandom.randint(5000, 10000))
else:
relay_3.off()
print("Relay_3 turned OFF") # For debugging
utime.sleep_ms(urandom.randint(3000, 7000))
# Update the OLED display
oled.fill(0)
oled.text("N: {}".format(calculatedNitrogen), 0, 0)
oled.text("P: {}".format(calculatedPhosphorous), 0, 20)
oled.text("K: {}".format(calculatedPotassium), 0, 40)
oled.show()
def calculateNitrogen():
# Simulate random nitrogen values between 0 and 200 mg/plant for testing
return urandom.randint(0, 200)
def calculatePhosphorous():
# Simulate random phosphorous values between 0 and 200 mg/plant for testing
return urandom.randint(0, 200)
def calculatePotassium():
# Simulate random potassium values between 0 and 200 mg/plant for testing
return urandom.randint(0, 200)
if __name__ == "__main__":
main()