Hi all, I have this code that measures and reports the level of fuel in a 275 vertical tank with rounded top and bottom. So because the depth readings will not be linear at the top and bottom I have a list of depth to gallon readings. But I don't quite know how to code so it makes a choice from a 44 line long list.
Thanks, Tom
/*
* fuel_level.ino
* using NodeMCU-12E v3 ESP8266 WiFi board and Waterproof HC-SR04 type ultrasonic sounders to measure the volume of fuel in vertical fuel tank.
* The sonar pings for the top of the fuel, gets the distance in inches,
* calculates depth of fuel as in and then calculates volume of fuel as gallons.
*
*
* pin mapping is different on these boards - CAUTION. Pin numbers are generally GPIO numbers
*
*/
#include <ESP8266WiFi.h>
// Set password to "" for open networks.
const char * ssid = "ASUS" ; //local wifi network SSID
const char * password = "********" ; //local network password
WiFiServer server (80);
//** CHANGE TO SUIT TANK DIMENSIONS
const int Depth = 50; //total depth of tank in inches, from sensor to base inside
const unsigned int Period = 2000; //period between pings, in milliseconds. i.e 1 munute = 60,000. max 65535. Want longer? use unsigned long
//** SENSOR PINS
const int trigPin = 5; // GPIO5, D1
const int echoPin = 4; // GPIO4, D2
// Global variables
int Gallons;
int Distance;
int FuelDepth;
int duration;
void setup () {
Serial.begin (115200);
pinMode(trigPin, OUTPUT); // Initializing Trigger Output and Echo Input
pinMode(echoPin, INPUT_PULLUP);
// Connect to the Wi-Fi network
Serial.println ();
Serial.println ();
Serial.print ( "Connecting with" );
Serial.println (ssid);
WiFi.begin (ssid, password);
while (WiFi.status () != WL_CONNECTED) {
delay (500);
Serial.print ( "." );
}
Serial.println ( "" );
Serial.println ( "Connected with WiFi." );
// Start of the Web Server
server.begin ();
Serial.println ( "Web server started." );
// This gets the IP address
Serial.print ( "This is the IP to connect:" );
Serial.print ( "http: //" );
Serial.print (WiFi.localIP ());
Serial.println ( "/" );
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Distance = duration / 127.000;
//***********Reading Fuel Tank
if (Distance >= Depth || Distance == 0 ) Distance = Depth; //check it does not go negative
FuelDepth = Depth - Distance;
delay(50);
// if (FuelDepth == 44, Gallons = 275);
// if (FuelDepth == 43, Gallons = 272);
// if (FuelDepth == 42, Gallons = 269);
// etc
delay(50);
// Check if a client has been connected
WiFiClient client = server.available ();
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response
if (currentLine.length() == 0) { // Check to see if the client request was just the IP address if it was just refresh
client.println("HTTP/1.1 200 OK"); // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
client.println("Content-Type:text/html"); // and a content-type so the client knows what's coming, then a blank line
client.println(""); // The HTTP response ends with another blank line:
// sends to network. works.
// print out the fuel depth.
client.print("fuel");
client.println(FuelDepth); // send to client
client.println(); // print blank line
delay(1); // delay in between reads for stability
break; // break out of the while loop:
}
else {
currentLine = ""; // if you got a newline, then clear currentLine:
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
pinMode(13, OUTPUT); // LED GPIO13 D7
digitalWrite(13, HIGH); //flash the LED on D7, just to let us know it's running
delay(50);
digitalWrite(13, LOW);
//************************* can be commented out, test use only
Serial.println();
Serial.println();
Serial.println("Tank fuel distance: " + String(Distance)); //print distance
Serial.println("Tank fuel depth: " + String(FuelDepth)); //print depth
Serial.println("Tank gallons: " + String(Gallons)); //print gallons
//***********************************************
delay(10);
//** can be commented out, test only
Serial.begin(115200); // Open serial console.
Serial.println();
//*******************************
delay(2000);
client.stop(); // close the connection:
Serial.println("client disconnected");
}