Write value in Virtual PIN (Blynk)

Hi all,
I know this is not a Blynk forum, but I have seen very good and helpful responses to other Blynk related question here, so I thought I´ll give it a shot.

I´m using a Wemos D1 Mini with A TTP223 ( very simple touchsensor) to send a notification over Blynk, when the button was touched.
That works quite well, but it is hardcoded and I would like to realize this over the app itself with sending the state of the sensor (1/0) to a virtual PIN and then use this virtual PIN in Blynk.

This is the working code with the "hardcoded notification"

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "XXX";

char ssid[] = "XXX";
char pass[] = "XXX";

int sensorPin = 16;   // Input pin for the Touch Sensor
int sensorValue = 0;  // Variable to store the value coming from the touch sensor

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

void loop()

{
  Blynk.run();
  sensorValue = digitalRead(sensorPin);
  if (sensorValue == 1) {
    Blynk.notify("Touchbutton touched"); //this is the hardcoded part
  }

}

Now I would like to write the "sensorValue" into a virtual PIN and tried the following, but it didnt work: ( I marked the changes bold) I´m pretty sure it is obvious for someone whats wrong here.
The "try and error" code below is not throwing any errors, it just doesn't work, when I set a notification with the eventor modul in Blynk with Virtual PIN 1 as trigger

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "XXX";

char ssid[] = "XXX";
char pass[] = "XXX";

int sensorPin = 16;   // Input pin for the Touch Sensor
int sensorValue = 0;  // Variable to store the value coming from the flame sensor

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // Setup a function to be called every second  
  timer.setInterval(1000L, sendTouch);
}

void sendTouch()
{
 
 Serial.println(sensorValue);
 Blynk.virtualWrite(V1, sensorValue);
}

Looking forward for a reply

where do you read (update) sensorValue :smiling_imp: :wink: ?

Good point. Since I heard its good to keep the loop clean I added it to the "send touch" (sensorValue = digitalRead(sensorPin):wink:

But I'm not sure it this is the proper way, but it works. YIIIHAAAAA :slight_smile:

Thank you very much.

But In general, would that be the proper way to tell Blynk, whats happening at my GPIO / Sensor PIN 16 (in regards of using a virtual PIN)?

void sendTouch()
{
 
 Serial.println(sensorValue);
 sensorValue = digitalRead(sensorPin);
 Blynk.virtualWrite(V1, sensorValue);
}

that's a lot of communication for something that will just send the same value most of the time...
I would only send it if it has changed, not every second

True,
I re-used the code from a temp sensor, where it makes perfect sense, to receive data every second. But here not so much.
From my research it sounds like I should use something like BLYNK_WRITE

int value = param.asInt();

But not quite sure how to implement it by now.

have a variable to keep in memory the previous value.
in the loop read the new value. if it has changed then call sendTouch() which will send it

Alternatively, code everything in sendTouch and keep the timer but you'll have up to a 1s latency to update the info

void sendTouch()
{
  static byte oldState = 255; // init with dummy value, real value are only 0 and 1
  byte newState = digitalRead(sensorValue);
  if (newState != oldState) {
    oldState = newState;
    Serial.println(sensorValue);
    Blynk.virtualWrite(V1, newState);
  }
}

Yes, Blynk.virtualWrite is the correct function. Have you properly configured the Virtual Pin in the project on your Mobile Device?

Also,

catweazle123:
The "try and error" code below is not throwing any errors ....

I find that hard to believe as that code contains no loop() function. It will not compile, and thus obviously, will not "work".

@J-M-L --> Thanks for the example I need to have a look into this and see if I can make it work for me.

@gfvalvo --> correct, it just slipped through while copying it, but it worked with adding the sensor value, as I described before. (Of course with the loop part :wink:

So now I´m trying to avoid sending so much data and use the BLYNK_WRITE in a similar like shown here:

BLYNK_READ(V2)
{
if(buttonState==HIGH)
  {
     Blynk.virtualWrite(2, HIGH);
  }
  else
  {
     Blynk.virtualWrite(2, LOW);
  }
}

My assumption would be, that I can remove the blynk timer and also most of the "send touch part", is that correct?

Whenever you make a code change, please post the ENTIRE program, not snippets.

Alright, so this is the part which (in my eyes) would be ok for a temperatur sensor (then including other libraries) to read and forward the data to Virtual PIN 1 (which was my main goal here), however I used it with the touchsensor and therefore changed the 1000ms to 100, to make my touchsensor reacts faster:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "X";

char ssid[] = "X";
char pass[] = "X";

int sensorPin = 16;   // Input pin for the Touch Sensor
int sensorValue = 0;  // Variable to store the value coming from the touch sensor

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(100L, sendTouch); // Setup a function to be called every 100 Millisecond  
}

void sendTouch()
{
 
 Serial.println(sensorValue);
 sensorValue = digitalRead(sensorPin);
 Blynk.virtualWrite(V1, sensorValue);
}
}

void loop()

{
  Blynk.run();
  timer.run();

}

The issues with sending to many values for nothing ( I just wanna know, when the button has been touched) I tried to resolve with this code, but it is more a very poor try to implement the BLYNK_WRITE in here: Also here, no error, but also no success, the BLYNK app is not receiving any value.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "X";

char ssid[] = "x";
char pass[] = "x";

int sensorPin = 16;   // Input pin for the Touch Sensor
int sensorValue = 0;  // Variable to store the value coming from the touch sensor

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

void sendTouch()
{
 
 Serial.println(sensorValue);
 sensorValue = digitalRead(sensorPin);
}



BLYNK_READ(V1)
{
if(sensorValue== HIGH)
  {
     Blynk.virtualWrite(1, HIGH);
  }
  else
  {
     Blynk.virtualWrite(1, LOW);
  }
}

void loop()

{
  Blynk.run();

}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.