Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// Demo of ESP32 serving Ping1D sensor data via web interface
// Tested on a SparkFun ESP32 MicroMod with ATP carrier board
// ESP32 Arduino Core version 2.0.2
// Blue Robotics ping-arduino version 0.1.2 (930afed)
// ArduinoJson version 6.19.2
// ESP32 Arduino core
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>
#include <ESPmDNS.h>
// Blue Robotics Ping library
#include <ping1d.h>
// ArduinoJson library
#include <ArduinoJson.h>
const char *ssid = "ping"; // WiFi SSID for the network the ESP32 will create
const char *pass = "PingDemo!"; // WiFi password for the network the ESP32 will create
const char *host = "ping"; // hostname used for mDNS - you can navigate to http://ping.local
// IP address scheme used for the SoftAP
IPAddress esp32_ip(192, 168, 3, 1);
IPAddress gateway(192, 168, 3, 1);
IPAddress subnet(255, 255, 255, 0);
Ping1D ping { Serial2 }; // Serial2 is pins 16 and 17
WebServer server(80);
void handleRoot() {
// send the static page contents
// in a real application this should probably come from SPIFFS rather than being hardcoded
server.send(200, "text/html", "<!DOCTYPE html><html><head><title>ESP32 Ping1D Demo</title><script>function updateState() { const xhr = new XMLHttpRequest(); xhr.onload = function() { stateData = JSON.parse(this.responseText); document.getElementById('distance').innerHTML = stateData['distance']; document.getElementById('confidence').innerHTML = stateData['confidence']; };'GET', 'state'); xhr.send(); }</script></head><body><h1>ESP32 Ping1D Demo</h1><table><tr><td><b>Distance</b></td><td><div id='distance'></div></td></tr><tr><td><b>Confidence</b></td><td><div id='confidence'></div></td</tr></table><script>window.setInterval(updateState, 1000);</script></body></html>");
void handleState() {
// return the latest values from the Ping1D in a JSON dictionary
// in a real application this should be dynamically allocated and there should be bounds checking on serialization to a buffer
StaticJsonDocument<200> jsonDoc;
jsonDoc["distance"] = ping.distance();
jsonDoc["confidence"] = ping.confidence();
char payload[100];
serializeJson(jsonDoc, payload);
server.send(200, "application/json", payload);
void handle404() {
server.send(404, "text/plain", "Resource not found");
void setup() {
// init UARTs
Serial.begin(115200); // monitor UART - exposed to USB
Serial2.begin(115200); // Ping1D UART on pin 16 and 17
// configure ESP32 as an AP
WiFi.softAP(ssid, pass);
WiFi.softAPConfig(esp32_ip, gateway, subnet);
Serial.println("SoftAP configured");
// init mDNS
if (MDNS.begin(host)) {
Serial.println("mDNS started");
server.on("/", handleRoot); // serves the HTML and JavaScript code
server.on("/state", handleState); // responds with the JSON encoded Ping1D state
server.onNotFound(handle404); // catch all - responds with HTTP 404
Serial.println("HTTP server started");
// advertise the _http._tcp service
MDNS.addService("http", "tcp", 80);
Serial.println("Advertising _http._tcp");
void loop() {
ping.update(); // Ping1D housekeeping
server.handleClient(); // HTTP server housekeeping
delay(2); // yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment