Skip to content
Wish Lists Cart
0 items

#25 Smart Greenhouse IoT Project With PicoBricks

24 Oct 2023
#25 Smart Greenhouse IoT Project With PicoBricks

In this project, we will prepare a simple glass smarthouse with IOT technology and PicoBricks. We will use PicoBricks with the ESP8266 wifi module in this greenhouse. In this way, we will turn the plant-house into an object that we can track over the Internet.

Table of Contents

    Hello Maker! We will make a Smart greenhouse project today! You can check every detail here! Please get contact us when you make your own!

    The rapid changes in climate due to the effect of global warming cause a decrease in productivity in agricultural activities. In the 1500s, Daniel Barbaro built the first known greenhouse in history. Plant-houses are suitable environments for growing plants that can provide controllable air, water, heat and light conditions.

    In greenhouses, heaters are used to balance the heat, electric water motors for irrigation, fans are used to regulate humidity and to provide pollination. With the development of technology, the producer can follow the status of the greenhouse with his phone from anywhere and can do the work that needs to be done. The general name of this technology is Internet of Things (IOT)

    Special sensors are used to measure temperature, humidity and oxygen content in plant-house. In addition, special sensors measuring soil moisture are used to decide on irrigation. Electronically controlled drip irrigation systems are used to increase irrigation efficiency.

    Details and Algorithm

    The glasshouse model you will prepare will include a soil moisture sensor, and a DHT11 temperature and humidity sensor hanging from the top. A submersible pump will be placed in the water tank outside the model, and the hose coming out of the end of the pump will go to the ground in the greenhouse. Picoboard will be placed in a suitable place outside the greenhouse model.

    When Picobricks starts, it starts to broadcast wifi thanks to the ESP8266 wifi module. When we enter the IP address of Esp8266 from the smart phone connected to the same network, we encounter the web page where we will control the Greenhouse. Here we can see the temperature and humidity values. If we wish, we can start the irrigation process by giving the irrigation command.

    Components for Smart Greenhouse Project

    1X Pump
    1X Soil Humidity Sensor
    1X ESP8266 Wifi Module
    PicoBricks Smart Greenhouse Kit
    Jumper Cables
    Easy Connection Cables

    Wiring Diagram

    You can code and run Picobricks’ modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

    Step by Step of the Project

    1-Detach the floor of the model from the SR-2 coded part in the Greenhouse kit.

    2- Attach the pieces in the middle of the SR-3 piece to the floor of the glasshouse.

    3- Remove the inner walls of the greenhouse from the SR-4 part and attach it to the ground.

    4- Remove the arches in SR-1 and SR-3 and place them on the plant-house floor.

    5-Cover the rectangular area where soil will be placed with cling film. After irrigation, you will protect the model parts. Pour the plant soil into the greenhouse. Fill so that there is no empty space.

    6- Insert the parts of the SR-4 into the notches on the greenhouse.

    7-Thread the remaining two thin flat pieces of SR-4 through the holes on both sides of the greenhouse from the underside. This process makes the area more robust.

    Wiring Diagram

    You can code and run Picobricks’ modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

    MicroBlocks Codes of the PicoBricks


    Microblocks Run Tab

    MicroPython Codes of the PicoBricks

    
    import time
    import network
    import socket
    from machine import Pin, ADC
    from picobricks import SSD1306_I2C, DHT11
    from time import sleep
    WIDTH = 128
    HEIGHT = 64
    sda=machine.Pin(4)
    scl=machine.Pin(5)
    i2c=machine.I2C(0,sda=sda, scl=scl, freq=1000000)
    oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
    
    motor_1 = Pin(21, Pin.OUT)
    motor_2 = Pin(22, Pin.OUT)
    
    smo_sensor=ADC(27)
    dht_sensor = DHT11(Pin(11))
    dht_read_time = time.time() # Defined a variable to keep last DHT11 read time
    
    #Connect to Wifi
    ssid = "WiFi ID"
    password = "WiFi Password"
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    max_wait = 10
    status = wlan.ifconfig()
    
    oled.text("Power On",30,0)
    oled.text("Waiting for ",20, 30)
    oled.text("Connection",23, 40)
    oled.show()
    
    while max_wait > 0:
        if wlan.status() < 0 or wlan.status() >= 3:
            break
        max_wait -=1
        print("waiting for connection...")
        time.sleep(1)
        
    if wlan.status() !=3:
        print('network connection failed. Please Check ID and PASSWORD')
    else:
        print('connected')
        status = wlan.ifconfig()
        print( 'ip = ' + status[0] )
    
    
    oled.fill(0)
    
    
    html = """<!DOCTYPE html><html>
    <head><meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="data:,">
    <style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
    .buttonBlue { background-color: #0000FF; border: 2px solid #000000;; color: white; padding: 20px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }
    .buttonOrange { background-color: #FFA500; border: 2px solid #000000;; color: Black; padding: 20px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }
    text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
    </style></head>
    <body><center><h1>Smart Green House</h1></center><br><br>
    <form><center>
    <center> <button class="buttonBlue" name="watering" value="watering" type="submit">WATERING</button>
    <br><br>
    <center> <button class="buttonOrange" name="check" value="status" type="submit">Check Status</button>
    </form>
    <br><br>
    <br><br>
    <p>%s<p></body></html>
    """
    html2 = """<!DOCTYPE html><html>
    <head><meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="data:,">
    <body><center></center>
    <form></form>
    
    <p>%s<p></body></html>
    """
    
    # Open socket
    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(1)
    print('listening on', addr)
    oled.text("IP",50, 0)
    oled.text(str(status[0]),20, 20)
    oled.text("Connected",25, 40)
    oled.show()
    # Listen for connections, serve client
    tempexp = str()
    humexp = str()
    soilexp = str()
    while True:
        
        if time.time() - dht_read_time >= 3:
            dht_read_time = time.time()
            try:
                dht_sensor.measure()
            except Exception as e:
                pass
        try:       
            cl, addr = s.accept()
            print('client connected from', addr)
            request = cl.recv(1024)
            print("request:")
            print(request)
            request = str(request)
            watering = request.find('watering')
            checkstt = request.find('check')
            
            print( 'watering = ' + str(watering))
            print( 'checkstt = ' + str(checkstt))
            
            if watering == 8: # Sulama
                print("watering")
                motor_1.high()
                motor_2.high()
                sleep(1)
                motor_1.low()
                motor_2.low()
                if watering == 8:
                    dhtstt = "Watering for 1 sec..."
            else: # Info
                smo=round((smo_sensor.read_u16()/65535)*100)
                temp=dht_sensor.temperature
                hum=dht_sensor.humidity
                dhtstt = "VALUES"
                soilstt = "Soil Sensor Value: "
                soilexp = str(smo) + "%"
                humexp = "Huminity: " +  str(hum) + "% "
                tempexp = "Temperature: "+ str(temp) + "% "
                       
            # Create and send response
            stateis2 = tempexp + humexp + " Soil: " + soilexp
            stateis = dhtstt
            response2 = html2 % stateis2
            response = html % stateis
            cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
            cl.send(response)
            cl.send(response2)
            cl.close()
            
        except OSError as e:
            cl.close()
            print('connection closed')
    

    Arduino C Codes of the PicoBricks

    
    #include <DHT.h>
    #define RX 0
    #define TX 1
    
    #define LIMIT_TEMPERATURE     30
    #define DHTPIN                11
    #define DHTTYPE               DHT11
    #define smo_sensor            27
    #define motor                 22
    #define DEBUG true
    
    DHT dht(DHTPIN, DHTTYPE);
    int connectionId;
    
    void setup() {
      Serial1.begin(115200);
      dht.begin();
      pinMode(smo_sensor, INPUT);
      pinMode(motor, OUTPUT);
    
      sendData("AT+RST\r\n", 2000, DEBUG); // reset module
      sendData("AT+GMR\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+CIPSERVER=0\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+RST\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+RESTORE\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point
      sendData("AT+CWJAP=\"WIFI_ID\",\"WIFI_PASSWORD\"\r\n", 5000, DEBUG); // ADD YOUR OWN WIFI ID AND PASSWORD
      delay(3000);
      sendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
      delay(3000);
      sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
      delay(1000);
      sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80
      delay(1000);
    }
    
    void loop() {
      if (Serial1.find("+IPD,")) {
        delay(300);
        connectionId = Serial1.read() - 48;
        String serialIncoming = Serial1.readStringUntil('\r');
        Serial.print("SERIAL_INCOMING:");
        Serial.println(serialIncoming);
    
        if (serialIncoming.indexOf("/WATERING") > 0) {
          Serial.println("Irrigation Start");
          digitalWrite(motor, HIGH);
          delay(1000); // 10 sec.
          digitalWrite(motor, LOW);
          Serial.println("Irrigation Finished");
          Serial.println("! Incoming connection - sending WATERING webpage");
          String html = "";
          html += "<html>";
          html += "<body><center><H1>Irrigation Complete.<br/></H1></center>";
          html += "</body></html>";
          espsend(html);
        }
        if (serialIncoming.indexOf("/SERA") > 0) {
          delay(300);
    
          float smo = analogRead(smo_sensor);
          float smopercent = (460-smo)*100.0/115.0 ; //min ve max değerleri değişken.
          Serial.print("SMO: %");
          Serial.println(smo);
    
          float temperature = dht.readTemperature();
          Serial.print("Temp: ");
          Serial.println(temperature);
    
          float humidity = dht.readHumidity();
          Serial.print("Hum: ");
          Serial.println(humidity);
          
          Serial.println("! Incoming connection - sending SERA webpage");
          String html = "";
          html += "<html>";
          html += "<body><center><H1>TEMPERATURE<br/></H1></center>";
          html += "<center><H2>";
          html += (String)temperature;
          html += " C<br/></H2></center>";
    
          html += "<body><center><H1>HUMIDITY<br/></H1></center>";
          html += "<center><H2>";
          html += (String)humidity;
          html += "%<br/></H2></center>";  
          
          html += "<body><center><H1>SMO<br/></H1></center>";
          html += "<center><H2>";
          html += (String)smopercent;
          html += "%<br/></H2></center>";  
              
          html += "</body></html>";
          espsend(html);
        }
        else
          Serial.println("! Incoming connection - sending MAIN webpage");
        String html = "";
        html += "<html>";
        html += "<body><center><H1>CONNECTED.<br/></H1></center>";
        html += "<center><a href='/SERA'><h4>INFO:Get Sensor Data</a></br><a href='/WATERING'>WATERING:Run Water Pump</a></h4></center>";
        html += "</body></html>";
        espsend(html);
        String closeCommand = "AT+CIPCLOSE=";  ////////////////close the socket connection////esp command
        closeCommand += connectionId; // append connection id
        closeCommand += "\r\n";
        sendData(closeCommand, 3000, DEBUG);
    
      }
    
    }
    //////////////////////////////sends data from ESP to webpage///////////////////////////
    
    void espsend(String d)
    {
      String cipSend = " AT+CIPSEND=";
      cipSend += connectionId;
      cipSend += ",";
      cipSend += d.length();
      cipSend += "\r\n";
      sendData(cipSend, 1000, DEBUG);
      sendData(d, 1000, DEBUG);
    }
    
    //////////////gets the data from esp and displays in serial monitor///////////////////////
    
    String sendData(String command, const int timeout, boolean debug)
    {
      String response = "";
      Serial1.print(command);
      long int time = millis();
      while ( (time + timeout) > millis())
      {
        while (Serial1.available())
        {
          char c = Serial1.read(); // read the next character.
          response += c;
        }
      }
    
      if (debug)
      {
        Serial.print(response); //displays the esp response messages in arduino Serial monitor
      }
      return response;
    }
    
    Project GITHUB Page
    Prev Post
    Next Post

    Thanks for subscribing!

    This email has been registered!

    Shop the look
    Choose Options

    Edit Option

    Have Questions?

    Back In Stock Notification

    Compare

    Product SKURatingDescription Collection Availability Product Type Other Details
    this is just a warning
    Login
    Shopping Cart
    0 items
    Same Day Shipping No Extra Costs
    Easy Returns Guarantee Return with Ease
    Secure Checkout Secure Payment