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
}