@bwaii you need to edit your post and fix the formatting.
Triple backticks need to be on a line of their own, and you need to replace the characters you’ve used here with triple backticks…
In addition, posting snippets of code is rarely useful if you want a sensible answer to your question.
I’m sorry. I tried really hard to get the backtick thing right.
I can post the whole sketch if needed. I didn’t want to clog things up with extra stuff to wade through if it isn’t needed. It works, I just didn’t know if virtualWrite in the _WRITE function was kosher or bad programming and would come back to bite me in some infinite loop. Kind of an Arduino Groundhog Day.
You appear to be misusing the Blynk.syncVirtual(vPin) command. You should only need to use this once, when the device boots-up or reconnects to Blynk. From that point onwards you have all the tools you need to track the value of your variables within your sketch without the need to ask Blynk for the current value.
In addition, I suspect that you’re misusing the Blynk.logEvent() command too, but without seeing your entire sketch and understanding what types of situations will trigger the Blynk.logEvent() command, and how frequently that might happen, it’s difficult to say.
You certainly don’t appear to have a Blynk friendly void loop if it contains anything other than Blynk.run() and timer.run() although there are potential exceptions to this rule, but it’s difficult to say without understanding more about your project and seeing your fill sketch.
To answer your original question, there is nothing wrong with doing a Blynk.virtualwrite(vPin) in a BLYNK_WRITE(vPin) callback function.
This is to monitor a remote water filter and dispenser. It dispenses 20L at a time between 20 and 30 times a week. Then the filter pumps come on for about 45min. As I only messaged blynk when there was a use (20 times a week) I put it in the Loop. What I didn’t count on was something going bollywonkers in the hardware and registering a use and pump cycle 100 times a second. SO I have changed the code to try to incorpoate the changes you suggested. If you would be willing to look at the code and make suggestion I would appreciate the effort. Just don’t tell me how stupid I am all at once. Do it little by little. Things have changed since I started w/ GWBASIC in DOS 3.3
The console has a number box to count uses, 2 LED to indicate ON and two override OFF switches to force pumps off.
//Lite2
#define BLYNK_TEMPLATE_ID "TMP...Ov5"
#define BLYNK_TEMPLATE_NAME "Acqua...TE2"
#define BLYNK_AUTH_TOKEN "N-LucUG...8FLALy5f"
#define BLYNK_FIRMWARE_VERSION "0.1.2"
#define BLYNK_PRINT Serial
// Lite v1
//#define BLYNK_TEMPLATE_ID "TMPL...5qF"
//#define BLYNK_TEMPLATE_NAME "Acq...eLITE"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "MinBlynkOTAESP32.h" // My own Blynk OTA setup. Add these 2 to sketch
const byte ROPin = 4;
const byte XferPin = 15;
const byte CoinPin=23;
const byte LEDPin = 2;
const byte ROPwrPin=17;
const byte XferPwrPin=16;
bool ROState;
bool XferState;
bool CoinState;
double SoldCnt;
//char auth[] = "98tcAOA-jBw...ji3POsGK-";//AQLite v1
char auth[] = "N-LucUGnE2hhm...8FLALy5f"; //AQLite v2
// Your WiFi credentials.
//char ssid[] = "BA";
//char pass[] = "15...oln";
char ssid[] = "Infor...staurante";
char pass[] = "12...78";
BlynkTimer timer;
void setup() {
pinMode(ROPin, INPUT_PULLUP);
pinMode(XferPin, INPUT_PULLUP);
pinMode(CoinPin, INPUT_PULLUP);
pinMode (LEDPin, OUTPUT) ; // led
pinMode (ROPwrPin, OUTPUT) ;
pinMode (XferPwrPin, OUTPUT) ;
digitalWrite(ROPwrPin,HIGH); // OFF (relay module is inverted)
digitalWrite(XferPwrPin,HIGH);
Serial.begin(115200);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) { // endless loop if no wifi
delay(500);
Serial.print(".");
int LEDState=digitalRead(LEDPin);
digitalWrite(LEDPin, !LEDState);
}
Serial.println("WiFi connected");
Blynk.begin(auth, ssid, pass);
timer.setInterval(120000L, UpdateOnStatus); //120k=2m, 300k=5
timer.setInterval(1000L, PinCk);
Blynk.syncVirtual(V0,V2,V4); //sync relay w/ V switches & sold count
//Blynk.disconnect();
}
void loop(){
Blynk.run();
timer.run();
}
void UpdateOnStatus (){ // confirm on status w app every 2 sec
if (ROState) Blynk.virtualWrite(V1, ROState);
if (XferState) Blynk.virtualWrite(V3, XferState);
}
void PinCk(){
static bool OldROState, OldXferState, OldCoinState;
ROState = !digitalRead(ROPin); //invert for pullup
if (ROState != OldROState){ //immediate state change, + timer 2m repeats
Blynk.virtualWrite(V1, ROState);
// if (ROState) Blynk.logEvent("ro_on");
OldROState = ROState;
}
XferState=!digitalRead(XferPin);
if (XferState != OldXferState){
Blynk.virtualWrite(V3, XferState);
// if (XferState) Blynk.logEvent("xfer_on"); //redundant. I'll check both if told RO is on
OldXferState = XferState;
}
CoinState= !digitalRead(CoinPin);
if (CoinState != OldCoinState){
if (CoinState) {
SoldCnt++;
Blynk.virtualWrite(V0, SoldCnt);
Blynk.logEvent("sold", int(SoldCnt));
}
OldCoinState = CoinState;
}
if (XferState | ROState | CoinState) {digitalWrite(LEDPin, HIGH);} //Turn on LED if a pump is on
else {digitalWrite(LEDPin, LOW);}
}
BLYNK_WRITE(V0){ //seems redundant, but allows console to change value here
if (!CoinState){ //if CoinState, device initiated write, if !CoinState, app init and must update SoldCnt w/ new value
SoldCnt = param.asDouble(); // assigning incoming value from pin V0 to a variable
}
}
BLYNK_WRITE(V2) {
int ROOverride=param.asInt();
digitalWrite(ROPwrPin, ROOverride);
}
BLYNK_WRITE(V4){
int XferOverride=param.asInt();
digitalWrite(XferPwrPin, XferOverride);
}
The code would be far easier to follow if you had much more descriptive in-code documentation in the form of comments like “ we arrive here when x,y,z so we now want set variable a to this value so that….”
You also have lots of commented-out lines, which confuse things unnecessarily, and some duplicate lines such as what you define the Blynk auth token.
What’s the logic behind manually managing the WiFi connection, then using Blynk.begin() which will automatically create a WiFi connection?
You know those sci-fi movies where they find an alien ship and try to fly it with out really understanding how it works? That’s me. The core of this code is borrowed and cobbled together with other code and I don’t really understand how it works. I actually wanted to ask about that but didn’t know how. I REMed it out to stop the code from messaging Blynk and was surprised when stuff still went through. So I can kill the whole “while {}” loop? (I added the 2 LED lines so I could see what it was doing.
I use it in 2 places. Write it at home, use it in the field. I held old token when I made a new web template in case I needed to go back.
I used that term so much when I first started C++ programming, but it confused the hell out of people who hadn’t cut their teeth on BASIC or Dos scripting
but then you have these…
and this…
It would be far mare sensible to drop the auth variable and use Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); instead.