Skip to content

Instantly share code, notes, and snippets.

@jmadden91
Last active March 25, 2020 05:56
Show Gist options
  • Save jmadden91/8d083d8bfc8168892d9c00766f6177c3 to your computer and use it in GitHub Desktop.
Save jmadden91/8d083d8bfc8168892d9c00766f6177c3 to your computer and use it in GitHub Desktop.
La Pavoni Heater Control
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
int a = 1;
int b = 1;
int c = 1;
// Fill in your WiFi router SSID and password
//Home Wifi Settings
const char* ssid = "SSID"; //CHANGE TO YOUR WIFI SSID
const char* password = "password"; //CHANGE TO YOUR WIFI PASSWORD
//AP Mode Settings
const char* apssid = "PavoniAP";
ESP8266WebServer server(80);
const int relayPin = D1;
const char INDEX_HTML[] =
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
"<title>La Pavoni Heater Control</title>"
"<style>"
"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""
"</style>"
"</head>"
"<body>"
"<h1>La Pavoni Heater Control</h1>"
"<FORM action=\"/\" method=\"post\">"
"<P>"
"Warmup<BR>"
"<INPUT type=\"text\" name=\"warmup\"<BR><BR>"
"On Timer<BR>"
"<INPUT type=\"text\" name=\"ontime\"<BR><BR>"
"Off Timer<BR>"
"<INPUT type=\"text\" name=\"offtime\"<BR><BR>"
"<INPUT type=\"submit\" value=\"Set\">"
//"<INPUT type=\"submit\" value=\"Retrieve\">"
"</P>"
"</FORM>"
"</body>"
"</html>";
void handleRoot()
{
if (server.hasArg("warmup")) {
handleSubmit();
}
else {
server.send(200, "text/html", INDEX_HTML);
}
}
void handleSubmit()
{
String stringwarmup;
String stringontime;
String stringofftime;
stringwarmup = server.arg("warmup");
a = stringwarmup.toInt()*1000;
eeWriteInt(0,a);
stringontime = server.arg("ontime");
b = stringontime.toInt()*1000;
eeWriteInt(4,b);
stringofftime = server.arg("offtime");
c = stringofftime.toInt()*1000;
eeWriteInt(8,c);
server.send(200, "text/html", INDEX_HTML);
toggleRelay();
}
void toggleRelay() {
//void toggleRelay(int ontime, int offtime) {
server.handleClient();
while(1){
digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
delay(c);
digitalWrite(relayPin, HIGH); // turn off relay with voltage LOW
delay(b);
server.handleClient();
}
}
void eeWriteInt(int pos, int val) {
byte* p = (byte*) &val;
EEPROM.write(pos, *p);
EEPROM.write(pos + 1, *(p + 1));
EEPROM.write(pos + 2, *(p + 2));
EEPROM.write(pos + 3, *(p + 3));
EEPROM.commit();
}
int eeGetInt(int pos) {
int val;
byte* p = (byte*) &val;
*p = EEPROM.read(pos);
*(p + 1) = EEPROM.read(pos + 1);
*(p + 2) = EEPROM.read(pos + 2);
*(p + 3) = EEPROM.read(pos + 3);
return val;
}
void setup(void)
{
EEPROM.begin(512);
Serial.begin(115200);
//Try for a an existing wifi connection//
WiFi.begin(ssid, password); //Connect to your WiFi router
delay(5000);
if (WiFi.status() != WL_CONNECTED) {
WiFi.softAP(apssid);
Serial.println("No Wifi found. Creating Access Point");
Serial.println(apssid);
Serial.println(WiFi.softAPIP());
}
else {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
// If first flash use these values to set EEPROM
// You must reflash using the below values to avoid having to setup the timers every boot
// a = 1;
// b = 1;
// c = 1;
// Otherwise use the stored EEPROM values
a = eeGetInt(0);
b = eeGetInt(4);
c = eeGetInt(8);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); //
delay(a); // warmup timer
server.on("/", handleRoot);
server.begin();
}
void loop(void)
{
toggleRelay();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment