added Ingo Randolf's MQTT client wrapper

add-license-1
Martin Ger 2017-11-15 22:10:24 +01:00
rodzic 724bf8f830
commit d54abcb23e
5 zmienionych plików z 444 dodań i 3 usunięć

Wyświetl plik

@ -19,9 +19,11 @@ The broker does not yet support:
- non-clear sessions
- TLS
## API
If you are searching for a complete ready-to-run MQTT broker for the ESP8266 with additional features (persistent configuration, scripting support and much more) have a look at https://github.com/martin-ger/esp_mqtt .
The broker is started by simply including:
## API MQTT Broker
The MQTT broker is started by simply including:
```c
#include "uMQTTBroker.h"
@ -71,4 +73,20 @@ Sample: in the Arduino setup() initialize the WiFi connection (client or SoftAP,
MQTT_server_start(1883, 30, 30);
```
You can find a sample sketch in the examples.
You can find a sample sketch here https://github.com/martin-ger/uMQTTBroker/tree/master/examples .
## API MQTT Client
To use the MQTT client functionality include:
```c
#include "MQTT.h"
```
This code is taken from Ingo Randolf from esp-mqtt-arduino (https://github.com/i-n-g-o/esp-mqtt-arduino). It is a wrapper to tuanpmt's esp_mqtt client library. Look here https://github.com/i-n-g-o/esp-mqtt-arduino/tree/master/examples for code samples.
# Thanks
- tuanpmt for esp_mqtt (https://github.com/tuanpmt/esp_mqtt )
- Ingo Randolf for esp-mqtt-arduino (https://github.com/i-n-g-o/esp-mqtt-arduino)
- Ian Craggs for mqtt_topic
- many others contributing to open software (for the ESP8266)

267
src/MQTT.cpp 100644
Wyświetl plik

@ -0,0 +1,267 @@
/*//-------------------------------------------------------------------------------
* MQTT.cpp
*
* Implementation file for MQTT Wrapper
*
* Wrapper for Arduino written by Ingo Randolf during
* eTextiles Summercamp 2015.
*
* This library is intended to be used with esp8266 modules.
*
*
* This class wraps a slightly modified version of mqtt
* for esp8266 written by Tuan PM.
* Original code: https://github.com/tuanpmt/esp_mqtt
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
//-------------------------------------------------------------------------------*/
#include "MQTT.h"
#include "user_interface.h"
#include "osapi.h"
#include "os_type.h"
#include "mqtt/debug.h"
//------------------------------------------------------------------------------------
// mqtt internal callbacks
//------------------------------------------------------------------------------------
static void mqttConnectedCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
MQTT* _this = (MQTT*)client->user_data;
if (_this && _this->onMqttConnectedCb) {
_this->onMqttConnectedCb();
}
}
static void mqttDisconnectedCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
MQTT* _this = (MQTT*)client->user_data;
if (_this && _this->onMqttDisconnectedCb) {
_this->onMqttDisconnectedCb();
}
}
static void mqttPublishedCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
MQTT* _this = (MQTT*)client->user_data;
if (_this && _this->onMqttPublishedCb) {
_this->onMqttPublishedCb();
}
}
static void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
MQTT_Client* client = (MQTT_Client*)args;
MQTT* _this = (MQTT*)client->user_data;
if (_this) {
_this->_onMqttDataCb(topic, topic_len, data, data_len);
}
}
static void mqttTimeoutCb(uint32_t *args)
{
MQTT_Client* client = (MQTT_Client*)args;
MQTT* _this = (MQTT*)client->user_data;
// if (_this && _this->onMqttTimeoutCb) {
// _this->onMqttTimeoutCb();
// }
}
//------------------------------------------------------------------------------------
// MQTT class implementation
//------------------------------------------------------------------------------------
MQTT::MQTT(const char* client_id, const char* host, uint32_t port) :
onMqttConnectedCb(0)
,onMqttDisconnectedCb(0)
,onMqttPublishedCb(0)
,onMqttDataCb(0)
,onMqttDataRawCb(0)
{
// init connections
MQTT_InitConnection(&mqttClient, (uint8_t*)host, port, 0);
// init client
if ( !MQTT_InitClient(&mqttClient, (uint8_t*)client_id, (uint8_t*)"", (uint8_t*)"", 120, 1) ) {
MQTT_INFO("Failed to initialize properly. Check MQTT version.\r\n");
}
// init LWT
MQTT_InitLWT(&mqttClient, (uint8_t*)"/lwt", (uint8_t*)"offline", 0, 0);
// set user data
mqttClient.user_data = (void*)this;
// setup callbacks
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
MQTT_OnData(&mqttClient, mqttDataCb);
MQTT_OnTimeout(&mqttClient, mqttTimeoutCb);
}
MQTT::~MQTT()
{
MQTT_DeleteClient(&mqttClient);
}
/*
*/
void MQTT::setClientId(const char* client_id)
{
MQTT_SetUserId(&mqttClient, client_id);
}
void MQTT::setUserPwd(const char* user, const char* pwd)
{
MQTT_SetUserPwd(&mqttClient, user, pwd);
}
/*
*/
void MQTT::connect()
{
MQTT_Connect(&mqttClient);
}
void MQTT::disconnect()
{
MQTT_Disconnect(&mqttClient);
}
bool MQTT::isConnected()
{
return (mqttClient.connState >= TCP_CONNECTED);
}
/*
*/
bool MQTT::publish(const char* topic, const char* buf, uint32_t buf_len, int qos, int retain)
{
return MQTT_Publish(&mqttClient, topic, buf, buf_len, qos, retain);
}
bool MQTT::publish(String& topic, String& data, int qos, int retain)
{
return publish(topic.c_str(), data.c_str(), data.length(), qos, retain);
}
bool MQTT::publish(String& topic, const char* buf, uint32_t buf_len, int qos, int retain)
{
return publish(topic.c_str(), buf, buf_len, qos, retain);
}
bool MQTT::publish(const char* topic, String& data, int qos, int retain)
{
return publish(topic, data.c_str(), data.length(), qos, retain);
}
/*
*/
bool MQTT::subscribe(const char* topic, uint8_t qos)
{
return MQTT_Subscribe(&mqttClient, (char*)topic, qos);
}
bool MQTT::subscribe(const String& topic, uint8_t qos)
{
return MQTT_Subscribe(&mqttClient, (char*)topic.c_str(), qos);
}
//-------------------------------------------------------------------------------
// set user callback functions
//-------------------------------------------------------------------------------
void MQTT::onConnected( void (*function)(void) )
{
onMqttConnectedCb = function;
}
void MQTT::onDisconnected( void (*function)(void) )
{
onMqttDisconnectedCb = function;
}
void MQTT::onPublished( void (*function)(void) )
{
onMqttPublishedCb = function;
}
void MQTT::onData( void (*function)(String&, String&) )
{
onMqttDataCb = function;
}
void MQTT::onData( void (*function)(const char*, uint32_t, const char*, uint32_t) )
{
onMqttDataRawCb = function;
}
// internal callback, calling user CB
void MQTT::_onMqttDataCb(const char* topic, uint32_t topic_len, const char* buf, uint32_t buf_len)
{
if (onMqttDataRawCb) {
onMqttDataRawCb(topic, topic_len, buf, buf_len);
}
if (onMqttDataCb) {
char* topicCpy = (char*)malloc(topic_len+1);
memcpy(topicCpy, topic, topic_len);
topicCpy[topic_len] = 0;
// string it
String topicStr(topicCpy);
char* bufCpy = (char*)malloc(buf_len+1);
memcpy(bufCpy, buf, buf_len);
bufCpy[buf_len] = 0;
// string it
String bufStr(bufCpy);
onMqttDataCb(topicStr, bufStr);
free(topicCpy);
free(bufCpy);
}
}

