Help with new Blynk

I would like to know how to make my code work with the new blynk. Its a WOL that worked with the old Mobile Blynk app, but with the new one my protect just disappeared.


I have this set up as datastreams

this is the code, I find wright it but it worked, the “-” that I used are just to fill in the spaces with my personal info I don’t want to show,. It worked perfectly, but just today the old blynk mobila app just shut down and it stoped working.

#define BLYNK_TEMPLATE_ID "TMPLEaiejjC_"
#define BLYNK_DEVICE_NAME "SDMNAS"

#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[] = "---------";
const char ssid[] = "--------";
const char pass[] = --------";

const IPAddress ip(--------);
const IPAddress gateway(--------);
const IPAddress bcastAddr(--------);
const IPAddress subnet(--------);
const IPAddress dns(--------);

//WOL device config
const IPAddress device_ip(--------);
byte macAddr[6] = {--------};

//Alert config
const char email[] = "--------";
const char device_name[] = "--------";
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;

//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(115200);
#endif

	connectWiFi();
	connectBlynk();

	//if (Blynk.connected()) {
		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;

		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!");
			}
		}

		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;
	}
}

BLYNK_READ(STATE_PIN) {
	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");
	}
}```


that is the last part of my config, just one button and that’s it

@Santi_Dome 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:
```

Pete.

done, thanks.

https://github.com/Pnoxi/ESP8266_Blynk_WOL_Server , this was the original tutorial, and I made it work following it, but with the change to Blynk IoT it stopped working and havent managed to make it work.

I have been trying and still nothing, the problem is mainly Blink not finding the device in the app to add it, but it is online and connected to the network, I saw this in my router setting and its says the ESP8266 is online.

I’m confused by this statement and the fact that you’ve included a screenshot of the mobile dashboard for the device in your post. You can’t get to this point if you’ve not added the device.

Does the device show as being online in the web console?
What do you see in your serial monitor when you boot the device?

Also, you have datastreams called PING and RSSI which you’ve defined as having minimum values of 0 and maximum values of 1. Any values outside of this range will be ignored, so when you do get the device working you wont see meaningful values in the widgets attached to these datastreams until you change these min/max values.

Pete.

ok, sorry to be this confusing. I have been working on it and I’m going to make a better summary: the problem is that I can’t find the device, the last thing I tries was this, code below.

This is the code, and it doesn’t pop up in the devices, then I found this code from Blynk, and when I use it I can see the device, so I just need a way of stitching those two codes together so that it appears as a device and I can use it, but also have the WOL part, or just make some adjustments to my new code for it to connect to the app.

#define BLYNK_DEVICE_NAME "SDMNAS"
#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[] = "-------------";
const char ssid[] = "-------------";
const char pass[] = "-------------";

const IPAddress ip(-------------);
const IPAddress gateway(-------------);
const IPAddress bcastAddr(-------------);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress dns(8, 8, 8, 8);

//WOL device config
const IPAddress device_ip(-------------);
byte macAddr[6] = {-------------;

//Alert config
const char email[] = "-------------";
const char device_name[] = "SDMNAS";
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;

//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(115200);
#endif

	connectWiFi();
	connectBlynk();

	//if (Blynk.connected()) {
		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;

		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!");
			}
		}

		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;
	}
}

BLYNK_READ(STATE_PIN) {
	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");
	}
}

if there is any way you could help me, hit me up, is something I realy need and could even pay something for it, its for my proyect and I really need it, I might not be able to pay much, for if it s something that can be done, tell me.

One of these sketches appears to be a Blynk Edgent sket6c (but its difficult to tell because you’ve posted a screenshot rather than the actual sketch), and the other is a non-Edgent sketch.

When you use the Edgent example, you provision the device (adding a dynamic auth token and telling the device the WiFi SSID and password) via the app.

When you use the non-Edgent sketch you don’t do the provisioning via the app. Instead, you hard-code the auth token, SSID and WiFi password in the sketch, here:

It really would help if you answered my questions…

Pete.

ok ok, sorry. It shows online when I use the code that I took a screenshot of, that one is only to connect to the app and it works. There is nothing in the serial monitor with the new code.

Forget that sketch, focus on the one that you posted.

You’ll need to un-comment this line…

Have you actually filled-in this information in your sketch…

If so, where did you obtain the auth token from?

Pete.

the auth token i got it from blynk cloud, its under my proyect.

I dont understand this

it doesnt let me comment because im a new user but hopefully we can comunicate by me edditing my old comments, hopefully we can communicate like this, if not is there a way to talk to you ?

also i Know that where is says auth i need to put the auth token, and i already put the wifi ssid and password, I just replaced them with-------- to not show it here

Screenshot please.

Remove the “//” characters from the beginning of the line of code to turn it from a comment into a command.

Are you saying that you HAVE obtained the auth token, but you don’t understand that it needs to go in the sketch were it says auth?
And that you don’t understand that the WiFi SSID and WiFi password need to go in ssid and pass?

Pete.

1 Like

Sorry to be hard to work with, ill try to answer a bit better.

like that?

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

yes, i have already put all those variables including the auth token that blynk cloud gave me.

Also I was wondering what part of my code could change for the app to set up the wifi ssid and password, instead of me putting it in the code, and for the app to pick it up.