Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Publish to Thingspeak mqtt broker not working

Hello.
I want to publish a numeric value to Thingspeak mqtt broker but can't seem to make it work.
Here is my config file:

{
"update": {
"timeout": 300,
"commit_timeout": 0,
"url": "",
"interval": 0,
"ssl_ca_file": "ca.pem",
"ssl_client_cert_file": "",
"ssl_server_name": "",
"enable_post": true
},
"device": {
"id": "esp8266_0D575E",
"password": ""
},
"debug": {
"udp_log_addr": "",
"mbedtls_level": 0,
"level": 2,
"filter": "",
"stdout_uart": 0,
"stderr_uart": 0,
"factory_reset_gpio": -1,
"mg_mgr_hexdump_file": "",
"stdout_topic": "",
"stderr_topic": ""
},
"sys": {
"mount": {
"path": "",
"dev_type": "",
"dev_opts": "",
"fs_type": "",
"fs_opts": ""
},
"tz_spec": "",
"wdt_timeout": 30,
"pref_ota_lib": "",
"atca": {
"enable": false,
"i2c_addr": 96,
"ecdh_slots_mask": 12
}
},
"conf_acl": "",
"i2c": {
"enable": true,
"freq": 100000,
"debug": false,
"sda_gpio": 12,
"scl_gpio": 14
},
"mqtt": {
"enable": true,
"server": "mqtt.thingspeak.com:1883",
"client_id": "439683", <-------------------------------- Channel ID
"user": "*********************", <-------------------- thingspeak account username
"pass": "********************", <--------------------- thingspeak account password
"reconnect_timeout_min": 2,
"reconnect_timeout_max": 60,
"ssl_cert": "",
"ssl_key": "",
"ssl_ca_cert": "",
"ssl_cipher_suites": "",
"ssl_psk_identity": "",
"ssl_psk_key": "",
"clean_session": true,
"keep_alive": 60,
"will_topic": "",
"will_message": "",
"max_qos": 2,
"recv_mbuf_limit": 3072
},
"shadow": {
"lib": ""
},
"aws": {
"thing_name": "esp8266_0D575E",
"greengrass": {
"enable": false,
"reconnect_timeout_min": 2,
"reconnect_timeout_max": 60
}
},
"wifi": {
"sta": {
"enable": true,
"ssid": "mynet",
"pass": "***********",
"user": "",
"anon_identity": "",
"cert": "",
"key": "",
"ca_cert": "",
"ip": "",
"netmask": "",
"gw": "",
"nameserver": "",
"dhcp_hostname": ""
},
"ap": {
"enable": false,
"ssid": "Mongoose_??????",
"pass": "Mongoose",
"hidden": false,
"channel": 6,
"max_connections": 10,
"ip": "192.168.4.1",
"netmask": "255.255.255.0",
"gw": "192.168.4.1",
"dhcp_start": "192.168.4.2",
"dhcp_end": "192.168.4.100",
"trigger_on_gpio": -1,
"disable_after": 0,
"hostname": "",
"keep_enabled": true
}
},
"http": {
"enable": true,
"listen_addr": "80",
"document_root": "/",
"ssl_cert": "",
"ssl_key": "",
"ssl_ca_cert": "",
"upload_acl": "
",
"hidden_files": "",
"auth_domain": "",
"auth_file": ""
},
"rpc": {
"enable": true,
"max_frame_size": 4096,
"max_queue_length": 25,
"default_out_channel_idle_close_timeout": 10,
"acl_file": "",
"auth_domain": "",
"auth_file": "",
"ws": {
"enable": true,
"server_address": "",
"reconnect_interval_min": 1,
"reconnect_interval_max": 60,
"ssl_server_name": "",
"ssl_ca_file": "",
"ssl_client_cert_file": ""
},
"mqtt": {
"enable": false, <------------------
"topic": ""
},
"uart": {
"uart_no": 0,
"baud_rate": 115200,
"fc_type": 2,
"wait_for_start_frame": true
}
},
"dash": {
"enable": false,
"token": "",
"server": "wss://dash.mongoose-os.com/api/v1/rpc",
"ca_file": "ca.pem",
"send_logs": true,
"stats_interval": 10,
"ota_chunk_size": 1024
},
"dns_sd": {
"enable": false,
"host_name": "mOS_esp8266_??????",
"txt": "",
"ttl": 120
},
"gcp": {
"enable": false,
"project": "",
"region": "",
"registry": "",
"device": "",
"key": "",
"token_ttl": 3600
},
"sntp": {
"enable": true,
"server": "time.google.com",
"retry_min": 1,
"retry_max": 30,
"update_interval": 7200
},
"spi": {
"enable": false,
"debug": false,
"miso_gpio": 12,
"mosi_gpio": 13,
"sclk_gpio": 14,
"cs0_gpio": 15,
"cs1_gpio": -1,
"cs2_gpio": -1
},
"pins": {
"led": 2,
"button": 0
},
"mjs": {
"generate_jsc": true
}
}

++++++++++++++++++++++++++++

And my init.js file:

load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');

let led = Cfg.get('pins.led');
let button = Cfg.get('pins.button');
//let topic = '/devices/' + Cfg.get('device.id') + '/events';
let topic = '/channels/439683/publish/fields/field1/writeAPIkey'; <-----------------

print('LED GPIO:', led, 'button GPIO:', button);

let getInfo = function() {
return JSON.stringify({
total_ram: Sys.total_ram(),
free_ram: Sys.free_ram()
});
};

// Blink built-in LED every second
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
Timer.set(1000 /* 1 sec */, Timer.REPEAT, function() {
let value = GPIO.toggle(led);
print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
}, null);

// 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, 200, function() {
//let message = getInfo();
let message = JSON.stringify(15); <-------------------------
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);

++++++++++++++

When I push the button it show on console Published:... but thingspeak doesn't update with the value.

Thank you.

Comments

  • Hello rikkman,

    let topic = '/channels/439683/publish/fields/field1/writeAPIkey'; <-----------------

    The leading "/" ist not mentioned in the docu. Does it work without ?

    Regards
    Holger

Sign In or Register to comment.