Sistema de riego iot — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by jmmorenoa1.

Components

Sketch code

// =====================================================
// SISTEMA DE RIEGO IoT
// Universidad INCCA de Colombia
// =====================================================


// -----------------------------------------------------
// Pines
// -----------------------------------------------------

const int SENSOR_HUMEDAD = A0;
const int SENSOR_LUZ = A1;

const int NIVEL_BAJO = 2;
const int NIVEL_ALTO = 3;


// -----------------------------------------------------
// Temporización
// -----------------------------------------------------

unsigned long ultimaTransmision = 0;

const unsigned long PERIODO_TX = 500;


// =====================================================
// Promedio ADC
// =====================================================

int leerPromedioADC(int pin) {

  long suma = 0;

  for (int i = 0; i < 10; i++) {

    suma += analogRead(pin);

    delay(5);
  }

  return suma / 10;
}


// =====================================================
// SETUP
// =====================================================

void setup() {

  // UART hardware del Arduino
  // D1 = TX
  // D0 = RX

  Serial.begin(9600);


  pinMode(
    NIVEL_BAJO,
    INPUT
  );

  pinMode(
    NIVEL_ALTO,
    INPUT
  );
}


// =====================================================
// LOOP
// =====================================================

void loop() {

  unsigned long ahora =
    millis();


  if (
    ahora - ultimaTransmision
      >= PERIODO_TX
  ) {

    ultimaTransmision =
      ahora;


    // ================================================
    // 1. LECTURA ADC
    // ================================================

    int adcHumedad =
      leerPromedioADC(
        SENSOR_HUMEDAD
      );

    int adcLuz =
      leerPromedioADC(
        SENSOR_LUZ
      );


    // ================================================
    // 2. CONVERSIÓN A PORCENTAJE
    //
    // Arduino UNO ADC = 10 bits
    // 0 - 1023
    // ================================================

    float humedad =

      adcHumedad *
      (100.0 / 675.0);


    float luz =

      adcLuz *
      (100.0 / 675.0);

      humedad = constrain(humedad, 0.0, 100.0);

      luz = constrain(luz, 0.0, 100.0);

    // ================================================
    // 3. NIVELES
    // ================================================

    int nivelBajo =
      digitalRead(
        NIVEL_BAJO
      );

    int nivelAlto =
      digitalRead(
        NIVEL_ALTO
      );


    // ================================================
    // 4. TRAMA UART
    // ================================================

    Serial.print("<H:");

    Serial.print(
      humedad,
      1
    );


    Serial.print(",L:");

    Serial.print(
      luz,
      1
    );


    Serial.print(",NB:");

    Serial.print(
      nivelBajo
    );


    Serial.print(",NA:");

    Serial.print(
      nivelAlto
    );


    Serial.println(">");
  }
}