Herramientas de usuario

Herramientas del sitio


proyectos:monitorclima_humedad_suelo

Tabla de Contenidos

Materiales

Diagrama

Codigo

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
 
#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
//Estructura para guardar estados de led y bomba
struct LED {
  int pin;        // Pin en arduino
  boolean isON;   // Esta encedido? 
  boolean state;  // Estado LOW/HIGH
  int sw;
  boolean swstate;
  boolean swoldstate;
  boolean pressed;
};
 
 
const int AirValue = 590;   // Reemplazar este valor por lectura de sensor humerdad en el aire
const int WaterValue = 350;  //Reemplazar este valor por lectura de sensor humerdad en agua
int MoistSoilValue = 0;
int MoistSoilPercent=0;
struct LED hSuelo={3,false,HIGH,2,LOW,LOW,false}; //Sensor de humerdad de suelo HIGH for NO (Relay Normally Open)
 
// Refresh time every N miliseconds
unsigned long timeNow = 0;
unsigned long timePrev = 0;
unsigned int timeWait = 5000;
 
 
void setup() {
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
 
  //Initialize Soil Pump
  pinMode(hSuelo.pin, OUTPUT);
  digitalWrite(hSuelo.pin, hSuelo.state);
 
  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }
  oled.clearDisplay();
  oled.display();
  delay(2000); // wait for initializing
}
 
void loop() {
 
  timeNow = millis(); 
 
  // Si han pasado al menos timeWait(5000=5 Segundos) desde la ultima lectura del sensor
  if (timeNow-timePrev >= timeWait )    
  {   
    timePrev = timeNow;   
 
    //Lee valores sensor de humedad de suelo 
    MoistSoilValue = analogRead(A0);  //Read soil sensor
    Serial.println(MoistSoilValue);
    MoistSoilPercent = map(MoistSoilValue, AirValue, WaterValue, 0, 100);
    (MoistSoilPercent > 100) && (MoistSoilPercent=100);
    (MoistSoilPercent < 0) && (MoistSoilPercent=0);
 
    Serial.print(MoistSoilPercent);
    Serial.println("%");
 
    refreshOLED(MoistSoilPercent);
 
  }//End if (timeNow-timePrev >= timeWait )   
}
 
//Funcion para actualizar pantalla OLED
void refreshOLED( int lSoilPercent ){
    oled.clearDisplay();
 
    oled.setCursor(0,0);  //  X,Y
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    oled.print("Hum. Suelo:   Bomba:");// 21 char with fontsize 1 and 20 with font 2 
    oled.setCursor(0,20);  //  X,Y
    oled.setTextSize(2);
    oled.setTextColor(WHITE);
    oled.print(" ");
    oled.print(lSoilPercent);
    oled.print("%  ");
    if (lSoilPercent < 60) { 
      hSuelo.state=LOW; hSuelo.isON=true;
      oled.print("ON");
    }else{
      hSuelo.state=HIGH; hSuelo.isON=false;
      oled.print("OFF");
    }
 
   digitalWrite(hSuelo.pin, hSuelo.state);
   oled.display();
}

Referencias

proyectos/monitorclima_humedad_suelo.txt · Última modificación: por manuel.floresv