---
# System prepended metadata

title: Watering a Plant

---

# Watering a Plant 
an IoT-project 
by Per Björnberg (pbjdk08)
## Description of the project
The idea of the project is to do a automatic watering machine. To be more specific : A microcontroler shall monitor and messure the dryness of the soil where plants grow. There will be a tank working as a resorvar. The tank should be either a bathtube or just a large container. Water should fetched with a pump or just opening a valve in the bottom of the container.It should be possible to motoring how much water is left. There should be possible to check the function from a distance either an email och a webpage with data.Some warning should be sent if the resorvar has a low level. 
## What did I expect to learn. 
The goal for me by this project is to learn how to programming a microcontrolers and different connected sensors and their interaction. Also learn about different communication protocol and how use communitcation between sensors and microcontrolers and a user. In my project I wanted that there where both analog and digital sensors and also some funktion were a motor was used. In my guess of timeconsuption for the project where approx 1-2 week for learning the basic about the microcontroler and the programming of it. One week for sensors and communication interface 
## Hardware - plan and reality. 
In the begining was I planning to use the recommended hardware, microcontroler from Pycom incl sensors, a 5V pump, sensor for messuaring water level, a valve using 11V, ultrasonic sensor and finaly a pressure sensor. All this was bougth from ElectroKit. Luckiel I also bought an IoT-bundel with an Arduino and an extra Arduino MKR 1010 Wifi and a Rasberry PI Zero. Here was I carried away a bit, so my expences totally was round 3000- 3500 sek, I guess.
The Arduino was the microcontroller I used for my project why will I explain later.

## The project
First I must say that Discord an I are not frends. I tried to use it asking questions but I could read but not write in the different forums, but it didn't work for me. Then I realized I have to fix this project on my own. Started out trying to program the Pycom thru VisualStudio IDE which I have used before. I coulden't get in connact with the Pycon. Switch to Atom IDE after 2 days was able to program the Pycom. Next problem was to connect the sensors to the Pysense, this couldn't I solve quickliy if I were able to finnish the project on time. Then I started out with my Arduino MKR 1010 Wifi. This worked out well, connected the waterlevel sensor and could get a different between wet,dry and some thing in between. Next problem,board couldn't give enough "power" to start the motor. Also tried the L239 circuit but no sucess. Next test is to solve by a motorCarrier shield but right now I have a diod lighting up when the motor should start. It was ment to messure the waterlevel but since output from the sensor is upto 5V and the Arduino can maximum take 3.5 V must I transform the analog input to be between 0-3,5V. This is sort of the same problem as I have with my motor, a stable external 5V.  Next challench was to connect to some WiFy here i used a tutorial from Arduinos (https://www.arduino.cc/en/Guide/MKRWiFi1010/connecting-to-wifi-network) that also created a web page ![](https://i.imgur.com/RPs1cDq.jpg) 
I just change this little change in the code to get values from my Arduino.

![](https://i.imgur.com/Z2Dz4H0.jpg)

The IDE I used was the Arduinos IDE 1.8 which works but not so much more. The next problem is that the Web-page doesn't update until I switch the LED. I would like it to update simuntanlesy.


## Finalremarks
A picture of my project. As it is right now
![](https://i.imgur.com/2SGVwmu.jpg)
If I had writen my project in Pyton that should be a simple task to put all the different readings in a list or a vector even a database should be easy but in my application which is more just checking my plants there fore any saved data is not of intrest. The plan for this project is waiting for the motorshield and power supply so I can finnish the funtionallity and then contact some of the free web hosting to upload my webpage to.


# Appendix  The Code

```
#include <WiFiNINA.h>
#include <ArduinoMotorCarrier.h>

char ssid[] = "????";             //  your network SSID (name) between the " "
char pass[] = "????";      // your network password between the " "
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;      //connection status
WiFiServer server(80);            //server socket

WiFiClient client = server.available();

int ledPin = 2;
int motorPin = 5;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  while (!Serial);
  
  enable_WiFi();
  connect_WiFi();

  server.begin();
  printWifiStatus();

}

void loop() {
  client = server.available();

  if (client) {
    printWEB();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");

  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void enable_WiFi() {
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }
}

void connect_WiFi() {
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
}

void printWEB() {

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
           
            //create the buttons
            client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the LED off<br><br>");
            
            int water_level_reading = analogRead(A1); // max value = 1024
            if (water_level_reading > 700) {
              digitalWrite(motorPin,HIGH);
              }
              else{
              digitalWrite(motorPin,LOW);
              }

              
            client.print("<br>Reading water level from analog pin A1: ");
            client.println(water_level_reading);
            int temp_level_reading = analogRead(A6);
            client.print("<br>Reading tempratur level from analog pin A6: ");
            client.println(temp_level_reading); 
           
            
            

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        if (currentLine.endsWith("GET /H")) {
        digitalWrite(ledPin, HIGH);        
        }
        if (currentLine.endsWith("GET /L")) {
        digitalWrite(ledPin, LOW);       
        }

      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

```
