Skip to content

Commit

Permalink
added water level sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
dimecho committed May 16, 2024
1 parent e7bac70 commit f6ab864
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 21 deletions.
42 changes: 30 additions & 12 deletions ESPTiny-Plant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Remember: Brand new ESP-12 short GPIO0 to GND (flash mode) then UART TX/RX
#define DEBUG 0
#define THREADED 0
//#define ARDUINO_SIGNING 0
#define EEPROM_ID 0x3BDAB917 //Identify Sketch by EEPROM
#define EEPROM_ID 0x3BDAB918 //Identify Sketch by EEPROM
#define ASYNCWEBSERVER_SSL 0
#define ASYNCWEBSERVER_DNS 0
/*
Expand Down Expand Up @@ -98,6 +98,8 @@ SMTP_Message message;
#include <ESPAsyncWebServer.h>
#if ASYNCWEBSERVER_DNS
#include <ESPAsyncDNSServer.h>
#else
#include <ESP8266mDNS.h>
#endif
//#include <CapacitiveSensor.h>
//#include <time.h>
Expand Down Expand Up @@ -323,7 +325,7 @@ uint8_t ON_TIME = 0; //from 6am
uint8_t OFF_TIME = 0; //to 6pm
uint16_t LOG_INTERVAL_S = 0;
uint16_t DEEP_SLEEP_S = 0;
char PNP_ADC[] = "01"; //0=NPN|1=PNP, ADC sensitivity
char PNP_ADC[] = "010"; //0=NPN|1=PNP, ADC sensitivity, Water Level Sensor 0=Disable|1=Enable
//uint8_t ADC_ERROR_OFFSET = 64; //WAKE_RF_DISABLED offset

WiFiUDP ntpUDP;
Expand Down Expand Up @@ -578,8 +580,13 @@ void setupWiFi(uint8_t timeout) {
//DNS Server
//==========
#if ASYNCWEBSERVER_DNS
dnsServer.setErrorReplyCode(AsyncDNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP());
dnsServer.setTTL(300);
dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure);
dnsServer.start(53, "captive.apple.com", WiFi.softAPIP());
//dnsServer.setErrorReplyCode(AsyncDNSReplyCode::NoError);
//dnsServer.start(53, "*", WiFi.softAPIP());
#else
MDNS.begin(WIRELESS_SSID);
#endif
NETWORK_IP = WiFi.softAPIP().toString();

Expand Down Expand Up @@ -790,10 +797,16 @@ void setupWebServer() {
//==============================================
server.on("/api", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam("adc")) {
uint8_t adc = request->getParam(0)->value().toInt();
if (adc == 1) {
adc = sensorPin;
}else{
adc = watersensorPin;
}
#ifdef ESP8266
uint16_t moisture = sensorRead_ESP8266(sensorPin);
uint16_t moisture = sensorRead_ESP8266(adc);
#else
uint16_t moisture = sensorRead(sensorPin);
uint16_t moisture = sensorRead(adc);
#endif
request->send(200, text_plain, String(moisture));
/*}else if (request->hasParam("chipid", true)) {
Expand Down Expand Up @@ -999,7 +1012,7 @@ void setupWebServer() {
return request->requestAuthentication();
#endif
String updateURL = "http://" + NETWORK_IP + "/update";
String updateHTML = "<!DOCTYPE html><html><body><form method=POST action='" + updateURL + "' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=firmware><input type=submit value='Update Firmware'></form><br><form method=POST action='" + updateURL + "' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=filesystem><input type=submit value='Update Filesystem'></form></body></html>";
String updateHTML = "<!DOCTYPE html><html><body><form method=POST action='" + updateURL + "' enctype='multipart/form-data' accept-charset='UTF-8'><input type=file accept='.bin,.signed' name=firmware><input type=submit value='Update Firmware'></form><br><form method=POST action='" + updateURL + "' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=filesystem><input type=submit value='Update Filesystem'></form></body></html>";
AsyncWebServerResponse *response = request->beginResponse(200, text_html, updateHTML);
request->send(response);
});
Expand Down Expand Up @@ -1336,11 +1349,16 @@ void loop() {
/*
loopback wire from water jug to A0 powered from GPIO12
*/
//#ifdef ESP8266
//uint16_t moisture = sensorRead_ESP8266(watersensorPin);
//#else
//uint16_t moisture = sensorRead(watersensorPin);
//#endif
if (PNP_ADC[2] == '1') {
#ifdef ESP8266
moisture = sensorRead_ESP8266(watersensorPin);
#else
moisture = sensorRead(watersensorPin);
#endif
if(moisture < 1000) {
rtcData.emptyBottle = 11;
}
}

