Data logging - Day view


#1

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.


#2

the screenshot you shared does not open. can share a new one.


#3


#4

Here is the full day data log

The other photo is zoomed in to show the non-binary values being included in the binary data


#5

i am not sure why there are values in between. can you share your code.


#6

It is currently 10:39pm as shown in the screenshot, but the Day data log is not the full 24 hrs


#7

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));
}

#8

in place of this:

use this:
Cayenne.virtualWrite(DoorOpen, 1, "digital_sensor", "d");

this adds a two-state widget having either 1 or 0 value. but on the mobile app this widget does not give a live data view.


#9

Thanks very much. Will make that change. Is there a ref doc with info on what arguments can be passed?

Rgds

Brian


#10

Do you have any thoughts on the data Day view not showing the full prev 24 hrs? Attached are BMP temp and press plots


#11

no. can you keep the sensor ON for one more day and check after that.


#12

Hi

Thanks for the ref doc. I’ve attached two screenshots. Both are taken now, one is week view and the other is day view


#13

Hi

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.


#14

can you share the code after the changes i have suggested you.


#15

code as requested

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
}


#16

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


#17

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.

Cheers,

B


#18

what i think is when the door is on keep the two state widget 1 while 0 when close.

unsigned long lastMillis = 0;
unsigned long lastMillis1 = 0;

if(currentDoorState == Open) { // action if door is Open
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
Serial.println(“Open”);
Cayenne.virtualWrite(DoorOpen, 1, “digital_sensor”, “d”);
}
}
if(currentDoorState == Closed) { // action if door is Open
  if (millis() - lastMillis1 > 10000) {
    lastMillis1 = millis();
Serial.println(“Open”);
Cayenne.virtualWrite(DoorOpen, 0, “digital_sensor”, “d”);
}
}

#19

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?


#20

i m not sure why it is doing so.may be the way you have added the code. did you try the above code ?