Converting Cayenne Arduino LIbrary sketches to Cayenne MQTT Arduino

Example sketch conversion

Following is an Cayenne Arduino sketch which drives a TSL2561 luminosity sensor, a DS18B20 temperature sensor, and a LED associated with a Cayenne button actuator widget.

Original Arduino Library Sketch:

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// If you're not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
#include <CayenneEthernet.h>

// Virtual Pin of the TSL2561 widget.
#define VIRTUAL_PIN V1
// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PINT V8

// Address used to read from the TSL2561. This is determined by the ADDR pin on the TSL2561.
// If ADDR is unconnected it means the sensor will use TSL2561_ADDR_FLOAT (0x39) for the address.  See the TSL2561 datasheet for more info.
const int address = TSL2561_ADDR_FLOAT;

//Physical pin of the DS18B20 sensor
const int tmpPin = 7;

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "YOUR_CAYENNE_TOKEN";

void setup()
{
 Serial.begin(9600);
 Cayenne.begin(token);
 if (!tsl.begin())
 {
    CAYENNE_LOG("No TSL2561 detected");
 }

 tsl.enableAutoRange(true);
 /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
 tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
 // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
 // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
 sensors.begin();
}

void loop()
{
 Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin for the TSL2561 Sensor
CAYENNE_OUT(VIRTUAL_PIN)
{
 // Send the command to get luminosity.
 sensors_event_t event;
 tsl.getEvent(&event);

 if (event.light)
 {
    // Send the value to Cayenne in lux.
    Cayenne.luxWrite(VIRTUAL_PIN, event.light);
 }
 else
 {
    /* If event.light = 0 lux the sensor is probably saturated
    and no reliable data could be generated! */
    CAYENNE_LOG("No sensor data");
 }
}

// This function is called when the Cayenne widget requests data for the Virtual Pin for the DS18B20 Sensor
CAYENNE_OUT(VIRTUAL_PINT)
{
 // Send the command to get temperatures.
 sensors.requestTemperatures();
 // This command writes the temperature in Celsius to the Virtual Pin.
 Cayenne.celsiusWrite(VIRTUAL_PINT, sensors.getTempCByIndex(0));
 // To send the temperature in Fahrenheit use the corresponding code below.
 //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

Cayenne MQTT Arduino LIbrary sketch:

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "YOUR_MQTT_USERNAME";
char password[] = "YOUR_MQTT_PASSWORD";
char clientID[] = "YOUR_MQTT_CLIENT_ID";

unsigned long lastMillis = 0;

// Virtual Pin of the TSL2561 widget.
const int VIRTUAL_PIN = 1;
// Virtual Pin of the DS18B20 widget.
const int VIRTUAL_PINT = 8;

// Address used to read from the TSL2561. This is determined by the ADDR pin on the TSL2561.
// If ADDR is unconnected it means the sensor will use TSL2561_ADDR_FLOAT (0x39) for the address.  See the TSL2561 datasheet for more info.
const int address = TSL2561_ADDR_FLOAT;

//Physical pin of the DS18B20 sensor
const int tmpPin = 7;

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

void setup() {
   Serial.begin(9600);
   Cayenne.begin(username, password, clientID);
 if (!tsl.begin())
 {
    CAYENNE_LOG("No TSL2561 detected");
 }

 tsl.enableAutoRange(true);

 tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);     
 sensors.begin();
}

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);
    tslfunc(VIRTUAL_PIN);
    ds18b20func(VIRTUAL_PINT);
       //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);
   }
}

//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");
}

void ds18b20func(int VIRTUAL_PINT)
{
 // Send the command to get temperatures.
 sensors.requestTemperatures();
 // This command writes the temperature in Celsius to the Virtual Pin.
 Cayenne.celsiusWrite(VIRTUAL_PINT, sensors.getTempCByIndex(0));
 // To send the temperature in Fahrenheit use the corresponding code below.
 //Cayenne.fahrenheitWrite(VIRTUAL_PINT, sensors.getTempFByIndex(0));
}

// This function is called when the Cayenne widget requests data for the Virtual Pin for the TSL2561 Sensor
void tslfunc(int VIRTUAL_PIN)
{
 // Send the command to get luminosity.
 sensors_event_t event;
 tsl.getEvent(&event);

 if (event.light)
 {
    // Send the value to Cayenne in lux.
    Cayenne.luxWrite(VIRTUAL_PIN, event.light);
 }
 else
 {
    CAYENNE_LOG("No sensor data");
 }
}
4 Likes