Skip to content

Commit

Permalink
Added support for real-time trim of TDS K-value
Browse files Browse the repository at this point in the history
  • Loading branch information
ehedman committed Oct 2, 2022
1 parent a65dc95 commit 7422a90
Show file tree
Hide file tree
Showing 11 changed files with 2,243 additions and 1,957 deletions.
5 changes: 5 additions & 0 deletions examples/fs/calDn_xml.shtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tdsdata>
<kvalue>
<!--#KVDN-->
</kvalue>
</tdsdata>
5 changes: 5 additions & 0 deletions examples/fs/calUp_xml.shtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tdsdata>
<kvalue>
<!--#KVUP-->
</kvalue>
</tdsdata>
72 changes: 60 additions & 12 deletions examples/fs/ssi.shtml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@
font-size: inherit;
}

#kvalue {
width: 50px;
float: right;
padding: 0;
border: none;
background: inherit;
visibility: hidden;
}

.input-group:has(:invalid) { /* has() not supported on firefox 106 */
border: 2px solid red;
animation: shake 0.82s cubic-bezier(.36,.07,.19,.97) both;
Expand Down Expand Up @@ -190,7 +199,16 @@

<script>

var timerObj;
const amonth = 2628288;
const UrlBase = 0;
const urlCalUp = 1;
const urlCalDn = 2;
var urlIndx = UrlBase;
const urlDest = ["ssi_xml.shtml", "calUp_xml.shtml", "calDn_xml.shtml"];
var doAsynch = true;

var curKValue = <!--#KVAL-->;

function setColor(value, obj)
{
Expand All @@ -202,26 +220,27 @@

function loadXMLDoc()
{
var totvPo = 0.0;
var totvObj = document.getElementById("totv");
var totvPr = parseInt(totvObj.value, 10);
var gtotv = parseInt(document.getElementById("gtotv").value, 10);
var fvol = parseInt(document.getElementById("fvol").value, 10);
var tnkv = parseInt(document.getElementById("tnkv").value, 10);
var tdsObj = document.getElementById("tdsDiv");

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
if (this.readyState == 4 && this.status == 200 && urlIndx == UrlBase)
{
var totvObj = document.getElementById("totv");
var totvPr = parseInt(totvObj.value, 10);
var gtotv = parseInt(document.getElementById("gtotv").value, 10);
var fvol = parseInt(document.getElementById("fvol").value, 10);
var tnkv = parseInt(document.getElementById("tnkv").value, 10);

parser = new DOMParser();
xmlDoc = parser.parseFromString(this.responseText,"text/xml");
var flrt = parseFloat(xmlDoc.getElementsByTagName("flrt")[0].childNodes[0].nodeValue);
var wtemp = parseFloat(xmlDoc.getElementsByTagName("temp")[0].childNodes[0].nodeValue);
totvPo = parseInt(xmlDoc.getElementsByTagName("totv")[0].childNodes[0].nodeValue);
var totvPo = parseInt(xmlDoc.getElementsByTagName("totv")[0].childNodes[0].nodeValue);
var tds = xmlDoc.getElementsByTagName("tds")[0].childNodes[0].nodeValue.trim();
document.getElementById("kvalue").value = xmlDoc.getElementsByTagName("kvalue")[0].childNodes[0].nodeValue.trim();

if (totvPo > totvPr && totvPr <= tnkv && flrt > 0.0) {

Expand Down Expand Up @@ -253,6 +272,7 @@
}

document.getElementById("h3header").innerHTML = hostname + " " + time + fl;

if (tds !== "0") {
var cell = document.getElementsByClassName("tdsTr");
cell[0].style.display = "table-cell";
Expand All @@ -265,12 +285,31 @@
tdsObj.innerHTML = tds + "ppm" + temp;
}
document.getElementById("uptm").value = xmlDoc.getElementsByTagName("uptm")[0].childNodes[0].nodeValue.trim();
} else if (this.readyState == 4 && this.status == 200 && urlIndx > UrlBase)
{
urlIndx = UrlBase;
doAsynch = true;
timerObj = setInterval(function () {loadXMLDoc();}, 2000);
}
};
xhttp.open("GET", "ssi_xml.shtml", true);
xhttp.open("GET", urlDest[urlIndx], doAsynch);
xhttp.send();
}

function updKValue(obj)
{
if (obj.value > curKValue) {
urlIndx = urlCalUp;
curKValue += 0.1;
} else if (obj.value < curKValue) {
urlIndx = urlCalDn;
curKValue -= 0.1;
}
clearInterval(timerObj);
doAsynch = false;
loadXMLDoc();
}

function upd_country(obj)
{
if (obj.options[obj.selectedIndex].text == "Select new")
Expand Down Expand Up @@ -392,6 +431,11 @@
return validIPmsg(obj, true);
}

function showKvalue(arg)
{
document.getElementById("kvalue").style.visibility = arg;
}

function update_vols()
{
var totv = parseInt(document.getElementById("totv").value, 10);
Expand Down Expand Up @@ -677,8 +721,12 @@

<tr>
<td class="tdsTr"><label>TDS Value</label></td>
<td class="tdsTr"><div translate="no" id="tdsDiv"><!--#TDS--></div></td>
<td class="tdsTr" title="150-300: Good/Fair: 300+ Poor">Water Quality</td>
<td class="tdsTr">
<div id="tdsDiv" translate="no"></div> <!-- inner-html updates -->
</td>
<td class="tdsTr" onmouseover="showKvalue('visible')" onmouseout="showKvalue('hidden')" title="150-300: Good/Fair: 300+ Poor">
<input id="kvalue" onchange="updKValue(this)" title="TDS K-value trim +/- 0.1 step range is 0.6-2.0" type="number" min="0.6" max="2.0" step="0.1" value="<!--#KVAL-->">Water Quality
</td>
</tr>

<tr>
Expand Down Expand Up @@ -741,7 +789,7 @@ Changing any of the SSID, Password or Country Code parameters will result a lost
rebooted. You then have to regain the connection by joining your host to that new AP WiFi network.</p>

<script>
setInterval(function () {loadXMLDoc();}, 2000);
timerObj = setInterval(function () {loadXMLDoc();}, 2000);
</script>

</div>
Expand Down
3 changes: 3 additions & 0 deletions examples/fs/ssi_xml.shtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<tds>
<!--#TDS-->
</tds>
<kvalue>
<!--#KVAL-->
</kvalue>
<temp>
<!--#TEMP-->
</temp>
Expand Down
8 changes: 3 additions & 5 deletions examples/one_wire_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* C to C++ interface
*/
#ifndef _WATERCTRL_H_
#ifdef __cplusplus

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -67,23 +67,21 @@ rom_address_t &one_wire_get_address(int index)
{
One_wire obj = One_wire(oneWire._data_pin);
return obj.get_address(index);

}

void one_wire_single_device_read_rom(rom_address_t &rom_address)
{
One_wire obj = One_wire(oneWire._data_pin);

obj.single_device_read_rom(rom_address);

}


#ifdef __cplusplus
}
#endif

#else /* _WATERCTRL_H_ */
#else /* __cplusplus */

#define ROMSize 8

Expand All @@ -100,6 +98,6 @@ rom_address_t one_wire_address_from_hex(const char *hex_address);
rom_address_t one_wire_get_address(int index);
void one_wire_single_device_read_rom(rom_address_t *rom_address);

#endif
#endif /* __cplusplus */


10 changes: 10 additions & 0 deletions examples/ssi.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ u16_t __time_critical_func(ssi_handler)(int iIndex, char *pcInsert, int iInsertL
case TEMP: // Water temp
printed = snprintf(pcInsert, iInsertLen, "%3.1f", sdata.waterTemp);
break;
case KVAL: // K value calibration
printed = snprintf(pcInsert, iInsertLen, "%.1f", pdata.kValue);
break;
case KVUP: // K value calibration Up
printed = snprintf(pcInsert, iInsertLen, "%.1f", pdata.kValue); if (pdata.kValue +0.1 < 2.1) {pdata.kValue += 0.1;}
break;
case KVDN: // K value calibration Down
printed = snprintf(pcInsert, iInsertLen, "%.1f", pdata.kValue); if (pdata.kValue -0.1 > 0.5) {pdata.kValue -= 0.1;}
break;
case VERS: // Program version
printed = snprintf(pcInsert, iInsertLen, "%.1f", pdata.version);
break;
Expand Down Expand Up @@ -365,6 +374,7 @@ err_t httpd_post_receive_data(void *connection, struct pbuf *p)
case FQV: pdata.sensFq = (float)atof(dec); break;
case FAGE: pdata.filterAge = (float)atol(dec); break;
case FVOL: pdata.filterVolume = (float)atof(dec); break;
case KVAL: pdata.kValue = (float)atof(dec); break;
case APM: apMode = atoi(dec); break;
default: /* unknown/ignored tag */ break;
}
Expand Down
3 changes: 3 additions & 0 deletions examples/ssi.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
TAG(FLRT) \
TAG(TDS) \
TAG(TEMP) \
TAG(KVAL) \
TAG(KVUP) \
TAG(KVDN) \
TAG(VERS) \
TAG(IPAD) \
TAG(IPAGW) \
Expand Down
45 changes: 22 additions & 23 deletions examples/water-cnfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ static void set_rtc(struct tm *utc, time_t *epoch)

#endif /* HAS_RTC */


#if defined HAS_TDS

/**
Expand All @@ -383,14 +382,14 @@ static void set_rtc(struct tm *utc, time_t *epoch)
static float getTemperature(void)
{
static rom_address_t address;
static bool init;

one_wire_single_device_read_rom(&address);

// printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x\n", address.rom[0], address.rom[1],
// address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7]);
if (init == false) {
one_wire_single_device_read_rom(&address);
init = true;
}

one_wire_convert_temperature(&address, true, false);
//printf("Temperature: %3.1foC\n", one_wire_temperature(&address));

return one_wire_temperature(&address);
}
Expand All @@ -402,25 +401,26 @@ static float getTemperature(void)
}
#endif /* HAS_TEMPS */

#define TDSFACTOR 0.5 // tds = ec / 2

/**
* 2-bit conversion, assume max value == ADC_VREF == 3.3 V
*/
static const float adcConvFactor = 3.3f / (1 << 12);

/**
* Return TDS value
*/
void tdsConvert(shared_data *sdata)
* Return TDS value for a Gravity TDS module
* TDS module output voltage: 0 ~ 2.3V @ Range: 0 ~ 1000ppm
*/
void tdsConvert(shared_data *sdata, persistent_data *pdata)
{
const float kValue = 1.0;
sdata->waterTemp = getTemperature();
const float TdsFactor = 0.5; // TDS value is half of the electrical conductivity value, that is: TDS = EC / 2.
//pdata->kValue // K value of the probe, eventually calibrated in a known (xxx ppm) water buffer solution
const float adcConv = 3.3f / (1 << 12); // 2-bit conversion, assume max value == ADC_VREF == 3.3 V for pico
float voltage = adc_read() * adcConv; // Voltage reading copensated for 3.3 volt
float temperature = getTemperature(); // The temp sensors value in Co

if (voltage > 3.3 || temperature > 55.0 || temperature < 1) {
return; // A glitch of bad readings
}
sdata->waterTemp = temperature;

float voltage = adc_read() * adcConvFactor;
float ecValue=(133.42*voltage*voltage*voltage - 255.86*voltage*voltage + 857.39*voltage)*kValue;
float ecValue25 = ecValue / (1.0+0.02*(sdata->waterTemp-25.0)); //temperature compensation
sdata->tdsValue = ecValue25 * TDSFACTOR;
float ecValue=(133.42*voltage*voltage*voltage - 255.86*voltage*voltage + 857.39*voltage)*pdata->kValue; // Before compensation
float ecValue25 = ecValue / (1.0+0.02*(sdata->waterTemp-25.0)); // After compensation (1.0+0.02*(Measured_Temperature-25.0))
sdata->tdsValue = ecValue25 * TdsFactor;
}

#else /* HAS_TDS */
Expand All @@ -430,7 +430,6 @@ void tdsConvert(shared_data *sdata)
APP_UNUSED_ARG(sdata);
}


