Created
August 25, 2022 03:09
-
-
Save bfritscher/7dd52da3c7f3b999bff811b3a574dab1 to your computer and use it in GitHub Desktop.
esp8266 nodemcu hc-sr04 with web server and wifi AP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* | |
ESP8266 Server + HC-SR04 ultrasound | |
required libraries: | |
- https://github.com/me-no-dev/ESPAsyncTCP | |
- https://github.com/me-no-dev/ESPAsyncWebServer | |
Adapted from | |
Rui Santos | |
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-hc-sr04-ultrasonic-arduino/ | |
Complete project details at https://randomnerdtutorials.com/esp8266-nodemcu-access-point-ap-web-server/ | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*********/ | |
// Import required libraries | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <Hash.h> | |
#include <ESPAsyncTCP.h> | |
#include <ESPAsyncWebServer.h> | |
const char* ssid = "ESP8266-Access-Point"; | |
// const char* password = "123456789"; | |
const int trigPin = 12; | |
const int echoPin = 14; | |
//define sound velocity in cm/uS /2 | |
#define HALF_SOUND_VELOCITY 0.0171 | |
long duration; | |
float distanceCm; | |
// Create AsyncWebServer object on port 80 | |
AsyncWebServer server(80); | |
// Generally, you should use "unsigned long" for variables that hold time | |
// The value will quickly become too large for an int to store | |
unsigned long previousMillis = 0; // will store last time DHT was updated | |
// Updates DHT readings every 1 seconds | |
const long interval = 1000; | |
const char index_html[] PROGMEM = R"rawliteral( | |
<!DOCTYPE HTML><html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style> | |
html { | |
font-family: Arial; | |
display: inline-block; | |
margin: 0px auto; | |
text-align: center; | |
} | |
h2 { font-size: 3.0rem; } | |
p { font-size: 3.0rem; } | |
.units { font-size: 1.2rem; } | |
.dht-labels{ | |
font-size: 1.5rem; | |
vertical-align:middle; | |
padding-bottom: 15px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>ESP8266 Server</h2> | |
<p> | |
<span class="dht-labels">Distance</span> | |
<span id="distance">%DISTANCE%</span> | |
<span class="units">cm</span> | |
</p> | |
</body> | |
<script> | |
setInterval(function ( ) { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
document.getElementById("distance").innerHTML = this.responseText; | |
} | |
}; | |
xhttp.open("GET", "/distance", true); | |
xhttp.send(); | |
}, 1000 ) ; | |
</script> | |
</html>)rawliteral"; | |
// Replaces placeholder with DHT values | |
String processor(const String& var){ | |
if(var == "DISTANCE"){ | |
return String(distanceCm); | |
} | |
return String(); | |
} | |
void setup(){ | |
// Serial port for debugging purposes | |
Serial.begin(115200); | |
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | |
pinMode(echoPin, INPUT); // Sets the echoPin as an Input | |
Serial.print("Setting AP (Access Point)…"); | |
// Remove the password parameter, if you want the AP (Access Point) to be open | |
WiFi.softAP(ssid); | |
IPAddress IP = WiFi.softAPIP(); | |
Serial.print("AP IP address: "); | |
Serial.println(IP); | |
// Print ESP8266 Local IP Address | |
Serial.println(WiFi.localIP()); | |
// Route for root / web page | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send_P(200, "text/html", index_html, processor); | |
}); | |
server.on("/distance", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send_P(200, "text/plain", String(distanceCm).c_str()); | |
}); | |
// Start server | |
server.begin(); | |
} | |
void loop(){ | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
// Clears the trigPin | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
// Sets the trigPin on HIGH state for 10 micro seconds | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Reads the echoPin, returns the sound wave travel time in microseconds | |
duration = pulseIn(echoPin, HIGH); | |
// Calculate the distance | |
distanceCm = duration * HALF_SOUND_VELOCITY; | |
// Prints the distance on the Serial Monitor | |
Serial.print("Distance (cm): "); | |
Serial.println(distanceCm); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment