ESP32 - send a push notification from the Arduino ESP32 device to your phone

Sending a push notification to your phone from an event from an ESP32 is simple.

What you'll need (if you are using a Mac for development):

Arduino IDE

Arduino for ESP32

Serial port simulator for ESP32 to upload your code to the board

Wifi network

IFTTT account

IFTTT app on your phone

Wifi network that your ESP32 board can access


Architecture


Create an IFTTT applet

This should read something like this:

If Maker Event "notification", then Send a notification from the IFTTT app

Change the notification template

Go to the settings of your new IFTTT applet and change the template to include "Value1", like this:


Get your Webhook endpoint in IFTTT

On your applet, click the triangle looking thing (webhook), then Settings, or simply go to this URL https://ifttt.com/maker_webhooks/settings 

Visit the private URL in your browser, replace the trigger name with "notification", so it reads like

...trigger/notification/with/...

and in the Value1 field type in a test message. Click "Test it" and your phone should receive the push.

Code to put on your ESP32 - Arduino

The following code will send a POST request to IFTTT to trigger a message. Replace the values from the previous settings page.


#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "YOUR WIFI NETWORK NAME";
const char* password = "YOUR WIFI PASSWORD";

//Your IFTTT webhook private address
const char* serverName = "https://maker.ifttt.com/trigger/notification/with/key/CHANGE_THIS_IS_THE_KEY";

const int BUTTON_PIN = 0;
const int LED_PIN = 2;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  //Send an HTTP POST request on button press
  if (WiFi.status() == WL_CONNECTED) {
    if (digitalRead(BUTTON_PIN) == LOW)
    {
      while (digitalRead(BUTTON_PIN) == LOW)
        ; // Wait for button to be released
      HTTPClient http;

      http.begin(serverName);

      http.addHeader("Content-Type", "application/json");
      String httpRequestData = "{\"value1\":\"YOUR MESSAGE GOES HERE\"}";
      int httpResponseCode = http.POST(httpRequestData);

      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);

      http.end();
    }
  }
  else {
    Serial.println("WiFi Disconnected");
    delay(5000);
  }
  lastTime = millis();
  delay(100);
}

Comments

Popular posts from this blog

MurMurHash3, an ultra fast hash algorithm for C# / .NET

Octoprint as a systemd service - running it with high priority