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

Hi All and PeteKnight
Sorry for the late reply.
While I was looking around my ESP8266 system I found that the 5V rail would drop when I operated a optically coupled relay.
I figured that I needed to attend to this first so I purchased another relay board but this time one that would operate on the 12V rail.
To my surprise the ESP8266 could not drive the opto circuit to operate the relay.
I put back the original 5V opto relay but used a dropping resistor of 470 ohms in series with the 12V supply.
There was a great improvement in the stability of the 5V rail.
I also purchased an ESP32 with an external antenna as the the WiFi signal at the ESP8266 was quite low.
Both these purchases took some time to arrive.
Then it was a matter of programming the ESP32.
I was using my development laptop which operated under Win 7 using the legacy version of Arduino.
It had worked for the ESP8266 but I had continuing error messages telling me there was something wrong.
I then installed the latest Arduino IDE on a Windows 10 machine.
Again error messages, the ESP wasn’t connected because of an incorrect driver and also the baud rate was incorrect at the COM port.
Finally I managed to get the ESP32 loaded.
The external antenna worked well with the weak WiFi signal but in testing, when turning off the modem to simulate WiFi dropout, the ESP32 would not connect to the WiFi, operating in exactly the same manner as the ESP8266.
Again the only way to connect was to power down and then power up, so unfortunately the ā€œrestartā€ instruction doesn’t appear to have fixed the problem.
Any suggestions would be appreciated.

Chris K

I have to say that I was totally baffled by this rep,y, u til I realised that it was really in response to this topic…

As you a now using different hardware, and therefore will have needed to make some changes to your code, I’d suggest that you do the following…

  1. explain exactly what type of ESP32 Dev board you are using.
  2. post your ESP32 code.
  3. explain what you see in your serial monitor when you disable your WiFi

Your 12v/5v rail and relay information meant nothing to me, as you’ve not share any info about your hardware setup.

Pete.