To turn on the led you need to load the gpio_api.js file.
LED Turn ON/OFF
Note : This below example was tested mongoose os with java script app demo-js in ESP8266 NodeMCU Dev Board(ESP-12E)
- This below example turn on the LED GPIO-4 (NodeMCU Dev Board digital pin 2)
- Set the GPIO pin mode as OUPUT uisng gpio set mode function ex: GPIO.set_mode(led, GPIO.MODE_OUTPUT)
- Turn off the LED call the gpio write function with low-0 ex : GPIO.write(led, 0) .
- Turn on the LED call the gpio write function with low-1 ex : GPIO.write(led, 1) .
Code
load('api_gpio.js'); // Configure LED let led = 4; //lde pin GPIO04 GPIO.set_mode(led, GPIO.MODE_OUTPUT); GPIO.write(led,1);
LED turn ON/OFF using button
- Use Boot button(GPIO-0)
- The example code used blink in-build LED, It’s connected to GPIO-10.
- You can change any other pin Ex : led = 4; // Get LED GPIO-04 pin;
- Note : Code 1 and Two are same application and functionality. Main difference is calling function.
Code 1 :
- Used GPIO-0 pin (Boot button in NodeMCU Dev Board), as interrupt and used Button interrupt handler function.
// http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Mongoose-os/ // Tested By : Arun(20170430) // Example Name : AEW_LED-Blink_Using_Button.js // Firmware : Mongoose OS for ESP32 //*******************************// // This example demonstrates how to react on a button press by toggling LED. // // To try this example, // 1. Download <code>{{EJS0}}</code> tool from https://mongoose-os.com/software.html // 2. Run <code>{{EJS1}}</code> tool and install Mongoose OS // 3. In the UI, navigate to the <code>{{EJS2}}</code> tab and load this example // Load Mongoose OS API load('api_gpio.js'); // Configure LED let led = ffi('int get_led_gpio_pin()')(); // Get built-in LED GPIO pin GPIO.set_mode(led, GPIO.MODE_OUTPUT); let pin = 0; // GPIO 0 is typically a 'Flash' button GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) { let value = GPIO.toggle(led); print('Button press, pin:', x, 'LED pin:', led, ' Value: ', value); }, true); print('Flash button is configured on GPIO pin ', pin); print('Press the flash button now!');
Code 2:
- In this code i was used GPIO-0 pin (Boot button in NodeMCU dev Kit) as interrupt and used interrupt handler function.
- If your using interruot handler function then you need o enable the interrupt using GPIO.enable_int(pin) function
load('api_gpio.js'); GPIO.set_mode(4, 1); //GPIO.write(10, 0); GPIO.set_int_handler(0, GPIO.INT_EDGE_NEG, function(pin) { print('Pin', 0, 'got interrupt'); GPIO.toggle(4); }, null); GPIO.enable_int(0);