View Single Post
Old 01-03-16, 03:29 PM   #24
Acuario
Apprentice EcoRenovator
 
Join Date: May 2011
Location: Tortosa, Spain
Posts: 218
Thanks: 2
Thanked 75 Times in 44 Posts
Default

10. Ethernet.ino (part 2)
//Save settings received from web page
//GET /savesettings?defTime=5&defTemp=-2 HTTP/1.1
void saveSettings(char * buf) {
char str1[150];
char *s = str1, *t = NULL;
char sbuf[50];
char keyname[10];
int val;
bool res;

str1[0] = '\0';
const char *start = strchr(buf, '?') + 1;
strncat(str1, start, strcspn(start, " "));
DEBUG_PRINTST("Extracted %s\n", str1);

while ((t = strtok(s, "&")) != NULL) {
s = NULL;
sprintf(sbuf, "%s", t);

sscanf(sbuf, "%32[^=]=%d", keyname, &val);
SerialPrint2("Name=%s, value=%d\n", keyname, val);

//Running time of defrost cycle
if (StrContains(keyname, "defTime")) {
if (defTime != val)
EEPROM_write_byte(DEFROSTTIME, val);
defTime = val;
}

//Temperature of condensor to start defrost cycle
else if (StrContains(keyname, "defStartT")) {
if (defTemp != val)
EEPROM_write_byte(DEFROSTTEMP, val + DEFROST_OFFSET);
defTemp = val;
}
else if (StrContains(keyname, "hys")) {
if (hysterisis != val)
EEPROM_write_byte(HYSTERISIS, val);
hysterisis = val;
}

//Temperature to stop defrost. Overrides timer
else if (StrContains(keyname, "defStopT")) {
if (defUpperTemp != val)
EEPROM_write_byte(DEFROSTMAXTEMP, val);
defUpperTemp = val;
}

//Desired room temperature
else if (StrContains(keyname, "roomTemp")) {
if (desiredRoomTemp != val)
{
res = sendBAC1000Command(TEMPSETTING, 0, 1, 0, 0, val * 2, 0);
bacSetTemp = val;
desiredRoomTemp = val;
EEPROM_write_byte(DESIREDROOMTEMP, desiredRoomTemp);
}
}

//Maximum water temperature
else if (StrContains(keyname, "waterTemp")) {
if (maxWaterTemp != val)
EEPROM_write_byte(MAXWATERTEMP, val);
maxWaterTemp = val;
}

//Min time between defrost cycles
else if (StrContains(keyname, "defCycle")) {
if (timeBetweenDefrost != val)
EEPROM_write_byte(TIMEBETWEENDEFROST, val);
timeBetweenDefrost = val;
}

//System power state - on or off
else if (StrContains(keyname, "sysState")) {
if (operatingState != val)
{
res = sendBAC1000Command(POWERONOFF, 0, 1, (val << 4), 0, 0, 0);
operatingState = val;
powerStateBAC = val;
EEPROM_write_byte(LASTSYSTEMSTATE, operatingState);
}
}

//System operating mode heat/cool
else if (StrContains(keyname, "sysMode")) {
if (operatingMode != val)
{
res = sendBAC1000Command(MODESETTING, 0, 1, (val << 5), 0, 0, 0);
operatingMode = val;
operatingModeBAC = val;
EEPROM_write_byte(LASTSYSTEMMODE, operatingState);
}
}
}
}

//Set RTC time
void setTime(char * buf) {
tmElements_t tm;
char str1[150];
char *s = str1, *t = NULL;
char sbuf[50];
char keyname[10];
int val;
bool res;

str1[0] = '\0';
const char *start = strchr(buf, '?') + 1;
strncat(str1, start, strcspn(start, " "));
DEBUG_PRINTST("Extracted %s\n", str1);

while ((t = strtok(s, "&")) != NULL) {
s = NULL;
sprintf(sbuf, "%s", t);

sscanf(sbuf, "%32[^=]=%d", keyname, &val);
DEBUG_PRINTST("Name=%s, value=%d\n", keyname, val);

if (StrContains(keyname, "hour")) {
tm.Hour = val;
}
else if (StrContains(keyname, "minute")) {
tm.Minute = val;
}
else if (StrContains(keyname, "day")) {
tm.Day = val;
}
else if (StrContains(keyname, "month")) {
tm.Month = val;
}
else if (StrContains(keyname, "year")) {
tm.Year = val;
}
RTC.write(tm);
}
}

