COMMANDS

~/Downloads/arduino-ide_2.3.4_Linux_64bit.AppImage &> /dev/null
sudo apt install python3-pip
pip3 install pyserial
sudo usermod -aG userName dialout
sudo chmod a+rw /dev/ttyUSB0

EXAMPLES

(1) HTTP API Enpoints with JSON

// ESP32-WROOM-32U

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

// Wifi credentials
const char* ssid = "wifi_name";
const char* password = "wifi_password";

// Create a WebServer object on port 80
WebServer server(80);

// Handler for "/"
void handleRoot() {
  String body = "<!DOCTYPE html><html><head><title>ESP32</title></head><body>";
  body += "<h1>ESP32 HTTP Server!</h1>\n\n";
  body += "<a href=\"/status\">Show Status</a><br>\n";
  body += "<a href=\"/push\">Push Status</a><br>\n";
  body += "<a href=\"/serial\">Output Status</a><br>\n";
  body += "</body></html>";
  server.send(200, "text/html", body);
  Serial.println("Client IP: " + server.client().remoteIP().toString());
}

String getStatusJson() {
  StaticJsonDocument<512> jsonData;
  jsonData["wifi_connected"] = String(WiFi.status() == WL_CONNECTED ? "true" : "false");
  jsonData["signal_strength"] = String(WiFi.RSSI());
  jsonData["ssid"] = WiFi.localIP().toString();
  jsonData["ip"] = WiFi.localIP().toString();
  jsonData["uptime"] = String(millis() / 1000);
  jsonData["free_heap"] = String(ESP.getFreeHeap());
  jsonData["chip_model"] = String(ESP.getChipModel());
  jsonData["flash_size"] = String(ESP.getFlashChipSize());
  String jsonString;
  serializeJson(jsonData, jsonString);
  return jsonString;
}

void pushNotification(const String &message = ""){
  // Perform an HTTP GET request
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("http://ntfy.sh/subscription_topic");
    http.addHeader("Content-Type", "application/json");

    String payload;
    if (message.length() > 0){
      payload = message;
    } else {
      payload = getStatusJson();
    }

    int httpResponseCode = http.POST(payload);
    if (httpResponseCode == 200){
      Serial.println("Push notification succeeded!");
    } else {
      Serial.println("Push failed with error: " + httpResponseCode);
    }

    http.end();
  }
}

// Handler for "/status"
void handleStatus() {
  server.send(200, "application/json", getStatusJson());
}

// Handler for "/push"
void handlePush() {
  pushNotification();
  server.send(200, "text/plain", "");
}

// Handler for "/serial"
void handleSerial() {
  Serial.println(getStatusJson());
  server.send(200, "text/plain", "");
}

// Handler for 404
void handleNotFound() {
  server.send(404, "text/plain", "404: Not Found");
}

void setup() {
  // Setting up the Serial
  Serial.begin(115200);
  delay(1000);

  Serial.println("");
  Serial.println("");
  Serial.println("Serial Started");

  // Connect to Wi-Fi
  Serial.println("Starting WIFI");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting...");
  }
  Serial.println("Connected to Wi-Fi!");
  Serial.println("IP Address: " + WiFi.localIP().toString());

  // Define URL routes
  server.on("/", handleRoot);
  server.on("/status", handleStatus);
  server.on("/push", handlePush);
  server.on("/serial", handleSerial);
  server.onNotFound(handleNotFound);

  // Start the server
  server.begin();
  Serial.println("HTTP server started");

  // Push notification
  StaticJsonDocument<48> jsonData;
  jsonData["ip"] = WiFi.localIP().toString();
  String jsonString;
  serializeJson(jsonData, jsonString);
  pushNotification(jsonString);
}

void loop() {
  // Handle client requests
  server.handleClient();
}

(in progress)


DOCUMENTATIONS

  • ESP32 WebServer [Link]
  • ESP32 HTTPClient [Link]
  • ArduinoJson [Link]

ONLINE SERVICES

ntfy (pronounced notify) is a simple HTTP-based pub-sub service that sends notifications to your phone or desktop [Link]. Since it is open-source, it can be easily self-hosted [Link].

healthchecks.io is another HTTP-based open source tool that allows one to get general events from many origins, displayed in a single dashboard, and generate notifications for failures or absence of events [Link]. Read more about self-hosting it at [Link].