How to Send a Push Notification to Your Phone With the Press of a Button
by netsnettok in Circuits > Arduino
1221 Views, 1 Favorites, 0 Comments
How to Send a Push Notification to Your Phone With the Press of a Button
Tutorial on how to send push notifications to your smartphone by pressing a push button. This tutorial uses the services of Pushingbox and Pushbullet.
Supplies
Hardware
- ESP8266 NodeMCU | Amazon link
- Pushbutton | Amazon link
- Connecting wires | Amazon link
Software & Websites
Connect Pushbutton to ESP8266 NodeMCU
Connect the Pushbutton to the ESP8266 NodeMCU and to your computer.
Check to See If the Button Works
Open Arduino IDE.
Go to File > Examples > 01.Basics > DigitalReadSerial
Be sure to change the int Pushbutton to the correct pin. In my case, it was on D0.
int pushButton = D0;
Also make sure the amount of baud matches up with your serial monitor. I set mine to 115200 baud.
Serial.begin(115200);
Upload your code and open the Serial Monitor. If you press the button and you see 0's appear, your button works!
Setting Up Pushingbox
Go to the Pushingbox website and create an account.
After creating an account you will see the dashboard. In the menu bar, select My Services.
Adding a Service
Click on the Add a service button and select Pushbullet service. Give your service a fitting name.
Now it asks us to fill in an Acces token, but we don't have one yet.
Getting the Acces Token
Go to Pushbullet.com and create an account. After creating an account, go to Settings and click on Generate acces key. You now have your Acces key.
Switch back to the Pushingbox website and fill it in the form. Click on Submit.
Create a Scenario on Pushingbox
On the menu bar, click on My Scenarios. Think up a name for your scenario and click on Add.
Adding an Action
Click on Add an action. Select the service you just made and think up a title and the message you want to send. Click on Submit.
Attempting to Set Up the Internet Connection in Arduino
Setting Up the Internet Connection in Arduino
After about an hour of troubleshooting I found this tutorial online which showed how to connect the esp8266 to wifi. I made a new file in Arduino IDE and pasted the code I found. Enter your wifi ssid and password.
#include "ESP8266WiFi.h"
const char* ssid = "ssid"; //Enter SSID
const char* password = "password"; //Enter Password
void setup(void)
{
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connection Successful");
Serial.print("The IP Address of ESP8266 Module is: ");
Serial.print(WiFi.localIP());// Print the IP address
}
void loop()
{
// EMPTY
}
Upload the code and open the Serial Monitor. It should be connected now.
Connect to Pushingbox
Add the following code underneath your wifi password. Be sure to add the device id from Pushingbox. You can find your device id on the Pushingbox website under My scenarios.
const char* host = "api.pushingbox.com";
const char* devid = "DEV ID HERE";
int pushButton = D0;
int value = 0;
Add the following code into your void loop(). This code will make sure the information is sent towards Pushingbot.
int buttonState = digitalRead(pushButton);
delay(5000);
value = digitalRead(pushButton);
if(value = 1){ //value from sensor
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/pushingbox";
url += "?devid=";
url += devid;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}else{;}
Upload the code.
Download Pushbullet App
Download the Pushbullet App to your smartphone. Here's a GooglePlay Store link.
Log in with your account and enable push notifications in the app.
You should now be bombarded with push notifications. However, we only want to receive a push notification when we're pressing the button.
Send a Notification by the Press of a Button
While looking at a tutorial about a button control LED, I noticed my function was wrong. It was:
if(value = 1)
But it should be:
if(value == 0)
When the button isn't pressed, it's value is set at 1. When the button is pressed it's value is set at 0. So because it's constantly at 1, it continues to send pushnotifications.
After we change the function and upload the code, it works as intended!
Now it won't constantly send push notifactions, but only when I press the button.