Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "CayenneWiFi.h"
#include "CayenneWiFiClient.h"
#include "WiFiServer.h"
#include "WiFiUdp.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <PLDuino.h>
#include <PLDTouch.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
/*
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[] = "xxxxxxxxx";
// 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[] = "rv9th9dgvs";
void setup()
{
PLDuino::init();
PLDuino::enableLCD();
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_YELLOW);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(4);
tft.println("ESP02 ERROR");
// RELAYS
// set digital pin to output
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
Serial.begin(9600);
Wire.begin();
Cayenne.begin(token, ssid, password);
// 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");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment