"""
IoT for poultry Production
r
Date 01/01/2024
version: 1.0
*********Sensors********************
OLED display
DHT22 temperature and humidity sensor
Magnetic door sensor
ds18x20.py --> temperature sensor
****** Actuator ********************
4 channel relay
**** PIN configuration *****
OLED_SDA-> Pin(0)
OLED_SCL-> Pin(1)
"""
# import your library
from machine import Pin, I2C
import time
from ssd1306 import SSD1306_I2C
import onewire
import ds18x20
import dht
# connect to Wi-Fi
# connect to MQTT
# configure GPIO pins OLED
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# display the following info on the OLED
oled.fill(0) # Clear the OLED display
oled.text(" IoT Poultry", 0, 0)
oled.text(" Supervised by:",5, 10)
oled.text("Dr. O. Elijah ",5, 20)
oled.text(" Designed by:", 7, 30)
oled.text("Aliyu, Faruk, Iliyasu ", 5, 40)
oled.text("Ahmad, Abdullahi", 5, 50)
oled.show()
time.sleep(5) # Sleep for 5 seconds
oled.fill(0) # Clear the display
oled.show()
# configure GPIO pins for the 4 channel Relay
bulb_1 = Pin(2, Pin.OUT) # control bulb 1
bulb_2 = Pin(3, Pin.OUT) # control bulb 2
water_pump = Pin(4, Pin.OUT) # control water pump 3
Feeder = Pin(5, Pin.OUT) # control feeder
# configure GPIO for electronic lock
door_lock = Pin(6, Pin.OUT)
# configure GPIO for electronic contact sensor
contact_sensor = (7, Pin.OUT)
# configure GPIO for DHT22
dht_sensor = dht.DHT22(Pin(14))
# configure GPIO pin for temperature sensor
temp_pin = Pin(7)
# Create a OneWire object and a DS18X20 object
ow = onewire.OneWire(temp_pin)
temp_sensor = ds18x20.DS18X20(ow)
# configure GPIO pin for pH sensor
# initialize paramters
oled_msg = " IoT Poutlry"
# function to read temperature sensor
def read_temperature():
try:
dht_sensor.measure() # it reads actual voltage values
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print('Temperture: %3.2f C' %temp)
print('Humidity: %3.2f %%' %hum)
return [temp, hum]
except OSError as e:
print('Failed to read sensor')
return " "
# function to on relay
def on_relay(pin):
pin.value(1)
# function to off relay
def off_relay(pin):
pin.value(0)
# function display reading on OLED
def display_default(temp, humidity):
oled_msg = " IoT Fish Pond"
oled.text(f"Temp_value: {temp}",0,10 )
oled.text(f"Temp_value: {humidity}",0,20 )
oled.show()
while True:
oled_msg = " IoT Poultry"
temp, humidity = read_temperature()
display_default(temp,humidity )