#include #include "CayenneDefines.h" #include #include #include #include char token[] = "token"; const int tmp102Address = 0x48; TMP102 tmpSensor(tmp102Address); const int address = TSL2561_ADDR_FLOAT; Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345); float voltage0; float voltage1; float voltage2; #define VOLTAGE0 V0 #define VOLTAGE1 V1 #define VOLTAGE2 V2 #define LIGHT_PIN V3 #define TEMPERATURE_PIN V4 #define BUTTON V5 #define RELAY_DIGITAL_PIN 4 void setup() { Serial.begin(9600); Wire.begin(); Cayenne.begin(token); if (!tsl.begin()) { CAYENNE_LOG("No TSL2561 detected"); while (1); } 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 */ pinMode(RELAY_DIGITAL_PIN, OUTPUT); } void loop() { Cayenne.run(); int analogValue0 = analogRead(A0); int analogValue1 = analogRead(A1); int analogValue2 = analogRead(A2); int a = 12; float b = 0.00483398437; // 5V/1024=b voltage0 = analogValue0 * a * b; voltage1 = analogValue1 * a * b; voltage2 = analogValue2 * a * b; delay(500); } CAYENNE_OUT(VOLTAGE0) { Cayenne.virtualWrite(VOLTAGE0, voltage0); } CAYENNE_OUT(VOLTAGE1) { Cayenne.virtualWrite(VOLTAGE1, voltage1); } CAYENNE_OUT(VOLTAGE2) { Cayenne.virtualWrite(VOLTAGE2, voltage2); } CAYENNE_OUT(LIGHT_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(LIGHT_PIN, event.light); } else { /* If event.light = 0 lux the sensor is probably saturated and no reliable data could be generated! */ CAYENNE_LOG("Sensor overload"); } } CAYENNE_OUT(TEMPERATURE_PIN) { // This command writes the temperature in Celsius to the Virtual Pin. //Cayenne.celsiusWrite(VIRTUAL_PIN, tmpSensor.getCelsius()); // To send the temperature in Fahrenheit or Kelvin use the corresponding code below. Cayenne.fahrenheitWrite(TEMPERATURE_PIN, tmpSensor.getFahrenheit()); //Cayenne.kelvinWrite(VIRTUAL_PIN, tmpSensor.getKelvin()); } CAYENNE_IN(BUTTON) { // get value sent from dashboard int currentValue = getValue.asInt(); // 0 to 1 // assuming you wire your relay as normally open if (currentValue == 0) { digitalWrite(RELAY_DIGITAL_PIN, HIGH); } else { digitalWrite(RELAY_DIGITAL_PIN, LOW); } }