Help me integrate working code into Blynk - ESP8266 WOL via Blynk

Hi, I have tried this code on my generic ESP8266 and it does what it’s supposed to do, namely my PC powers up when the code runs.

#include <ESP8266WiFi.h>

#include <WiFiUdp.h>

#include <WakeOnLan.h>

WiFiUDP UDP;

WakeOnLan WOL(UDP);

const char* ssid     = "xxx";

const char* password = "xxx";

void wakeMyPC() {

    const char *MACAddress = "xx:xx:xx:xx:xx:xx";

 

    WOL.sendMagicPacket(MACAddress); // Send Wake On Lan packet with the above MAC address. Default to port 9.

    // WOL.sendMagicPacket(MACAddress, 7); // Change the port number

}

void setup()

{

    WOL.setRepeat(3, 100); // Optional, repeat the packet three times with 100ms between. WARNING delay() is used between send packet function.

    WiFi.mode(WIFI_STA);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {

        delay(500);

        Serial.print(".");

    }

    WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); // Optional  => To calculate the broadcast address, otherwise 255.255.255.255 is used (which is denied in some networks).

   

    wakeMyPC();

   

}

void loop()

{

}

however I am a noob and can’t figure out how to integrate it into Blynk. I have tried this project but for some reason the board restarts every couple seconds (confirmed by the Monitor in the Arduino IDE). I suppose it’s the code but I’m too noob to fix it.
Any help appreciated. Thanks!

@horiaene 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.

Pete.

Thanks Pete, now it looks way cleaner!

While waiting for a response on this matter I had the idea to ask ChatGPT for a solution, not being overly optimistic about it. Lo and behold, I actually got a working code that does what I wanted… for anyone interested, here it is:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <WakeOnLan.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

WiFiUDP UDP;
WakeOnLan WOL(UDP);

BlynkTimer timer;

void wakeMyPC() {
  const char *MACAddress = "xx:xx:xx:xx:xx:xx";
  
  WOL.sendMagicPacket(MACAddress);
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  WOL.setRepeat(3, 100);
  WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());

  timer.setInterval(100L, []() {
    Blynk.run();
  });
}

void loop() {
  timer.run();
}

BLYNK_WRITE(V1) { // Virtual pin 1 is mapped to the button widget
  if (param.asInt()) {
    wakeMyPC();
    Serial.println("Sending Wake-On-LAN packet...");
  }
}

Only an AI bot would do this!

You don’t need the BlynkTimer object declaration, or the code above in your void setup, or timer.run() in your void loop.
You just need Blynk.run() in your void loop.

Pete.

1 Like