**esp_mqtt** ========== This is MQTT client library for ESP8266, port from: [MQTT client library for Contiki](https://github.com/esar/contiki-mqtt) **Features:** * Support subscribing, publishing, authentication, will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client). * Support multiple connection (to multiple hosts). * **Support SSL connection (max 1024 bit key size)** * Easy to setup and use **Usage** ```c #include "ets_sys.h" #include "driver/uart.h" #include "osapi.h" #include "mqtt.h" #include "wifi.h" #include "config.h" #include "debug.h" #include "gpio.h" #include "user_interface.h" MQTT_Client mqttClient; void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ MQTT_Connect(&mqttClient); MQTT_Subscribe(&mqttClient, "/test/topic"); MQTT_Subscribe(&mqttClient, "/test2/topic"); } } void mqttConnectedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; INFO("MQTT: Connected\r\n"); } void mqttDisconnectedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; INFO("MQTT: Disconnected\r\n"); } void mqttPublishedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; INFO("MQTT: Published\r\n"); } void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { char topicBuf[64], dataBuf[64]; MQTT_Client* client = (MQTT_Client*)args; os_memcpy(topicBuf, topic, topic_len); topicBuf[topic_len] = 0; os_memcpy(dataBuf, data, data_len); dataBuf[data_len] = 0; INFO("MQTT topic: %s, data: %s \r\n", topicBuf, dataBuf); /* Echo back to /echo channel*/ MQTT_Publish(client, "/echo", dataBuf, data_len, 0, 0); } void mqttPublishedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; INFO("MQTT: Published\r\n"); } void user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); os_delay_us(1000000); CFG_Load(); MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, SEC_NONSSL); MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive); MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnData(&mqttClient, mqttDataCb); WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb); INFO("\r\nSystem started ...\r\n"); } ``` **Publish message** ```c void MQTT_Publish( MQTT_Client *client, const char* topic, const char* data, int data_length, int qos, int retain); ``` **Default configuration** See: *include/user_config.h* and *include/config.c* If you want to load new default configurations, just change the value of CFG_HOLDER in ***include/user_config.h*** Now in the Makefile, it will erase section hold the user configuration at 0x3C000 ```bash flash: firmware/0x00000.bin firmware/0x40000.bin $(PYTHON) $(ESPTOOL) -p $(ESPPORT) write_flash 0x00000 firmware/0x00000.bin 0x3C000 $(BLANKER) 0x40000 firmware/0x40000.bin ``` **Create SSL Self sign** ``` openssl req -x509 -newkey rsa:1024 -keyout key.pem -out cert.pem -days XXX ``` **SSL Mqtt broker for test** ```javascript var mosca = require('mosca') var SECURE_KEY = __dirname + '/key.pem'; var SECURE_CERT = __dirname + '/cert.pem'; var ascoltatore = { //using ascoltatore type: 'mongo', url: 'mongodb://localhost:27017/mqtt', pubsubCollection: 'ascoltatori', mongo: {} }; var moscaSettings = { port: 8440, stats: false, backend: ascoltatore, persistence: { factory: mosca.persistence.Mongo, url: 'mongodb://localhost:27017/mqtt' }, secure : { keyPath: SECURE_KEY, certPath: SECURE_CERT, port: 8443 } }; var server = new mosca.Server(moscaSettings); server.on('ready', setup); server.on('clientConnected', function(client) { console.log('client connected', client.id); }); // fired when a message is received server.on('published', function(packet, client) { console.log('Published', packet.payload); }); // fired when the mqtt server is ready function setup() { console.log('Mosca server is up and running') } ``` **Be careful:** This library is not fully supported for too long messages. **Status:** *Alpha release.* [MQTT Broker for test](https://github.com/mcollina/mosca) [MQTT Client for test](https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=en) **Contributing:** ***Feel free to contribute to the project in any way you like!*** **Requried:** esp_iot_sdk_v0.9.4_14_12_19 **Authors:** [Tuan PM](https://twitter.com/TuanPMT) **Donations** Invite me to a coffee [![Donate](https://www.paypalobjects.com/en_US/GB/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=JR9RVLFC4GE6J) **LICENSE - "MIT License"** Copyright (c) 2014-2015 Tuan PM, https://twitter.com/TuanPMT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.