Virtual pin history showing unusual values

Hello,
I’m using an esp8266 to control my garage. Everything works, except for data collection. What I am trying to do is to write a “0” or “1” to virtual pin 3 whenever my garage opens or closes, detected by an ultrasonic sensor. However, when I look at the pin history, I get values that seem to come out of nowhere. The only possible values should be 0 or 1, as the only code involving v3 is virtualWrite(3, 0) and virtualWrite(3, 1)
screenshot of csv file (values are col A, timestamp col B, not sure what col C is for:


Using Blynk cloud server on version 0.6.1


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

const static double MILLIS_TO_INCH = .0067515;

const int trigPin = 0;
const int echoPin = 4;

boolean isGarageOpen = false;

unsigned long time_of_last_garage_cycle = 0;

//cycle through 1 rest, 2 clear trig, 3 write trig pin, and 4 wait for echo to measure sensor
unsigned long current_interval = 0; //used for all different states of ultrasonic sensor
unsigned long timer_start_time = 0;
const static int REST_TIME = 1000000; //how often to measure distance
boolean isResting = true; //ultrasonic sensor not active
const static int CLEAR_TIME = 2;
boolean isClearingTrigPin = false;
const static int WRITE_TIME = 10;
boolean isWritingTrigPin = false;
const static int TIMEOUT = 20000; //if ultrasonic sensor takes longer than 20 ms
//to return value assume distance is large enough for garage to be open
//boolean isAwaitingEcho = false; //commented because not using interrupts to measure ultrasonic


boolean text_sent = false; //ensure notification of garage open >30 min only sent once

//int time1 = 0; //start and
//int time2 = 0; //end time for measuring ultrasonic

//void ICACHE_RAM_ATTR echoFall ();
//void ICACHE_RAM_ATTR echoRise ();

void setup()
{
  digitalWrite(5, HIGH);
  // Debug console
  //  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);
  //  pinMode(echoPin, INPUT_PULLUP);
  //  attachInterrupt(digitalPinToInterrupt(echoPin), echoFall, FALLING);
  //  attachInterrupt(digitalPinToInterrupt(echoPin), echoRise, RISING);

  current_interval = REST_TIME; //schedule first ultrasonic sensor measurement
  timer_start_time = micros();
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}


void loop()
{
  updateUltrasonic();
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

void updateUltrasonic() {
  unsigned long t = micros() - timer_start_time;
  if (t >= current_interval) {
    if (isResting) {
      isClearingTrigPin = true;
      isResting = false;
      digitalWrite(trigPin, LOW);
      timer_start_time = micros();
      current_interval = CLEAR_TIME;
    }
    else if (isClearingTrigPin) {
      isClearingTrigPin = false;
      isWritingTrigPin = true;
      digitalWrite(trigPin, HIGH);
      timer_start_time = micros();
      current_interval = WRITE_TIME;

    }
    else if (isWritingTrigPin) {
      isWritingTrigPin = false;
      digitalWrite(trigPin, LOW);
      //      isAwaitingEcho = true;
      //      time_of_next_event = millis() + TIMEOUT;
      unsigned long measuredDist = pulseIn(echoPin, HIGH);
      measuredDist = measuredDist * MILLIS_TO_INCH;
      //      Serial.println(measuredDist);
      boolean state;
      String msg;
      if (measuredDist > 16) {
        state = false;
        msg = "Closed ";
      }
      else {
        state = true;
        msg = "Opened ";
      }
      if (state != isGarageOpen) { //garage changed state aka cycled
        time_of_last_garage_cycle = millis();
        isGarageOpen = state;
        Blynk.virtualWrite(1, isGarageOpen);
        if (isGarageOpen) {
          Blynk.virtualWrite(1, "Garage is open");
          Blynk.virtualWrite(3, 1);
        }
        else {
          Blynk.virtualWrite(1, "Garage is closed");
          Blynk.virtualWrite(3, 0);
          text_sent = false; //reset email notif
        }
      }
      msg += formatMillis(millis() - time_of_last_garage_cycle) + " ago";
      Blynk.virtualWrite(2, msg); //time since last cycle

      if (isGarageOpen && !text_sent && millis() - time_of_last_garage_cycle >= 1800000) {
        Blynk.notify("your garage has been open for over 30 minutes");
        text_sent = true;
      }

      //      isAwaitingEcho = false;
      isResting = true;
      timer_start_time = micros();
      current_interval = REST_TIME;
    }
    //    else if (isAwaitingEcho) { //reached only if timeout occurs
    //      Serial.println("timeout");
    //      echoFall();
    //    }
  }
}

String formatMillis(unsigned long m) {
  m = m / 1000;
  int hours = m / 3600;
  m = m % 3600;
  int minutes = m / 60;
  int seconds = m % 60;
  return String(hours) + " hr, " + String(minutes) + " min, " + String(seconds) + " sec";
}

If you read this:
https://docs.blynk.cc/#widgets-displays-superchart
in particular the bit that says:

Superchart supports currently 2 types of granularity:

  • Minute granularity - 1h , 6h , 1d ;
  • Hour granularity - 1w , 1m , 3m ;

This means that minimum chart update interval is 1 minute for 1h , 6h , 1d periods. 1 hour for 1w , 1m and 3m periods. As Blynk Cloud is free to use we have a limit on how many data you can store. At the moment Blynk Cloud accepts 1 message per minute per pin. In case you send your data more frequently your values will be averaged. For example, in case you send value 10 at 12:12:05 and than again 12 at 12:12:45 as result in chart you’ll see value 11 for 12:12.

In order to see data in chart you need to use either widgets with “Frequency reading” interval (in that case your app should be open and running) or you can use Blynk.virtualWrite on hardware side. Every Blynk.virtualWrite command is stored on server automatically. In that case you don’t need application to be up and running.

I realise that you aren’t using SuperChart, but the data stored for a pin is stored in the same way.
The 0.5 values that you are seeing is where the garage door has been both open and closed within a 1 minute window, so the values of 0 and 1 are averaged to 0.5.

Pete.

1 Like

You could use table widget to store events. It is a bit uncomfortable as you need to take care of correct indexing number, but it is the most convenient way to store events.

Console can be used in the same way as table - it’s easier to do as it’s ordered in time, without need for keeping track of IDs.

Only disadvantage is that you can only store the past 25 values, vs the 100 of a table. Both of these are configurable on a local server.