Saturday, 8 July 2017

Moisture control via Thingspeak and Virtuino

Hello Guys,

Last summer i had to control the moisture of the soil of my KIWI farm, KIWIs are very sensitive on moisture. 



So i started by buying the parts that i needed as :

A solar battery charger 

http://www.ebay.com/itm/10A-20A-30A-12V-24V-Solar-Panel-Charger-Controller-Battery-Regulator-USB-LCD-New/222544619442?ssPageName=STRK%3AMEBIDX%3AIT&var=521389261689&_trksid=p2057872.m2749.l2649


An arduino nano 

http://www.ebay.com/itm/Mini-USB-Nano-V3-0-ATmega328P-CH340G-FT232-5V-16M-Micro-controller-Board-Arduino/172440100565?ssPageName=STRK%3AMEBIDX%3AIT&var=471281026540&_trksid=p2057872.m2749.l2649


A GPS module 

http://www.ebay.com/itm/Wireless-SIM900-850-900-1800-1900-MHz-Extension-Module-GSM-GPRS-Board-Antenna/262880453797?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

4 sensors moisture 

http://www.ebay.com/itm/Soil-Hygrometer-Humidity-Detection-Module-Moisture-Water-Sensor-Fr-Arduino-/382075296681?epid=847232274&hash=item58f5765ba9:g:LKUAAOSwrhBZD9n6

A terminal for Arduino nano 

http://www.ebay.com/itm/Nano-Terminal-Adapter-for-the-Arduino-Nano-V3-0-AVR-ATMEGA328P-AU-Module-Board-N/391792372956?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

A DC2DC converter 

http://www.ebay.com/itm/Buck-Step-down-LM2596-Power-Converter-Module-DC-4-0-40-to-1-3-37V-LED-Voltmeter/161476280982?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

A Battery of 7AmpH

Two plastic boxes - watertight

Wire connectors
http://www.ebay.com/itm/Car-Waterproof-Electrical-Wire-Connector-Plug-Terminals-HID-1-2-3-4-5-6-Way-Pin/261420505061?ssPageName=STRK%3AMEBIDX%3AIT&var=560309998216&_trksid=p2055119.m1438.l2649

Solar panel

http://www.ebay.com/itm/18V-10W-Solar-Charger-Panel-External-Portable-Battery-Car-W-Crocodile-Clips-/262485873542?hash=item3d1d609386:g:K1oAAOSwMNxXYjzu


Here is how i install the mositure sensors in the field 




Next step was to add the parts in two small boxes ( i could not find a big one ) 
and here is the connections



The Power unit that includes the charger and the Battery



The CPU unit , Arduino and the moisture adapters



The panel and the wire to the sensors






Here is the software 



#include <SoftwareSerial.h>
#include <String.h>

SoftwareSerial GPRS1(3, 4);


float Humi10cm = 0.0;  // first sensor at 10 cm depth
float Humi10cm2 = 0.0; // Second sensor at 10 cm depth
float Humi20cm = 0.0;  // Thirth sensor at 20 cm depth
float Humi20cm2 = 0.0;  // Forth sensor at 20 cm depth
float Power;                     //Battery level






float Humi10 = 0.0;
float Humi10c = 0.0;
float Humi20 = 0.0;
float Humi20c = 0.0;
float Pow;

long Repeat = 900000;
long TempTime;

int x = 0;
long previous;
int8_t answer;

void setup()
{
  GPRS1.begin(9600);
  Serial.begin(9600);
}


void loop()
{
  if (GPRS1.available())
    Serial.write(GPRS1.read());
  {
    Pow = analogRead (0) ; //Sets analog pin 1 as input
    Power = Pow / 41.3 ; //Val1 divided by 4.092
    Serial . println (Power) ; //Prints to serial monitor window
  }
   {
    Humi10 = analogRead (1) ; //Sets analog pin 1 as input
    Humi10cm = Humi10 * 0.097 ; //Val1 divided by 0.097
    Serial . println (Humi10cm) ; //Prints to serial monitor window
  }
    {
    Humi10c = analogRead (2) ; //Sets analog pin 1 as input
   Humi10cm2 = Humi10c * 0.097 ; //Val1 divided by 0.097
    Serial . println (Humi10cm2) ; //Prints to serial monitor window
  }
    {
    Humi20 = analogRead (3) ; //Sets analog pin 1 as input
    Humi20cm = Humi20 * 0.097 ; //Val1 divided by 0.097
    Serial . println (Humi20cm) ; //Prints to serial monitor window
  }
    {
    Humi20c = analogRead (4) ; //Sets analog pin 1 as input
    Humi20cm2 = Humi20c * 0.097 ; //Val1 divided by 0.097
    Serial . println (Humi20cm2) ; //Prints to serial monitor window
  }


  if (millis() - TempTime >= Repeat ) {     //How often to send data
 
    Senddata();

    TempTime = millis();
  }


  delay(5000);


}

void Senddata()
{
  GPRS1.println("AT");
  delay(1000);
  GPRS1.println("AT+CPIN?");
  delay(1000);
  GPRS1.println("AT+CREG?");
  delay(1000);
  GPRS1.println("AT+CGATT?");
  delay(1000);
  GPRS1.println("AT+CIPSHUT");
  delay(1000);
  GPRS1.println("AT+CIPSTATUS");
  delay(2000);
  GPRS1.println("AT+CIPMUX=0");
  delay(2000);
  ShowSerialData();
  GPRS1.println("AT+CSTT=\"internet\"");//start task and setting the APN,
  delay(1000);
  ShowSerialData();
  GPRS1.println("AT+CIICR");//bring up wireless connection
  delay(3000);
  ShowSerialData();
  GPRS1.println("AT+CIFSR");//get local IP adress
  delay(2000);
  ShowSerialData();
  GPRS1.println("AT+CIPSPRT=0");
  delay(3000);
  ShowSerialData();
  GPRS1.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000);
  ShowSerialData();
  GPRS1.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();
  String str = "GET http://api.thingspeak.com/update?api_key=FWJ7P4K4T5IATJ5X&field1=" + String(Humi10cm) + "&field2=" + String(Humi10cm2) + "&field3=" + String(Humi20cm)+ "&field4=" + String(Humi20cm2);
  GPRS1.println(str);//begin send data to remote server
  delay(4000);
  ShowSerialData();
  GPRS1.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet
  GPRS1.println();
  ShowSerialData();
  GPRS1.println("AT+CIPSHUT");//close the connection
  delay(100);
  ShowSerialData();
}

void ShowSerialData()
{
  while (GPRS1.available() != 0)
    Serial.write(GPRS1.read());
}





int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout)
{

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while ( GPRS1.available() > 0) GPRS1.read();   // Clean the input buffer

  GPRS1.println(ATcommand);
  Serial.println(ATcommand);

  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {

    if (GPRS1.available() != 0) {
      if (x == 100)
      {
        strncpy(response, response + 1, 99);
        response[99] = GPRS1.read();
      }
      else
      {
        response[x] = GPRS1.read();
        x++;
      }
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout)
{

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while ( GPRS1.available() > 0) GPRS1.read();   // Clean the input buffer

  GPRS1.println(ATcommand);    // Send the AT command
  Serial.println(ATcommand);

  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {

    if (GPRS1.available() != 0) {
      response[x] = GPRS1.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        answer = 1;
      }
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer2) != NULL)
      {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}