ESP8266 Mongoose OS Interface Button
JavaScript for Mongoose OS, designed to run on ESP8266 or ESP32 boards. The example demonstrates how to use the ESP8266 Mongoose OS Interface Button functionality to detect a button press and print a message to the console. Also refer here for some more example: https://github.com/aruneworld/ESP8266/tree/master/Mongoose-OS/Examples
Why is This Useful?
- Testing button inputs quickly.
- Implementing user interaction (e.g., reset WiFi, toggle modes).
- Demonstrates non-blocking event-driven programming.
This is very useful for IoT applications where you need manual input. For example:
- Resetting WiFi credentials.
- Switching modes (normal, config).
- Triggering a manual data upload.
By using Button handling, developers can create simple yet powerful interfaces without requiring additional hardware.
Code of ESP8266 Mongoose OS Interface Button
this code configures GPIO0 as a button using the Button API. When pressed, it prints a message to the console.
// https://www.aruneworld.com/ // Tested By : Arun(20170430) // Example Name : AEW_Button_Message.js // Firmware : Mongoose OS for ESP32 //*******************************// // This example demonstrates how to react on a button press // by printing a message on a console. // // To try this example, // 1. Downloadmostool from https://mongoose-os.com/software.html // 2. Runmostool and install Mongoose OS // 3. In the UI, navigate to theExamplestab and load this example // Load Mongoose OS API load('api_gpio.js'); 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) { print('Button press, pin: ', x); }, true); print('Flash button is configured on GPIO pin ', pin); print('Press the flash button now!');
Code Explanation
Loading the Mongoose OS API
load('api_gpio.js');
This line imports the GPIO (General Purpose Input/Output) library of Mongoose OS. It allows you to configure pins on the ESP8266/ESP32 for input (like a button) or output (like an LED).
Defining the Button Pin
let pin = 0;
- Here, GPIO pin 0 is selected.
- GPIO0 is connected to the Flash button On most ESP8266 boards. This makes it convenient for testing without connecting external hardware.
Configuring the Button Handler
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
print('Button press, pin: ', x);
}, true);
GPIO.set_button_handler()sets up an interrupt to detect button presses.GPIO.PULL_UPenables the internal pull-up resistor (important because button pins must be stable).GPIO.INT_EDGE_NEGtriggers on a falling edge (when the button is pressed).200is the debounce time in milliseconds (removes false presses).- The
function(x)is the callback function, which prints"Button press, pin: X".
This is the core logic that enables the interface Button to react when pressed.
Startup Messages
print('Flash button is configured on GPIO pin ', pin);
print('Press the flash button now!');
These lines just notify the user that the button has been configured. When you press the button, the console will show "Button press, pin: 0".