esp_mqtt/README.md

150 wiersze
3.7 KiB
Markdown
Czysty Zwykły widok Historia

2014-12-31 04:32:06 +00:00
**esp_mqtt**
==========
[![](https://travis-ci.org/tuanpmt/esp_mqtt.svg?branch=master)](https://travis-ci.org/tuanpmt/esp_mqtt)
2015-03-05 10:43:13 +00:00
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
2015-02-15 14:44:39 +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).
2016-09-09 13:49:00 +00:00
* Support SSL connection
2015-02-15 14:44:39 +00:00
* Easy to setup and use
2016-09-09 13:49:00 +00:00
***Prerequire:***
2015-02-15 14:19:21 +00:00
2016-09-09 13:49:00 +00:00
- ESPTOOL.PY: https://github.com/themadinventor/esptool
- SDK 2.0 or higher: http://bbs.espressif.com/viewtopic.php?f=46&t=2451
- ESP8266 compiler:
+ OSX or Linux: http://tuanpm.net/esp8266-development-kit-on-mac-os-yosemite-and-eclipse-ide/
+ Windows: http://programs74.ru/udkew-en.html
2015-02-15 14:19:21 +00:00
2016-09-09 13:49:00 +00:00
**Compile:**
2015-02-15 14:19:21 +00:00
2016-09-09 13:49:00 +00:00
- Copy file `include/user_config.sample.h` to `include/user_config.local.h` and change settings, included: SSID, PASS, MQTT configurations ...
2016-09-09 13:49:00 +00:00
Make sure to add PYTHON PATH and compile PATH to Eclipse environment variable if using Eclipse
2015-02-15 14:44:39 +00:00
2015-02-15 14:19:21 +00:00
```bash
2015-09-03 06:19:15 +00:00
git clone --recursive https://github.com/tuanpmt/esp_mqtt
2015-02-15 14:19:21 +00:00
cd esp_mqtt
#clean
make clean
#make
2016-09-09 13:49:00 +00:00
make SDK_BASE=/tools/esp8266/sdk/ESP8266_NONOS_SDK ESPTOOL=tools/esp8266/esptool/esptool.py all
2015-02-15 14:19:21 +00:00
#flash
2016-09-09 13:49:00 +00:00
make ESPPORT=/dev/ttyUSB0 flash
2015-02-15 14:19:21 +00:00
```
2014-12-31 04:32:06 +00:00
**Usage**
See file: `user/user_main.c`
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
2016-09-09 13:49:00 +00:00
See: **include/user_config.sample.h**
**Define protocol name in include/user_config.local.h**
2015-02-06 02:47:01 +00:00
```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/*/
```
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')
}
```
2016-09-09 13:49:00 +00:00
**Example projects using esp_mqtt:**
2015-01-18 07:58:19 +00:00
- [https://github.com/eadf/esp_mqtt_lcd](https://github.com/eadf/esp_mqtt_lcd)
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!
2014-12-31 04:32:06 +00:00
**Authors:**
[Tuan PM](https://twitter.com/TuanPMT)
**LICENSE - "MIT License"**