--GPIO36/Channel0 and GPIO39/Channel3 soil humiditiy sensors -- -- List of *all* available ADC1 channels on ESP32 WROOM module: -- 0: GPIO36, 1: GPIO37, 2: GPIO38, 3: GPIO39, 4: GPIO32, 5: GPIO33, 6: GPIO34, 7: GPIO35 function irrigationmeasure() adc.setup(adc.ADC1, 0, adc.ATTEN_11db) humidity1adc = ADCmeasure(0, 20) adc.setup(adc.ADC1, 3, adc.ATTEN_11db) humidity2adc = ADCmeasure(3, 20) function humidity_percent(voltage, result) local result if voltage > 0.7 then result = (voltage - 1.15) / 0.016 if result > 100 then result = 100 end if result < 0 then result = 0 end result = math.floor(result) result = 100 - result return result else return -1 end end -- 1.1 Volt = 100 percent humiditiy -- 2.7 Volt = 0 percent humidity local Vref11dB = Vref * 0.0034 humidity1_sensor_voltage = (humidity1adc / 4095) * Vref11dB humidity1 = humidity_percent(humidity1_sensor_voltage) printv(3,"Humidity1_sensor voltage: ", humidity1_sensor_voltage, "V") printv(3,"Humidity1_sensor adc: ", humidity1adc) printv(2,"Humidity1_sensor percent: ", humidity1) humidity2_sensor_voltage = (humidity2adc / 4095) * Vref11dB humidity2 = humidity_percent(humidity2_sensor_voltage) printv(3,"Humidity2_sensor voltage: ", humidity2_sensor_voltage, "V") printv(3,"Humidity2_sensor adc: ", humidity2adc) printv(2,"Humidity2_sensor percent: ", humidity2) -- Tank gauge 170 Ohm (full) to 0 Ohm (empty), series resistor 820 Ohm adc.setup(adc.ADC1, 7, adc.ATTEN_11db) tankgaugeadc = ADCmeasure(7, 2) if tankgaugeadc > 4000 then printv(1, "Water tank gauge sensor not connected!") tankgauge = -1 else adc.setup(adc.ADC1, 7, adc.ATTEN_0db) tankgaugeadc = ADCmeasure(7, 40) tankgauge = tankgaugeadc / 8.85 tankgauge = math.floor(tankgauge) tankgauge = tankgauge / 2 adc.setup(adc.ADC1, 7, adc.ATTEN_11db) end --if tankgauge ~= nil and tankgauge > 100 then tankgauge = 100 end printv(2, "Water tank level at", tankgauge, "percent") printv(3, "Water tank sensor ADC value:", tankgaugeadc) irrigation = {humidity1, humidity2, tankgauge} return irrigation end irrigationmeasure() if V_out < low_voltage_disconnect and charge_state_int < 30 then print("Disabled power output due to low battery voltage") -- gpio.wakeup(14, gpio.INTR_LOW) gpio.write(14, 0) low_voltage_disconnect_state = 0 gpio.write(12, 0) gpio.write(13, 0) gpio.write(26, 0) gpio.write(27, 0) node.dsleep(60000000) end if V_out > 12.3 and load_disabled == false then -- gpio.wakeup(14, gpio.INTR_HIGH) -- gpio.write(14, 1) low_voltage_disconnect_state = 1 printv(2,"Sufficient battery charge available") end -- Water valves and their respective GPIOs: -- Valve1 = GPIO12, Valve2 = GPIO13, Valve3 = GPIO26, Valve4 = GPIO27 -- It is assumed that Valves 1 and 2 (GPIO 12 and 13) are watering the planting bed monitored by humidity sensor 1. -- It is assumed that Valves 3 and 4 (GPIO 26 and 27) are watering the planting bed monitored by humidity sensor 2. if not i_nbld then i_nbld = false end if not i_lvl1 then i_lvl1 = 95 end -- Humidity percent if not i_lvl2 then i_lvl2 = 95 end -- Humidity percent if not i_hr then i_hr = 3 end -- Full hour if not i_vlv_opn then i_vlv_opn = 10 end -- Minutes if i_vlv_opn > 29 then i_vlv_opn = 29 end if i_nbld == true and tankgauge > 0 and low_voltage_disconnect_state == 1 and localTime["hour"] == i_hr and localTime["min"] < i_vlv_opn then -- Turn on load power output of OpenMPPT that drives the water pump and the electric water valves gpio.write(14, 1) if humidity1 < i_lvl1 then printv(2,"Watering valve 1 is now open for", i_vlv_opn, "minutes" ) --gpio.write(26, 0) --gpio.write(27, 0) gpio.write(12, 1) --gpio.write(13, 0) --gpio.write(26, 0) --gpio.write(27, 0) end if humidity1 >= i_lvl1 then printv(2,"Watering valve 1 is now closed" ) --gpio.write(26, 0) --gpio.write(27, 0) gpio.write(12, 0) --gpio.write(13, 0) --gpio.write(26, 0) --gpio.write(27, 0) gpio.write(14, 0) end end if i_nbld == true and tankgauge > 0 and low_voltage_disconnect_state == 1 and localTime["hour"] == i_hr and localTime["min"] < 2 * i_vlv_opn and localTime["min"] >= i_vlv_opn then -- Turn on load power output of OpenMPPT that drives the water pump and the electric water valves if humidity2 < i_lvl2 then printv(2,"Watering valves 2, 3, and 4 are now open for", i_vlv_opn, "minutes" ) gpio.write(14, 1) gpio.write(12, 0) gpio.write(13, 1) gpio.write(26, 1) gpio.write(27, 1) gpio.write(12, 0) end if humidity2 >= i_lvl1 then printv(2,"Watering valves 2,3,4 are now closed" ) gpio.write(14, 0) gpio.write(26, 0) gpio.write(27, 0) gpio.write(12, 0) gpio.write(13, 0) gpio.write(26, 0) gpio.write(27, 0) end end if i_nbld == false or tankgauge == 0 or localTime["hour"] ~= i_hr or localTime["min"] > 2 * i_vlv_opn or humidity2 >= i_lvl2 then gpio.write(14, 0) gpio.write(12, 0) gpio.write(13, 0) gpio.write(26, 0) gpio.write(27, 0) end if tankgauge == 0 then printv(1,"ALERT: Water tank is running dry!") end if tankgauge == -1 then printv(1,"ALERT: No information from water tank level sensor!") end