kindr wrote: it would be possible to send an example? I am only a beginner for which I apologize
Try this. Code was not tested, but you'll get the idea.
#include <Esp8266EasyIoT.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define HUMIDITY_SENSOR_DIGITAL_PIN 2
#define RELAY_1 13 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
Esp8266EasyIoT esp;
SoftwareSerial serialEsp(10, 11);
DHT dht;
float lastTemp;
float lastHum;
Esp8266EasyIoTMsg msgHum(CHILD_ID_HUM, V_HUM);
Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP);
void setup()
{
serialEsp.begin(9600);
Serial.begin(115200);
Serial.println("EasyIoTEsp init");
esp.begin(incomingMessage, 3, &serialEsp, &Serial);
//esp.begin(NULL, &serialEsp);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
pinMode(13, OUTPUT);
// Serial.println("present S_HUM");
esp.present(CHILD_ID_HUM, S_HUM);
// Serial.println("present S_TEMP");
esp.present(CHILD_ID_TEMP, S_TEMP);
pinMode(RELAY_1, OUTPUT);
esp.present(3, S_DIGITAL_OUTPUT);
}
void loop()
{
while(!esp.process());
delay(dht.getMinimumSamplingPeriod());
while(!esp.process());
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
}
else if (temperature != lastTemp)
{
lastTemp = temperature;
esp.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
}
else if (humidity != lastHum)
{
lastHum = humidity;
esp.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
void incomingMessage(const Esp8266EasyIoTMsg &message) {
// We only expect one type of message from controller. But we better check anyway.
Serial.println("New message");
if (message.type==S_DIGITAL_OUTPUT) {
// Change relay state
digitalWrite(message.sensor-3+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}