Infineon Team
Published © MIT

Smart Greenhouse powered by MicroPython

You want the perfect environment for your plants to grow? Here is the solution!

AdvancedFull instructions providedOver 2 days1,246
Smart Greenhouse powered by MicroPython

Things used in this project

Hardware components

CY8CPROTO-062-4343W
Infineon CY8CPROTO-062-4343W
×1
High-Side-Switch Shield with PROFET +2 12V BTS700x-1EPP
Infineon High-Side-Switch Shield with PROFET +2 12V BTS700x-1EPP
×1
TLS4120 5V CORE-BOARD
Infineon TLS4120 5V CORE-BOARD
×1
EVAL TDM3883 3.3VOUT
Infineon EVAL TDM3883 3.3VOUT
×1
XENSIV™ PAS CO2 Shield2Go Board
Infineon XENSIV™ PAS CO2 Shield2Go Board
×1
Grove - I2C High Accuracy Temp&Humi Sensor (SHT35)
Seeed Studio Grove - I2C High Accuracy Temp&Humi Sensor (SHT35)
×1
KIT XMC LED DALI 20 RGB
Infineon KIT XMC LED DALI 20 RGB
×1
Adafruit Stemma Moisture
×1
IKEA ÅKERBÄR Greenhouse
×2
Waterpump
×1
Hosepipe
×1
12V PC Cooling fans
×2
Stripboard
×1
12V/2A Power Adapter
×1
RGB-LED Stripe 12V
×1
Heating film
×1

Software apps and online services

MicroPython
MicroPython

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)
Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Buck Converter Housing TLS412033V COREBOARD TOBO1

Plexiglas Window of the Greenhouse

PSOC6 Place Holder left side

PSOC6 Place Holder right side

Water Tank

Profet

I2C Bus Housing

Buck Converter Housing EVAL_TDM3883_3.3Vout

Schematics

Wiring overview

Code

Data Manager

MicroPython
import time
from machine import Pin, I2C, RTC
from sht3x import SHT3X
import stemma_soil_sensor
import seesaw
import pasco2 as sensor
import led_lighting_shield

class DataManager:
    
    def ambient_measurement(self, co2Obj):
        '''this method gets all the measurable ambient values,
        packs the into a tupel (ambient values) and returns that tupel as the following:
        
        ambient_values[0] = co2ppm
        ambient_values[1] = soil_moisture
        ambient_values[2] = soil_temperature
        ambient_values[3] = humidity
        ambient_values[4] = air_temp
        '''
        #get CO2 Value
        co2ppm = co2_read_sensor_data(co2Obj)
        #get soil moisture and temperature
        soil_moisture = seesaw.get_moisture()
        soil_temperature = seesaw.get_temp()
        #get humidity and air temperature
        sht_value = sht.get_measurement()
        humidity = sht_value["humidity"]
        air_temp = sht_value["temp_celsius"]
        #pack the tupel and return ambient values 
        ambient_values = (co2ppm, soil_moisture, soil_temperature, humidity, air_temp)
        return (ambient_values)

    def daylight_control():
        '''Gets the current time, interprets the hour, and switches the lights based on the time of day'''
        thistime = rtc.now()  # Returns tuple with year, month, day, hour, minute, second, millis
        thishour = thistime[4]  # Fifth element of rtc tuple is the current hour (0-23)
        global dayflag  # Initialize dayflag
        global daylight # Initialize daylight

        # Interpret the current time and set or delete the dayflag
        if (thishour > 7) and (thishour < 19):  # Using logical AND instead of bitwise AND
            dayflag = 1
        else:
            dayflag = 0
            
        # Switch the light on and off based on the time of day
        if (dayflag == 1) and (daylight == 0):
            board.light_on()
            daylight = 1
        elif (dayflag == 1) and (daylight == 1):
            daylight = 1
        elif (dayflag == 0) and (daylight == 1):
            board.light_off()
            daylight = 0
        else:
            daylight = 0
            
    def co2_read_sensor_data(co2Obj):
        ''' Helper function to read sensor data and stream to cloud DB
            Args :   co2Obj - Sensor Object
            Returns: Co2 Value in Parts per million as an int
        '''
        # wait for the value to be ready
        time.sleep(10)
        #get co2 values
        co2ppm = co2Obj.get_co2_value()
        if co2ppm == -1:
            print("[co2-module] : Measurement not ready yet")
        else:
            print("[co2-module] : co2 ppm value: ", co2ppm)
            return co2ppm

