ssl disabled, works now
This commit is contained in:
parent
aadd078cfc
commit
b43ed9f5bc
15
.cproject
15
.cproject
@ -21,15 +21,12 @@
|
||||
<option id="io.sloeber.compiler.cpp.sketch.option.incpath.1774627043" name="Include Paths (-I)" superClass="io.sloeber.compiler.cpp.sketch.option.incpath" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/core}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/variant}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/EEPROM}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/Arduino-Temperature-Control-Library-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/OneWire-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/pubsubclient}""/>
|
||||
</option>
|
||||
<inputType id="io.sloeber.compiler.cpp.sketch.input.1656989259" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
|
||||
</tool>
|
||||
@ -37,15 +34,12 @@
|
||||
<option id="io.sloeber.compiler.c.sketch.option.incpath.440900696" name="Include Paths (-I)" superClass="io.sloeber.compiler.c.sketch.option.incpath" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/core}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/variant}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/EEPROM}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/Arduino-Temperature-Control-Library-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/OneWire-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/pubsubclient}""/>
|
||||
</option>
|
||||
<inputType id="io.sloeber.compiler.c.sketch.input.1442761052" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
|
||||
</tool>
|
||||
@ -53,15 +47,12 @@
|
||||
<option id="io.sloeber.compiler.asm.sketch.option.incpath.353711540" name="Include Paths (-I)" superClass="io.sloeber.compiler.asm.sketch.option.incpath" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/core}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/core/variant}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/pubsubclient/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/EEPROM}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WebServer/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/ESP8266WiFi/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/Arduino-Temperature-Control-Library-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/libraries/OneWire-master}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/WifiRelay/pubsubclient}""/>
|
||||
</option>
|
||||
<inputType id="io.sloeber.compiler.S.sketch.input.262987757" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
|
||||
</tool>
|
||||
|
15
.project
15
.project
@ -47,11 +47,6 @@
|
||||
<type>2</type>
|
||||
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>libraries/Arduino-Temperature-Control-Library-master</name>
|
||||
<type>2</type>
|
||||
<location>/home/wn/Arduino/libraries/Arduino-Temperature-Control-Library-master</location>
|
||||
</link>
|
||||
<link>
|
||||
<name>libraries/EEPROM</name>
|
||||
<type>2</type>
|
||||
@ -67,15 +62,5 @@
|
||||
<type>2</type>
|
||||
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>libraries/OneWire-master</name>
|
||||
<type>2</type>
|
||||
<location>/home/wn/Arduino/libraries/OneWire-master</location>
|
||||
</link>
|
||||
<link>
|
||||
<name>libraries/pubsubclient</name>
|
||||
<type>2</type>
|
||||
<location>/home/wn/Arduino/libraries/pubsubclient</location>
|
||||
</link>
|
||||
</linkedResources>
|
||||
</projectDescription>
|
||||
|
@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.DTS
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.DTS/value=3600
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.LOCAL/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.LOCAL/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.LOCAL/value=1531253422
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.LOCAL/value=1531300637
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.UTC/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.UTC/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.UTC/value=1531246222
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.UTC/value=1531293437
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.ZONE/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.ZONE/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/A.EXTRA.TIME.ZONE/value=3600
|
||||
@ -466,10 +466,10 @@ environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.BOARD_NAM
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module)
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.COM_PORT/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.COM_PORT/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.COM_PORT/value=/dev/ttyUSB1
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.COM_PORT/value=/dev/ttyUSB2
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.ECLIPSE_LOCATION/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.ECLIPSE_LOCATION/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}/////
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}//////////
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.EXTRA.ALL/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.EXTRA.ALL/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.140663409/JANTJE.EXTRA.ALL/value=
|
||||
|
@ -12,17 +12,14 @@ configItems = [
|
||||
{"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"},
|
||||
{"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"},
|
||||
{"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":32, "default":"broker.hottis.de"},
|
||||
{"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"WifiRelay"},
|
||||
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
|
||||
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"WifiRelay"},
|
||||
{"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
|
||||
{"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/WifiRelay1/State"},
|
||||
{"label":"MQTT Debug Topic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/WifiRelay1/Debug"},
|
||||
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}
|
||||
]
|
||||
|
||||
|
||||
magic = 0xC0DE0000
|
||||
magic = 0xC0DE0001
|
||||
appName = "ESP8266 based WifiRelay"
|
||||
confWifiSsid = "espconfig"
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
// #define ESP8266
|
||||
#define ESP8266
|
||||
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
|
||||
tConfigBlock configBlock;
|
||||
const uint32_t MAGIC = 3235774464;
|
||||
const uint32_t MAGIC = 3235774465;
|
||||
const char* CONFIG_SSID = "espconfig";
|
||||
extern ESP8266WebServer webServer;
|
||||
|
||||
@ -32,10 +32,7 @@ void configServeIndex() {
|
||||
strcpy(configBlock.wifiSsid, "test");
|
||||
strcpy(configBlock.wifiKey, "geheim");
|
||||
strcpy(configBlock.mqttBroker, "broker.hottis.de");
|
||||
strcpy(configBlock.mqttUser, "WifiRelay");
|
||||
strcpy(configBlock.mqttPass, "geheim123");
|
||||
strcpy(configBlock.mqttClientId, "WifiRelay");
|
||||
configBlock.mqttPort = 8883;
|
||||
strcpy(configBlock.mqttTopic, "IoT/WifiRelay1/State");
|
||||
strcpy(configBlock.mqttDebugTopic, "IoT/WifiRelay1/Debug");
|
||||
configBlock.debugMode = 0;
|
||||
@ -134,36 +131,6 @@ void configServeIndex() {
|
||||
buffer += configBlock.mqttBroker;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
" />"
|
||||
" </td>"
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td>"
|
||||
" <label for\"mqttUser\">MQTT Username</label>"
|
||||
" </td><td>"
|
||||
" <input type=\"text\" name=\"mqttUser\" id=\"mqttUser\" ";
|
||||
|
||||
buffer += " size=\"32\" ";
|
||||
buffer += " value=\"";
|
||||
buffer += configBlock.mqttUser;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
" />"
|
||||
" </td>"
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td>"
|
||||
" <label for\"mqttPass\">MQTT Password</label>"
|
||||
" </td><td>"
|
||||
" <input type=\"text\" name=\"mqttPass\" id=\"mqttPass\" ";
|
||||
|
||||
buffer += " size=\"32\" ";
|
||||
buffer += " value=\"";
|
||||
buffer += configBlock.mqttPass;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
" />"
|
||||
" </td>"
|
||||
@ -179,20 +146,6 @@ void configServeIndex() {
|
||||
buffer += configBlock.mqttClientId;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
" />"
|
||||
" </td>"
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td>"
|
||||
" <label for\"mqttPort\">MQTT Port</label>"
|
||||
" </td><td>"
|
||||
" <input type=\"text\" name=\"mqttPort\" id=\"mqttPort\" ";
|
||||
|
||||
buffer += " value=\"";
|
||||
buffer += configBlock.mqttPort;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
" />"
|
||||
" </td>"
|
||||
@ -276,14 +229,8 @@ void configServeGetConfiguration() {
|
||||
strcpy(configBlock.wifiKey, arg.c_str());
|
||||
arg = webServer.arg("mqttBroker");
|
||||
strcpy(configBlock.mqttBroker, arg.c_str());
|
||||
arg = webServer.arg("mqttUser");
|
||||
strcpy(configBlock.mqttUser, arg.c_str());
|
||||
arg = webServer.arg("mqttPass");
|
||||
strcpy(configBlock.mqttPass, arg.c_str());
|
||||
arg = webServer.arg("mqttClientId");
|
||||
strcpy(configBlock.mqttClientId, arg.c_str());
|
||||
arg = webServer.arg("mqttPort");
|
||||
configBlock.mqttPort = atoi(arg.c_str());
|
||||
arg = webServer.arg("mqttTopic");
|
||||
strcpy(configBlock.mqttTopic, arg.c_str());
|
||||
arg = webServer.arg("mqttDebugTopic");
|
||||
@ -334,22 +281,10 @@ void showConfiguration() {
|
||||
Serial.print(configBlock.mqttBroker);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("mqttUser = <");
|
||||
Serial.print(configBlock.mqttUser);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("mqttPass = <");
|
||||
Serial.print(configBlock.mqttPass);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("mqttClientId = <");
|
||||
Serial.print(configBlock.mqttClientId);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("mqttPort = <");
|
||||
Serial.print(configBlock.mqttPort);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("mqttTopic = <");
|
||||
Serial.print(configBlock.mqttTopic);
|
||||
Serial.println(">");
|
||||
|
@ -5,10 +5,7 @@ typedef struct {
|
||||
char wifiSsid[32];
|
||||
char wifiKey[64];
|
||||
char mqttBroker[32];
|
||||
char mqttUser[32];
|
||||
char mqttPass[32];
|
||||
char mqttClientId[32];
|
||||
uint32_t mqttPort;
|
||||
char mqttTopic[64];
|
||||
char mqttDebugTopic[64];
|
||||
uint32_t debugMode;
|
||||
|
@ -4,6 +4,8 @@
|
||||
* Created on: Aug 20, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
#define MQTT_MAX_PACKET_SIZE 256
|
||||
@ -11,17 +13,19 @@
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
#define ESP8266
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include "configuration.h"
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
|
||||
WiFiClientSecure espClient;
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
|
||||
const char watchdogTopic[] = "IoT/Watchdog";
|
||||
bool watchdogEnabled = false;
|
||||
|
||||
|
||||
void setup_wifi() {
|
||||
@ -54,20 +58,15 @@ void setup_wifi() {
|
||||
|
||||
|
||||
|
||||
void setupProduction() {
|
||||
setup_wifi();
|
||||
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
||||
|
||||
pinMode(LIGHT_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
const uint8_t BUFSIZE = 128;
|
||||
Serial.println("callback");
|
||||
const uint16_t BUFSIZE = 256;
|
||||
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
|
||||
#ifdef DEBUG
|
||||
Serial.println("Received message too long, ignore it");
|
||||
Serial.print(length);
|
||||
Serial.println(", received message too long, ignore it");
|
||||
|
||||
#endif
|
||||
} else {
|
||||
char buffer[BUFSIZE];
|
||||
@ -89,33 +88,46 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
digitalWrite(LIGHT_PIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (! strcmp(topic, watchdogTopic)) {
|
||||
Serial.println("Feed watchdog");
|
||||
ESP.wdtFeed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setupProduction() {
|
||||
setup_wifi();
|
||||
client.setServer(configBlock.mqttBroker, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
pinMode(LIGHT_PIN, OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
#ifdef DEBUG
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
#endif
|
||||
// Attempt to connect
|
||||
//char clientId[128];
|
||||
//snprintf(clientId, 127, "esp%s", WiFi.macAddress().c_str());
|
||||
if (client.connect(configBlock.mqttClientId, configBlock.mqttUser, configBlock.mqttPass)) {
|
||||
if (client.connect(configBlock.mqttClientId)) {
|
||||
#ifdef DEBUG
|
||||
Serial.println("connected");
|
||||
#endif
|
||||
// Once connected, publish an announcement...
|
||||
//client.publish("IoT/espThermometer/status", "hello world");
|
||||
|
||||
client.setCallback(callback);
|
||||
client.subscribe(configBlock.mqttTopic);
|
||||
client.subscribe(watchdogTopic);
|
||||
|
||||
client.publish(configBlock.mqttDebugTopic, "hello world");
|
||||
client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str());
|
||||
|
||||
if (! watchdogEnabled) {
|
||||
watchdogEnabled = true;
|
||||
ESP.wdtEnable(25000);
|
||||
}
|
||||
} else {
|
||||
#ifdef DEBUG
|
||||
Serial.print("failed, rc=");
|
||||
@ -137,7 +149,6 @@ void mqtt_connect() {
|
||||
}
|
||||
|
||||
void loopProduction() {
|
||||
static uint32_t lastMillis = 0;
|
||||
mqtt_connect();
|
||||
}
|
||||
|
||||
|
670
pubsubclient/PubSubClient.cpp
Normal file
670
pubsubclient/PubSubClient.cpp
Normal file
@ -0,0 +1,670 @@
|
||||
/*
|
||||
PubSubClient.cpp - A simple client for MQTT.
|
||||
Nick O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#include "PubSubClient.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
#define INIT_FINGERPRINT() this->fingerprint = NULL;
|
||||
#else
|
||||
#define INIT_FINGERPRINT()
|
||||
#endif
|
||||
|
||||
PubSubClient::PubSubClient() {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
this->_client = NULL;
|
||||
this->stream = NULL;
|
||||
setCallback(NULL);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
PubSubClient::PubSubClient(WiFiClientSecure& client, const char* fingerprint) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
this->fingerprint = fingerprint;
|
||||
}
|
||||
#endif
|
||||
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr, port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip, port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id) {
|
||||
return connect(id,NULL,NULL,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass) {
|
||||
return connect(id,user,pass,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
|
||||
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
|
||||
if (!connected()) {
|
||||
int result = 0;
|
||||
|
||||
if (domain != NULL) {
|
||||
result = _client->connect(this->domain, this->port);
|
||||
} else {
|
||||
result = _client->connect(this->ip, this->port);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
if (fingerprint != NULL) {
|
||||
if (domain != NULL) {
|
||||
// there's only one way to set fingerprint: using the WiFiClientSecure-based constructor, so this cast is safe
|
||||
if (!static_cast<WiFiClientSecure*>(_client)->verify(fingerprint, domain)) {
|
||||
_state = MQTT_TLS_BAD_SERVER_CREDENTIALS;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
char buffer[16]; // IPv4 only (which is what IPAddress supports anyway)
|
||||
|
||||
ip.toString().toCharArray(buffer, 16);
|
||||
|
||||
if (!static_cast<WiFiClientSecure*>(_client)->verify(fingerprint, buffer)) {
|
||||
_state = MQTT_TLS_BAD_SERVER_CREDENTIALS;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (result == 1) {
|
||||
nextMsgId = 1;
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
unsigned int j;
|
||||
|
||||
#if MQTT_VERSION == MQTT_VERSION_3_1
|
||||
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION};
|
||||
#define MQTT_HEADER_VERSION_LENGTH 9
|
||||
#elif MQTT_VERSION == MQTT_VERSION_3_1_1
|
||||
uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION};
|
||||
#define MQTT_HEADER_VERSION_LENGTH 7
|
||||
#endif
|
||||
for (j = 0;j<MQTT_HEADER_VERSION_LENGTH;j++) {
|
||||
buffer[length++] = d[j];
|
||||
}
|
||||
|
||||
uint8_t v;
|
||||
if (willTopic) {
|
||||
v = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
} else {
|
||||
v = 0x02;
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
v = v|0x80;
|
||||
|
||||
if(pass != NULL) {
|
||||
v = v|(0x80>>1);
|
||||
}
|
||||
}
|
||||
|
||||
buffer[length++] = v;
|
||||
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) >> 8);
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF);
|
||||
length = writeString(id,buffer,length);
|
||||
if (willTopic) {
|
||||
length = writeString(willTopic,buffer,length);
|
||||
length = writeString(willMessage,buffer,length);
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
length = writeString(user,buffer,length);
|
||||
if(pass != NULL) {
|
||||
length = writeString(pass,buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
write(MQTTCONNECT,buffer,length-5);
|
||||
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
|
||||
while (!available()) {
|
||||
unsigned long t = millis();
|
||||
if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
|
||||
_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint8_t llen;
|
||||
uint16_t len = readPacket(&llen);
|
||||
|
||||
if (len == 4) {
|
||||
if (buffer[3] == 0) {
|
||||
lastInActivity = millis();
|
||||
pingOutstanding = false;
|
||||
_state = MQTT_CONNECTED;
|
||||
return true;
|
||||
} else {
|
||||
_state = buffer[3];
|
||||
}
|
||||
}
|
||||
_client->stop();
|
||||
} else {
|
||||
_state = MQTT_CONNECT_FAILED;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// return and cache the available number of bytes in the client;
|
||||
// remember to reduce the available count when consuming the buffer
|
||||
int PubSubClient::available() {
|
||||
if (_available == 0) {
|
||||
_available = _client->available();
|
||||
}
|
||||
return _available;
|
||||
}
|
||||
|
||||
// reads a byte into result
|
||||
boolean PubSubClient::readByte(uint8_t * result) {
|
||||
uint32_t previousMillis = millis();
|
||||
while(!available()) {
|
||||
uint32_t currentMillis = millis();
|
||||
if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*result = _client->read();
|
||||
_available -= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// reads a byte into result[*index] and increments index
|
||||
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
|
||||
uint16_t current_index = *index;
|
||||
uint8_t * write_address = &(result[current_index]);
|
||||
if(readByte(write_address)){
|
||||
*index = current_index + 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
uint16_t len = 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
|
||||
uint32_t multiplier = 1;
|
||||
uint16_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
uint16_t skip = 0;
|
||||
uint8_t start = 0;
|
||||
|
||||
do {
|
||||
if(!readByte(&digit)) return 0;
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((digit & 128) != 0);
|
||||
*lengthLength = len-1;
|
||||
|
||||
if (isPublish) {
|
||||
// Read in topic length to calculate bytes to skip over for Stream writing
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
|
||||
start = 2;
|
||||
if (buffer[0]&MQTTQOS1) {
|
||||
// skip message id
|
||||
skip += 2;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint16_t i = start;i<length;i++) {
|
||||
if(!readByte(&digit)) return 0;
|
||||
if (this->stream) {
|
||||
if (isPublish && len-*lengthLength-2>skip) {
|
||||
this->stream->write(digit);
|
||||
}
|
||||
}
|
||||
if (len < MQTT_MAX_PACKET_SIZE) {
|
||||
buffer[len] = digit;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
|
||||
if (!this->stream && len > MQTT_MAX_PACKET_SIZE) {
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean PubSubClient::loop() {
|
||||
if (connected()) {
|
||||
do {
|
||||
unsigned long t = millis();
|
||||
if ((t - lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - lastOutActivity > MQTT_KEEPALIVE*1000UL)) {
|
||||
if (pingOutstanding) {
|
||||
this->_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
} else {
|
||||
buffer[0] = MQTTPINGREQ;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
pingOutstanding = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (available()) {
|
||||
uint8_t llen;
|
||||
uint16_t len = readPacket(&llen);
|
||||
uint16_t msgId = 0;
|
||||
uint8_t *payload;
|
||||
if (len > 0) {
|
||||
lastInActivity = t;
|
||||
uint8_t type = buffer[0]&0xF0;
|
||||
if (type == MQTTPUBLISH) {
|
||||
if (callback) {
|
||||
uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; /* topic length in bytes */
|
||||
memmove(buffer+llen+2,buffer+llen+3,tl); /* move topic inside buffer 1 byte to front */
|
||||
buffer[llen+2+tl] = 0; /* end the topic as a 'C' string with \x00 */
|
||||
char *topic = (char*) buffer+llen+2;
|
||||
// msgId only present for QOS>0
|
||||
if ((buffer[0]&0x06) == MQTTQOS1) {
|
||||
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
|
||||
payload = buffer+llen+3+tl+2;
|
||||
callback(topic,payload,len-llen-3-tl-2);
|
||||
|
||||
buffer[0] = MQTTPUBACK;
|
||||
buffer[1] = 2;
|
||||
buffer[2] = (msgId >> 8);
|
||||
buffer[3] = (msgId & 0xFF);
|
||||
_client->write(buffer,4);
|
||||
lastOutActivity = t;
|
||||
|
||||
} else {
|
||||
payload = buffer+llen+3+tl;
|
||||
callback(topic,payload,len-llen-3-tl);
|
||||
}
|
||||
}
|
||||
} else if (type == MQTTPINGREQ) {
|
||||
buffer[0] = MQTTPINGRESP;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
} else if (type == MQTTPINGRESP) {
|
||||
pingOutstanding = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (_available > 0); // can't leave data in the buffer, or subsequent publish() calls
|
||||
// may fail (axTLS is only half-duplex, so writes will fail, to
|
||||
// avoid losing information)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload) {
|
||||
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
|
||||
return publish(topic,(const uint8_t*)payload,strlen(payload),retained);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
|
||||
return publish(topic, payload, plength, false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
|
||||
if (connected()) {
|
||||
if (MQTT_MAX_PACKET_SIZE < 5 + 2+strlen(topic) + plength) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
length = writeString(topic,buffer,length);
|
||||
uint16_t i;
|
||||
for (i=0;i<plength;i++) {
|
||||
buffer[length++] = payload[i];
|
||||
}
|
||||
uint8_t header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
return write(header,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
|
||||
uint8_t llen = 0;
|
||||
uint8_t digit;
|
||||
unsigned int rc = 0;
|
||||
uint16_t tlen;
|
||||
unsigned int pos = 0;
|
||||
unsigned int i;
|
||||
uint8_t header;
|
||||
unsigned int len;
|
||||
|
||||
if (!connected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tlen = strlen(topic);
|
||||
|
||||
header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
buffer[pos++] = header;
|
||||
len = plength + 2 + tlen;
|
||||
do {
|
||||
digit = len % 128;
|
||||
len = len / 128;
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
buffer[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
pos = writeString(topic,buffer,pos);
|
||||
|
||||
rc += _client->write(buffer,pos);
|
||||
|
||||
for (i=0;i<plength;i++) {
|
||||
rc += _client->write((char)pgm_read_byte_near(payload + i));
|
||||
}
|
||||
|
||||
lastOutActivity = millis();
|
||||
|
||||
return rc == tlen + 4 + plength;
|
||||
}
|
||||
|
||||
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
||||
uint8_t lenBuf[4];
|
||||
uint8_t llen = 0;
|
||||
uint8_t digit;
|
||||
uint8_t pos = 0;
|
||||
uint16_t rc;
|
||||
uint16_t len = length;
|
||||
do {
|
||||
digit = len % 128;
|
||||
len = len / 128;
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
lenBuf[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
buf[4-llen] = header;
|
||||
for (int i=0;i<llen;i++) {
|
||||
buf[5-llen+i] = lenBuf[i];
|
||||
}
|
||||
|
||||
#ifdef MQTT_MAX_TRANSFER_SIZE
|
||||
uint8_t* writeBuf = buf+(4-llen);
|
||||
uint16_t bytesRemaining = length+1+llen; //Match the length type
|
||||
uint8_t bytesToWrite;
|
||||
boolean result = true;
|
||||
while((bytesRemaining > 0) && result) {
|
||||
bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining;
|
||||
rc = _client->write(writeBuf,bytesToWrite);
|
||||
result = (rc == bytesToWrite);
|
||||
bytesRemaining -= rc;
|
||||
writeBuf += rc;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
rc = _client->write(buf+(4-llen),length+1+llen);
|
||||
lastOutActivity = millis();
|
||||
return (rc == 1+llen+length);
|
||||
#endif
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic) {
|
||||
return subscribe(topic, 0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
|
||||
if (qos < 0 || qos > 1) {
|
||||
return false;
|
||||
}
|
||||
if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
buffer[length++] = (nextMsgId >> 8);
|
||||
buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString((char*)topic, buffer,length);
|
||||
buffer[length++] = qos;
|
||||
return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::unsubscribe(const char* topic) {
|
||||
if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
uint16_t length = 5;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
buffer[length++] = (nextMsgId >> 8);
|
||||
buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString(topic, buffer,length);
|
||||
return write(MQTTUNSUBSCRIBE|MQTTQOS1,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
buffer[0] = MQTTDISCONNECT;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
_state = MQTT_DISCONNECTED;
|
||||
_client->stop();
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
|
||||
const char* idp = string;
|
||||
uint16_t i = 0;
|
||||
pos += 2;
|
||||
while (*idp) {
|
||||
buf[pos++] = *idp++;
|
||||
i++;
|
||||
}
|
||||
buf[pos-i-2] = (i >> 8);
|
||||
buf[pos-i-1] = (i & 0xFF);
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
boolean PubSubClient::connected() {
|
||||
boolean rc;
|
||||
if (_client == NULL ) {
|
||||
rc = false;
|
||||
} else {
|
||||
rc = (int)_client->connected();
|
||||
if (!rc) {
|
||||
if (this->_state == MQTT_CONNECTED) {
|
||||
this->_state = MQTT_CONNECTION_LOST;
|
||||
_client->flush();
|
||||
_client->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) {
|
||||
IPAddress addr(ip[0],ip[1],ip[2],ip[3]);
|
||||
return setServer(addr,port);
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) {
|
||||
this->ip = ip;
|
||||
this->port = port;
|
||||
this->domain = NULL;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) {
|
||||
this->domain = domain;
|
||||
this->port = port;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) {
|
||||
this->callback = callback;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setClient(Client& client){
|
||||
this->_client = &client;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setStream(Stream& stream){
|
||||
this->stream = &stream;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int PubSubClient::state() {
|
||||
return this->_state;
|
||||
}
|
160
pubsubclient/PubSubClient.h
Normal file
160
pubsubclient/PubSubClient.h
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
PubSubClient.h - A simple client for MQTT.
|
||||
Nick O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#ifndef PubSubClient_h
|
||||
#define PubSubClient_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "IPAddress.h"
|
||||
#include "Client.h"
|
||||
#include "Stream.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
#include "WiFiClientSecure.h"
|
||||
#endif
|
||||
|
||||
#define MQTT_VERSION_3_1 3
|
||||
#define MQTT_VERSION_3_1_1 4
|
||||
|
||||
// MQTT_VERSION : Pick the version
|
||||
//#define MQTT_VERSION MQTT_VERSION_3_1
|
||||
#ifndef MQTT_VERSION
|
||||
#define MQTT_VERSION MQTT_VERSION_3_1_1
|
||||
#endif
|
||||
|
||||
// MQTT_MAX_PACKET_SIZE : Maximum packet size
|
||||
#ifndef MQTT_MAX_PACKET_SIZE
|
||||
#define MQTT_MAX_PACKET_SIZE 128
|
||||
#endif
|
||||
|
||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds
|
||||
#ifndef MQTT_KEEPALIVE
|
||||
#define MQTT_KEEPALIVE 15
|
||||
#endif
|
||||
|
||||
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
|
||||
#ifndef MQTT_SOCKET_TIMEOUT
|
||||
#define MQTT_SOCKET_TIMEOUT 15
|
||||
#endif
|
||||
|
||||
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
|
||||
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
|
||||
// pass the entire MQTT packet in each write call.
|
||||
//#define MQTT_MAX_TRANSFER_SIZE 80
|
||||
|
||||
// Possible values for client.state()
|
||||
#define MQTT_TLS_BAD_SERVER_CREDENTIALS -5
|
||||
#define MQTT_CONNECTION_TIMEOUT -4
|
||||
#define MQTT_CONNECTION_LOST -3
|
||||
#define MQTT_CONNECT_FAILED -2
|
||||
#define MQTT_DISCONNECTED -1
|
||||
#define MQTT_CONNECTED 0
|
||||
#define MQTT_CONNECT_BAD_PROTOCOL 1
|
||||
#define MQTT_CONNECT_BAD_CLIENT_ID 2
|
||||
#define MQTT_CONNECT_UNAVAILABLE 3
|
||||
#define MQTT_CONNECT_BAD_CREDENTIALS 4
|
||||
#define MQTT_CONNECT_UNAUTHORIZED 5
|
||||
|
||||
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
|
||||
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
|
||||
#define MQTTPUBLISH 3 << 4 // Publish message
|
||||
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
|
||||
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
|
||||
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
|
||||
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
|
||||
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
|
||||
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
|
||||
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
|
||||
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
|
||||
#define MQTTPINGREQ 12 << 4 // PING Request
|
||||
#define MQTTPINGRESP 13 << 4 // PING Response
|
||||
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
|
||||
#define MQTTReserved 15 << 4 // Reserved
|
||||
|
||||
#define MQTTQOS0 (0 << 1)
|
||||
#define MQTTQOS1 (1 << 1)
|
||||
#define MQTTQOS2 (2 << 1)
|
||||
|
||||
#ifdef ESP8266
|
||||
#include <functional>
|
||||
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
|
||||
#else
|
||||
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
|
||||
#endif
|
||||
|
||||
class PubSubClient {
|
||||
private:
|
||||
Client* _client;
|
||||
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
|
||||
uint16_t nextMsgId;
|
||||
unsigned long lastOutActivity;
|
||||
unsigned long lastInActivity;
|
||||
int _available;
|
||||
bool pingOutstanding;
|
||||
MQTT_CALLBACK_SIGNATURE;
|
||||
int available();
|
||||
uint16_t readPacket(uint8_t*);
|
||||
boolean readByte(uint8_t * result);
|
||||
boolean readByte(uint8_t * result, uint16_t * index);
|
||||
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
||||
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
|
||||
IPAddress ip;
|
||||
const char* domain;
|
||||
uint16_t port;
|
||||
Stream* stream;
|
||||
int _state;
|
||||
|
||||
#ifdef ESP8266
|
||||
const char* fingerprint;
|
||||
#endif
|
||||
|
||||
public:
|
||||
PubSubClient();
|
||||
PubSubClient(Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
PubSubClient(uint8_t *, uint16_t, Client& client);
|
||||
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
PubSubClient(const char*, uint16_t, Client& client);
|
||||
PubSubClient(const char*, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
|
||||
#ifdef ESP8266
|
||||
PubSubClient(WiFiClientSecure& client, const char* fingerprint);
|
||||
#endif
|
||||
|
||||
PubSubClient& setServer(IPAddress ip, uint16_t port);
|
||||
PubSubClient& setServer(uint8_t * ip, uint16_t port);
|
||||
PubSubClient& setServer(const char * domain, uint16_t port);
|
||||
PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
|
||||
PubSubClient& setClient(Client& client);
|
||||
PubSubClient& setStream(Stream& stream);
|
||||
|
||||
boolean connect(const char* id);
|
||||
boolean connect(const char* id, const char* user, const char* pass);
|
||||
boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
||||
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
||||
void disconnect();
|
||||
boolean publish(const char* topic, const char* payload);
|
||||
boolean publish(const char* topic, const char* payload, boolean retained);
|
||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
|
||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||
boolean subscribe(const char* topic);
|
||||
boolean subscribe(const char* topic, uint8_t qos);
|
||||
boolean unsubscribe(const char* topic);
|
||||
boolean loop();
|
||||
boolean connected();
|
||||
int state();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user