echo '' ;

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.

  • 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

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

 


 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading