Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dodać klienta mqtt #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/mqtt_client/mqtt_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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();
}

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");
} else {
Serial.print(" failed, rc=");
Serial.print(state());
Serial.println(" retry in 3 seconds");
// Wait 3 seconds before retrying
delay(3000);
}
}
}
26 changes: 26 additions & 0 deletions lib/mqtt_client/mqtt_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#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);

};
2 changes: 1 addition & 1 deletion lib/wifi/wifi.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <ESP8266WiFi.h>
// #include <ESP8266WiFi.h>
#include <WiFiManager.h>

#define SSID "SynerMycha"
Expand Down
21 changes: 17 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#include "wifi.h"

#include <WiFiManager.h>
#include <PubSubClient.h>
#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);
// }

}