Help with new Blynk

Yes. Does that make the gauges work?

Pete.

no.

now it turns on the server by itself

without me touching the button

Okay, put it back to how it was. I’ll take a more detailed look at the logic later.

Pete.

ok, thanks.

Okay, give this sketch a try…

#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define DEBUG
#ifdef DEBUG
  #define BLYNK_PRINT Serial
  //#define BLYNK_DEBUG
  //#define ENABLE_DEBUG_PING
#endif

#define BLYNK_NO_BUILTIN  //disable built-in analog and digital operations.
//#define BLYNK_NO_INFO   //disable providing info about device to the server. (saving time)

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

//blynk colors
#define BLYNK_GREEN   "#23C48E"
#define BLYNK_BLUE    "#04C0F8"
#define BLYNK_YELLOW  "#ED9D00"
#define BLYNK_RED   "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"

//WiFi config
const char auth[] = "REDACTED";
const char ssid[] = "REDACTED";
const char pass[] = "REDACTED";

const IPAddress ip(192, 168, 0, 206);
const IPAddress gateway(192, 168, 1, 254);
const IPAddress bcastAddr(192, 168, 1, 255);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress dns(8, 8, 8, 8);

//WOL device config
const IPAddress device_ip(192, 168, 1, 67);
byte macAddr[6] = {0xE0, 0x69, 0x95, 0xDE, 0x07, 0x3D};

//Alert config
const char email[] = "REDACTED";
const char device_name[] = "REDACTED";
const uint16_t boot_time = 45;  //number for countdown (It does not represent seconds, read the known issues!)

//WOL
#define MAGIC_PACKET_LENGTH 102
#define PORT_WAKEONLAN 9
byte magicPacket[MAGIC_PACKET_LENGTH];
unsigned int localPort = 9;

WiFiUDP udp;

// virtual pins
#define STATE_PIN      V0
#define BUTTON_PIN     V1
#define PING_TIME_PIN  V2
#define RSSI_PIN       V3

//state
struct WOLServerState
  {
    bool IsOnline;
    uint16_t boot_time;
    bool boot_error;
    uint16_t ping;
    uint32_t previousMillis;
    uint32_t interval;
  };
WOLServerState currentState = { false, 0, false, 0, 0, 5000UL };

void setup()
{
  #ifdef DEBUG
    Serial.begin(74880);
  #endif

  connectWiFi();
  connectBlynk();

  if (udp.begin(localPort) == 1)
  {
    BLYNK_LOG("udp begin OK");
    buildMagicPacket();
  }
  else
  {
    delay(500);
    ESP.restart();
  }
}

void connectWiFi()
{
  WiFi.mode(WIFI_STA);
  WiFi.hostname("WOL server");
  WiFi.config(ip, dns, gateway, subnet);
  WiFi.begin(ssid, pass);

  int count = 0;
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(250);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(250);
    digitalWrite(LED_BUILTIN, LOW);

    count++;
    if (count > 20)
    {
      delay(500);
      ESP.restart();
    }
  }
  //BLYNK_LOG("WiFi connected, IP: %s", WiFi.localIP().toString());
}

void connectBlynk()
{
  Blynk.config(auth);
  Blynk.disconnect();

  int count = 0;
  while (Blynk.connect(10000) == false)
  {
    delay(250);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(250);
    digitalWrite(LED_BUILTIN, LOW);

    count++;
    if (count > 20)
    {
      delay(500);
      ESP.restart();
    }
  }
  BLYNK_LOG("Blynk connected");
}

void loop() {
  // Reconnect WiFi
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
    return;
  }

  // Reconnect to Blynk Cloud
  if (!Blynk.connected()) {
    connectBlynk();
    return;
  }

  uint32_t currentMillis = millis();
  if (currentMillis - currentState.previousMillis >= currentState.interval)
  {
    currentState.previousMillis = currentMillis;
    Update_Blynk(); // call the function that updates the Blynk app

    if (currentState.boot_time == 0)
    {
      currentState.interval = 5000UL;
    }
    else
    {
      currentState.boot_time--;
      if (currentState.boot_time == 0)
      {
        currentState.boot_error = true;
        //Blynk.email(email, "{DEVICE_NAME} : Alert", String(device_name) + " could not be turned on!"); // need to replace with log event later
      }
    }

    if (Ping.ping(device_ip, 1))
    {
      currentState.IsOnline = true;
      currentState.boot_error = false;
      currentState.boot_time = 0;
      currentState.ping = Ping.averageTime();
    }
    else
    {
      currentState.IsOnline = false;
      currentState.ping = 0;
    }
  }
  Blynk.run();
}

