#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "password";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "username";
char password[] = "password";
char clientID[] = "clientID";
unsigned long lastMillis = 0;
int led=LOW;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(4, INPUT); // button
pinMode(5, OUTPUT); // LED or relay
}
void loop() {
Cayenne.loop();
if (millis() - lastMillis > 100) {
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
if(digitalRead(4)==HIGH && led==LOW)
{
digitalWrite(5, HIGH);
led=HIGH;
Cayenne.virtualWrite(1,"1");
while(digitalRead(4)==HIGH)
{
delay(10);
}
}
if(digitalRead(4)==HIGH && led==HIGH)
{
digitalWrite(5, LOW);
led=LOW;
Cayenne.virtualWrite(1,"0");
while(digitalRead(4)==HIGH)
{
delay(10);
}
}
}
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
if (request.channel = 1)
{
String value;
value = (String)getValue.asString();
if (value == "1")
{
digitalWrite(5, HIGH);
led=HIGH;
}
else
{
digitalWrite(5, LOW);
led=LOW;
}
}
}
Comments