137 lines
3.3 KiB
C++
137 lines
3.3 KiB
C++
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
|
|
|
//needed for library
|
|
//#include <DNSServer.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
|
|
|
|
#include "WebContent.h"
|
|
|
|
#define RelaisPin D4 // Relais
|
|
#define SwitchPin D1 // Active Low, Switch between D1 and GND
|
|
|
|
#define R_ON 0// active Low = 0, otherwise 1
|
|
#define R_OFF 1// active Low = 1, otherwise 0
|
|
|
|
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
|
|
|
|
// time in millis the pump has been started
|
|
long pumpStarted = 0;
|
|
|
|
// Max time the pump is active
|
|
#define PUMP_ACTIVE_MILLIS 20000
|
|
|
|
void handleRoot(); // function prototypes for HTTP handlers
|
|
void handleOn();
|
|
void handleOff();
|
|
void handleNotFound();
|
|
void handleState();
|
|
|
|
void deactivatePump();
|
|
void activatePump();
|
|
|
|
void configModeCallback(WiFiManager *myWiFiManager)
|
|
{
|
|
Serial.println("Entered config mode");
|
|
Serial.println(WiFi.softAPIP());
|
|
//if you used auto generated SSID, print it
|
|
Serial.println(myWiFiManager->getConfigPortalSSID());
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
// put your setup code here, to run once:
|
|
Serial.begin(9600);
|
|
|
|
pinMode(RelaisPin, OUTPUT);
|
|
pinMode(SwitchPin, INPUT_PULLUP);
|
|
digitalWrite(RelaisPin, R_OFF);
|
|
|
|
//WiFiManager
|
|
//Local intialization. Once its business is done, there is no need to keep it around
|
|
WiFiManager wifiManager;
|
|
//reset settings - for testing
|
|
//wifiManager.resetSettings();
|
|
|
|
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
|
wifiManager.setAPCallback(configModeCallback);
|
|
|
|
//fetches ssid and pass and tries to connect
|
|
//if it does not connect it starts an access point with the specified name
|
|
//here "AutoConnectAP"
|
|
//and goes into a blocking loop awaiting configuration
|
|
if (!wifiManager.autoConnect())
|
|
{
|
|
Serial.println("failed to connect and hit timeout");
|
|
//reset and try again, or maybe put it to deep sleep
|
|
ESP.reset();
|
|
delay(1000);
|
|
}
|
|
|
|
//if you get here you have connected to the WiFi
|
|
Serial.println("connected...yeey :)");
|
|
|
|
server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
|
|
server.on("/on", handleOn);
|
|
server.on("/off", handleOff);
|
|
server.on("/state", handleState);
|
|
|
|
|
|
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
|
|
|
|
server.begin(); // Actually start the server
|
|
Serial.println("HTTP server started");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
server.handleClient(); // Listen for HTTP requests from clients
|
|
if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS) {
|
|
deactivatePump();
|
|
}
|
|
}
|
|
|
|
void handleRoot()
|
|
{
|
|
server.send(200, "text/html", index_html);
|
|
}
|
|
|
|
void activatePump() {
|
|
pumpStarted = millis();
|
|
digitalWrite(RelaisPin, R_ON);
|
|
}
|
|
|
|
void deactivatePump() {
|
|
digitalWrite(RelaisPin, R_OFF);
|
|
}
|
|
|
|
void handleOn()
|
|
{
|
|
activatePump();
|
|
server.send(200, "text/plain", "on");
|
|
|
|
}
|
|
|
|
void handleOff()
|
|
{
|
|
deactivatePump();
|
|
server.send(200, "text/plain", "off");
|
|
}
|
|
|
|
void handleState()
|
|
{
|
|
int state = digitalRead(RelaisPin);
|
|
String sstate = "off";
|
|
switch (state) {
|
|
case R_ON:
|
|
sstate = "on";
|
|
break;
|
|
}
|
|
server.send(200,"text/plain", sstate);
|
|
}
|
|
|
|
void handleNotFound()
|
|
{
|
|
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
|
|
}
|