Cannot get my code arduino correct

Hi Guys I a very new newbie to this Arduino IDE programming.

I have a water controller in my greenhouse but I want to be able to control it in paralell with four relays for the four zones of the controller
I have looked up various sites and did a cut and paste of what I thought would work, but unfortunately I cannot get this to compile without some sort of error.

I think I have managed to get the Blynk mobile side of it working but the arduino side is the issue.

I am using the old legacy of Arduino IDE version arduino-1.8.19-windows as I am running on Windows 7.
I believe I have installed the required libraries for the ESP8266.

Currently the compiler stops at line 81 with the message:
" a function-definition is not allowed her before ‘{’ token "

Any assistance would be greatly appreciated.

Here is the code that I have come up with.
Hopefully someone with more experience can gleam over the program and point out the error or errors.

Any assistance would be greatly appreciated.

Regards Chrisk

// Sketch name - GHWater
// Does not work. Error at line 81
// This program allows the control of four seperate relays via the Mobile Blynk App
#include <dummy.h>

#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxx"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "xxxxxx";
char pass[] = "xxxxxx!";
char auth[] = "xxxxxxxxxxxxxxxi"

BLYNK_WRITE(V0)
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 0)
  {
    digitalWrite(D0, LOW);  //Setting Digital PIN as LOW to turn OFF device if relay module is "active high"
    Serial.println("Water Left OFF");
  }
  if (value == 0)
  {
    digitalWrite(D0, HIGH);  //Setting Digital PIN as HIGH to turn ON device if relay module is "active high"
    Serial.println("Water Left ON");
  }
}

BLYNK_WRITE(V1)
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 1)
  {
    digitalWrite(D1, LOW);
    Serial.println("Water Right OFF");
  }
  if (value == 0)
  {
    digitalWrite(D1, HIGH);
    Serial.println("Water Right ON");
  }
}

BLYNK_WRITE(V2)
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 1)
  {
    digitalWrite(D2, LOW);
    Serial.println("Mist OFF");
  }
  if (value == 0)
  {
    digitalWrite(D2, HIGH);
    Serial.println("Mist OFF");
  }
}

BLYNK_WRITE(V3)
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 1)
  {
    digitalWrite(D3, LOW);
    Serial.println("Enable OFF");
  }
  if (value == 0)
  {
    digitalWrite(D3, HIGH);
    Serial.println("Enable ON");
  }

  void setup()
  {
    Serial.begin(115200);
    Blynk.begin(auth, ssid, pass);
    pinMode(D0, OUTPUT); //GPIO 16;
    pinMode(D1, OUTPUT); //GPIO 05;
    pinMode(D2, OUTPUT); //GPIO 04;
    pinMode(D3, OUTPUT); //GPIO 00;
  }
  {
    void loop()
    Blynk.run();
  }

You have three syntax errors in your code…

This line (15) is missing a semicolon at the end…

Your BLYNK_WRITE(V3) function is missing a closing curly bracket at the end…

You also have an opening curly bracket in the wrong place, near your void loop() section of code…

It should look like this…

void setup()
  {
    Serial.begin(115200);
    Blynk.begin(auth, ssid, pass);
    pinMode(D0, OUTPUT); //GPIO 16;
    pinMode(D1, OUTPUT); //GPIO 05;
    pinMode(D2, OUTPUT); //GPIO 04;
    pinMode(D3, OUTPUT); //GPIO 00;
  }

void loop()
  { // <<< This opening curly bracket has been moved to the correct location
    Blynk.run();
  }

In addition, you are doing something weird with your Blynk auth token.
Blynk gives you three lines of code to place at the beginning of your sketch, which includes the template ID, Template Name and Auth token…

However, you aren’t using the BLYNK_AUTH_TOKEN that Blynk is providing, instead, you’re defining another variable called auth and using that in your Blynk.begin() command…

It would be better to delete this line of code…

(which would also solve your first syntax error), and change your Blynk.begin() command to this…

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

Pete.

Thank you Pete
I was tearing my hair out with this code.
I had so many versions of this sketch with something wrong in each one.
With your corrections I then noticed I had the active lows and highs around the wrong way. That fixed, it is working as it should.

Thank you again, and all the best for the New Year
Chris

1 Like