Id made a mistake in my Lua code, but i cant find it.
The code does nothing ..., thats the problem.
In general its a code to fetch data from a easy selfmade soil moisture sensor.
Id tryed to port my Arduino Sketch to Nodemcu´s Lua.
How id built the sensor is shown in the code, if you are interested in building one.
Please help me,
Thanks in advance
function setup()
gpio.mode(voltageFlipPin1, gpio.OUTPUT);
gpio.mode(voltageFlipPin1, gpio.OUTPUT);
-- ADC pin is already a input
end
function setSensorPolarity(flip)
-- Flip pins for the moisture sensor
if(flip == true)then
gpio.write(voltageFlipPin1, gpio.HIGH);
gpio.write(voltageFlipPin2, gpio.LOW);
else if(flip == false) then
gpio.write(voltageFlipPin1, gpio.LOW);
gpio.write(voltageFlipPin2, gpio.HIGH);
end
end
function loop()
-- repeat loop infinite
repeat
setSensorPolarity(true);
delay(flipTimer);
val1 = adc.read(0)
delay(flipTimer);
setSensorPolarity(false);
delay(flipTimer);
-- invert the reading
val2 = 1023 - analogRead(sensorPin);
--
reportLevels(val1,val2);
until ( state == true )
end
function reportLevels(val1,val2)
--make a average and report the average to console
avg = (val1 + val2) / 2;
msg = "avg: ";
tempvar = msg + avg;
print(tempvar);
end
-- gamecompilers soil moisture code
-- Circuit :
-- Voltage devider : 620Kohm -> ADC -> 300Kohm
-- 6.8nF foil capacitor parallel to the 300Kohm resistor
wifi.setmode(wifi.STATION);
wifi.sta.config("APtest","Gurki");
print(wifi.sta.getip());
voltageFlipPin1 = 7;
voltageFlipPin2 = 5;
-- ADC pin -> adc.read(0)
gpio.mode(voltageFlipPin1, gpio.OUTPUT)
gpio.mode(voltageFlipPin1, gpio.OUTPUT)
-- state will never be true
state = false;
-- the time between flips, current circuit needs a min of 4ms
-- to let the capacitor become loaded
flipTimer = 1000;
setup();
loop();
end