As the title says... Chat on...

User avatar
By gamecompiler
#13403 Hi,

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

Code: Select allfunction 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
User avatar
By cal
#13441 Moin,

IMHO you are trying far to much in one step.
You can't copy arduino code which is written in C to lua and expect it to work with only
few tweaks.
Do your translation in small steps that you understand and can test.
To get familiar with lua syntax get a lua interpreter 5.1 for your platform and
try snippets there.

When I put your code on my nodemcu it gives immediate syntax errors.
Thats the same as on arduino. If you can't compile it won't run.

Just my EUR 0,02.

Carsten