Sketch
Sketch for HC-SR04 Connected to ESP8266 Updated
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Thing.h>
#include <WebThingAdapter.h>
#include <HCSR04.h>
#include <ESP8266wifi.h>
// defines pins numbers Note power supply set to 5V as Chinese sensors did not work with 3V
const int trigPin = 2; //D4
const int echoPin = 0; //D3
// defines variables
long duration;
int distance;
int litres;
//
//TODO: Hardcode wifi credentials here (and keep it private)
//
const char* ssid = "my network"; //Must include ""
const char* password = "password"; //Must include ""
//
/////////////////////////////////////////////////////////////////////
//
const char *deviceTypes[] = {"UltrasonicSensor", "Sensor", nullptr};
ThingDevice device("HCSR04","Water Tank Storage", deviceTypes);
// The ThingDevice represents the physical object we want to put on the web;
ThingProperty StorageProperty("Stored Litres", "HCSR04 Distance", NUMBER, "StorageProperty");
// ThingProperty variable_name( id, description, type, @type, label, unit, writable );
WebThingAdapter *adapter;
// The WebThingAdapter knows how to speak to the Web of Things API with our Gateway and handles all the translation necessary for the Gateway to discover and interact with the ThingDevice;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
// HCSR04.begin();
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("");
//
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
//
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//
//
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("ESP_HCSR04", WiFi.localIP());
device.addProperty(&StorageProperty);
adapter->addDevice(&device);
//
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(device.id);
// 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);
// Calculating the distance
distance= duration*0.034/0.2; //In mm was /2 in cms
Serial.print("Water Level Below Full Capacity: ");
Serial.print(distance, 4);
Serial.print(" mm, ");
}
void loop() {
// 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);
// Calculating the distance
distance= (duration*0.034/0.2)-230; //In mm was /2 in cms where 230 is distance from sensor to max water level tank full
// Total depth of 1330mm holds capacity of 15000 litres which equates to 11.27819549 litres per mm of depth
// water in tank =15000-(11.27819549*distance)
litres=15000-(11.27819549*distance); //Water_in_tank_in_litres
ThingPropertyValue value;
value.number = litres;
StorageProperty.setValue(value);
// Serial.println(temperatureInCelsius);
// delay(1000);
//
adapter->update();
// Prints the distance on the Serial Monitor
// Serial.print("Water Level Below Full Capacity: "); // Changed from Distance to water level
// Serial.print(distance-100); // Subtract distance in mm from sensor to max water level
// Serial.println(" mm Where 0 mm is Full and 1500 is empty "); // println adds line feed
delay(2000);
}