if (rtcData.emptyBottle < 3) {
rtcData.moistureLog += moisture;
Expand Down
24 changes: 22 additions & 2 deletions Web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ <h5 class="modal-title">WiFi Settings</h5>
<button class="btn btn-success w-100" type="button" id="testPump" onclick="testPump()">Test Pump</button>
</div>
<div class="col">
<button class="btn btn-success w-100" type="button" id="testSensor">Test Sensor</button>
<button class="btn btn-success w-100" type="button" id="testBattery" onclick="testBattery()">Test Battery</button>
</div>
<div class="col">
<button class="btn btn-success w-100" type="button" id="testSleep">Test Sleep</button>
Expand All @@ -467,6 +467,19 @@ <h5 class="modal-title">WiFi Settings</h5>
<button class="btn btn-block btn-success w-100" type="button" id="testSMTP" onclick="testEmail()">Test Email</button>
</div>
</div>
<div class="row m-2"></div>
<div class="row">
<div class="col">
<button class="btn btn-success w-100" type="button" id="testSoil" onclick="testSoil()">Test Soil Sensor</button>
</div>
<div class="col">
<button class="btn btn-success w-100" type="button" id="testWater" onclick="testWater()">Test Water Sensor</button>
</div>
<div class="col">
</div>
<div class="col">
</div>
</div>
<div class="row mt-4">
<div class="col">
<div class="progress progress-striped active">
Expand Down Expand Up @@ -501,13 +514,20 @@ <h5 class="modal-title">WiFi Settings</h5>
<input type="text" id="ADCSensitivity" name="ADCSensitivity" data-provider="slider">
</div>
<div class="col-lg-6 col-12">
<small>GUI Version: 1.0.4</small><br/>
<small>GUI Version: 1.0.5</small><br/>
<small id="coreVersion">Core Version: 3.0.2</small><br/>
<small id="sdkVersion">SDK Version: 2.2.2</small><br/>
<small id="fsVersion">LittleFS Version: 1.3.0</small><br/>
<small id="firmwareVersion">Firmware Version: 1.0</small><br/>
<small id="iram">IRAM: 0</small><br/>
<small id="dram">DRAM: 0</small>
<div class="form-check">
<label class="form-check-label">
<input type="hidden" id="WaterLevel" name="WaterLevel" value="0">
<input type="checkbox" id="WaterLevelCheckbox" class="form-check-input" onclick="HiddenCheck('WaterLevel',this)">
<small>Water Level Sensor</small>
</label>
</div>
</div>
</div>
<div class="row mt-2">
Expand Down
56 changes: 50 additions & 6 deletions Web/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function loadSVG(svgfile) {
adc.open('GET', 'api?adc=1', true);
adc.send();
adc.onloadend = function() {
if(xhr.status == 200) {
if(adc.status == 200) {
var a = parseInt(adc.responseText);

if(a > 1010 && a < 1024)
Expand Down Expand Up @@ -506,10 +506,13 @@ function loadSVG(svgfile) {
$('#EnableLogCheckbox').prop('checked', bool_value);
$('#EnableLogInterval').val(data['nvram'][LOG_INTERVAL]);

var pnp_adc = data['nvram'][PNP_ADC] + '00';
var pnp_adc = data['nvram'][PNP_ADC] + '000';

$('#EnablePNP').val(pnp_adc.charAt(0));
$('#ADCSensitivity').val(pnp_adc.charAt(1));
bool_value = pnp_adc.charAt(2) == '1' ? true : false;
$('#WaterLevel').val(pnp_adc.charAt(2));
$('#WaterLevelCheckbox').prop('checked', bool_value);

var rangeslider_parameters = {
skin: 'big',
Expand Down Expand Up @@ -597,16 +600,16 @@ function loadSVG(svgfile) {
if(this.value == 'PNP') {
notify('','PNP Transistor! Controled with LOW (Negative)', 'danger');
notify('','If you get this WRONG, Pump will run Non-Stop!', 'warning');
saveSetting(PNP_ADC, '1' + document.getElementById('ADCSensitivity').value);
saveSetting(PNP_ADC, '1' + document.getElementById('ADCSensitivity').value + document.getElementById('WaterLevel').value);
}else{
saveSetting(PNP_ADC, '0' + document.getElementById('ADCSensitivity').value);
saveSetting(PNP_ADC, '0' + document.getElementById('ADCSensitivity').value + document.getElementById('WaterLevel').value);
}
});
$('#ADCSensitivity').on('input', function() {
if(document.getElementById('EnablePNP').value == 'PNP') {
saveSetting(PNP_ADC, '1' + this.value);
saveSetting(PNP_ADC, '1' + this.value + document.getElementById('WaterLevel').value);
}else{
saveSetting(PNP_ADC, '0' + this.value);
saveSetting(PNP_ADC, '0' + this.value + document.getElementById('WaterLevel').value);
}
});
$('#fileLittleFS').change(function() {
Expand Down Expand Up @@ -1145,6 +1148,47 @@ function autoWiFiPower()
}
};

function testSoil()
{
var adc = new XMLHttpRequest();
adc.open('GET', 'api?adc=1', true);
adc.send();
adc.onloadend = function() {
if(adc.status == 200) {
var a = parseInt(adc.responseText);
notify('', 'Soil Moisture = ' + a, 'success');
}
}
};

function testWater()
{
if(document.getElementById('WaterLevel').value == '0')
{
notify('', 'Enable Water Sensor in Firmware', 'danger');
notify('', 'Hardware Mod is Required!', 'warning');
}else{
var adc = new XMLHttpRequest();
adc.open('GET', 'api?adc=2', true);
adc.send();
adc.onloadend = function() {
if(adc.status == 200) {
var a = parseInt(adc.responseText);
if(a > 1000) {
notify('', 'Water Level Above Pump', 'success');
}else{
notify('', 'Water Level Below Pump', 'danger');
}
}
}
}
};

function testBattery()
{

};

function testLED()
{

Expand Down
2 changes: 1 addition & 1 deletion Web/nvram.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"nvram": ["3.0.2|2.2.2|131077|1.0.0|0|0","0","0","3","3","7","Plant","","*****","0","60","0","192.168.8.8","255.255.255.0","192.168.8.8","192.168.8.8","4","700","0","2","0","2","","","","*****","Plant","000000000","","-28800","00000000618","10"]
"nvram": ["3.0.2|2.2.2|131077|1.0.0|0|0","0","0","3","3","7","Plant","","*****","0","60","0","192.168.8.8","255.255.255.0","192.168.8.8","192.168.8.8","4","700","0","2","0","2","","","","*****","Plant","000000000","","-28800","00000000618","100"]
}

0 comments on commit f6ab864

Please sign in to comment.