Now all my values are OK but I still have two problems
1 °) I am obliged to double my code
int x = analogRead (A0);
int y = map (x, 1023, 0, 0, 100);
for this to appear in the console and in the cayenne interface.
2 °) The data go back in cayenne I see them in data but I have no creation of Widjet.
Finally, how to mount 2 other soil moisture sensors on the one hand mounting but also in the code?
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT22
#define DHTPIN 4
#define Pin A0
#define VIRTUAL_PIN V1
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
//Variables for DHT11 values
float h, t, hif;
//Time to sleep - 10 minutes - delete a zero to set to 1 minute
int SleepTime = 60000000;
//Values sent/error booleans
bool CayenneSent = false;
bool ReadError = false;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Setup");
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
CayenneSent = false;
ReadError = true;
}
void loop() {
//Run Cayenne Functions
Cayenne.loop();
//Only check values if there is a ReadError
if (ReadError){
ReadError = false;
//Check if read failed and try until success
int WhileLoops = 0;
do {
WhileLoops++;
//We can't go too long without calling Cayenne.loop() so exit this loop after 2 seconds and set an error flag
if (WhileLoops >= 4){
Serial.println("Sensor Read Error");
ReadError = true;
break;
}
//Read temperature as Celsius
t = dht.readTemperature();
//Read humidity (percent)
h = dht.readHumidity();
//Read temperature as Celsius
t = dht.readTemperature();
//Calculate Heat Index as Celsius
hif = dht.computeHeatIndex(t, h);
delay(500);
} while (isnan(t) || isnan(h));
Serial.print("Humidity: ");
Serial.println(h);
Serial.print("Temperature (c): ");
Serial.println(t);
Serial.print("Heat Index (c): ");
Serial.println(hif);
//Read soil moisture
int x = analogRead(A0);
int y = map(x, 1023, 0 , 0, 100);
Serial.println(x);
Serial.print("Hygrométrie ");
Serial.println(y);
}
else {
//Check if we sent all values and sleep for 10 minutes if we did
if (CayenneSent){
Serial.println("Sent all values - sleeping");
delay(100);
ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);
delay(100);
}
}
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
//Check if there was a read error and skip sending values if there is
if (!ReadError){
//Send Values
Cayenne.virtualWrite(0, h, "rel_hum", "p");
Cayenne.virtualWrite(1, t, "temp", "c");
Cayenne.virtualWrite(2, hif, "temp", "c");
int x = analogRead(A0);
int y = map(x, 1023, 0 , 0, 100);
Cayenne.virtualWrite(V1,y);
delay(60000);
//Set CayenneSent to true so we know when to sleep
CayenneSent = true;
}
}