{
// Invert state, since button is "Active LOW"
int isButtonPressed = !digitalRead(2);
if (isButtonPressed) {
Serial.println("Button is pressed.");
// Note:
// We allow 1 notification per 15 seconds for now.
Blynk.notify("Yaaay... button is pressed!");
}
}
I can’t wrap head around that, could you elaborate more on int isButtonPressed = !digitalRead(2);?
I see that we are first declaring an integer of ‘isButtonPresses’ with the default reading being 2?
I am completely lost around this and I have even had my morning coffee.
I thought I could add some code after Blynk.virtualWrite such as
Serial.println("Device activated by Smartwatch");
Blynk.notify("Device activated by Smartwatch");
Doing this shows up the text in the serial monitor, but I don’t get a push notification. I have reinstalled the app and ensured the app has full access to android notifications and can override Do Not Disturb etc. Can I do it this way?
I have no idea how this relates to your question, but here here goes…
This is basically saying “set the integer variable isButtonPressed to the opposite of the result that you get when you do a digital read on pin GPIO2”
The exclamation mark operator “!” means NOT, which is effectively saying “the opposite of”, so if digitalRead(2) returns HIGH then the result od applying a NOT operator will be LOW (or 0 as it’s an integer).
Essentially, I found this code from the Blynk example browser
void notifyOnButtonPress()
{
// Invert state, since button is "Active LOW"
int isButtonPressed = !digitalRead(2);
if (isButtonPressed) {
Serial.println("Button is pressed.");
// Note:
// We allow 1 notification per 5 seconds for now.
Blynk.notify("Yaaay... button is pressed!");
// You can also use {DEVICE_NAME} placeholder for device name,
// that will be replaced by your device name on the server side.
//Blynk.notify("Yaaay... {DEVICE_NAME} button is pressed!");
}
}
But I don’t really understand it and why/if I would need to set it up in this format, it truly isn’t sinking in.
So, in plain language, this code is saying if the button is pressed (reading a high) then the integer value of isButtonPressed will read 0? Then the next line of code appears to be an if statement, but I don’t see a typical “if this is true then that” type of argument that follows.
I am sorry, I can’t understand logically how this code is playing a role to push an in-app notification?
As I now have separated the BLYNK_WRITE Codes, I am wondering why I couldn’t add the “Blynk.notify” statement as per the following code?
BLYNK_WRITE(V21) // Executes when the value of virtual pin 21 changes (By Smartwatch)
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(4,HIGH); // Set NodeMCU D2 to HIGH
timer.setTimeout(1000L, []() { // Wait 1s then...
digitalWrite(4,LOW); // Set NodeMCU D2 to LOW
Blynk.virtualWrite(V21,0); // Turn the widget attached to V21 Off
Serial.println("Spare Relay activated by Smartwatch");
Blynk.notify("Spare Relay activated by Smartwatch");
}); // END of Lambda Function
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(4,LOW); // Set NodeMCU D2 to LOW
}
}
Yes I do, with the following settings
Notify when hardware goes offline - Set to ON (and I do get those ones)
I’m not sure what’s happening with your notifications.
They work for me when using the Frankfurt cloud server, but I guess you’re using a different server.
Do notifications work if you do them with an API call?
But is what I have below in my code - specifically the line with Blynk.Notify meant to work? Do I have to add anything else perhaps?
Blynk.virtualWrite(V21,0); // Turn the widget attached to V21 Off
Serial.println("Spare Relay activated by Smartwatch");
Blynk.notify("Spare Relay activated by Smartwatch");
I have also setup Blynk to connect to Singapore server (closest to me in Australia) IP address of 188,166,206,43 - not sure if the IP address is right through, pinched that from elsewhere in this forum.
Add details :
• Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
• Smartphone OS (iOS or Android) + version
• Blynk server or local server
• Blynk Library version
• Add your sketch code. Code should be formatted as example below.