ESP32 Mongoose OS Interface MQTT
Mongoose OS Credentials configure
Two way to configure the MQTT Credentials in Mongoose OS file. This Example tested with mongoose os , demo-js app, windows 10, 64bit, mos tool, ESP32 DevKitC from ESPressif. ESP32 Mongoose OS Interface MQTT
- First one is using mos tool UI
- GO to 127.0.0.1:1992/ –> Device Config –> Change the MQTT Credential in MQTT Setting and Save with Reboot
- Afterwards its generate the new file name is conf9.json
- Second methods is change the mqtt Credential in conf0.json file
Required
- ESP32 Any kind of boards
- Mongoose OS firmware
- Mos Tool
- MQTT Server Credentials
- WiFi Crendentials
Note : This ESP32 Mongoose OS interface – MQTT is tested with Windows 10 64bit machine, mos tool(Web Browser based IDE for Mongoose OS), ESp32 DevkitC board from ESPressif.
Follow
- Make sure already set your WiFi Credentials (otherwise MQTT is not work, also check the MQTT Connected status in Terminal windows)
Code : init.js file
The code is designed to:
- Monitor a physical button on a microcontroller.
- Collect system information (RAM usage).
- Publish this info to an MQTT topic when the button is pressed.
- Monitor network connectivity events and print status to the console.
It uses the Mongoose OS APIs: GPIO, MQTT, System, Event, and Config.
load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_sys.js');
let button = Cfg.get('pins.button');
let topic = '/devices/' + Cfg.get('device.id') + '/events';
print('button GPIO:', button);
let getInfo = function() {
return JSON.stringify({
total_ram: Sys.total_ram(),
free_ram: Sys.free_ram()
});
};
// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 20, function() {
let message = getInfo();
let ok = MQTT.pub(topic, message, 1);
print('Published:', ok, topic, '->', message);
}, null);
// Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED';
} else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING';
} else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED';
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
}
print('== Net event:', ev, evs);
}, null);
Output
- Results : {“free_ram”:148676,”total_ram”:229296}
- Topic : /device/esp32_0255EC/events
Code Explanation
Loading Required APIs
load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_sys.js');
api_config.js→ Access device configuration (pins, device ID, etc.).api_events.js→ Handle system and network events.api_gpio.js→ Control GPIO pins, read/write buttons and LEDs.api_mqtt.js→ Publish/subscribe messages to MQTT broker.api_sys.js→ Access system-level info like RAM, uptime, etc.
Button and MQTT Topic Setup
let button = Cfg.get('pins.button');
let topic = '/devices/' + Cfg.get('device.id') + '/events';
print('button GPIO:', button);
- Reads the GPIO pin number for the button from configuration.
- Defines the MQTT topic dynamically using the device ID.
- Prints the GPIO pin for debugging.
System Info Function
let getInfo = function() {
return JSON.stringify({
total_ram: Sys.total_ram(),
free_ram: Sys.free_ram()
});
};
- Creates a function that returns system RAM info as a JSON string.
Sys.total_ram()→ Total RAM on the device.Sys.free_ram()→ Free RAM available.
Button Press Handler and MQTT Publish
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 20, function() {
let message = getInfo();
let ok = MQTT.pub(topic, message, 1);
print('Published:', ok, topic, '->', message);
}, null);
- Configures the button with pull-up resistor.
- Trigger occurs on falling edge (button pressed).
- Debounce interval:
20 ms. - On press:
- Reads system info using
getInfo(). - Publishes it to the MQTT topic with QoS = 1.
- Prints confirmation to the console.
- Reads system info using
Network Event Monitoring
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED';
} else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING';
} else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED';
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
}
print('== Net event:', ev, evs);
}, null);
- Monitors network events from the NET event group.
- Prints the current network status:
DISCONNECTED→ Not connected to WiFi.CONNECTING→ Attempting connection.CONNECTED→ WiFi connected but no IP yet.GOT_IP→ Successfully connected with IP.
Real-Time Usage
- IoT Device Monitoring: Track device health via MQTT on-demand.
- Remote Diagnostics: Send system RAM info when a physical button is pressed.
- Network Status Debugging: Real-time feedback on connectivity for troubleshooting.
- Embedded Projects: Useful in ESP32/ESP8266 or Mongoose OS projects for sensor monitoring, alerts, or control systems.