Program error using ESP8266

Hi Guys

Hoping you can help again. Thanks PeterKnight for the previous assistance.

I had my Blynk working well but occasionally WiFi would drop out and I had to restart it by powering down and up again.
That’s fine but the whole idea was to have it working while I was away.

I found this segment of program that would check it if it went down and it would restart.
Well I have inserted it into my program but I keep getting a compilation error of "a function-definition is not allowed here

before token"

The pink bar appears below the line Blynk_Write(V4).

If I delete that entire segement assuming it is faulty, the error appears just after the previous Blynk_Write(V3).
I have looked everywhere to find the problem but to no avail.

Could someone please assist in where I have gone wrong?.

Thanks in advance

Chrisk

// Sketch name - GHA03  8/03/2026 16:30
// Added auto-reconnect after loss of Wifi
// Added STOP to be able to halt current process - open circuits power to timers
// This a new version after boot interference with old outputs
// This program allows the control of four seperate relays via the Mobile Blynk App
// Sketch names must not have spaces in the name
#include <dummy.h>

#define BLYNK_TEMPLATE_ID "XXXXXX" // My Template ID gained from Blynk Template
#define BLYNK_TEMPLATE_NAME "XXXXX"  // My Template Name gained also from Blynk Template
#define BLYNK_AUTH_TOKEN "XXXXXi"  // My Blynk Authority Token for GHWater gained from Blynk Template 

#include <ESP8266WiFi.h> // Use this library
#include <BlynkSimpleEsp8266.h>  // Use this library

const char ssid[] = "XX"; // My network name
const char password[] = "XX"; // My network passwordword

void setup() {
  pinMode(D8, OUTPUT); //GPIO 15  Water Left
  pinMode(D2, OUTPUT); //GPIO 04  Water Right
  pinMode(D5, OUTPUT); //GPIO 14  Misting
  pinMode(D7, OUTPUT); //GPIO 13  Water Enable
  pinMode(D6, OUTPUT); //GPIO 12  ALL STOPPED

  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
  WiFi.begin(ssid, password);

  // Enable auto-reconnect
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected!");
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected, waiting for reconnection...");
  } else {

    // Program starts below

    BLYNK_WRITE(V0) // A write to pin D8 - Water Left
    {
      int value = param.asInt(); // Get interger value from V0
      Serial.println(value);  // Used to display value in Serial Monitor in Tools
      if (value == 0)
      {
        digitalWrite(D8, LOW);  // Setting Digital PIN LOW to turn OFF device if relay module is "active high"
        Serial.println("Water Left OFF");
      }
      if (value == 1)
      {
        digitalWrite(D8, HIGH);  //Setting Digital PIN HIGH to turn ON device if relay module is "active high"
        Serial.println("Water Left ON");
      }
    }

    BLYNK_WRITE(V1) // A write to pin D2 - Water Right
    {
      int value = param.asInt();
      Serial.println(value);
      if (value == 0)
      {
        digitalWrite(D2, LOW);
        Serial.println("Water Right OFF");
      }
      if (value == 1)
      {
        digitalWrite(D2, HIGH);
        Serial.println("Water Right ON");
      }
    }

    BLYNK_WRITE(V2) // A write to pin D5 - Mist
    {
      int value = param.asInt();
      Serial.println(value);
      if (value == 0)
      {
        digitalWrite(D5, LOW);
        Serial.println("Mist OFF");
      }
      if (value == 1)
      {
        digitalWrite(D5, HIGH);
        Serial.println("Mist ON");
      }
    }

    BLYNK_WRITE(V3) // A write to pin D7 - Water Enable
    {
      int value = param.asInt();
      Serial.println(value);
      if (value == 0)
      {
        digitalWrite(D7, LOW);
        Serial.println("Water Enable OFF");
      }
      if (value == 1)
      {
        digitalWrite(D7, HIGH);
        Serial.println("Water Enable ON");
      }
    }
    BLYNK_WRITE(V4) // A write to pin D6 - All STOPPED
    {
      int value = param.asInt();
      Serial.println(value);
      if (value == 0)
      {
        digitalWrite(D6, LOW);
        Serial.println("Water Enable OFF");
      }
      if (value == 1)
      {
        digitalWrite(D6, HIGH);
        Serial.println("Water Enable ON");
      }
    }
    delay(10000) // Check every 10 seconds for WiFi connection
    {
      Blynk.run();
    }

Forget trying to salvage the mess you’ve created by attempting to modify your void loop, the solution you’ve found won’t help anyway.

I’ll take a look at this later and suggest an alternative solution.

Pete.

Thank you Peter.

Chris

The Blynk.begin() command automatically creates the WiFi connection, so trying to manage that manually with a WiFi.begin(0) command makes no sense.
The best option is to restart your device if it loses WiFi connection and can’t automatically reconnect.

I’ve added-in some code to automatically synchronise the device with the Blynk server when it starts-up and connects to Blynk.

// Sketch name - GHA03  8/03/2026 16:30
// Added auto-reconnect after loss of Wifi
// Added STOP to be able to halt current process - open circuits power to timers
// This a new version after boot interference with old outputs
// This program allows the control of four seperate relays via the Mobile Blynk App
// Sketch names must not have spaces in the name
#include <dummy.h>

#define BLYNK_TEMPLATE_ID "XXXXXX" // My Template ID gained from Blynk Template
#define BLYNK_TEMPLATE_NAME "XXXXX"  // My Template Name gained also from Blynk Template
#define BLYNK_AUTH_TOKEN "XXXXXi"  // My Blynk Authority Token for GHWater gained from Blynk Template 

#include <ESP8266WiFi.h> // Use this library
#include <BlynkSimpleEsp8266.h>  // Use this library

const char ssid[] = "XX"; // My network name
const char password[] = "XX"; // My network password

void setup() 
{
  pinMode(D8, OUTPUT); //GPIO 15  Water Left
  pinMode(D2, OUTPUT); //GPIO 04  Water Right
  pinMode(D5, OUTPUT); //GPIO 14  Misting
  pinMode(D7, OUTPUT); //GPIO 13  Water Enable
  pinMode(D6, OUTPUT); //GPIO 12  ALL STOPPED

  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
}

BLYNK_CONNECTED()
{
  // When the device connects or re-connects to the Blunk server, request the current statuses for pins V0-4...
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
}

void loop() 
{
  if (WiFi.status() != WL_CONNECTED) 
  {
    Serial.println("No WiFi connection, restarting...");
    ESP.restart();
  }
  else
  {
    Blynk.run();
  }
}


// Program starts below

BLYNK_WRITE(V0) // A write to pin D8 - Water Left
{
  int value = param.asInt(); // Get interger value from V0
  Serial.println(value);  // Used to display value in Serial Monitor in Tools
  if (value == 0)
  {
    digitalWrite(D8, LOW);  // Setting Digital PIN LOW to turn OFF device if relay module is "active high"
    Serial.println("Water Left OFF");
  }
  if (value == 1)
  {
    digitalWrite(D8, HIGH);  //Setting Digital PIN HIGH to turn ON device if relay module is "active high"
    Serial.println("Water Left ON");
  }
}

BLYNK_WRITE(V1) // A write to pin D2 - Water Right
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 0)
  {
    digitalWrite(D2, LOW);
    Serial.println("Water Right OFF");
  }
  if (value == 1)
  {
    digitalWrite(D2, HIGH);
    Serial.println("Water Right ON");
  }
}

