|
| 1 | +// This example shows how to connect to Cayenne using an Arduino/Genuino MKR1000 and send/receive sample data. |
| 2 | + |
| 3 | +//#define CAYENNE_DEBUG |
| 4 | +#define CAYENNE_PRINT Serial |
| 5 | +#include <CayenneMQTTMKR1000.h> |
| 6 | + |
| 7 | +// WiFi network info. |
| 8 | +char ssid[] = "ssid"; |
| 9 | +char wifiPassword[] = "wifiPassword"; |
| 10 | + |
| 11 | +// Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
| 12 | +char username[] = "MQTT_USERNAME"; |
| 13 | +char password[] = "MQTT_PASSWORD"; |
| 14 | +char clientID[] = "CLIENT_ID"; |
| 15 | + |
| 16 | +unsigned long lastMillis = 0; |
| 17 | + |
| 18 | +void setup() { |
| 19 | + Serial.begin(9600); |
| 20 | + Cayenne.begin(username, password, clientID, ssid, wifiPassword); |
| 21 | +} |
| 22 | + |
| 23 | +void loop() { |
| 24 | + Cayenne.loop(); |
| 25 | + |
| 26 | + //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval. |
| 27 | + if (millis() - lastMillis > 10000) { |
| 28 | + lastMillis = millis(); |
| 29 | + //Write data to Cayenne here. This example just sends the current uptime in milliseconds. |
| 30 | + Cayenne.virtualWrite(0, lastMillis); |
| 31 | + //Some examples of other functions you can use to send data. |
| 32 | + //Cayenne.celsiusWrite(1, 22.0); |
| 33 | + //Cayenne.luxWrite(2, 700); |
| 34 | + //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +//Default function for processing actuator commands from the Cayenne Dashboard. |
| 39 | +//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands. |
| 40 | +CAYENNE_IN_DEFAULT() |
| 41 | +{ |
| 42 | + CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString()); |
| 43 | + //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message"); |
| 44 | +} |
0 commit comments