I am currently working on a small at home projecting involving a WeMos D1 Mini and an HC-SR05 ultrasound distance sensor. This is currently running a server which, on request, should return the distance between the sensor and whatever is close to it. I know for a fact the server works as the test function returns 6 without issue, but I cannot get a proper reading from the sensor. Are there any obvious issues with my code that I have missed? I know that NewPingESP8266 does not officially support the HC-SR05 but I see no real reason it should not work as its practically identical to the supported HC-SR04. Here is my code, thanks in advance!
#include <NewPingESP8266.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebSrv.h>
const char* ssid = "Distance sensor";
const char* password = "123456789";
NewPingESP8266 sonar(14, 12, 450); // replace 12 and 11 with actual pins
String readDist() {
String distance = String(sonar.ping());
Serial.println(distance);
return distance;
}
String TestFunc() {
return String(3+3);
}
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
AsyncWebServer server(80);
void setup() {
pinMode(14, OUTPUT);
pinMode(12, INPUT);
Serial.begin(115200);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
server.on("/distance", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", readDist().c_str());
});
server.on("/test", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", TestFunc().c_str());
});
server.begin();
delay(1000);
Serial.println(readDist());
}
void loop() {
}
When trying to use the distance function, it thinks for considerably longer than the test function, but returns 0.