Problem whit Mongoose MQTT + Mosquitto + ESP32

I’m problem because only send 1 message one time, and never send again, i’m studying mongoos recently but i didn’t found a example whit make this easy.
mos.yml:

config_schema:
  # NB: mqtt and mqtt1 must be identical.
  - ["mqtt", "o", {title: "MQTT settings"}]
  - ["mqtt.enable", "b", true, {title: "Enable MQTT"}]
  - ["mqtt.server", "s", "192.168.1.8:1883", {title: "MQTT server"}]
  - ["mqtt.client_id", "s", "", {title: "ClientID t send to the broker. Defaults to device.id."}]
  - ["mqtt.user", "s", "------", {title: "User name"}]
  - ["mqtt.pass", "s", "------", {title: "Password"}]
  - ["mqtt.reconnect_timeout_min", "d", 2.0, {title: "Starting reconnect timeout"}]
  - ["mqtt.reconnect_timeout_max", "d", 60.0, {title: "Maximum reconnect timeout"}]
  - ["mqtt.ssl_cert", "s", "", {title: "Client certificate to present to the server"}]
  - ["mqtt.ssl_key", "s", "", {title: "Private key corresponding to the certificate"}]
  - ["mqtt.ssl_ca_cert", "s", "", {title: "Verify server certificate using this CA bundle"}]
  - ["mqtt.ssl_cipher_suites", "s", "", {title: "Cipher suites to offer to the server"}]
  - ["mqtt.ssl_psk_identity", "s", "", {title: "PSK identity (must specify PSK cipher suites)"}]
  - ["mqtt.ssl_psk_key", "s", "", {title: "PSK key"}]
  - ["mqtt.clean_session", "b", true, {title: "Clean Session"}]
  - ["mqtt.keep_alive", "i", 60, {title: "Keep alive interval"}]
  - ["mqtt.will_topic", "s", "acaas", {title: "Will topic"}]
  - ["mqtt.will_message", "s", "Probamos con otra cosa diferente", {title: "Will message"}]
  - ["mqtt.will_retain", "b", false, {title: "Will retain flag"}]
  - ["mqtt.max_qos", "i", 2, {title: "Limit QoS of outgoing messages to at most this"}]
  - ["mqtt.recv_mbuf_limit", "i", 3072, {title: "Limit recv buffer size"}]
  - ["mqtt.require_time", "b", false, {title: "Only connect if (when) wall time is set"}]
  - ["mqtt.cloud_events", "b", true, {title: "Trigger cloud events when connected / disconnected"}]
  - ["mqtt.max_queue_length", "i", 5, {title: "Maximum queue length for buffering QoS 1+ messages. 0 to disable queue."}]
  # Alternative MQTT configuration. If enabled, client will alternate between mqtt and mqtt1
  # when unable to connect.
  - ["mqtt1", "mqtt", {title: "Backup MQTT settings"}]
  - ["mqtt1.enable", true]
  - ["mqtt1.server", "192.168.1.8:1883"]
  - ["debug.stdout_topic", "s", "", {title: "MQTT topic to publish STDOUT to"}]
  - ["debug.stderr_topic", "s", "", {title: "MQTT topic to publish STDERR to"}]
cdefs:
  MG_ENABLE_MQTT: 1
tags:
  - mqtt
  - c
  - js
  - net
  - docs:net:MQTT

libs:
  - origin: https://github.com/mongoose-os-libs/core
  - origin: https://github.com/mongoose-os-libs/boards
  - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/http-server
  - origin: https://github.com/mongoose-os-libs/mqtt
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/wifi-setup-web-ui

i have this JS(this code never found i don’t now why):

load('api_mqtt.js');
load('api_gpio.js');
let pin = 0, topic = 'acaas2';
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  let res = MQTT.pub('acaas2', JSON.stringify({ a: 1, b: 2 }), 1);
  print('Published:', res ? 'yes' : 'no');
}, null);

and i have 2 C code, I copied them both https://github.com/mongoose-os-libs/mqtt.git

Mosquitto is in another VM in the same WIFI configurate whit username and password
if any can help me i make me hapy :slight_smile:

Simplify your config_schema. No need to copy everything from the library. Override only the relevant keys.

config_schema:
  - ["mqtt.enable", true]
  - ["mqtt.server", "192.168.1.8:1883"]
  - ["mqtt.user", "------"]
  - ["mqtt.pass", "------"]

Your init.js looks ok.
Use mos console to monitor the connection status and the button.

You can find good examples in the places where examples are.

If you can read spanish, this site has some pieces of information and examples

thanks for your answer, in my case use windows 10 and i can’t use mos console:
Using port COM4
Error: Acceso denegado.
/src/go/src/github.com/mongoose-os/mos/cli/console.go:281: failed to open COM4
/src/go/src/github.com/mongoose-os/mos/cli/main.go:197: console failed
exit status 1.

