Closed thomasjoseph4321 closed 6 years ago
//uncomment these lines as well, write radius of tank and total height in units of inches,
//modify this line if you have to connect relay to some pin other than D4
//Connect trig of ultrasonic sensor at D1 and echo at D2, modify this line otherwise UltraSonicDistanceSensor distanceSensor(D1,D2); //D1 trig, D2=echo //these are default water level limits, these values will be applied until on reset until user sends new limits from webpage. int waterLevelLowerThreshold=3; int waterLevelUpperThreshold=12;
float volume; float liters; // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); Adafruit_MQTT_Publish waterLevel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/waterLevel");
/*** Sketch Code ****/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect();
String inputString = ""; // a string to hold incoming data String dataToSend=""; int waterLevelDownCount=0,waterLevelUpCount=0;
ESP8266WebServer server(80); void handleRoot() { server.send_P(200, "text/html;charset=UTF-8", index_html); }
void handleLevelRequest(){ server.send(200,"text",String(liters)); }
void handleNotFound(){
String message = "File Not Found\n\n";
server.send(404, "text/plain", message);
}
void handleStatus()
{
if(digitalRead(MOTOR_CONTROL_PIN)==0)//MOTOR ON
server.send(200, "text/plain","on");
else server.send(200, "text/plain","off");
}
void handleRangeSetting(){
waterLevelLowerThreshold=(server.arg(0)).toInt();
waterLevelUpperThreshold=(server.arg(1)).toInt();
Serial.print(waterLevelLowerThreshold);
Serial.print(":");
Serial.println(waterLevelUpperThreshold);
server.send(200, "text/plain", ""); }
void measure_Volume()
{
float heightInch=0.393701distanceSensor.measureDistanceCm();
Serial.println(heightInch);
if(heightInch>MAX_HEIGHT)
heightInch=MAX_HEIGHT;
if(heightInch<0)
heightInch=0;
volume=3.14RADIUSRADIUS(MAX_HEIGHT-heightInch);//MAX_HEIGHT-distance will give actual height,
liters=volume*0.0164 ;
Serial.println(liters);
if(liters<=waterLevelLowerThreshold)
waterLevelDownCount++;
else waterLevelDownCount=0;
if(liters>=waterLevelUpperThreshold)
waterLevelUpCount++;
else waterLevelUpCount=0;
waterLevel.publish(liters);
if(waterLevelDownCount==3)
{//TURN ON RELAY
Serial.println("motor turned on");
digitalWrite(MOTOR_CONTROL_PIN,LOW);//Relay is active LOW
}
if(waterLevelUpCount==3)
{//TURN OFF RELAY
Serial.println("motor turned off");
digitalWrite(MOTOR_CONTROL_PIN,HIGH);//Relay is active LOW
}
} void runPeriodicFunc() { static const unsigned long REFRESH_INTERVAL1 = 2100; // 2.1sec static unsigned long lastRefreshTime1 = 0;
if(millis() - lastRefreshTime1 >= REFRESH_INTERVAL1)
{
measure_Volume();
lastRefreshTime1 = millis();
} }
void MQTT_connect() { int8_t ret;
// Stop if already connected. if (mqtt.connected()) { return; }
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!"); }
void setup(void){ Serial.begin(115200); delay(100); pinMode(MOTOR_CONTROL_PIN, OUTPUT); WiFi.begin(ssid, password); Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP address:");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/level",handleLevelRequest);
server.on("/configRange",handleRangeSetting);
server.on("/motor_status",handleStatus);
server.onNotFound(handleNotFound);
server.begin(); Serial.println("HTTP server started"); }
void loop(void){ runPeriodicFunc();
MQTT_connect(); server.handleClient(); }
please install this library https://github.com/Martinsos/arduino-lib-hc-sr04
closing as no further response
Sir Thank you for this project Could assist me to resolve this error message I was received on sketch compiling time .i was following exact process you mentioned in README.MD file and also I am following your YouTube video.
ERROR MESSAGE
Arduino: 1.8.5 (Windows 7), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
waterLevel:182: error: expected constructor, destructor, or type conversion before 'distanceSensor'
UltraSonicDistanceSensor distanceSensor(D1,D2); //D1 trig, D2=echo
C:\Users\Thomas\AppData\Local\Temp\Rar$DIa12152.4961\waterLevel\waterLevel.ino: In function 'void measure_Volume()':
waterLevel:243: error: 'distanceSensor' was not declared in this scope
exit status 1 expected constructor, destructor, or type conversion before 'distanceSensor'
This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.
Complete Sketch
include
include
include
include "Adafruit_MQTT.h"
include "Adafruit_MQTT_Client.h"
const char index_html[] PROGMEM={"<!DOCTYPE html>\n" "\n" "
\n" "