esp_mqtt/README.md

241 wiersze
7.0 KiB
Markdown
Czysty Zwykły widok Historia

2014-12-31 04:32:06 +00:00
**esp_mqtt**
==========
This is MQTT client library for ESP8266, port from: [MQTT client library for Contiki](https://github.com/esar/contiki-mqtt) (thanks)
2014-12-31 04:32:06 +00:00
**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)
2014-12-31 04:32:06 +00:00
* 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"
#include "mem.h"
2014-12-31 04:32:06 +00:00
MQTT_Client mqttClient;
void wifiConnectCb(uint8_t status)
{
if(status == STATION_GOT_IP){
MQTT_Connect(&mqttClient);
} else {
MQTT_Disconnect(&mqttClient);
2014-12-31 04:32:06 +00:00
}
}
void mqttConnectedCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
INFO("MQTT: Connected\r\n");
MQTT_Subscribe(client, "/mqtt/topic/0", 0);
MQTT_Subscribe(client, "/mqtt/topic/1", 1);
MQTT_Subscribe(client, "/mqtt/topic/2", 2);
MQTT_Publish(client, "/mqtt/topic/0", "hello0", 6, 0, 0);
MQTT_Publish(client, "/mqtt/topic/1", "hello1", 6, 1, 0);
MQTT_Publish(client, "/mqtt/topic/2", "hello2", 6, 2, 0);
2014-12-31 04:32:06 +00:00
}
void mqttDisconnectedCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
INFO("MQTT: Disconnected\r\n");
}
2015-01-04 03:47:26 +00:00
void mqttPublishedCb(uint32_t *args)
2014-12-31 04:32:06 +00:00
{
2015-01-04 03:47:26 +00:00
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 = (char*)os_zalloc(topic_len+1),
*dataBuf = (char*)os_zalloc(data_len+1);
2015-01-04 03:47:26 +00:00
MQTT_Client* client = (MQTT_Client*)args;
2015-01-04 04:14:38 +00:00
os_memcpy(topicBuf, topic, topic_len);
2015-01-04 03:47:26 +00:00
topicBuf[topic_len] = 0;
os_memcpy(dataBuf, data, data_len);
dataBuf[data_len] = 0;
INFO("Receive topic: %s, data: %s \r\n", topicBuf, dataBuf);
os_free(topicBuf);
os_free(dataBuf);
2014-12-31 04:32:06 +00:00
}
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, sysCfg.security);
//MQTT_InitConnection(&mqttClient, "192.168.11.122", 1880, 0);
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive, 1);
//MQTT_InitClient(&mqttClient, "client_id", "user", "pass", 120, 1);
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);
2014-12-31 04:32:06 +00:00
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
2015-01-13 12:00:56 +00:00
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
2014-12-31 04:32:06 +00:00
MQTT_OnData(&mqttClient, mqttDataCb);
WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb);
INFO("\r\nSystem started ...\r\n");
}
2015-01-13 12:00:56 +00:00
2014-12-31 04:32:06 +00:00
```
2014-12-31 06:01:08 +00:00
2015-01-13 12:00:56 +00:00
**Publish message and Subscribe**
2014-12-31 06:01:08 +00:00
```c
2015-01-13 12:00:56 +00:00
/* TRUE if success */
BOOL MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos);
BOOL MQTT_Publish(MQTT_Client *client, const char* topic, const char* data, int data_length, int qos, int retain);
2014-12-31 06:01:08 +00:00
```
2015-02-06 02:47:01 +00:00
**Already support LWT: (Last Will and Testament)**
```c
/* Broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client don't send keepalive packet */
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);
```
2015-02-06 02:47:01 +00:00
#Default configuration
2015-02-06 02:47:01 +00:00
See: **include/user_config.h**
2015-02-06 02:47:01 +00:00
If you want to load new default configurations, just change the value of CFG_HOLDER in **include/user_config.h**
2015-02-06 02:47:01 +00:00
**Define protocol name in include/user_config.h**
```c
#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/
//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/
```
In the Makefile, it will erase section hold the user configuration at 0x3C000
2015-01-04 03:47:26 +00:00
```bash
flash: firmware/0x00000.bin firmware/0x40000.bin
$(PYTHON) $(ESPTOOL) -p $(ESPPORT) write_flash 0x00000 firmware/0x00000.bin 0x3C000 $(BLANKER) 0x40000 firmware/0x40000.bin
```
The BLANKER is the blank.bin file you find in your SDKs bin folder.
2014-12-31 06:15:35 +00:00
2014-12-31 04:32:06 +00:00
**Create SSL Self sign**
2014-12-31 04:32:06 +00:00
```
openssl req -x509 -newkey rsa:1024 -keyout key.pem -out cert.pem -days XXX
```
2014-12-31 06:15:35 +00:00
2014-12-31 04:32:06 +00:00
**SSL Mqtt broker for test**
2014-12-31 04:32:06 +00:00
```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 = {
2015-01-14 02:14:13 +00:00
port: 1880,
2014-12-31 04:32:06 +00:00
stats: false,
backend: ascoltatore,
persistence: {
factory: mosca.persistence.Mongo,
url: 'mongodb://localhost:27017/mqtt'
},
secure : {
keyPath: SECURE_KEY,
certPath: SECURE_CERT,
2015-01-14 02:14:13 +00:00
port: 1883
2014-12-31 04:32:06 +00:00
}
};
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')
}
```
**Example projects using esp_mqtt:**<br/>
- [https://github.com/eadf/esp_mqtt_lcd](https://github.com/eadf/esp_mqtt_lcd)
**Limited:**<br/>
- Not fully supported retransmit for QoS1 and QoS2
2014-12-31 06:15:35 +00:00
2015-01-18 07:58:19 +00:00
**Status:** *Pre release.*
[https://github.com/tuanpmt/esp_mqtt/releases](https://github.com/tuanpmt/esp_mqtt/releases)
2014-12-31 04:32:06 +00:00
[MQTT Broker for test](https://github.com/mcollina/mosca)
2014-12-31 05:11:05 +00:00
2014-12-31 04:32:06 +00:00
[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.