|
| 1 | +/* |
| 2 | +This example shows how to connect to Cayenne using an Arduino MKR1010 and send/receive sample data. |
| 3 | +The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager. |
| 4 | +Steps: |
| 5 | +1. Install the Arduino SAMD Boards from the Arduino IDE Boards Manager if you have not already done so. |
| 6 | +2. Install the WiFiNINA library. |
| 7 | +3. Select the MKR1010 board from the Arduino IDE Tools menu. |
| 8 | +4. Set the Cayenne authentication info to match the authentication info from the Dashboard. |
| 9 | +5. Set the network name and password. |
| 10 | +6. Compile and upload the sketch. |
| 11 | +7. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget. |
| 12 | +*/ |
| 13 | + |
| 14 | +//#define CAYENNE_DEBUG // Uncomment to show debug messages |
| 15 | +#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space |
| 16 | +#include <CayenneMQTTMKR1010.h> |
| 17 | + |
| 18 | +// WiFi network info. |
| 19 | +char ssid[] = "ssid"; |
| 20 | +char wifiPassword[] = "wifiPassword"; |
| 21 | + |
| 22 | +// Cayenne authentication info. This should be obtained from the Cayenne Dashboard. |
| 23 | +char username[] = "MQTT_USERNAME"; |
| 24 | +char password[] = "MQTT_PASSWORD"; |
| 25 | +char clientID[] = "CLIENT_ID"; |
| 26 | + |
| 27 | +void setup() { |
| 28 | + Serial.begin(9600); |
| 29 | + Cayenne.begin(username, password, clientID, ssid, wifiPassword); |
| 30 | +} |
| 31 | + |
| 32 | +void loop() { |
| 33 | + Cayenne.loop(); |
| 34 | +} |
| 35 | + |
| 36 | +// Default function for sending sensor data at intervals to Cayenne. |
| 37 | +// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data. |
| 38 | +CAYENNE_OUT_DEFAULT() |
| 39 | +{ |
| 40 | + // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0. |
| 41 | + Cayenne.virtualWrite(0, millis()); |
| 42 | + // Some examples of other functions you can use to send data. |
| 43 | + //Cayenne.celsiusWrite(1, 22.0); |
| 44 | + //Cayenne.luxWrite(2, 700); |
| 45 | + //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER); |
| 46 | +} |
| 47 | + |
| 48 | +// Default function for processing actuator commands from the Cayenne Dashboard. |
| 49 | +// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands. |
| 50 | +CAYENNE_IN_DEFAULT() |
| 51 | +{ |
| 52 | + CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString()); |
| 53 | + //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message"); |
| 54 | +} |
0 commit comments