Soil moisture sensor code esp32

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.

Pete.

Pete, he doesn’t need to use the triple backticks for you to tell him that he needs to clean up his loop! :smiley: But using all caps is kinda abusing :angry:

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.

Pete.

3 Likes

Hey already did, sorry i didnt mean to “shout”.

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…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Also, I’m pretty sure that “Water your plants” is not an event code but an event description, which won’t work.

Pete.

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?

I’d need to see the full sketch and the exact error message.

Pete.

I know that it is not correct at all, I have just been working in replacing the delays and some other minor things.

The error message is the following:
Compilation error: no matching function for call to ‘BlynkTimer::setInterval(long int, int&)’

Hope u can help, thank you in advance.

#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();

}

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!

Pete.

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.

Pete.