add infrastructure for mqtt subscriptions
This commit is contained in:
43
Network.cpp
43
Network.cpp
@@ -3,10 +3,12 @@
|
|||||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
Network* nwInst;
|
||||||
|
|
||||||
Network::Network()
|
Network::Network()
|
||||||
: _mqttClient(_wifiClient)
|
: _mqttClient(_wifiClient)
|
||||||
{
|
{
|
||||||
|
nwInst = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::initialize()
|
void Network::initialize()
|
||||||
@@ -24,15 +26,7 @@ void Network::initialize()
|
|||||||
// these are stored by the esp library
|
// these are stored by the esp library
|
||||||
//wm.resetSettings();
|
//wm.resetSettings();
|
||||||
|
|
||||||
// Automatically connect using saved credentials,
|
bool res = wm.autoConnect(); // password protected ap
|
||||||
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
|
|
||||||
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
|
|
||||||
// then goes into a blocking loop awaiting configuration and will return success result
|
|
||||||
|
|
||||||
bool res;
|
|
||||||
// res = wm.autoConnect(); // auto generated AP name from chipid
|
|
||||||
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
|
|
||||||
res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
|
|
||||||
|
|
||||||
if(!res) {
|
if(!res) {
|
||||||
Serial.println("Failed to connect");
|
Serial.println("Failed to connect");
|
||||||
@@ -45,6 +39,7 @@ void Network::initialize()
|
|||||||
}
|
}
|
||||||
|
|
||||||
_mqttClient.setServer("192.168.0.100", 1883);
|
_mqttClient.setServer("192.168.0.100", 1883);
|
||||||
|
_mqttClient.setCallback(Network::onMqttDataReceivedCallback);
|
||||||
_mqttClient.publish("nuki/test", "OK");
|
_mqttClient.publish("nuki/test", "OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +51,9 @@ bool Network::reconnect()
|
|||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (_mqttClient.connect("arduinoClient")) {
|
if (_mqttClient.connect("arduinoClient")) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// Once connected, publish an announcement...
|
|
||||||
_mqttClient.publish("outTopic","hello world");
|
|
||||||
// ... and resubscribe
|
// ... and resubscribe
|
||||||
_mqttClient.subscribe("inTopic");
|
_mqttClient.subscribe("nuki/cmd");
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(_mqttClient.state());
|
Serial.print(_mqttClient.state());
|
||||||
@@ -105,3 +99,26 @@ void Network::update()
|
|||||||
|
|
||||||
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
vTaskDelay( 100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned int length)
|
||||||
|
{
|
||||||
|
nwInst->onMqttDataReceived(topic, payload, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
|
||||||
|
{
|
||||||
|
char value[50];
|
||||||
|
size_t l = min(length, sizeof(value)-1);
|
||||||
|
|
||||||
|
for(int i=0; i<l; i++)
|
||||||
|
{
|
||||||
|
value[i] = payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
value[l] = 0;
|
||||||
|
|
||||||
|
if(strcmp(topic, "nuki/cmd") == 0)
|
||||||
|
{
|
||||||
|
Serial.println(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ public:
|
|||||||
void update();
|
void update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
||||||
|
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
|
||||||
|
|
||||||
bool reconnect();
|
bool reconnect();
|
||||||
|
|
||||||
PubSubClient _mqttClient;
|
PubSubClient _mqttClient;
|
||||||
|
|||||||
Reference in New Issue
Block a user