From b57c775021b51037172f08ca18786679bc84831f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw?= Date: Mon, 16 Mar 2020 21:28:57 +0100 Subject: [PATCH 1/2] add mqtt client --- lib/mqtt_client/mqtt_client.cpp | 62 +++++++++++++++++++++++++++++++++ lib/mqtt_client/mqtt_client.h | 26 ++++++++++++++ lib/wifi/wifi.h | 2 +- src/main.cpp | 21 ++++++++--- 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 lib/mqtt_client/mqtt_client.cpp create mode 100644 lib/mqtt_client/mqtt_client.h diff --git a/lib/mqtt_client/mqtt_client.cpp b/lib/mqtt_client/mqtt_client.cpp new file mode 100644 index 0000000..0d02c41 --- /dev/null +++ b/lib/mqtt_client/mqtt_client.cpp @@ -0,0 +1,62 @@ +#include "mqtt_client.h" + +MQTTClient::MQTTClient() : PubSubClient(this->wifi_client){ + + // const char* mqtt_server = MQTT_SERVER_IP; + // unsigned long lastMsg = 0; + // char msg[MSG_BUFFER_SIZE]; + // int value = 0; +} + +void MQTTClient::setup() { + setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT); + setCallback(this->callback); +} + + + +void MQTTClient::callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level + // but actually the LED is on; this is because + // it is active low on the ESP-01) + } else { + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH + } + +} + +void MQTTClient::assureConnection() { + if (!connected()) reconnect(); +} + +void MQTTClient::reconnect() { + + // Loop until we're reconnected + while (!connected()) { + Serial.print("Attempting MQTT connection..."); + + if (connect(MQTT_CLIENT_ID)) { + Serial.println("connected"); + // Once connecttry agained, publish an announcement... + // this->client.publish("outTopic", "hello world"); +// // ... and resubscribe +// this->client.subscribe("inTopic"); + } else { + Serial.print(" failed, rc="); + Serial.print(state()); + Serial.println(" retry in 3 seconds"); + // Wait 3 seconds before retrying + delay(3000); + } + } +} \ No newline at end of file diff --git a/lib/mqtt_client/mqtt_client.h b/lib/mqtt_client/mqtt_client.h new file mode 100644 index 0000000..4e6397b --- /dev/null +++ b/lib/mqtt_client/mqtt_client.h @@ -0,0 +1,26 @@ +#include +#include + +#define MQTT_SERVER_IP "192.168.1.100" +#define MQTT_SERVER_PORT 1883 +#define MQTT_CLIENT_ID "SynerMycha" +#define MSG_BUFFER_SIZE (50) + + +class MQTTClient : public PubSubClient { + public: + MQTTClient(); + + const char* mqtt_server; + + void setup(); + void reconnect(); + void assureConnection(); + + private: + // WiFiManager wifiManager; + WiFiClient wifi_client; + + static void callback(char* topic, byte* payload, unsigned int length); + +}; \ No newline at end of file diff --git a/lib/wifi/wifi.h b/lib/wifi/wifi.h index f2f70d9..8605a61 100644 --- a/lib/wifi/wifi.h +++ b/lib/wifi/wifi.h @@ -1,4 +1,4 @@ -#include +// #include #include #define SSID "SynerMycha" diff --git a/src/main.cpp b/src/main.cpp index 89feeb8..663a7c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,29 @@ #include "wifi.h" - -#include -#include +#include "mqtt_client.h" WIFI wifi; +MQTTClient mqtt_client; void setup() { + pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); wifi.setup(); - + mqtt_client.setup(); } + void loop() { + mqtt_client.assureConnection(); + mqtt_client.loop(); + + // unsigned long now = millis(); + // if (now - lastMsg > 2000) { + // lastMsg = now; + // ++value; + // snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); + // Serial.print("Publish message: "); + // Serial.println(msg); + // client.publish("outTopic", msg); + // } } \ No newline at end of file From caae60c54ea9179824f49e1b703f19e2ef5d109d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw?= Date: Mon, 16 Mar 2020 21:47:48 +0100 Subject: [PATCH 2/2] cleanup --- lib/mqtt_client/mqtt_client.cpp | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/lib/mqtt_client/mqtt_client.cpp b/lib/mqtt_client/mqtt_client.cpp index 0d02c41..3aadfc0 100644 --- a/lib/mqtt_client/mqtt_client.cpp +++ b/lib/mqtt_client/mqtt_client.cpp @@ -23,16 +23,6 @@ void MQTTClient::callback(char* topic, byte* payload, unsigned int length) { Serial.print((char)payload[i]); } Serial.println(); - - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '1') { - digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level - // but actually the LED is on; this is because - // it is active low on the ESP-01) - } else { - digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH - } - } void MQTTClient::assureConnection() { @@ -42,21 +32,17 @@ void MQTTClient::assureConnection() { void MQTTClient::reconnect() { // Loop until we're reconnected - while (!connected()) { - Serial.print("Attempting MQTT connection..."); + while (!connected()) { + Serial.print("Attempting MQTT connection..."); if (connect(MQTT_CLIENT_ID)) { - Serial.println("connected"); - // Once connecttry agained, publish an announcement... - // this->client.publish("outTopic", "hello world"); -// // ... and resubscribe -// this->client.subscribe("inTopic"); + Serial.println("connected"); } else { - Serial.print(" failed, rc="); - Serial.print(state()); - Serial.println(" retry in 3 seconds"); - // Wait 3 seconds before retrying - delay(3000); + Serial.print(" failed, rc="); + Serial.print(state()); + Serial.println(" retry in 3 seconds"); + // Wait 3 seconds before retrying + delay(3000); } } } \ No newline at end of file