RanjithkumarpvAdarsh BU
Created June 15, 2016

Smart IoT Based Agriculture System

IoT, Sensor, Actuators, Router, Blynk Board, Wireless, Pumping system, Database, Gateway and 802.15.4

550
Smart IoT Based Agriculture System

Things used in this project

Hardware components

SparkFun Blynk Board - ESP8266
SparkFun Blynk Board - ESP8266
Smart Device to control sensosrs data and actuators remotely
×1
Temperature Sensor
Temperature Sensor
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×5
LED (generic)
LED (generic)
×2
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1

Story

Read more

Schematics

Design Block Diagram

Complete Design Block Diagram

Code

Code

C/C++
BasicDraft ccode Copy
#include "DHT.h"

int SOIL_DIGITAL_PIN=2;
int SOIL_ANALOG_PIN=A0;  

int LIGHT_ANALOG_PIN=A1;

int DHT_PIN=4;

#define DHT_TYPE DHT11   // DHT 11 

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(9600);
 
  pinMode(SOIL_DIGITAL_PIN, INPUT);
 
  dht.begin();
}

void loop() {

    Serial.print("/soil/humidity ");
    Serial.print(map(analogRead(SOIL_ANALOG_PIN),200,1023,100,0));
//    Serial.print("/soil/alert ");
//    Serial.println(digitalRead(SOIL_DIGITAL_PIN));
    Serial.println();

  delay(1000);

  int lightSensorValue = analogRead(LIGHT_ANALOG_PIN);
  Serial.print("/light/intensity ");
  Serial.print(map(lightSensorValue,0,1023,10,0));
  Serial.println();

  delay(1000);

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("/error DHT");
  } else {
    Serial.print("/air/humidity ");
    Serial.print(humidity);
    Serial.println();
    Serial.print("/air/temperature ");
    Serial.print(temperature);
    Serial.println();
    Serial.print("/air/dewpoint ");
    Serial.print(dewPoint(temperature, humidity));
    Serial.println();
  }
  
  delay(1000); 
}


// from http://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor


/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

Python script on host (with Mosquitto)

Term 1
BROKER=test.mosquitto.org
mosquitto_sub -h $BROKER -t "sensor/#" -v 
Term 2
BROKER=test.mosquitto.org
python agrisensor.py $BROKER



import serial
import mosquitto
import os
import sys

broker=sys.argv[1]
#broker = "10.0.1.3"
#broker = "test.mosquitto.org"
port = 1883

# serialdev = '/dev/ttyACM0'
serialdev = '/dev/tty.usbserial-A400DNT0'

# for instance, an IPv6 address
sensorid="123456789"

topicPrefix="sensor/" + sensorid

#MQTT callbacks

def on_connect(mosq, obj, rc):
    if rc == 0:
        print("Connected successfully.")
    else:
        raise Exception


def on_disconnect(mosq, obj, rc):
    print("Disconnected successfully.")


def on_publish(mosq, obj, mid):
    print("Message "+str(mid)+" published.")


def on_subscribe(mosq, obj, mid, qos_list):
    print("Subscribe with mid "+str(mid)+" received.")


def on_unsubscribe(mosq, obj, mid):
    print("Unsubscribe with mid "+str(mid)+" received.")


def on_message(mosq, obj, msg):
    print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload)


#called on exit
#close serial, disconnect MQTT
def cleanup():
    print "Ending and cleaning up"
    ser.close()
    mqttc.disconnect()

try:
    print "Connecting... ", serialdev
    #connect to serial port
    ser = serial.Serial(serialdev, 9600, timeout=20)
except:
    print "Failed to connect serial"
    #unable to continue with no serial input
    raise SystemExit


try:
    ser.flushInput()
    #create an mqtt client
    mypid = os.getpid()
    client_uniq = "arduino_pub_"+str(mypid)
    mqttc = mosquitto.Mosquitto(client_uniq)

    #attach MQTT callbacks
    mqttc.on_connect = on_connect
    mqttc.on_disconnect = on_disconnect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    mqttc.on_unsubscribe = on_unsubscribe
    #mqttc.on_message = on_message

    #connect to broker
    mqttc.connect(broker, port, 60)

    #remain connected to broker
    #read data from serial and publish
    while mqttc.loop() == 0:
        line = ser.readline()
        #split line as it contains topic and value
        list = line.split(" ")
        #second list element is value

        topic = list[0].rstrip()
        if topic.startswith('/'):        
            value = list[1].rstrip()
            print(topic + " is " + value)
            mqttc.publish(topicPrefix + topic, value)
            
        pass


# handle list index error (i.e. assume no data received)
except (IndexError):
    print "No data received within serial timeout period"
    cleanup()
# handle app closure
except (KeyboardInterrupt):
    print "Interrupt received"
    cleanup()
except (RuntimeError):
    print "uh-oh! time to die"
    cleanup()

Credits

Ranjithkumarpv

Ranjithkumarpv

2 projects • 0 followers
good in IoT, I do projects on modern technology for example LORA,Co_app
Adarsh BU

Adarsh BU

3 projects • 2 followers
Currently working at Exilant Technologies, Bengaluru. • Previously working on CyPhys+ project in IISc.

Comments