Init

MicroPython
import time
from machine import Pin, I2C, RTC

class Init:
    
    def init_I2C():
        i2c = I2C(0, scl='P6_0', sda='P6_1', freq = 400000) # Correct I2C pins for PSOC6
        return i2c
    

    #Fans connected to P9_5
    def init_fans():
        fan = Pin('P9_5')
        fan.init(Pin.OUT, Pin.PULL_UP)
        fan.low()
        time.sleep_ms(100)
        return fan
    #Pump connected to P8_5
    def init_pump():
        pump = Pin('P8_5')
        pump.init(Pin.OUT, Pin.PULL_DOWN)
        pump.low()
        time.sleep_ms(100)
        return pump
    #Heating connected to P9_3
    def init_heating():
        heat = Pin('P9_3')
        heat.init(Pin.OUT, Pin.PULL_UP)
        heat.low()
        time.sleep_ms(100)
        return heat
    
    def co2_sensor_init():
        ''' Helper function to intialize sensor
            Args: None
            Returns: co2 object if success, else -1
        '''
        bus = i2c
        pasco2 = sensor.PASCO2(bus)
        init_status = pasco2.initialize()
        if init_status != 0:
            print("[co2-module] : Sensor setup failed!")
            return -1
        else:
            print("[co2-module] : Sensor setup done successfully!")
            return pasco2

RTC Manager

MicroPython
import time
from machine import RTC

class RTCManager:
    def get_pc_time(self):
        '''Gets the current date time of the laptop and converts it to a matching tuple for the RTC-Module of PSOC6.
        Returns this tuple in the correct order (year, month, mday, weekday, hour, minute, second, millis).'''
        today = time.localtime()
        year = today[0] + 1900
        month = today[1] + 1
        mday = today[2]
        hour = today[3]
        minute = today[4]
        second = today[5]
        weekday = today[6]
        return (year, month, mday, weekday, hour, minute, second, 0)

    def init_rtc(self, pc_time):
        '''Initializes the RTC module of the PSOC6 board with the current date time of the laptop.
        Returns RTC object.'''
        rtc = RTC()
        rtc.init(pc_time)  # Initialize RTC with specific date and time
        return rtc

Main

MicroPython
import time
from machine import Pin, I2C, RTC
from sht3x import SHT3X
import stemma_soil_sensor
import seesaw
import pasco2 as sensor
import led_lighting_shield
from rtc_manager import RTCManager
from init import Init

#init RTC 
current_time = RTCManager.get_pc_time()
RTCManager.init_rtc(current_time)

#init I2C, generate I2C object as well as sht35 and stemma soil sensor! 
i2c = Init.init_I2C()
sht = sht3x.SHT35(i2c)
seesaw = stemma_soil_sensor.StemmaSoilSensor(i2c)

#init LED Shield
led = led_lighting_shield.LEDLightingShield(i2c)

#init Pins
fan = Init.init_fans()
pump = Init.init_pump()
heat = Init.init_heat()

#init pasco2, generate co2-Object
pasco2 = Init.co2_sensor_init()
time.sleep(10)

#set threshold values
target_minsoil_moisture_threshold = 1000
target_max_humidity = 70
target_maxair_temperature = 30
target_minair_temperature = 12
target_minsoil_temperature = 10
target_co2 = 600

dbObj = database_connect()


def __main__():
    DataManager.daylight_control()
    ambient_values = DataManager.ambient_measurement(pasco2)
    stream_sensor_data_to_cloud(ambient_values, dbObj)
    
    if (ambient_values[0]<target_co2) | (ambient_values[3]>target_max_humidity) | (ambient_values[4]>target_maxair_temperature) :
        fan.high()
    else
        fan.low()
    #soil moisture and watering system 
    if ambient_values[1]<target_minsoil_moisture_threshold:
        pump.high()
    else
        pump.low()
    #soil temperature and air temperature
    if (ambient_values[2]<target_minsoil_temperature) |(ambient_values[4] < target_minair_temperature) :
        heat.high()
    else
        heat.low()
    time.sleep(2)
    
if name == "__main__":
    main()

    
    
    

Credits

Infineon Team

Infineon Team

75 projects • 116 followers

Comments