96
src/MQTT.h 100644
Wyświetl plik

@ -0,0 +1,96 @@
/*//-------------------------------------------------------------------------------
* MQTT.h
*
* Header file for MQTT Wrapper
*
* Wrapper for Arduino written by Ingo Randolf during
* eTextiles Summercamp 2015.
*
* This library is intended to be used with esp8266 modules.
*
*
* This class wraps a slightly modified version of mqtt
* for esp8266 written by Tuan PM.
* Original code: https://github.com/tuanpmt/esp_mqtt
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
//-------------------------------------------------------------------------------*/
#ifndef MQTT_WRAPPER_H
#define MQTT_WRAPPER_H
#include <Arduino.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#ifndef BOOL
#define BOOL boolean
#endif
extern "C" {
#include <stddef.h>
#include "mqtt/mqtt.h"
}
class MQTT
{
public:
MQTT(const char* client_id, const char* host, uint32_t port);
~MQTT();
void setClientId(const char* client_id);
void setUserPwd(const char* user, const char* pwd);
void connect();
void disconnect();
bool isConnected();
bool publish(const char* topic, const char* buf, uint32_t buf_len, int qos = 0, int retain = 0);
bool publish(String& topic, String& data, int qos = 0, int retain = 0);
bool publish(String& topic, const char* buf, uint32_t buf_len, int qos = 0, int retain = 0);
bool publish(const char* topic, String& data, int qos = 0, int retain = 0);
bool subscribe(const char* topic, uint8_t qos = 0);
bool subscribe(const String& topic, uint8_t qos = 0);
int getState() { return mqttClient.connState; };
// set callbacks
void onConnected( void (*)(void) );
void onDisconnected( void (*)(void) );
void onPublished( void (*)(void) );
void onData( void (*)(String&, String&) );
void onData( void (*)(const char*, uint32_t, const char*, uint32_t) );
// user callbacks
void (*onMqttConnectedCb)(void);
void (*onMqttDisconnectedCb)(void);
void (*onMqttPublishedCb)(void);
void (*onMqttDataCb) (String&, String&);
void (*onMqttDataRawCb) (const char*, uint32_t, const char*, uint32_t);
// internal callback
void _onMqttDataCb(const char*, uint32_t, const char*, uint32_t);
private:
MQTT_Client mqttClient;
};
#endif

