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;
}

Saturday, 27 May 2017

How to set timing on RTC

Lets assume that you bought a RTC for irrigation or any other project and you wish to add the correct day and year, time as well . so here is how to do that

Thats the  RTC unit that you can buy for less than 2 $ from Ebay

  http://www.ebay.com/itm/Small-DS3231-AT24C32-IIC-High-Precision-Real-Time-Clock-Module-Arduino-/282248813661?hash=item41b7571c5d:g:Df8AAOSwHMJYIffZ

The wiring




Here is the code


#include "Wire.h"
#include <LiquidCrystal.h>
#define DS1307_ADDRESS 0x68
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int button_pin = 12;           // YOU ADD HIGH AT THIS PIN TO SETUP THE TIME
int buttonState = 0;

byte set_second = 0;                 //YOU ADD THE CURRENT DAY, TIME ETC
byte set_minute = 06;
byte set_hour = 18;
byte set_day = 7;
byte set_date = 27;
byte set_month = 5;
byte set_year = 17;

byte read_second;
byte read_minute;
byte read_hour;
byte read_day;
byte read_date;
byte read_month;
byte read_year;

byte bcd_to_Dec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}

byte dec_to_bcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(button_pin, INPUT);
  buttonState = digitalRead(button_pin);

  if (buttonState == HIGH) {
    Wire.beginTransmission(DS1307_ADDRESS);
    Wire.write(0);
    Wire.write(dec_to_bcd(set_second));
    Wire.write(dec_to_bcd(set_minute));
    Wire.write(dec_to_bcd(set_hour));
    Wire.write(dec_to_bcd(set_day));
    Wire.write(dec_to_bcd(set_date));
    Wire.write(dec_to_bcd(set_month));
    Wire.write(dec_to_bcd(set_year));
    Wire.endTransmission();
  } else {}

 lcd.begin(16, 2);
 lcd.clear();

}

void loop() {
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);
  read_second = bcd_to_Dec(Wire.read());
  read_minute = bcd_to_Dec(Wire.read());
  read_hour = bcd_to_Dec(Wire.read());
  read_day = bcd_to_Dec(Wire.read());
  read_date = bcd_to_Dec(Wire.read());
  read_month = bcd_to_Dec(Wire.read());
  read_year = bcd_to_Dec(Wire.read());

  lcd.clear();
  Serial.println();

  lcd.setCursor(0, 0);
  if (read_hour < 10)
  {
    lcd.print("0");
    Serial.print ("0");
  }
  lcd.print(read_hour);
  Serial.print (read_hour);
  lcd.print(":");
  Serial.print(":");

  if (read_minute < 10)
  {
    lcd.print("0");
    Serial.print ("0");
  }
  lcd.print(read_minute);
  Serial.print (read_minute);
  lcd.print(":");
  Serial.print(":");


  if (read_second < 10)
  {
    lcd.print("0");
    Serial.print ("0");
  }
  lcd.print(read_second);
  Serial.print (read_second);
  Serial.println();

if (  read_day == 1)
  {
    lcd.print("Sun");
    Serial.print ("Sun");
  }
if (  read_day == 2)
  {
    lcd.print("Mon");
    Serial.print ("Mon");
  }

if (  read_day == 3)
  {
    lcd.print("Tue");
    Serial.print ("Tue");

  }

if (  read_day == 4)
  {
    lcd.print("Wed");
    Serial.print ("Wed");
  }
if (  read_day == 5)
  {
    lcd.print("Thu");
    Serial.print ("Thu");
  }

if (  read_day == 6)
  {
    lcd.print("Fri");
    Serial.print ("Fri");
  }

if (  read_day == 7)
  {
    lcd.print("Sat");
    Serial.print ("Sat");

  }
   
    Serial.println();
  lcd.setCursor(0, 1);
  lcd.print(read_date);
  Serial.print (read_date);
  lcd.print("/");
  Serial.print("/");
  lcd.print(read_month);
  Serial.print (read_month);
  lcd.print("/");
  Serial.print("/");
  lcd.print(read_year);
  Serial.print (read_year);



  delay(1000);
}