Blynk does not send realtime and accurate data

Please help.
I have mega+esp8266 as a shield+local server.

I use arduino to read a reed sensor in actuator so I can monitor the actuator whether actuator extend or retract and the position.

When it runs without blynk it works fine and smooth, every position of actuator is represented by percentage. Here is the code without blynk.

#define A 53
#define B 52
int persen;
int counter = 0;
int diam;
int akhir;
void setup() {
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  Serial.begin(9600);
  akhir=digitalRead(A);}

void loop() {
    diam=digitalRead(A);
  if (diam!=akhir){
    if(digitalRead(B)== HIGH){
      counter ++;
      }else{
        counter --;}
      persen = map(counter, 0, 530, 0, 100);
      Serial.print("OPEN: ");
      Serial.print(persen);
      Serial.println("%");}
  delay(1);
  akhir=diam;}

But when I want to monitor the actuator position through my phone use blynk, the percentage does not represent the position, the actuator keep extend or retract but the percentage does not show smoothly, sometime it get stuck. Here is the code with blynk.

#define A 53
#define B 52
int persen;
int counter = 0;
int diam;
int akhir;
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

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

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115000

ESP8266 wifi(&EspSerial);

BlynkTimer timer;
void sensor(){
diam=digitalRead(A);
  if (diam!=akhir){
    if(digitalRead(B)== HIGH){
      counter ++;
      }else{
        counter --;}
      persen = map(counter, 0, 530, 0, 100);
      Serial.print("OPEN: ");
      Serial.print(persen);
      Serial.println("%");
      akhir=diam;}
      Blynk.virtualWrite(V5, persen);}


void setup (){
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  Serial.begin (9600);
  akhir=digitalRead(A);
  timer.setInterval(1, sensor);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(1);

  Blynk.begin(auth, wifi, ssid, pass, "192.168.23.1", 8080);
  delay(100);
}

void loop (){ 
  timer.run();
  Blynk.run();
}

I tried to put the code of the sensor in the void loop without blynk.timer but the result is the same.

I sent the picture of the actuator and sensor.
Thank you.

there is always going to be some delay. the data must be sent from device through internet to the server, then to your phone. While all of this happens quite quickly, it is not instant. do you really need the position updated every 1/1000th of a second? Can you live with a little lower resolution? Maybe try every 1/10th of a second timer.setInterval(100, sensor);, and adjust your counter routine.

Also with such a low timer interval (1ms) you are flooding the server, and it is probably “throttling” the data being sent. This may also happen witth the timer interval I suggested as well.

I have installed local server in my laptop, does it still flooding my local server and “throttling” sending data?

Good question. I don’t use local server, so cant answer that. You may have to adjust a setting.

You may also get better results using an interrupt routing to count the rotations. But not certain how this whole thing functions to be sure.

Maybe a description to add to what we are seeing in the picture might be helpful. Does that magnet continuously spin when extending/retracting. does it switch directions? how fast? etc.

May be it will not be a problem if the data have to get some delay to be displayed in my phone, I want to get the accuracy of the position by percentage then send it to my phone with some delay (100ms) it will be a problem. But I got the actuator keep extend or retract with false percentage of position.

Yap. Try to instal local server, it easy.

Yes the magnet continously spin when actuator extending and retracting, yes it switch direction, I use digitalpin 53 to read the sensor to count ++ when digitalpin 52 HIGH, and count – when digitalpin 52 LOW.

I have just measured the rotation, it’s about 150 rpm.

so pin 52 is always high or always low depending on if the actuator is extending or retracting?

Yes, pin 52 connected to a relay when actuator extending relay on and it gives pin 52 signal HIGH, when retracting relay off and gives pin 52 LOW.

So when pin 52 HIGH, pin 53 keep reading sensor and the code manipulates it to increase the value, when pin 52 LOW, code manipulates to decrease the value.

so maybe something like this. This may need some modification, but should give you an idea.

Make sure you are using the correct pullup/pulldown resistors on your reed switch and direction switch.

in setup

add this,

attachInterrupt(digitalPinToInterrupt(A), sensor, rising) //or maybe falling, depending on wiring?

and change the timer to this

timer.setInterval(500, position); //update position every .5 seconds?

modify your sensor function to this

void sensor()
{
    if(digitalRead(B)== HIGH){
      counter ++;
      }else{
        counter --;}
      counter = map(counter, 0, 530, 0, 100);
      Serial.print("OPEN: ");
      Serial.print(counter);
      Serial.println("%");
  }

and add this function

void position(){
Blynk.virtualWrite(V5, counter);
}

One thing to note is that if the counter will need to be zeroed. That is the board will need to be booted/reset when the actuator is fully retracted. if it happens to reset when it is partially extended, it will need to be re-zeroed.

Thankyou, I will try it tomorrow, it has been early morning here in Indonesia and I don’t sleep yet. May be we can be friend in facebook or whatapp so I can share my works. Here is my facebook Jonu Hermawan