Help with my sketch

So I slapped your code into my IDE to see if I could make it compile. I think I have it there. There is just a warning about re-defining VIRTUAL_PIN that you may want to resolve, since when it’s defined a 2nd time its going to overwrite whatever you where up to the 1st time.

You can compare this to above to see everything that I changed, I’m struggling with the whole list but off the top of my head:

  • cleaned up some brackets around your CAYENNE_IN statements that were confusing the compiler
  • changed some of the #include statements at the top of the file
  • Cayenne.begin() was missing ssid and password parameters, so I added those

Not 100% if this is going to do exactly what you’re looking for without further tweaking, but it at least compiles now!

#include "CayenneWiFi.h"
#include "CayenneWiFiClient.h"
#include "WiFiServer.h"
#include "WiFiUdp.h"



/*
Cayenne Ethernet Example

This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100
and runs the main communication loop.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
// #include <CayenneEthernet.h>
#define VIRTUAL_PIN 1
#define RELAY_DIGITAL_PIN 4
#include <CayenneTMP102.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Wire.h>
#define VIRTUAL_PIN V2

// Your network name and password.
char ssid[] = "free";
char password[] = "xxxxxxx";

// Address used to read from the TMP102. This is determined by the ADD0 pin on the TMP102.
// Connecting it to ground means the sensor will use 0x48 for the address.  See the TMP102 datasheet for more info.
const int tmp102Address = 0x48;

// 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;

// TEMPURATURE
TMP102 tmpSensor(tmp102Address);
//LUX
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);

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


void setup()
{
   // RELAYS
   // set digital pin to output
  pinMode(RELAY_DIGITAL_PIN, OUTPUT);
  
  Serial.begin(9600);
  Wire.begin();
  
{

  Cayenne.begin(token, ssid, password);   //HERE'S THE RUB//

}                                                                                                                                                                    
  // TSL
  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 */

}


CAYENNE_IN(VIRTUAL_PIN)


// Button Section
{
  // 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);
  }
}









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

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)

// Tempurature
{
 // 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(VIRTUAL_PIN, tmpSensor.getFahrenheit());
  //Cayenne.kelvinWrite(VIRTUAL_PIN, tmpSensor.getKelvin());

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