BLYNK_WRITE(V2) // A write to pin D5 - Mist
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 0)
  {
    digitalWrite(D5, LOW);
    Serial.println("Mist OFF");
  }
  if (value == 1)
  {
    digitalWrite(D5, HIGH);
    Serial.println("Mist ON");
  }
}

BLYNK_WRITE(V3) // A write to pin D7 - Water Enable
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 0)
  {
    digitalWrite(D7, LOW);
    Serial.println("Water Enable OFF");
  }
  if (value == 1)
  {
    digitalWrite(D7, HIGH);
    Serial.println("Water Enable ON");
  }
}
BLYNK_WRITE(V4) // A write to pin D6 - All STOPPED
{
  int value = param.asInt();
  Serial.println(value);
  if (value == 0)
  {
    digitalWrite(D6, LOW);
    Serial.println("Water Enable OFF");
  }
  if (value == 1)
  {
    digitalWrite(D6, HIGH);
    Serial.println("Water Enable ON");
  }
}

I’m curious why you have this line in your sketch…

What is in your dummy.h file?

BTW, I’ve not tested the loss of WiFi functionality, so you should ensure that it works correctly.

Pete.

Hi Peter
Being new to Blynk I looked over the internet and found snippets of files that I could use.
#include <dummy.h) appeared in one of the programs and it seemed to have worked with it in, so I figured it was necessary.

After finding the file in Arduino IDE, all that was in it was the message -

// This file is here only to silence warnings from Arduino IDE

// Currently IDE doesn’t support no-code libraries, like this collection of example sketches.

So with no data within it I can reasonably assume it is unnecessary.

I will load this into my ESP and give a go.
I will get back to you after a fortnight or so to advise how it is going.

Currently the best I have had without dropouts was about six days.

It is amazing how one can look at code and not see an error.
With the highlighted program on the forum I noticed the Serial.println in (V3) and (V4) were the same which I have now corrected.

Thank you Peter for the time you put in to assist people like me in the Blynk Community.
You are a real CHAMPION.

Chrisk

1 Like