Does anyone know the reason that data logging in the DAY view, has data from 24 hours previous up to 4 hours previous, instead of the full 24 hour period? Screenshot_2018-04-21-19-56-28%5B1%5D|281x500
This screenshot has another anomaly that I don’t understand. It is a Value widget monitoring a two state door switch, so why does the data have non-binary values.
NOTE V3 is the binary door state
The current and prev door state are BOOLEAN values
//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
//#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <CayenneMQTTEthernet.h>
#include <EEPROM.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] =
char password[] =
char clientID[] =
// Virtual Pins
#define DoorButton V0 // Virtual pin for Door Button on Dashboard
#define BAROMETER_PIN V1
#define TEMPERATURE_PIN V2
#define DoorState V3 // use Value widget for door state data tracking.
#define DoorOpen V4 // use Two-State widget
#define DoorClosed V5 // use Two-State widget
// Board Pins
#define RELAY_CONTROL_PIN 3 // Door close relay pin
#define DoorStateSensorPin 4 // Door Position sensor
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
boolean bmpSensorDetected = true;
boolean currentDoorState = 0;
boolean prevDoorState = 0;
boolean currentButtonState = 0;
boolean prevButtonState = 0;
// code from Cayenne guru
//int x = 0; // Initialise as door button inactive (same as currentButtonState)
//int y = 0;
unsigned long lastMillis = 0;
unsigned long lastMillis1 = 0;
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
// set digital pin to output
pinMode(RELAY_CONTROL_PIN, OUTPUT);
digitalWrite(RELAY_CONTROL_PIN, HIGH); // set initial relay state to normally open
pinMode(DoorStateSensorPin, INPUT);
// Set up Door State
currentDoorState = digitalRead(DoorStateSensorPin); // read initial door position
prevDoorState = digitalRead(DoorStateSensorPin); // set up previous door state
if (!bmp.begin())
{
CAYENNE_LOG("No BMP sensor detected");
bmpSensorDetected = false;
}
// Initial read door state
Serial.print("Setup: currentDoorState = ");
if(currentDoorState == 0) { // action if door is Open
Serial.println("Open");
Cayenne.virtualWrite(DoorOpen, 1);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 0);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
if(currentDoorState == 1) { // action if door is closed
Serial.println("Closed");
Cayenne.virtualWrite(DoorOpen, 0);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 1);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
// prevDoorState = currentDoorState;
Cayenne.virtualWrite(DoorState, currentDoorState); // immediately send change to Cayenne Dashboard
}
void loop() {
Cayenne.loop(); // Main Cayenne function
// Code from Cayenne guru accesses Dashboard Button state immediately
currentDoorState = digitalRead(DoorStateSensorPin);
// Serial.print("currentDoorState = ");
// Serial.println(currentDoorState);
if ( currentDoorState == 0 && currentButtonState == 1) //door is open AND Door Button is active
{
digitalWrite(RELAY_CONTROL_PIN, currentButtonState);//turn on relay
delay(500); //this delay in your code is very small.
currentButtonState = 0;
digitalWrite(RELAY_CONTROL_PIN, currentButtonState);//turn off relay
if (millis() - lastMillis > 1500) {
lastMillis = millis();
Cayenne.virtualWrite(DoorButton, LOW);//make dashboard button to low
}
}
else //door is closed
{
if (millis() - lastMillis1 > 1500) {
lastMillis1 = millis();
Cayenne.virtualWrite(DoorButton, LOW);//make dashboard button to low
}
}
// Write Button state to microcontroller immediately when state changes AND door is open
// if(currentButtonState != prevButtonState && DoorState == Open) {
// digitalWrite(RELAY_CONTROL_PIN, LOW); // turn on relay
// delay(100);
// digitalWrite(RELAY_CONTROL_PIN, HIGH); // turn off relay 100mS later
// }
currentDoorState = digitalRead(DoorStateSensorPin); // read door state every time thru loop()
if (currentDoorState != prevDoorState) { // only act if door state changes from prev state
Serial.print("Loop: currentDoorState = ");
if(currentDoorState == 0) { // action if door is Open
Serial.println("Open");
Cayenne.virtualWrite(DoorOpen, 1);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 0);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
if(currentDoorState == 1) { // action if door is closed
Serial.println("Closed");
Cayenne.virtualWrite(DoorOpen, 0);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 1);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
prevDoorState = currentDoorState;
Cayenne.virtualWrite(DoorState, currentDoorState); // immediately send change to Cayenne Dashboard
}
}
// Code from Cayenne guru
CAYENNE_IN(DoorButton) // Get current value of Close Door button
{
currentButtonState = getValue.asInt();
Serial.print("currentButtonState = ");
Serial.println(currentButtonState);
prevButtonState = currentButtonState; // copy Door button state to prevButtonState
}
// CAYENNE_IN(DoorButton) // Get current value of Close Door button
//{
// if (getValue.asInt() == 1) { // if button is active
// switch (digitalRead(DoorStateSensorPin)) { // check door state
// case 0: // if open, do this
// digitalWrite(RELAY_CONTROL_PIN, LOW); // turn on relay
// delay(100);
// digitalWrite(RELAY_CONTROL_PIN, HIGH); // turn off relay 100mS later
// Cayenne.virtualWrite(DoorButton,LOW); // reset button state to inactive
// break;
// case 1: // if closed, do this
// Cayenne.virtualWrite(DoorButton,LOW); // reset button state to inactive
// break;
// }
// }
// else Cayenne.virtualWrite(DoorButton,LOW); // reset button state to inactive
//}
// This function is called when the Cayenne widget requests data for the barometer's Virtual Pin.
CAYENNE_OUT(BAROMETER_PIN)
{
if (bmpSensorDetected)
{
// Send the command to get data.
sensors_event_t event;
bmp.getEvent(&event);
float Pressure;
Pressure = event.pressure;
float hPa = Pressure/100;
Serial.print(Pressure);
Serial.print(" ");
if (event.pressure)
{
// Send the value to Cayenne in hectopascals.
Cayenne.hectoPascalWrite(BAROMETER_PIN, Pressure);
}
}
else
{
CAYENNE_LOG("No BMP sensor detected");
}
}
// This function is called when the Cayenne widget requests data for the temperature's Virtual Pin.
CAYENNE_OUT(TEMPERATURE_PIN)
{
if (bmpSensorDetected)
{
float temperature;
bmp.getTemperature(&temperature);
Serial.println(temperature);
// Send the value to Cayenne in Celsius.
Cayenne.celsiusWrite(TEMPERATURE_PIN, temperature);
}
else
{
CAYENNE_LOG("No BMP sensor detected");
}
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(DoorState)
{
Cayenne.virtualWrite(DoorState, digitalRead(DoorStateSensorPin));
}
Here is a screenshot from the web interface. The view is after making the change you suggested previously. Both the web and mobile aps are showing the same anomaly, so it is in the data collection somewhere. As a reminder, this is a boolean door state variable which is being displayed on a Value widget in order to get a history log.
if(currentDoorState == Open) { // action if door is Open
Serial.println(“Open”);
Cayenne.virtualWrite(DoorOpen, 1, “digital_sensor”, “d”);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 0, “digital_sensor”, “d”);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
if(currentDoorState == Closed) { // action if door is closed
Serial.println(“Closed”);
Cayenne.virtualWrite(DoorOpen, 0, “digital_sensor”, “d”);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
Cayenne.virtualWrite(DoorClosed, 1, “digital_sensor”, “d”);
delay(1500); // delay limits data flow to Cayenne dashboard to 1 cmd/sec
}
why are you making it on and then off after 1.5ec. is there are specific reason for this. with this you might hit rate limit and have connection interrupt .Sending MQTT messages within rate limits
I want to have the widget respond in near to real-time as possible. Two of the widgets are 2-State for simply displaying the door position, the third is the Value widget which records the event log. I’ll try them at 2000ms and see if that makes a difference. By using delay(), I am forcing a pause in the code so that nothing happens until the time has elapsed, therefore the next Cayenne.Write should be clear to run. At least, that is my understanding of the timing delay limitations. Please correct me if I’m wrong here.
I see that you are introducing a 10sec delay before writing the door state to the widget, but I don’t understand why a boolean argument, can be read as an analog number. It is sending a 1 or a 0, nothing else. So why is the Cayenne data processing s/w giving the fractional results?