adds plotting, basic response to POST request

This commit is contained in:
Brendan Haines 2017-02-24 17:48:44 -07:00
parent 5b36251906
commit 58d8f0771d

View File

@ -7,10 +7,14 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
/////////////////////////
///// USER SETTINGS ///// ///// USER SETTINGS /////
/////////////////////////
// WiFi Settings // WiFi Settings
const char *ssid = "ESPap"; const char *ssid = "KilnControlV1";
const char *password = "thereisnospoon"; const char *password = "thereisnospoon";
// Temperature Offsets // Temperature Offsets
@ -25,53 +29,66 @@ int CS1 = 16;
int CS2 = 14; int CS2 = 14;
int CS3 = 15; int CS3 = 15;
///// END USER SETTINGS /////
// Thermocouple stuff //////////////////////////////
///// END: USER SETTINGS /////
//////////////////////////////
// Thermocouple
MAX6675 t1(CLK0, CS1, MISO0); MAX6675 t1(CLK0, CS1, MISO0);
MAX6675 t2(CLK0, CS2, MISO0); MAX6675 t2(CLK0, CS2, MISO0);
MAX6675 t3(CLK0, CS3, MISO0); MAX6675 t3(CLK0, CS3, MISO0);
int temp1, temp2, temp3; int temp1, temp2, temp3;
// Output Stuff // Control
LiquidCrystal_I2C lcd(0x3f, 20, 4);
Adafruit_MCP23017 mcp;
// WiFi Stuff
ESP8266WebServer server(80);
// Control Stuff
int onTemp = 70; int onTemp = 70;
int offTemp = 80; int offTemp = 80;
// Output
LiquidCrystal_I2C lcd(0x3f, 20, 4);
Adafruit_MCP23017 mcp;
//////////////////////
///// WEB SERVER ///// ///// WEB SERVER /////
//////////////////////
// WiFi
ESP8266WebServer server(80);
const String header = "<head><title>Kiln Controller V1</title></head>";
const String navbar = "<table border=\"1\"><nav><tr><td><a href=\"/\">Home</a></td><td><a href=\"/temps\">Temperature Display</a></td><td><a href=\"/settings\">Settings</a></td><td><a href=\"/config\">Configuration</a></td></tr></nav></table>";
void handleRoot() { void handleRoot() {
server.send(200, "text/html", server.send(200, "text/html",
"<h1>Kiln Controller V1</h1>" header + "<h1>Kiln Controller V1</h1>" + navbar
"<a href=\"/temps\">Real Time Temperature Readout</a><br>"
"<a href=\"/settings\">Temperature Setpoints</a><br>"
"<a href=\"/config\">Configuration</a><br>"
); );
} }
void handleTemps() { void handleTemps() {
server.send(200, "text/html", server.send(200, "text/html",
"<h1>Temperature Readout</h1>" header + "<h1>Kiln Controller V1</h1><h2>Temperature Display</h2>" + navbar +
"<a href=\"/\">Home</a>" "<br><img src=\"/tempgraph.svg\">"
); );
} }
void handleSettings() { void handleSettings() {
server.send(200, "text/html", server.send(200, "text/html",
"<h1>Settings</h1>" header + "<h1>Kiln Controller V1</h1><h2>Settings</h2>" + navbar +
"<a href=\"/\">Home</a>" "<form name=\"myform\" action=\"/settings\" method=\"post\">"
"<br>" +
"<button type=\"submit\">Submit</button>" +
"</form>"
); );
} }
void handleSettingsUpdate() {
mcp.digitalWrite(1, HIGH);
}
void handleConfig() { void handleConfig() {
server.send(200, "text/html", server.send(200, "text/html",
"<h1>Configuration</h1>" header + "<h1>Configuration</h1>" + navbar
"<a href=\"/\">Home</a>"
); );
} }
@ -92,7 +109,12 @@ void handleNotFound() {
server.send ( 404, "text/plain", temp ); server.send ( 404, "text/plain", temp );
} }
/////////////////
///// SETUP ///// ///// SETUP /////
/////////////////
void setup() { void setup() {
// Onboard LED // Onboard LED
pinMode(2, OUTPUT); pinMode(2, OUTPUT);
@ -125,18 +147,19 @@ void setup() {
mcp.digitalWrite(3, LOW); mcp.digitalWrite(3, LOW);
// Temperature Inital Values // Temperature Inital Values
temp1 = t1.readFahrenheit(); temp1 = 0;
temp2 = t2.readFahrenheit(); temp2 = 0;
temp3 = t3.readFahrenheit(); temp3 = 0;
// Access Point Setup // Access Point Setup
WiFi.softAP(ssid, password); WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP(); IPAddress myIP = WiFi.softAPIP();
server.on("/", handleRoot); server.on("/", handleRoot);
server.on("/temps", handleTemps); server.on("/temps", handleTemps);
server.on("/settings", handleSettings); server.on("/settings", HTTP_GET, handleSettings);
server.on("/settings", HTTP_POST, handleSettingsUpdate);
server.on("/config", handleConfig); server.on("/config", handleConfig);
server.on ("/test.svg", tempGraph); server.on ("/tempgraph.svg", tempGraph);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.begin(); server.begin();
lcd.clear(); lcd.clear();
@ -154,68 +177,145 @@ void loop() {
server.handleClient(); server.handleClient();
// Get values // Get values
temp1 = t1.readFahrenheit() + t1off; int ntemp1 = t1.readFahrenheit() + t1off;
temp2 = t2.readFahrenheit() + t2off; int ntemp2 = t2.readFahrenheit() + t2off;
temp3 = t3.readFahrenheit() + t3off; int ntemp3 = t3.readFahrenheit() + t3off;
temp1 = (ntemp1 != 2147483647 ? ntemp1 : temp1);
temp2 = (ntemp2 != 2147483647 ? ntemp2 : temp2);
temp3 = (ntemp3 != 2147483647 ? ntemp3 : temp3);
// Write to LCD // Write to LCD
lcd.clear(); lcd.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("1: "); lcd.printf("1:%4d%cF", temp1, 0xDF);
lcd.print(temp1);
lcd.print(" F");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print("2: "); lcd.printf("2:%4d%cF", temp2, 0xDF);
lcd.print(temp2);
lcd.print(" F");
lcd.setCursor(0, 2); lcd.setCursor(0, 2);
lcd.print("3: "); lcd.printf("3:%4d%cF", temp3, 0xDF);
lcd.print(temp3);
lcd.print(" F");
// Heartbeat LED // Heartbeat LED
if (WiFi.softAPgetStationNum() == 0) { if (WiFi.softAPgetStationNum() == 0) {
digitalWrite(2, LOW); digitalWrite(2, LOW);
delay(100); delay(10);
digitalWrite(2, HIGH); digitalWrite(2, HIGH);
delay(900); delay(990);
} else { } else {
digitalWrite(2, LOW); digitalWrite(2, LOW);
delay(50); delay(5);
digitalWrite(2, HIGH); digitalWrite(2, HIGH);
delay(100); delay(100);
digitalWrite(2, LOW); digitalWrite(2, LOW);
delay(50); delay(1);
digitalWrite(2, HIGH); digitalWrite(2, HIGH);
delay(800); delay(980);
} }
} }
void drawGraph(int xvalues[], int yvalues[], int width, int height, int r, int g, int b) {
////////////////////
///// PLOTTING /////
////////////////////
/**
x[] is x values
y[] is y values corresponding to x values
len is the length of both x and y
*/
void drawGraph(int x[], int y[], int len) {
String out = ""; String out = "";
char temp[100]; char temp[100];
sprintf(temp, "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"%d\" height=\"%d\">\n", width, height); // Find max/min x,y values
out += temp; int maxY = y[0];
int minY = y[0];
for (int i = 1; i < len; i++) {
if (y[i] > maxY) {
maxY = y[i];
} else if (y[i] < minY) {
minY = y[i];
}
}
sprintf(temp, "<rect width=\"%d\" height=\"%d\" fill=\"rgb(%d,%d,%d)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n", width, height, r, g, b); int maxX = x[0];
out += temp; int minX = x[0];
for (int i = 1; i < len; i++) {
if (x[i] > maxX) {
maxX = x[i];
} else if (x[i] < minX) {
minY = x[i];
}
}
float yscale = 92.0 / (maxY - minY);
float xscale = 484.0 / (maxX - minX);
// Chart area
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"560\" height=\"195\">\n";
out += "<rect width=\"560\" height=\"195\" style=\"fill:rgb(232,240,255);stroke-width:2;stroke:rgb(0,0,0)\"/>\n";
out += "<rect x=\"50\" y=\"10\" width=\"500\" height=\"110\" style=\"fill:white;stroke-width:1;stroke:rgb(0,0,0)\"/>";
// Vertical axis labels
out += "<text x=\"10\" y=\"66\" fill=\"black\">F</text>";
sprintf(temp, "<text x=\"10\" y=\"25\" fill=\"black\">%d</text>", maxY);
out += temp;
sprintf(temp, "<text x=\"10\" y=\"117\" fill=\"black\">%d</text>", minY);
out += temp;
out += "<line x1=\"45\" y1=\"19\" x2=\"55\" y2=\"19\" style=\"stroke:black;stroke-width:1\"></line>";
out += "<line x1=\"45\" y1=\"111\" x2=\"55\" y2=\"111\" style=\"stroke:black;stroke-width:1\"></line>";
// Horizontal axis labels
int horizLabelSpacing = (maxX - minX) / 4;
out += "<text x=\"275\" y=\"185\" fill=\"black\">HH:MM</text>";
sprintf(temp, "<text x=\"53\" y=\"130\" fill=\"black\" transform=\"rotate(90 53,130)\">%02d:%02d </text>", minX / 60, minX % 60);
out += temp;
sprintf(temp, "<text x=\"174\" y=\"130\" fill=\"black\" transform=\"rotate(90 174,130)\">%02d:%02d </text>", (1 * horizLabelSpacing + minX) / 60, (1 * horizLabelSpacing + minX) % 60);
out += temp;
sprintf(temp, "<text x=\"295\" y=\"130\" fill=\"black\" transform=\"rotate(90 295,130)\">%02d:%02d </text>", (2 * horizLabelSpacing + minX) / 60, (2 * horizLabelSpacing + minX) % 60);
out += temp;
sprintf(temp, "<text x=\"416\" y=\"130\" fill=\"black\" transform=\"rotate(90 416,130)\">%02d:%02d </text>", (3 * horizLabelSpacing + minX) / 60, (3 * horizLabelSpacing + minX) % 60);
out += temp;
sprintf(temp, "<text x=\"535\" y=\"130\" fill=\"black\" transform=\"rotate(90 535,130)\">%02d:%02d </text>", maxX / 60, maxX % 60);
out += temp;
out += "<line x1=\"58\" y1=\"115\" x2=\"58\" y2=\"125\" style=\"stroke:black;stroke-width:1\"></line>";
out += "<line x1=\"179\" y1=\"115\" x2=\"179\" y2=\"125\" style=\"stroke:black;stroke-width:1\"></line>";
out += "<line x1=\"300\" y1=\"115\" x2=\"300\" y2=\"125\" style=\"stroke:black;stroke-width:1\"></line>";
out += "<line x1=\"421\" y1=\"115\" x2=\"421\" y2=\"125\" style=\"stroke:black;stroke-width:1\"></line>";
out += "<line x1=\"540\" y1=\"115\" x2=\"540\" y2=\"125\" style=\"stroke:black;stroke-width:1\"></line>";
out += "Sorry, your browser does not support inline SVG.";
// Plot polyline
out += "<g stroke=\"black\">\n"; out += "<g stroke=\"black\">\n";
int y = rand() % 130; out += "<polyline points=\"";
// for (int i = 0; i < size(times); i ++) { for (int i = 0; i < len; i++) {
// int y2 = rand() % 130; sprintf(temp, "%d,%d ", (int)(58 + (xscale * (x[i] - minX))), (int)(111 - (yscale * (y[i] - minY))));
// sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2); out += temp;
// out += temp; }
// y = y2; out += "\" style=\"fill:none;stroke:black;stroke-width:2\" />";
// }
out += "</g>\n</svg>\n"; out += "</g>\n</svg>\n";
server.send ( 200, "image/svg+xml", out); server.send ( 200, "image/svg+xml", out);
} }
void tempGraph() { void tempGraph() {
int times[] = {1, 2, 3, 4, 5, 6}; lcd.clear();
int temps[] = {1, 2, 3, 1, 5, 3}; int times[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100};
drawGraph(times, temps, 400, 150, 232, 240, 255); int temps[] = {70, 75, 81, 86, 90, 94, 97, 100, 103, 107,
110, 113, 116, 130, 146, 160, 173, 186, 194, 205,
220, 240, 255, 268, 282, 300, 300, 336, 335
};
drawGraph(times, temps, 29);
lcd.setCursor(0, 0);
for (int i = 0; i < 6; i++) {
lcd.print(times[i]);
lcd.print(" ");
}
lcd.setCursor(0, 1);
for (int i = 0; i < sizeof(times) / sizeof(int); i++) {
lcd.print(temps[i]);
lcd.print(" ");
}
} }