Created
March 5, 2021 18:57
-
-
Save meksor/5ab009ecdf40595c019edc5d3aeb56ff to your computer and use it in GitHub Desktop.
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
#include <ESP8266WiFi.h> | |
#include <NTPClient.h> | |
#include <FS.h> | |
#include <WiFiUdp.h> | |
#include <ESP8266WebServer.h> | |
#define DEBUG true | |
#define PIN_TRIGGER D5 | |
#define PIN_ECHO D6 | |
// WiFi | |
const char *ssid = "default"; | |
const char *password = "password"; | |
WiFiClient client; | |
// Time | |
const long utcOffsetInSeconds = 3600; | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP, "ts1.univie.ac.at", utcOffsetInSeconds); | |
// Web Server | |
ESP8266WebServer server(80); | |
// CSV File | |
File f; | |
// === Setup === | |
void setup() { | |
#ifdef DEBUG | |
Serial.begin(115200); | |
#endif | |
setupUltrasonic(PIN_TRIGGER, PIN_ECHO, 1); | |
setupWifi(); | |
setupNtp(); | |
setupFs(); | |
setupServer(); | |
} | |
void setupServer() { | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
} | |
void handleNotFound() { | |
if (loadFromSpiffs(server.uri())) | |
return; | |
String message = "File Not Detected\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i = 0; i < server.args(); i++) | |
{ | |
message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
#ifdef DEBUG | |
Serial.println(message); | |
#endif | |
} | |
// | |
// Return the file from the Flash | |
// | |
bool loadFromSpiffs(String path) | |
{ | |
// | |
// Default content-type. | |
// | |
String dataType = "text/plain"; | |
// | |
// Indexes are index.htm | |
// | |
if (path.endsWith("/")) | |
path += "index.htm"; | |
// | |
// Guess content-type by searching for suffix. | |
// | |
if (path.endsWith(".htm")) | |
dataType = "text/html"; | |
else if (path.endsWith(".css")) | |
dataType = "text/css"; | |
else if (path.endsWith(".js")) | |
dataType = "application/javascript"; | |
else if (path.endsWith(".png")) | |
dataType = "image/png"; | |
else if (path.endsWith(".gif")) | |
dataType = "image/gif"; | |
else if (path.endsWith(".jpg")) | |
dataType = "image/jpeg"; | |
else if (path.endsWith(".ico")) | |
dataType = "image/x-icon"; | |
else if (path.endsWith(".xml")) | |
dataType = "text/xml"; | |
else if (path.endsWith(".pdf")) | |
dataType = "application/pdf"; | |
else if (path.endsWith(".zip")) | |
dataType = "application/zip"; | |
// | |
// Open the file, if we can | |
// | |
File dataFile = SPIFFS.open(path.c_str(), "r"); | |
if ( ! dataFile ) | |
return false; | |
// | |
// Serve it. | |
// | |
server.streamFile(dataFile, dataType); | |
dataFile.close(); | |
return true; | |
} | |
// const char *csvHeader = "epoch,distance"; | |
void setupFs() { | |
SPIFFS.begin(); | |
#ifdef DEBUG | |
Serial.println("data.csv size:"); | |
Serial.println(f.size()); | |
#endif | |
} | |
void setupNtp() { | |
timeClient.begin(); | |
timeClient.update(); | |
#ifdef DEBUG | |
Serial.println("Current Time:"); | |
Serial.println(timeClient.getFormattedTime()); | |
#endif | |
} | |
void setupWifi() { | |
WiFi.begin(ssid, password); | |
int retries = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
#ifdef DEBUG | |
Serial.print ("."); | |
#endif | |
} | |
#ifdef DEBUG | |
Serial.println(F("WiFi connected!")); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
#endif | |
} | |
void setupUltrasonic(short trigger, short echo, short no) { | |
// initialize pins for HC-SR04 | |
pinMode(trigger, OUTPUT); | |
pinMode(echo, INPUT); | |
// reset the trigger pin and wait a half a second | |
digitalWrite(trigger, LOW); | |
delayMicroseconds(500); | |
// initial test measurement | |
float duration = measureDistance(trigger, echo); | |
#ifdef DEBUG | |
Serial.print("Initial test measurement Sensor "); | |
Serial.print(no); | |
Serial.print(": "); | |
Serial.print(duration); | |
Serial.println(); | |
#endif | |
} | |
// === / Setup === | |
// === Loop === | |
void loop() { | |
float dist = measureDistance(PIN_TRIGGER, PIN_ECHO); | |
if (dist < 150.) { | |
#ifdef DEBUG | |
Serial.println("=== Detected Object ==="); | |
Serial.print("Distance: "); | |
Serial.print(dist); | |
Serial.print("cm (?)"); | |
Serial.println(); | |
Serial.print("Timestamp: "); | |
Serial.print(timeClient.getFormattedTime()); | |
Serial.println(); | |
#endif | |
writeData(dist, timeClient.getEpochTime()); | |
} else { | |
server.handleClient(); | |
} | |
delay(10); | |
} | |
void writeData(float dist, unsigned long epoch) { | |
f = SPIFFS.open("/data.csv", "a"); | |
if (f) { | |
f.print(dist); | |
f.print(","); | |
f.print(epoch); | |
f.println(); | |
f.flush(); | |
} | |
f.close(); | |
} | |
float measureDistance(const short &trigger, const short &echo) { | |
long duration = 0; | |
delayMicroseconds(5); | |
// trigger | |
noInterrupts(); | |
digitalWrite(trigger, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigger, LOW); | |
// reads the echoPin, returns the sound wave travel time in microseconds | |
// timeout in microseconds corresponds to roughly 4m distance to the object at | |
// roughly 4°C, if its warmer the distance gets bigger | |
duration = pulseIn(echo, HIGH, 24000); | |
interrupts(); | |
// pulseIn will only return 0 if it timed out. | |
// therefore we reset the HC-SR04 to increase measurment accuracy | |
if (duration == 0) { // if the measurment timed out | |
pinMode(echo, OUTPUT); // then we set echo pin to output mode | |
digitalWrite(echo, LOW); // we send a LOW pulse to the echo pin | |
delayMicroseconds(200); | |
pinMode(echo, INPUT); // and finally we come back to input mode | |
} | |
if (!duration) duration = 22520; | |
return duration / 56.3; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment