G
Guest
So here is the webserver code. This may or may not compile, I tried to clean it up so you can see just the webserver portions. I cannot stress enough to look at the WebDuino examples and start from there. If you are not familiar with how webservers work and in general how the HTTP protocol works it may be helpful to read up on that as well.
In general, I crafted the HTML page on my PC until the page looked the way I wanted. This included getting it to look right on my iPhone. From there, I transferred it into my sketch and made it "dynamic". This is done by outputting the variables or using case statements to make them into something human readable.
I also take parameters on the URL to change modes and set variables. These are passed with an HTTP GET, and I don't remember if I wrote that part myself or not. The buttons at the end of the code send URL parameters that actually make the changes.
This is generally difficult to maintain. Making updates is difficult, since it requires re-downloading the sketch. In my spare time, I'd make the webserver have an API for reading/writing data. Then the webpages are static, but use AJAX to make them dynamic. Since I think I recall seeing a webserver for Ardunio that can read files off an SD card, that could make updating easier. You could also host the webpages anywhere, and only have the Arduino handle AJAX requests.
Oh, and if anyone wants to tackle updating the Arduino over the network that'd be sweet. It can be done with PXE or tftp, but I haven't found working code. That would eliminate the laptop next to the Arduino for updates.
In general, I crafted the HTML page on my PC until the page looked the way I wanted. This included getting it to look right on my iPhone. From there, I transferred it into my sketch and made it "dynamic". This is done by outputting the variables or using case statements to make them into something human readable.
I also take parameters on the URL to change modes and set variables. These are passed with an HTTP GET, and I don't remember if I wrote that part myself or not. The buttons at the end of the code send URL parameters that actually make the changes.
This is generally difficult to maintain. Making updates is difficult, since it requires re-downloading the sketch. In my spare time, I'd make the webserver have an API for reading/writing data. Then the webpages are static, but use AJAX to make them dynamic. Since I think I recall seeing a webserver for Ardunio that can read files off an SD card, that could make updating easier. You could also host the webpages anywhere, and only have the Arduino handle AJAX requests.
Oh, and if anyone wants to tackle updating the Arduino over the network that'd be sweet. It can be done with PXE or tftp, but I haven't found working code. That would eliminate the laptop next to the Arduino for updates.
Code:
// webserver from [url]https://github.com/sirleech/Webduino[/url]
#include "WebServer.h"
// no-cost stream operator as described at
// [url]http://sundial.org/arduino/?page_id=119[/url]
template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }
//ethernet and webserver setup
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
#define PREFIX ""
WebServer webserver(PREFIX, 80);
//the code that handles the webserver request
void writeCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
if (type == WebServer::POST)
{
bool repeat;
char name[16], value[16];
do
{
repeat = server.readPOSTparam(name, 16, value, 16);
if(String(name)==String("value")){
//I don't think this code is being used - it's left over from an example
digitalWrite(2,atoi(value));
}
} while (repeat);
server.httpSeeOther(PREFIX "/index");
}
else
{
char name[16], value[16];
URLPARAM_RESULT rc;
//handle parameters on the end of the URL
if (strlen(url_tail))
{
while (strlen(url_tail))
{
rc = server.nextURLparam(&url_tail, name, 32, value, 32);
if (rc != URLPARAM_EOS)
{
if(String(name)==String("manual")){
manual=atoi(value);
}
if(String(name)==String("manfeed")){
manfeed=atoi(value);
}
if(String(name)==String("heatmode")){
heatmode=atoi(value);
}
}
}
}
server.httpSuccess();
//output the HTML of the web page
server << "<head>";
server << "<title>Pool Controller</title>";
server << "<meta id='viewport' name='viewport' content='width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' />";
server << "<style type='text/css'>";
server << "table {font-family: arial; font-size:10pt;}";
server << "button {font-family: arial; font-size:10pt;}";
server << "div {text-align:left; font-family: arial; font-size:10pt;}";
server << "</style>";
server << "</head><body>";
//refresh
server << "<button style=\"width:280;height:30\" onClick='window.location=\"/index\"'>[b]Refresh[/b]</button>";
server << "<table><tr>";
server << "<td>Date: " << month() << "/" << day() << "/" << year() << "</td>";
server << "<td colspan=2>Time: " << hourFormat12() << ":" << minute() << ":" << second() << " ";
if(isAM()){
server.print("AM");
}else{
server.print("PM");
}
server << "</td>";
server << "</tr><tr>";
server << "<td>SWT: " << ai_swt << "</td>";
server << "<td>RWT: " << ai_rwt << "</td>";
server << "<td>ORP: " << orp << "</td>";
server << "</tr><tr>";
server << "<td>Amps: " << ai_amps << "</td>";
server << "<td>Pressure: " << ai_psi << "</td>";
server << "</tr><tr>";
switch (manual){
case 1:
server << "<td>Pump:Auto</td>";
break;
case 2:
server << "<td>Pump:Low</td>";
break;
case 3:
server << "<td>Pump:High</td>";
break;
case 4:
server << "<td>Pump:Off</td>";
break;
}
switch (manfeed){
case 1:
server << "<td>Feed:Auto</td>";
break;
case 2:
server << "<td>Feed:On</td>";
break;
case 3:
server << "<td>Feed:Semi</td>";
break;
case 4:
server << "<td>Feed:Off</td>";
break;
}
switch (heatmode){
case 1:
server << "<td>Heat:Auto</td>";
break;
case 2:
server << "<td>Heat:On</td>";
break;
case 3:
server << "<td>Heat:Semi</td>";
break;
case 4:
server << "<td>Heat:Off</td>";
break;
}
server << "</tr><tr>";
server << "<td>S/S: " << do_ss << " ";
server << "L/H: " << do_hl << "</td>";
server << "<td>Feed: " << do_feed << "</td>";
server << "<td>Heat: " << do_heat << "</td>";
server << "</tr><tr>";
server << "<td>pH:" << ph << "</td>";
server << "<td>Feed En:" << orp_enable << "</td>";
server << "<td>Heat En:" << heat_enable << "</td>";
server << "</tr></table>";
//pump mode
server << "<hr />";
server << "<div>Pump Control</div>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manual=1\"'>[b]Auto[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manual=2\"'>[b]Low[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manual=3\"'>[b]High[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manual=4\"'>[b]Off[/b]</button>";
//feed mode
server << "<div>Feed Control</div>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manfeed=1\"'>[b]Auto[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manfeed=3\"'>[b]Semi[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manfeed=2\"'>[b]On[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?manfeed=4\"'>[b]Off[/b]</button>";
//heat mode
server << "<div>Heat Mode</div>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?heatmode=1\"'>[b]Auto[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?heatmode=3\"'>[b]Semi[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?heatmode=2\"'>[b]On[/b]</button>";
server << "<button style=\"width:70;height:30\" onClick='window.location=\"/index?heatmode=4\"'>[b]Off[/b]</button>
";
server << "</body>";
}
}
void setup() {
//startup the network card and servers
Ethernet.begin(mac, ip);
webserver.begin();
//setup webserver
webserver.setDefaultCommand(&writeCmd);
webserver.addCommand("index", &writeCmd);
)
void loop() {
webserver.processConnection();
}