Error al leer sensores en Wemos Mega

Estoy intentando leer dos sensores, de humedad y movimiento, en una placa Wemos Mega a través de wifi, ESP6288. En principio, el programa compila y lee los valores, pero me dan siempre los mismos, en ambos sensores. No se que estoy haciendo mal. Me urge para el trabajo final de un máster. Adjunto imagen del montaje y el código en arduino.


/****************************************/
/*               LIBRERÍAS              */
/****************************************/
#include <ESP8266WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <stdio.h>


/****************************************/
/*      CONEXIÓN WIFI Y THINGSPEAK      */
/****************************************/
const char* ssid = "XXXXXXX";// this constant is defined in my credentials file
const char* password = "XXXXXXXX";// ditto
int keyIndex = 0;            // your network key Index number (needed only for WEP)

WiFiClient  client;

char* topic = "channels/XXXXXX/publish/XXXXXXXXXXXXXXXX"; //channels/<channelID>/publish/API
char* server = "mqtt.thingspeak.com";

unsigned long myChannelNumber = XXXXXXXX;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXX";

int number = 0;


/****************************************/
/*    CONSTANTES SENSOR DE HUMEDAD      */
/****************************************/
const int AirValue = 550;  //you need to replace this value with Value_1
const int WaterValue = 250;  //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent = 0;


/****************************************/
/*     CONSTANTES SENSOR MOVIMIENTO     */
/****************************************/
int pinSensorMov = 22;
int estadoSensorMov = LOW;
int valor = 0;
int alertaMov = 0;

/****************************************/
/*            FUNCIÓN SETUP             */
/****************************************/
void setup() {
  
  Serial.begin(115200);  // Initialize serial

  while (!Serial) {
   Serial.print("?"); ; // wait for serial port to connect. Needed for native USB port only
  }
  
  WiFi.mode(WIFI_STA); 
  ThingSpeak.begin(client);  // Initialize ThingSpeak

  pinMode(pinSensorMov, INPUT);
  delay(20000);

}

/****************************************/
/*             FUNCIÓN LOOP             */
/****************************************/
void loop() {
  
  /* Conexión Wifi */
  if(WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while(WiFi.status() != WL_CONNECTED) {
      WiFi.begin(ssid, password);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }

  /* Sensor Humedad Suelo */
  soilMoistureValue = analogRead(A0);  //poner el sensor en el suelo
 
  Serial.print("Valor humedad del suelo: ");
  Serial.println(soilMoistureValue);
  
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
   
  if(soilmoisturepercent >= 100) {
    Serial.print("Porcentaje de humedad: ");
    Serial.println("100 %");
    soilMoistureValue = 100;
    soilmoisturepercent = 100;
  }
  else if(soilmoisturepercent <=0) {
    Serial.print("Porcentaje de humedad: ");
    Serial.println("0 %");
    soilMoistureValue = 0;
    soilmoisturepercent = 0;
  }
  else if(soilmoisturepercent > 0 && soilmoisturepercent < 100) {
    Serial.print("Porcentaje de humedad: ");
    Serial.print(soilmoisturepercent);
    Serial.println("%");
  }

  /* Sensor Movimiento */
  valor = digitalRead(pinSensorMov);
  Serial.println(valor);
  
  if (valor == HIGH){
    alertaMov = 1;
    if (estadoSensorMov == LOW){
      Serial.println("Sensor activado");
      estadoSensorMov = HIGH;
      
    }
  }
  else{
    alertaMov = 0;
    if (estadoSensorMov == HIGH){
      Serial.println("Sensor parado");
      estadoSensorMov = LOW;
      
    }
  }

  /* Envío de datos a thingspeak */
  ThingSpeak.setField(1, soilmoisturepercent);
  ThingSpeak.setField(3, alertaMov);
  
  
  //Escribir to ThingSpeak.
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  
  if(x == 200){
    Serial.println("Channel update successful. X");
  }
  else{
    Serial.println("Problem updating channel X. HTTP error code " + String(x));
  }

  delay(20000); // Wait 20 seconds to update the channel again
}

Su publicacion se MUEVE a su ubicacion actual ya que es mas adecuada.

Estas seguro que los valores de humedad de suelo varian entre 500 y 250 y que 500 es tu 0 y 250 tu 100%. Me parece raro.

He probado un montón de rangos, por internet vi que eran entre 620 y 310, vamos que he puesto de 10 a 100 y de 10 a 1000. Y acabo de invertir los valores como me has dicho, que creo que tienes razón, y me sigue saliendo lo mismo. Vamos, lo mismo si tengo seco el sensor como si lo meto en un vaso de agua. Lo curioso es que en el otro sensor ocurre lo mismo, siempre da 0, aunque le ponga la mano delante durante media hora.
Captura de pantalla 2021-10-30 a las 10.50.03

Lo primero es hacer que tu sensor funcione porque no creo que lo haga bien.
Asi que olvidemos el envío de datos a Thingspeak.

Dime que pasa con el sensor de suelo cuando usas esto.
Ponlo en el sustrato húmedo y luego retíralo para que te de en contraste dos lecturas diferentes.
Sería ideal tener 3 situaciones. Una con sustrato (tierra) muy húmeda, una regular y otra seca.


/****************************************/
/*    CONSTANTES SENSOR DE HUMEDAD      */
/****************************************/
const int AirValue      = 550;   //you need to replace this value with Value_1
const int WaterValue    = 250; //you need to replace this value with Value_2
int soilMoistureValue   = 0;
int soilmoisturepercent = 0;

/****************************************/
/*     CONSTANTES SENSOR MOVIMIENTO     */
/****************************************/
int pinSensorMov    = 22;
int estadoSensorMov = LOW;
int valor, valorAnt = 0;
int alertaMov       = 0;

/****************************************/
/*            FUNCIÓN SETUP             */
/****************************************/
void setup(){

  Serial.begin(115200); // Initialize serial

  pinMode(pinSensorMov, INPUT);
  delay(2000);
}

/****************************************/
/*             FUNCIÓN LOOP             */
/****************************************/
void loop() {

  /* Sensor Humedad Suelo */
  soilMoistureValue = analogRead(A0); //poner el sensor en el suelo

  Serial.print("Valor humedad del suelo: ");
  Serial.println(soilMoistureValue);

  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  Serial.print("Porcentaje de humedad: ");
  Serial.print(soilmoisturepercent);
  Serial.println("%");

  /* Sensor Movimiento */
  valor = digitalRead(pinSensorMov);
  Serial.println(valor);

  if (valor != valorAnt)   {
      if (!valor)     {
          Serial.println("Sensor activado");
          alertaMov = 0;
      } else {
          Serial.println("Sensor parado");
          alertaMov = 1;
      }
  }
  valorAnt = valor;

  delay(10000); // Espero 10 seguntos para proxima lectura
}

En el caso del sensor de movimiento he simplificado tu código. Usas demasiadas cosas reiterativas. Incluso esa variable alertaMov esta de mas porque valor ya es tu variable alerta.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.