Change my mos.yml but it still doesn’t work.

Ohh, you are using the mos UI. Never used it, but I believe the right pane is the console output.
You should see something like this:

[Jun 19 11:18:01.329] mgos_mqtt_conn.c:468    MQTT0 connecting to 192.168.0.71:1883
[Jun 19 11:18:01.344] mgos_mqtt_conn.c:227    MQTT0 TCP connect ok (0)
[Jun 19 11:18:01.356] mgos_mqtt_conn.c:271    MQTT0 CONNACK 0
...
[Jun 19 11:18:22.365] Published: yes
[Jun 19 11:18:32.650] Published: yes
[Jun 19 11:18:32.867] Published: yes
[Jun 19 11:18:37.723] Published: yes
... stop a mqtt broker ...
[Jun 19 11:18:57.427] mgos_mqtt_conn.c:253    MQTT0 Disconnect
[Jun 19 11:18:57.430] mgos_mqtt_conn.c:544    MQTT0 connecting after 2161 ms
...
[Jun 19 11:20:02.811] mgos_mqtt_conn.c:544    MQTT0 connecting after 61108 ms
[Jun 19 11:21:03.931] mgos_mqtt_conn.c:468    MQTT0 connecting to 192.168.0.71:1883
[Jun 19 11:21:03.942] mgos_mqtt_conn.c:227    MQTT0 TCP connect error (-9)
[Jun 19 11:21:03.945] mgos_mqtt_conn.c:253    MQTT0 Disconnect
[Jun 19 11:21:03.948] mgos_mqtt_conn.c:544    MQTT0 connecting after 4346 ms
[Jun 19 11:21:08.302] mgos_mqtt_conn.c:468    MQTT0 connecting to 192.168.0.42:1883
[Jun 19 11:21:08.312] mgos_mqtt_conn.c:227    MQTT0 TCP connect ok (0)
[Jun 19 11:21:08.324] mgos_mqtt_conn.c:271    MQTT0 CONNACK 0
[Jun 19 11:36:18.734] Published: yes
[Jun 19 11:36:19.010] Published: yes

Using 2 mosquitto brokers without authentication and your test code.

The suggestion about mos.yml was to make it more readable.

mongooseos1

this is my console, but every time I press the “boot” button it never sends me "Published: yes"
this are my new mos.yml:

author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

filesystem:
  - fs
config_schema:
  - ["mqtt.enable", true]
  - ["mqtt.server", "192.168.1.10:1883"]
cdefs:
  MG_ENABLE_MQTT: 1

libs:
  - origin: https://github.com/mongoose-os-libs/mqtt
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/wifi-setup-web-ui

manifest_version: 2017-09-29

mongooseos2
mongooseos3
init.js is the same code posted in my quiestion

Don’t use the boot button.
Use another GPIO!=0. Modify your init.js accordingly.

Sorry, in my case don’t work, you may share the code with me
??

mos.yml

author: Liviu Nicolescu <nliviu@gmail.com>
description: A Mongoose OS app skeleton based on https://github.com/mongoose-os-apps/empty
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

manifest_version: 2017-09-29

# Optional. List of tags for online search.
tags:
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
sources:
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
filesystem:
  - fs

config_schema:
  - ["debug.level", 2]
  - ["mqtt.enable", true]
  - ["mqtt.server", "mqtt_ip:1883"] # replace mqtt_ip with your mqtt broker's ip

libs:
  - origin: https://github.com/mongoose-os-libs/boards
  - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/rpc-ws
  - origin: https://github.com/mongoose-os-libs/sntp
  - origin: https://github.com/mongoose-os-libs/mqtt
  - origin: https://github.com/mongoose-os-libs/mjs

conds:
  - when: mos.platform != "ubuntu"
    apply:
      sources:

      libs:
        - origin: https://github.com/mongoose-os-libs/wifi
        - origin: https://github.com/mongoose-os-libs/ota-http-server

  - when: mos.platform == "esp32"
    apply:
      config_schema:
      build_vars:
        ESP_IDF_SDKCONFIG_OPTS: >
          ${build_vars.ESP_IDF_SDKCONFIG_OPTS} 
          CONFIG_ESP32_REV_MIN_1=y
  - when: mos.platform == "esp8266"
    apply:
      config_schema:

init.js

load('api_mqtt.js');
load('api_gpio.js');

let pin = 0, topic = 'acaas2';

GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function (gpio) {
  print('Button press, GPIO' + JSON.stringify(gpio));
  if (MQTT.isConnected()) {
    let res = MQTT.pub('acaas2', JSON.stringify({a: 1, b: 2}), 1);
    print('Published:', res ? 'yes' : 'no');
  } else {
    print('*** MQTT is not connected ***');
  }
}, null);

thanks, your code works for mi :grinning::grinning::grinning::grinning:
this is realy good…