For some reason Arduino is giving me “‘relay’ was not declared in this scope” on line 77 of my code. I’m planning on running a relay based off the status of V1-3 and V4. I’m using the NodeMCU
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "g-zo8C8skr7RglPQneF7IlGRWVKscg2w";
char ssid[] = "75fennellwest";
char pass[] = "fennellhouse75";
BLYNK_WRITE(V0)
{
int pinValue0 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $5 gas: ");
Serial.println(pinValue0);
}
BLYNK_WRITE(V1)
{
int pinValue1 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $10 gas: ");
Serial.println(pinValue1);
}
BLYNK_WRITE(V2)
{
int pinValue2 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $20 gas: ");
Serial.println(pinValue2);
}
BLYNK_WRITE(V3)
{
int pinValue3 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $50 gas: ");
Serial.println(pinValue3);
}
BLYNK_WRITE(V4)
{
int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
int relay = 5;
volatile byte relayState = HIGH;
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay, OUTPUT);
Blynk.begin(auth, ssid, pass);
// 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()
{
Blynk.run();
//if (pinValue0 > 0) {
// digitalWrite(relay, HIGH);
//} else {
// digitalWrite(relay, LOW);
}
//Serial.print()
}
Try put int relay = 5 outside BLYNK_WRITE(V4)
Such as after char pass [] or before void setup()
According to my understanding, any attached pin should be declared outsite any function
(correct me if i’m wrong)
I’m not expert, I’m new. But i’m trying to help others based on my experience.
Atikahamdan
What you’re doing here is declaring the integer variable relay within the BLYNK_WRITE(V4) function…
BLYNK_WRITE(V4)
{
int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
int relay = 5;
volatile byte relayState = HIGH;
}
This causes it to be local to just that function, so invisible to the rest of your sketch, hence why when you try to reference relay in your void setup the compiler says “What! relay, I don’t know this relay variable!”.
Move the definition out of the function, preferably up to the top of your code where the rest of your variables are declared. It would be sensible to do the same with your relayState variable, although I’m not sure why you’re declaring that as volatile
I hope that the code in your void loop that’s commented-out at the moment is going to remain that way too?
This worked, thank you I appreciate the help. I also have an issue now ‘pinValue0’ was not declared in this scope. I thought when I had int pinValue0 = param.asInt(); that was a global int
The error is one line 88 if (pinValue0 = 1) + (pinValue4 = 1);
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "g-zo8C8skr7RglPQneF7IlGRWVKscg2w";
char ssid[] = "75fennellwest";
char pass[] = "fennellhouse75";
BLYNK_WRITE(V0)
{
int pinValue0 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $5 gas: ");
Serial.println(pinValue0);
}
BLYNK_WRITE(V1)
{
int pinValue1 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $10 gas: ");
Serial.println(pinValue1);
}
BLYNK_WRITE(V2)
{
int pinValue2 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $20 gas: ");
Serial.println(pinValue2);
}
BLYNK_WRITE(V3)
{
int pinValue3 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $50 gas: ");
Serial.println(pinValue3);
}
BLYNK_WRITE(V4)
{
int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
}
int relay = 5;
volatile byte relayState = HIGH;
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay, OUTPUT);
Blynk.begin(auth, ssid, pass);
// 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()
{
Blynk.run();
if (pinValue0 = 1) + (pinValue4 = 1); {
digitalWrite(relay, HIGH);
} else {+
digitalWrite(relay, LOW);
}
Serial.print("Dispense $5 worth of gas");
//if (pinValue1 = 1) + (pinValue4 = 1); {
// digitalWrite(relay, HIGH);
//} else {
// digitalWrite(relay, LOW);
//}
//Serial.print("Dispense $10 worth of gas");
//if (pinValue2 = 1) + (pinValue4 = 1) ;{
// digitalWrite(relay, HIGH);
//} else {
// digitalWrite(relay, LOW);
//}
//
//Serial.print("Dispense $20 worth of gas");
//if (pinValue3 = 1) + (pinValue4 = 1); {
// digitalWrite(relay, HIGH);
//} else {
// digitalWrite(relay, LOW);
//}
//
//Serial.print("Dispense $50 worth of gas");
This is the same issue as with ‘relay’ . . . you have declared it inside the BLYNK_WRITE(V0) function, therefore it is invisible outside that function . . . when you try to use that variable in your loop() it is ‘invisible’.
If you want to use a variable(s) within multiple functions then you should declare them before your setup, outside as any function (known as a Global variable).
Once you get this working, you should then look to remove the code from your loop(), which will create much more stable interaction with Blynk.
Keeping void loop() clean is a new one for me. I’m attempting to have a relay operate when any V0-3 && V4 both are high. My issue is the digitalWrite(relay, HIGH); Serial.println("Dispense $5 worth of gas"); line isnt doing anything. Do is this because its in the loop()
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "g-zo8C8skr7RglPQneF7IlGRWVKscg2w";
char ssid[] = "75fennellwest";
char pass[] = "fennellhouse75";
int relay = 5;
int pinValue0=0;
int pinValue1=0;
int pinValue2=0;
int pinValue3=0;
int pinValue4=0;
BLYNK_WRITE(V0)
{
int pinValue0 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $5 gas: ");
Serial.println(pinValue0);
}
BLYNK_WRITE(V1)
{
int pinValue1 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $10 gas: ");
Serial.println(pinValue1);
}
BLYNK_WRITE(V2)
{
int pinValue2 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $20 gas: ");
Serial.println(pinValue2);
}
BLYNK_WRITE(V3)
{
int pinValue3 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $50 gas: ");
Serial.println(pinValue3);
}
BLYNK_WRITE(V4)
{
int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay, OUTPUT);
Blynk.begin(auth, ssid, pass);
// 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()
{
Blynk.run();
if ( pinValue0 == 1 && pinValue4 == 1)
{
pinValue0 = 1;
pinValue4 = 1;
digitalWrite(relay, HIGH);
Serial.println("Dispense $5 worth of gas");
Serial.println(0);
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue1 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $10 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue2 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $20 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue3 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $50 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
}```
remove the extra int declarations in the BLYNK_WRITE() function.
As for keeping the loop() clean, read the information in the link provided by @Bill_Donnelly. When using BLYNK you need to incorporate the use of timers.
Then create a function that checks if this is true (in other words move this comparison out of your void loop into a function) and call it as the last line of each of your BLYNK_WRITE(Vx) callback functions.
I attempted to do this but it is still not outputting and I assume this is because my pinValue4 is a local variable ? Is there a way for my function to access the value of V4 somehow?
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "g-";
char ssid[] = "";
char pass[] = "";
int relay = 5;
int pinValue0=0;
int pinValue1=0;
int pinValue2=0;
int pinValue3=0;
int pinValue4=0;
BLYNK_WRITE(V0)
{
int pinValue0 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $5 gas: ");
Serial.println(pinValue0);
if ( pinValue0 == 1 && pinValue4 == 1)
{
pinValue0 = 1;
pinValue4 = 1;
digitalWrite(relay, HIGH);
Serial.println("Dispense $5 worth of gas");
Serial.println(0);
}
else {
digitalWrite(relay, LOW);
}
}
BLYNK_WRITE(V1)
{
int pinValue1 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $10 gas: ");
Serial.println(pinValue1);
if ( pinValue1 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $10 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
}
BLYNK_WRITE(V2)
{
int pinValue2 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $20 gas: ");
Serial.println(pinValue2);
if ( pinValue2 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $20 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
}
BLYNK_WRITE(V3)
{
int pinValue3 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Customer requests $50 gas: ");
Serial.println(pinValue3);
if ( pinValue3 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $50 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
}
BLYNK_WRITE(V4)
{
int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay, OUTPUT);
Blynk.begin(auth, ssid, pass);
// 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);
// timer.setInterval(1000L, sensorDataSend); //timer will run every sec
// Blynk.virtualWrite(V1, sensorValue); // sending sensor value to Blynk app
}
void loop()
{
Blynk.run();
}```
You re-declare them inside the BLYNK_WRITE functions. So for example, you need
int pinValue4;
BLYNK_WRITE(V4)
{
pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
}
And to expand on what @PeteKnight is saying, as an example:
int pinValue4;
BLYNK_WRITE(V4)
{
pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Confirm Purchase: ");
Serial.println(pinValue4);
getGas();
}
void getGas()
{
if ( pinValue0 == 1 && pinValue4 == 1)
{
pinValue0 = 1;
pinValue4 = 1;
digitalWrite(relay, HIGH);
Serial.println("Dispense $5 worth of gas");
Serial.println(0);
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue1 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $10 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue2 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $20 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
if ( pinValue3 == 1 && pinValue4 == 1)
{
digitalWrite(relay, HIGH);
Serial.println("Dispense $50 worth of gas");
}
else {
digitalWrite(relay, LOW);
}
}
But, the question is, he declared pinValueX inside BLYNK_WRITE(VX). How to get the total purchase since the pinValueX is local declare. How to make all these pinValueX as the global declare ?
Sorry for my bad english, i understand what is meant by him and what he want to to…
Plus if you read the FULL article re clean loop(), and look at the timers, you’ll see how to do everything you wish to achieve using timers, simple customs functions, and a clean loop.
As @Adam_Baker has three active topics about the same project, I’m locking this one and one other, and asking that all future posts about this project are made ion the following topic: