#define CAYENNE_PRINT Serial //Library needed to connect to the Cayenne. #include // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. char username[] = "MQTTusername"; char password[] = "MQTTpassword"; char clientID[] = "ClientID"; //Variable required for countdown to send data. unsigned long lastMillis = 0; //Previous output state. int led=LOW; void setup() { //Starting communication via serial interface at 9600 baud. Serial.begin(9600); //Starting Cayenne library and start communication with the Cayenne MQTT server. Cayenne.begin(username, password, clientID); //Set pin 4 as input. pinMode(4, INPUT); //Set pin 5 as output. You can then connect relay, LED, motor, ... pinMode(5, OUTPUT); } void loop() { Cayenne.loop(); //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval. if(millis() - lastMillis > 10000) { lastMillis = millis(); //Write data to Cayenne here. This example just sends the current uptime in milliseconds. Cayenne.virtualWrite(0, lastMillis); //Some examples of other functions you can use to send data. //Cayenne.celsiusWrite(1, 22.0); //Cayenne.luxWrite(2, 700); //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER); } if(digitalRead(4)==HIGH && led==LOW) //When you press the button and the LED is OFF, then { digitalWrite(5, HIGH); //Turn ON LED. led=HIGH; //Save that is LED ON. Cayenne.virtualWrite(1,"1"); //Sends the value to Cayenne about that the LED is ON. while(digitalRead(4)==HIGH) { delay(10); } } if(digitalRead(4)==HIGH && led==HIGH) //When you press the button and the LED is ON, then { digitalWrite(5, LOW); //Turn OFF LED. led=LOW; //Save that is LED OFF. Cayenne.virtualWrite(1,"0"); //Sends the value to Cayenne about that the LED is OFF. while(digitalRead(4)==HIGH) { delay(10); } } } //Default function for processing actuator commands from the Cayenne Dashboard. //You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands. CAYENNE_IN_DEFAULT() { CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString()); //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message"); if (request.channel = 1) { //Variables for input values. String value; value = (String)getValue.asString(); //When you press the button on the Cayenne Dashboard, then if (value == "1") { digitalWrite(5, HIGH); //Turn ON LED. led=HIGH; //Save that is LED ON. } else { digitalWrite(5, LOW); //Turn OFF LED. led=LOW; //Save that is LED OFF. } } }