Wyświetl plik

@ -944,3 +944,59 @@ void ICACHE_FLASH_ATTR MQTT_OnPublished(MQTT_Client * mqttClient, MqttCallback p
void ICACHE_FLASH_ATTR MQTT_OnTimeout(MQTT_Client * mqttClient, MqttCallback timeoutCb) {
mqttClient->timeoutCb = timeoutCb;
}
void ICACHE_FLASH_ATTR
MQTT_SetUserId(MQTT_Client *mqttClient, const char* client_id)
{
if (mqttClient->connect_info.client_id != 0) {
os_free(mqttClient->connect_info.client_id);
mqttClient->connect_info.client_id = 0;
}
uint32_t len = os_strlen(client_id);
mqttClient->connect_info.client_id = (uint8_t*)os_zalloc(len + 1);
if (len) {
os_strcpy(mqttClient->connect_info.client_id, client_id);
}
mqttClient->connect_info.client_id[len] = 0;
}
void ICACHE_FLASH_ATTR
MQTT_SetUserPwd(MQTT_Client *mqttClient, const char* user, const char* pwd)
{
uint32_t len;
// free username
if (mqttClient->connect_info.username != 0) {
os_free(mqttClient->connect_info.username);
mqttClient->connect_info.username = 0;
}
// free password
if (mqttClient->connect_info.password != 0) {
os_free(mqttClient->connect_info.password);
mqttClient->connect_info.password = 0;
}
// copy username
len = os_strlen(user);
mqttClient->connect_info.username = (uint8_t*)os_zalloc(len + 1);
if (len) {
os_strcpy(mqttClient->connect_info.username, user);
}
mqttClient->connect_info.username[len] = 0;
// copy password
len = os_strlen(pwd);
mqttClient->connect_info.password = (uint8_t*)os_zalloc(len + 1);
if (len) {
os_strcpy(mqttClient->connect_info.password, pwd);
}
mqttClient->connect_info.password[len] = 0;
}

Wyświetl plik

@ -135,6 +135,10 @@ void ICACHE_FLASH_ATTR MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* hos
BOOL ICACHE_FLASH_ATTR MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession);
void ICACHE_FLASH_ATTR MQTT_DeleteClient(MQTT_Client *mqttClient);
void ICACHE_FLASH_ATTR MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, uint8_t will_qos, uint8_t will_retain);
void ICACHE_FLASH_ATTR MQTT_SetUserId(MQTT_Client *mqttClient, const char* client_id);
void ICACHE_FLASH_ATTR MQTT_SetUserPwd(MQTT_Client *mqttClient, const char* user_id, const char* pwd);
void ICACHE_FLASH_ATTR MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
void ICACHE_FLASH_ATTR MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
void ICACHE_FLASH_ATTR MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb);