THANKS FOR ALL
IT WORKS PERFECTLY! IT CHANGE THE STATE AT SERVER!
Here is my code for everyone who needs! it include 1 relay and one switch at pin 2, with arduino pull up resistor! only have to put gnd with pin 2 and the relay will change. Relay its at pin 6.
void loop()
{
esp.process();
estadoActual = digitalRead(pin);
if (estadoAnterior != estadoActual) // ha habido un cambio de estado
{
contador++; // cuenta los cambios de estado
int validarPar = contador % 2; // solo queremos los cambios pares
if (validarPar != 1) // si el cambio es par
{
digitalWrite(RELAY_1,!digitalRead(RELAY_1));
Esp8266EasyIoTMsg msgRelay1(1, V_DIGITAL_VALUE);
esp.send(msgRelay1.set(!digitalRead(RELAY_1)));
}
estadoAnterior = estadoActual;
}
}
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==V_DIGITAL_VALUE) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
}
}