#endif /* HAS_TDS */

#ifdef HAS_NET
Expand Down
7 changes: 5 additions & 2 deletions examples/water-ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ void water_ctrl(void)
pdata.filterVolume = 0.0;
pdata.filterAge = time(NULL) + SDAY;
pdata.sensFq = SENS_FQC;
pdata.kValue = 1.0;
pdata.version = (float)atof(sdata.versionString);
pdata.rebootCount = 0;
pdata.rebootTime = time(NULL);
Expand Down Expand Up @@ -545,12 +546,12 @@ void water_ctrl(void)
}
clearLog(HDR_OK);
printHdr("Flowing");
printf("FlowFreq=%dHz\n", FlowFreq);
//printf("FlowFreq=%dHz\n", FlowFreq);
printLog("FLOW=%.1fL/M", sdata.flowRate);
printLog("USED=%.0fL", used);
printLog("REM=%.0fL", pdata.tankVolume - used);
sdata.totVolume = sessLitre + pdata.totVolume;
tdsConvert(&sdata);
tdsConvert(&sdata, &pdata);
DEV_SET_PWM(DEF_PWM);

} else if (tmo-- <= 0) {
Expand Down Expand Up @@ -688,6 +689,8 @@ void water_ctrl(void)
sleep_ms(1000);
#endif /* HAS_NET */

tdsConvert(&sdata, &pdata);

if (doSave == true && delSave-- <= 0) { // Avoid repeated saves for rapid events
printf("Delayed save\n");
if (write_flash(&pdata) == false) {
Expand Down
7 changes: 4 additions & 3 deletions examples/water-ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
/*
* The RTC hat is attached
*/
//#define HAS_RTC
#define HAS_RTC

/**
* An Gravity TDS meter is attached to GPIO 26
*/
//#define HAS_TDS
#define HAS_TDS

/**
* Support for temp sensor dsb18b20 and oterhers
Expand Down Expand Up @@ -114,6 +114,7 @@ typedef struct p_data {
float tankVolume;
float filterVolume;
float sensFq;
float kValue;
time_t filterAge;
float version;
time_t rebootTime;
Expand All @@ -129,7 +130,7 @@ extern bool read_flash(persistent_data *pdata);
extern bool write_flash(persistent_data *new_data);
extern void goDormant(uint8_t dpin, persistent_data *pdata, shared_data *sdata, int lostPing);
extern time_t _time(time_t *tloc);
extern void tdsConvert(shared_data *sdata);
extern void tdsConvert(shared_data *sdata, persistent_data *pdata);

#ifdef HAS_NET

Expand Down
Loading

0 comments on commit 7422a90

Please sign in to comment.