So here is the final solution I came up with:
// JJD 2/21/18 Working IR sensor added manually (not using Cayennne wizard)
// jeremydeprisco.com
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
//Light stuff
#define lightPin A0
#define VIRTUAL_PIN V1 //
// WiFi network info.
char ssid = âYOURNETWORKâ;
char wifiPassword = âYOURPWâ;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = âGet from Cayenneâ;
char password = âGet from Cayenneâ;
char clientID = âGet from Cayenneâ;
int lightBright = 0; // for IR
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop() {
Cayenne.loop();
getLightData();
displayData();
//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);
Cayenne.virtualWrite(3, lightBright);
}
}
// * Get Light Sensor data
void getLightData(void)
{
lightBright = 0;
delay (500);
int N = 3;
for(int i = 0; i < N; i++) // read sensor âNâ times and get the average
{
lightBright += analogRead(lightPin);
delay(150);
}
//digitalWrite (lightPinVcc, LOW);
lightBright = lightBright/N;
lightBright = map(lightBright, 380, 0, 0, 100); // not sure what this does yet
}
// * Display data at Serial Monitor
void displayData(void)
{
if (lightBright > 0) {
Serial.print(" Dark: â);
Serial.println(lightBright);
}
else {
Serial.print(â Light: ");
Serial.println(lightBright);
}
}