void returnSetupPage(EthernetClient cl, bool save) {
DEBUG_PRINTLN("Setup page");

// send a standard http response header
cl.println(F("Content-Type: text/html"));
cl.println(F("Connection: close")); // the connection will be closed after completion of the response
cl.println();
cl.println(F("<!DOCTYPE HTML>"));
cl.println(F("<html>"));
cl.println(F("<!DOCTYPE HTML><html><head>"));
cl.println(F("<link rel='stylesheet' type='text/css' href='http://www.tortosaforum.com/heatpump/heatpumpcss.css' />"));
cl.println(F("<title>Heat pump - SETUP</title></head><body>"));

cl.println(F("<H1>Heat Pump Controller - SETUP</H1>"));
//cl.println(F("<form action='savesettings' method='post'><table border='1' width='50%'>"));
cl.println(F("<form action='savesettings'><table border='1' width='30%'>"));
cl.println(F("<tr><th>Setting</th><th>Value</th></tr>"));
cl.print(F("<tr><td>Defrost run time</td><td><input type='number' name='defTime' min='1' max='10' value='"));
cl.print(defTime);
cl.println(F("'></td></tr>"));
cl.print(F("<tr><td>Defrost start temperature</td><td><input type='number' name='defStartT' min='-15' max='5' value='"));
cl.print(defTemp);
cl.println(F("'></td></tr>"));
cl.print(F("<tr><td>Defrost stop temperature</td><td><input type='number' name='defStopT' min='0' max='15' value='"));
cl.print(defUpperTemp);
cl.println(F("'></td></tr>"));
cl.print(F("<tr><td>Time between defrost cycles</td><td><input type='number' name='defCycle' min='15' max='60' value='"));
cl.print(timeBetweenDefrost);
cl.println(F("'></td></tr>"));
cl.print(F("<tr><td>Max water temperature</td><td><input type='number' name='waterTemp' min='1' max='40' value='"));
cl.print(maxWaterTemp);
cl.println(F("'></td></tr>"));
cl.print(F("<tr><td>Hysterisis</td><td><input type='number' name='hys' min='1' max='15' value='"));
cl.print(hysterisis);
cl.println(F("'></td></tr>"));

cl.print(F("<tr><td>Desired room temperature</td><td><input type='number' name='roomTemp' min='1' max='40' value='"));
cl.print(bacSetTemp);
cl.println(F("'></td></tr>"));

cl.print(F("<tr><td>System state</td><td><input type='radio' name='sysState' value='1'"));
if (operatingState == TRUE)
cl.println(F(" checked>ON<input type='radio' name='sysState' value='0'>Off</td></tr>"));
else
cl.println(F(">On<input type='radio' name='sysState' value='0' checked>Off</td></tr>"));

cl.println(F("<tr><td>Mode</td><td><input type='radio' name='sysMode' value='0'"));
if (operatingModeBAC == 0)
cl.println(F(" checked>Cool<input type='radio' name='sysMode' value='1'>Heat</td></tr>"));
else
cl.println(F(">Cool<input type='radio' name='sysMode' value='1' checked>Heat</td></tr>"));
cl.println(F("</table><br><input type='submit' value='Save'></form>"));
cl.println(F("<BR><BR>"));

//Table to set time/date
cl.println(F("<form action='settime'><table border='1' width='30%'>"));
cl.println(F("<tr><th>Setting</th><th>Value</th></tr>"));
cl.print(F("<tr><td>Hour</td><td><input type='number' name='hour' id='hour' min='1' max='23'></td></tr>"));
cl.print(F("<tr><td>Minute</td><td><input type='number' name='minute' id='minute' min='1' max='59'></td></tr>"));
cl.print(F("<tr><td>Day</td><td><input type='number' name='day' id='day' min='1' max='31'></td></tr>"));
cl.print(F("<tr><td>Month</td><td><input type='number' name='month' id='month' min='1' max='12'></td></tr>"));
cl.print(F("<tr><td>Year</td><td><input type='number' name='year' id='year' min='2015' max='2030'></td></tr>"));
cl.println(F("</table><br><input type='submit' value='Set'></form>"));
cl.println(F("<BR><BR>"));
cl.println(F("<script>"));
cl.println(F("var d = new Date();"));
cl.println(F("document.getElementById('hour').valu e = d.getHours();"));
cl.println(F("document.getElementById('minute').va lue = d.getMinutes();"));
cl.println(F("document.getElementById('day').value = d.getDate();"));
cl.println(F("var month = d.getMonth() + 1;"));
cl.println(F("document.getElementById('month').val ue = month;"));
cl.println(F("document.getElementById('year').valu e = d.getFullYear();"));
cl.println(F("</script>"));

cl.println(F("<a href=\"/?defrostOn\"\">Start Defrost</a>"));
cl.println(F("<a href=\"/?defrostOff\"\">Stop Defrost</a>"));

if (save == TRUE)
cl.println(F("<script>alert('Settings saved');</script>"));
if(defrostFlag == TRUE)
cl.println(F("<p>Defrost running..</p>"));
else
cl.println(F("<p>Defrost not running..</p>"));
cl.println(F("</body></html>"));
}
Acuario is offline   Reply With Quote