Hey, I do need help with my code for a very important school project, I am really new into Arduino IDE and Blynk so it is probably very wrong.
This is the code that I have done for a soil moisture sensor with Blynk and it gives me an âexpected unqualified-id before â{â tokenâ in the line that i put the emoji.
#define BLYNK_TEMPLATE_ID "TMPL5ENYgsRN5"
#define BLYNK_TEMPLATE_NAME "SENSOR HUMITAT"
#define BLYNK_AUTH_TOKEN "vrviGfLqh-OWNSSkBJC_TbH9wDa1lKIA"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = ("vrviGfLqh-OWNSSkBJC_TbH9wDa1lKIA");
const int sensorPin = 32;
int sensorState = 0;
int lastState = 0;
void setup()
{
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, "MOVISTAR_PLUS_8562", "FV9NHxMt5S9drH7Lo6u9"); //wifi name and password
pinMode(sensorPin, INPUT);
}
void loop()
{
Blynk.run();
sensorState = digitalRead(sensorPin); Serial.println(sensorState);
if (sensorState== 1 && lastState == 0) {
Serial.println("needs water, send notification");
Blynk.logEvent("Water your plants");
lastState = 1;
delay(1000);
//send notification
}
}
**{** :arrow_left: :arrow_left: :arrow_left:
else if (sensorState== 1 && lastState == 1) { //do nothing, has not been watered yet Serial.println("has not been watered yet"); delay(1000);
}
else {
//st
Serial.println("does not need water");
lastState = 0;
delay(1000);
}
@NARDUUU Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you canât find the correct symbol on your keyboard.
Also âshoutingâ (typing everything in capitals) doesnât come across as friendly.
Thatâs true, but it wonât answer his issue about the rouge opening curly bracket, that causes most of his code sit outside the void loop anyway.
But, my personal approach is not to engage with people who ignore the Information about triple backticks until they correct that error. What they do next tells me a lot about the type of person Iâm dealing with. Some will ignore my request to edit their post and instead leave their Unformatted code as it is and repost the code within the same topic. Others will ignore my advice to copy/paste the triple backtick example I provide, and attempt to use different characters. Some will do both, or something different.
Having the knowledge about how well people respond to simple instructions tells me a lot about their attention to detail and also implies quite a bit about their analytical thinking abilities. This helps to inform my decisions about how much of my personal time Iâm going to invest in assisting that person.
After 24 hours new users will lose the ability to edit their post, so I set a bookmark with a 24 hour reminder and when I receive that reminder I go back and delete their code if they havenât fixed the formatting.
This also tells me quite a bit about how the interactions with this person are likely to proceed, because it gets quite painful when a personâs visits to the forum are more than 24 hours apart and it turns into a game of postal chess.
Just my approach though, others can take whatever approach they prefer.
Could you tell me how to reintroduce the code in my void loop, and where is the mistake?
Because as I said I am really new into this, also I did just create my account yesterday five minutes before this post and I did not know the rules, my bad.
Thanks in advance.
If you made better use of indents, youâd be able to see where your problem lies.
Each pair of opening and closing curly brackets need to match, and indenting them helps you to identify the matching pairs. The Arduino IDE also helps you to do this by highlighting the matching bracket when you select one.
void loop()
{
Blynk.run();
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
if (sensorState== 1 && lastState == 0)
{
Serial.println("needs water, send notification");
Blynk.logEvent("Water your plants");
lastState = 1;
delay(1000);
//send notification
}
} // This closing curly bracket matches the one at the start of your void loop
// everything below this point makes no sense to the C++ compiler...
{
else if (sensorState== 1 && lastState == 1) { //do nothing, has not been watered yet Serial.println("has not been watered yet"); delay(1000);
}
else
{
//st
Serial.println("does not need water");
lastState = 0;
delay(1000);
}
There are lots of other issues with your code though. You shouldnât have a cluttered void loop like this, with delays to control the timing. Instead you should use BlynkTimer to call a function, as described hereâŚ
Ok, thanks for your help it has been really useful, but I still have one doubt.
Do you know why Arduino IDE says there is no matching function to BlynkTimer even tho I have already got the Blynk library installed?
You have a semicolon ; at the end of your function name, so the compiler doesnât recognise it as a function.
Remove the semicolon and I think it will solve that problem.
You didnât take my advice about indents though, and this looks a messâŚ
void sensorDataSend();
{
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
if (sensorState== 1 && lastState == 0)
{
Serial.println("needs water, send notification");
Blynk.logEvent("test_event");
lastState = 1;
//send notification
}
{ // this opening curly bracket is redundant
else if (sensorState== 1 && lastState == 1)
{
//do nothing, has not been watered yet Serial.println("has not been watered yet"); delay(1000);
}
} // this closing bracket is redundant
else
{
//st
Serial.println("does not need water");
lastState = 0;
}
}
} // this shouldnât be here!
Hey respect your advice, as I said I am really a newbie in coding and I just searched up in google and I thought that the indents were the spaces before typing, arenât they?
Thatâs why you thought that I did not take your advice, but I tried, thank you anyway.
Could you help me with this error?
no matching function for call to âBlynkTimer::setInterval(long int, int&)â
It still says that thereâs no function for BlynkTimer even tho I got the blynk library installed.
Thanks in advance, I let you see the full sketch here.
#define BLYNK_TEMPLATE_ID "TMPL5ENYgsRN5"
#define BLYNK_TEMPLATE_NAME "SENSOR HUMITAT"
#define BLYNK_AUTH_TOKEN "vrviGfLqh-OWNSSkBJC_TbH9wDa1lKIA"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = ("vrviGfLqh-OWNSSkBJC_TbH9wDa1lKIA");
const int sensorPin = 32;
int sensorState = 0;
int lastState = 0;
int sensorDataSend = 0;
BlynkTimer timer; //announcing timer
void setup()
{
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, "MOVISTAR_PLUS_8562", "FV9NHxMt5S9drH7Lo6u9"); //wifi name and password
pinMode(sensorPin, INPUT);
timer.setInterval(1000L, sensorDataSend);
}
void sensorDataSend();
{
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
if (sensorState== 1 && lastState == 0)
{
Serial.println("needs water, send notification");
Blynk.logEvent("test_event");
lastState = 1;
//send notification
}
else if (sensorState== 1 && lastState == 1)
{
//do nothing, has not been watered yet Serial.println("has not been watered yet"); delay(1000);
}
else
{
//st
Serial.println("does not need water");
lastState = 0;
}
}
void loop()
{
Blynk.run();
timer.run();
}
Iâve already told you what I think is causing that error, but youâve chosen to ignore that advice too!
But, Iâve closed this topic, as youâve created a new topic about the same issue.
Please donât do this in future, it simply makes life much more difficult for moderators cleaning-up the mess that this creates.