Created
December 5, 2019 14:56
-
-
Save lightfromshadows/bf11f43be102e7601d1b764daad002e0 to your computer and use it in GitHub Desktop.
Smart Sump/Condensate Pump w/ Webserver
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
const char ROOT_page[] PROGMEM = R"=====( | |
<HTML> | |
<HEAD> | |
<TITLE>Sump Pump</TITLE> | |
</HEAD> | |
<BODY> | |
<CENTER> | |
<B>Online. #VALUE# / #TIME# hours</B> | |
</CENTER> | |
</BODY> | |
</HTML> | |
)====="; |
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 <PubSubClient.h> | |
#include <ESP8266WebServer.h> | |
extern const char ROOT_page[]; | |
#define RELAY_PIN D2 | |
#define FLOAT_PIN D3 | |
#define PUMP_TIME 7000 | |
unsigned long ONE_HOUR = 3600000; | |
unsigned long runCount = 0; | |
unsigned long runHours = 0; | |
unsigned long lastTime = 0; | |
unsigned long elapsedTime = 0; | |
const char* ssid = "SSID"; | |
const char* password = "password"; | |
ESP8266WebServer server(80); //Server on port 80 | |
void handleRoot() { | |
String s = ROOT_page; //Read HTML contents | |
s.replace("#VALUE#", String(runCount)); | |
s.replace("#TIME#", String(runHours)); | |
server.send(200, "text/html", s); //Send web page | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); //Connect to your WiFi router | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(250); | |
Serial.print("."); | |
} | |
//If connection successful show IP address in serial monitor | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); //IP address assigned to your ESP | |
server.on("/", handleRoot); //Which routine to handle at root location | |
server.begin(); //Start server | |
Serial.println("HTTP server started"); | |
pinMode(RELAY_PIN, OUTPUT); | |
pinMode(FLOAT_PIN, INPUT_PULLUP); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
unsigned long now = millis(); | |
elapsedTime += now - lastTime; | |
while (elapsedTime > ONE_HOUR) | |
{ | |
runHours++; | |
elapsedTime -= ONE_HOUR; | |
} | |
lastTime = now; | |
bool floating = !digitalRead(FLOAT_PIN); | |
if (floating) { | |
digitalWrite(RELAY_PIN, HIGH); | |
runCount++; | |
delay(PUMP_TIME); | |
} | |
server.handleClient(); //Handle client requests | |
digitalWrite(RELAY_PIN, LOW); | |
// delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment