Go Down

Topic: need to get data from sensor every 30 minutes (Read 213 times) previous topic - next topic


Good day,

I need help running my code. Im trying out an automatic watering idea with humidity temp and lumens. When water goes below 60% it automatically will irrigate the pot. My codes a mess but I got it to work. What i need to do now is to read the data from sensors every 30 minutes or 1 hour and upload it via rpi. Any idea how can I do this. So technically my main concern is on the data to be read after every 30 minutes. here is my code

Code: [Select]
 * Sensors - Setup and Test - LCD incorporated
 *       DHT-22 sensor (pin D4) to read temperature and humidity values
 *       LDR Sensor (Pin A0) used as luminosity Sensor ==> 0% full dark to 100% full light
 *       SoilSensor (Pin A1) used to read soil Moisture - Humidity 
 *   ==> I2C LCD 4/20 for local monitoring (SDA ==> NANO Pin A4  SCL ==> NANO Pin A5)
 * MJRoBot 21Aug16

// Include DHT Library
#include <DHT.h>

// Including Libraries for I2C LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Sensor definitions
#define DHTPIN A3         // DHT data pin connected to Arduino pin 5
#define DHTTYPE DHT11    // DHT 22 (if your sensor is the DHT 11, only change this line by: #define DHTTYPE DHT11)
#define LDR_PIN A1        // used for Luminosity (LDR) Sensor Input
#define SOIL_MOIST_PIN A0 // used for Soil Moisture Sensor Input
#define SMS_VCC 7 // used for Soil Moisture Sensor "Power Supply" (VCC)
#define SMS_GND 6 // used for Soil Moisture Sensor "GND"
#define MOISTURE_SENSOR_INPUT_A  0//analog input of the moisture sensor
#define MOISTURE_SENSOR_INPUT_D  4//digital input of the moisture sensor(useless right now)

#define CALIB_POT_INPUT_A 0//Potentiometer(10k) input(analog)
#define WATER_SUPPLY_OUTPUT 5 // to turn ON the water pump(To TIP122)

#define DEFAULT_MAX_WATER_LEVEL 0.6 //default to fill upto
#define MOISTURE_REFRESH_INTERVAL 1 //moisture refresh interval for waterPlant() in seconds

#define DEBUG 1 //comment out during final compilation
// Variables to be used by Sensor
int tempDHT;
int humDHT;
int lumen;
int soilMoist;

// Initialize the DHT sensor

// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 20 chars and 4 line display (use I2C scan to confirm address)

double getPotInputInPercent(); // to return the CALIB_POT input in percent
double getMoistSensorInputInPercent(); //to return the Moisture Sensor input in percent
void waterPlant(double toPercent); // to water the plant to the given moisture level

int period = 1000;
unsigned long time_now = 0;

void setup()
  Serial.println("Sensor readings"); 
  lcd.begin ();//set the pump OFF by default

  pinMode(MOISTURE_SENSOR_INPUT_D,INPUT); //set pins as inputs and outputs
void loop()

  double req_moisture_level = getPotInputInPercent();  //the value set on POT
  double current_moisture = getMoistSensorInputInPercent();//get current moisture value

  if(current_moisture < req_moisture_level )
     { //if the moisture level falls below the minimum required
    waterPlant(req_moisture_level); //water the plants

void waterPlant(double toPercent = DEFAULT_MAX_WATER_LEVEL)
  digitalWrite(WATER_SUPPLY_OUTPUT,HIGH); //turn the pump ON
  double d = getMoistSensorInputInPercent();
  while(d < toPercent)
  { //wait till the the moisture level reach the required minimum
   d = getMoistSensorInputInPercent();
  digitalWrite(WATER_SUPPLY_OUTPUT,LOW); //turn pump OFF
double getPotInputInPercent()
  return analogRead(CALIB_POT_INPUT_A)/1023.0;

double getMoistSensorInputInPercent()
  return 1 - analogRead(MOISTURE_SENSOR_INPUT_A)/1023.0;
  }//the moisture sensor provides max val at minimum moisture level

* Read data from Sensors
void readSensors(void)
  tempDHT = dht.readTemperature();   //Read temperature and humidity values from DHT sensor:
  humDHT = dht.readHumidity();
  lumen = getLumen(LDR_PIN);
  soilMoist = getSoilMoist();

* Capture luminosity data: 0% full dark to 100% full light
int getLumen(int anaPin)
  int anaValue = 0;
  for(int i = 0; i < 10; i++) // read sensor 10X ang get the average
    anaValue += analogRead(anaPin);   
  anaValue = anaValue/10; //Light under 300; Dark over 800
  anaValue = map(anaValue, 1023, 0, 0, 100); //LDRDark:0  ==> light 100%

  return anaValue; 

* Capture soil Moisture data
int getSoilMoist()
  int anaValue = 0;
  for(int i = 0; i < 10; i++) // read sensor 10X ang get the average
    digitalWrite(SMS_VCC,LOW);   // drive a current through the divider in one direction
    delay(1000);   // wait a moment for capacitance effects to settle
    anaValue += analogRead(SOIL_MOIST_PIN);

    digitalWrite(SMS_VCC,HIGH);   // reverse the current
    delay(1000);   // give as much time in 'reverse' as in 'forward'
    digitalWrite(SMS_VCC,LOW);   // stop the current
  anaValue = anaValue/10;
  anaValue = map(anaValue, 1023, 0, 0, 100); //xxx:0  ==> yyy 100%
  return anaValue;

* Showing capured data at Serial Monitor
void printData(void)
  Serial.print("   Temp DHT ==> ");
  Serial.print("oC  Hum DHT ==> ");
  Serial.print("%  Luminosity ==> ");
  Serial.print("%  Soil Moisture ==> ");

* Showing capured data at LCD
void showDataLCD(void)
  lcd.setCursor (0,0);
  lcd.print("  Control  Station");
  lcd.setCursor (0,1);
  lcd.print("                    ");
  lcd.setCursor (0,2);
  lcd.print("Temp: ");
  lcd.print("oC  Hum: ");
  lcd.print("%  ");
  lcd.setCursor (0,3);
  lcd.print("Ligh: ");
  lcd.print("  Soil: ");
  lcd.setCursor (0,0);


Hey curious to know if you ever found out how to do this? I am interested in the same exact thing!


Jun 12, 2019, 05:02 pm Last Edit: Jun 12, 2019, 05:03 pm by jremington
One method:
Code: [Select]
void loop() {


That'll do, but a much better solution can be found in Blink Without Delay.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up