// Generate magic packet
void buildMagicPacket()
{
  memset(magicPacket, 0xFF, 6);
  for (int i = 0; i < 16; i++)
  {
    int ofs = i * sizeof(macAddr) + 6;
    memcpy(&magicPacket[ofs], macAddr, sizeof(macAddr));
  }
}

//BLYNK_CONNECTED()
//{
//  Blynk.syncVirtual(BUTTON_PIN);
//}

// BOOT PC button handler of application
BLYNK_WRITE(BUTTON_PIN)
{
  if (!currentState.IsOnline && currentState.boot_time == 0)
  {
    BLYNK_LOG("AppButtonWakeOnLan: value=%d", param.asInt());
    udp.beginPacket(bcastAddr, PORT_WAKEONLAN);
    udp.write(magicPacket, MAGIC_PACKET_LENGTH);
    udp.endPacket();

    currentState.boot_time = boot_time;
    currentState.interval = 1000UL;
  }
}

Update_Blynk()
{
  Blynk.virtualWrite(RSSI_PIN, WiFi.RSSI());
  Blynk.virtualWrite(PING_TIME_PIN, currentState.ping);

  if (currentState.IsOnline) 
  {
    Blynk.setProperty(STATE_PIN, "color", BLYNK_GREEN);
    Blynk.virtualWrite(STATE_PIN, String(device_name) + " is Online");

    Blynk.setProperty(BUTTON_PIN, "color", BLYNK_DARK_BLUE);
    Blynk.setProperty(BUTTON_PIN, "offLabel", String(device_name) + " running...");
    Blynk.setProperty(BUTTON_PIN, "onLabel", String(device_name) + " running...");
  }
  else if (!currentState.IsOnline && currentState.boot_time > 0)
  {
    Blynk.setProperty(STATE_PIN, "color", BLYNK_BLUE);
    Blynk.virtualWrite(STATE_PIN, "Waiting for ping...");

    Blynk.setProperty(BUTTON_PIN, "color", BLYNK_YELLOW);
    Blynk.setProperty(BUTTON_PIN, "offLabel", currentState.boot_time);
    Blynk.setProperty(BUTTON_PIN, "onLabel", "Waiting for ping...");
  }
  else if (!currentState.IsOnline && currentState.boot_time == 0 && currentState.boot_error)
  {
    Blynk.setProperty(STATE_PIN, "color", BLYNK_RED);
    Blynk.virtualWrite(STATE_PIN, "Oops! Something happened, Try It Again!");

    Blynk.setProperty(BUTTON_PIN, "color", BLYNK_YELLOW);
    Blynk.setProperty(BUTTON_PIN, "offLabel", "Try It Again");
    Blynk.setProperty(BUTTON_PIN, "onLabel", "Magic Packet has been sent");
  }
  else
  {
    Blynk.setProperty(STATE_PIN, "color", BLYNK_RED);
    Blynk.virtualWrite(STATE_PIN, String(device_name) + " is Offline");

    Blynk.setProperty(BUTTON_PIN, "color", BLYNK_BLUE);
    Blynk.setProperty(BUTTON_PIN, "offLabel", "Turn On");
    Blynk.setProperty(BUTTON_PIN, "onLabel", "Magic Packet has been sent");
  }
}

Pete.

1 Like

Gave an error, this is the error

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "LOLIN(WEMOS) D1 mini (clone), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, DOUT (compatible), 40MHz, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

sketch_may02a:222:16: error: expected constructor, destructor, or type conversion before ';' token

  222 | Update_Blynk()

      |                ^

C:\Users\santi\Documents\Arduino\sketch_may02a\sketch_may02a.ino: In function 'void loop()':

sketch_may02a:159:5: error: 'Update_Blynk' was not declared in this scope

  159 |     Update_Blynk(); // call the function that updates the Blynk app

      |     ^~~~~~~~~~~~

C:\Users\santi\Documents\Arduino\sketch_may02a\sketch_may02a.ino: At global scope:

sketch_may02a:222:1: error: ISO C++ forbids declaration of 'Update_Blynk' with no type [-fpermissive]

  222 | Update_Blynk()

      | ^~~~~~~~~~~~

Multiple libraries were found for "BlynkSimpleEsp8266.h"

 Used: C:\Users\santi\Documents\Arduino\libraries\blynk-library-master

 Not used: C:\Users\santi\Documents\Arduino\libraries\Blynk

exit status 1

expected constructor, destructor, or type conversion before ';' token



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Sorry, that should say void Update_Blynk()

Pete.

THANKS, now everything works perfectly, thanks for all the help, I was a bit of a pain in the ass but thanks for all the help. Just one last thing, could you delete or sensor my personal details in your sketch, and I will do on mien too so that its not here in teh forum, thanks.

Done.

Pete.

thanks.