Search code examples
mongoose-os

GPIO.read() not working in mongoose os (mos tool)


I am using ESP32. I am working with inbuilt led on GPIO 2. I want to read the level of pin.

let led = 2;
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
GPIO.write(led, 0);
Timer.set(1000, true, function(){
print(GPIO.read(led)==0);
 if (GPIO.read(led)==0){
GPIO.write(led, 0);
 }
else{
GPIO.write(led, 1);
 }}, null);

the print statement only printing ‘0’ on console although led light is on. what’s wrong with code and why GPIO.read() not working properly?


Solution

  • gpio.read() canno read state of output pin hence

    I solve this issue by importing a c function from "mgos_gpio.h" in init.js. in main.c, imported "mgos_gpio.h" and in init.js file, I use below code-

    let readPinVal = ffi('bool mgos_gpio_read_out(int)');
    let pinStatus = readPinVal(pinNo);