updates
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#include <Arduino.h>
|
||||
#include "Application.h"
|
||||
#include "Config.h"
|
||||
|
||||
void Application::begin()
|
||||
{
|
||||
Serial.println("-------------------------");
|
||||
Serial.println(" MQTTPanel32");
|
||||
Serial.print(" Version: ");
|
||||
Serial.println(Config::VERSION);
|
||||
Serial.println("-------------------------");
|
||||
|
||||
wifi.begin();
|
||||
|
||||
mqtt.begin();
|
||||
|
||||
display.begin();
|
||||
|
||||
|
||||
display.showStatus(
|
||||
wifi.ip(),
|
||||
mqtt.connected() ? "verbunden" : "offline"
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
void Application::loop()
|
||||
{
|
||||
wifi.loop();
|
||||
|
||||
mqtt.loop();
|
||||
|
||||
display.loop();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "WifiManager.h"
|
||||
#include "MqttManager.h"
|
||||
#include "Display/DisplayManager.h"
|
||||
|
||||
|
||||
class Application
|
||||
{
|
||||
public:
|
||||
void begin();
|
||||
void loop();
|
||||
|
||||
private:
|
||||
|
||||
WifiManager wifi;
|
||||
MqttManager mqtt;
|
||||
DisplayManager display;
|
||||
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
namespace Config
|
||||
{
|
||||
// WLAN
|
||||
constexpr const char* WIFI_SSID = "RS20W";
|
||||
constexpr const char* WIFI_PASSWORD = "***";
|
||||
|
||||
// MQTT
|
||||
constexpr const char* MQTT_HOST = "mqtt.diereuthers.home";
|
||||
constexpr uint16_t MQTT_PORT = 1883;
|
||||
constexpr const char* MQTT_CLIENT = "MQTTPanel32";
|
||||
|
||||
constexpr const char* MQTT_STATUS_TOPIC =
|
||||
"mqttpanel32/status";
|
||||
|
||||
constexpr const char* MQTT_COMMAND_TOPIC =
|
||||
"mqttpanel32/command";
|
||||
|
||||
|
||||
// Firmware
|
||||
constexpr const char* VERSION = "0.1.0";
|
||||
constexpr const char* HOSTNAME = "mqttpanel32";
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "DisplayManager.h"
|
||||
|
||||
|
||||
void DisplayManager::begin()
|
||||
{
|
||||
Serial.println("DisplayManager gestartet");
|
||||
}
|
||||
|
||||
|
||||
void DisplayManager::showStatus(
|
||||
const String& wifi,
|
||||
const String& mqtt
|
||||
)
|
||||
{
|
||||
Serial.println("----------------");
|
||||
Serial.println("DISPLAY");
|
||||
Serial.print("WLAN: ");
|
||||
Serial.println(wifi);
|
||||
|
||||
Serial.print("MQTT: ");
|
||||
Serial.println(mqtt);
|
||||
|
||||
Serial.println("----------------");
|
||||
}
|
||||
|
||||
|
||||
void DisplayManager::loop()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
class DisplayManager
|
||||
{
|
||||
public:
|
||||
|
||||
void begin();
|
||||
|
||||
void showStatus(
|
||||
const String& wifi,
|
||||
const String& mqtt
|
||||
);
|
||||
|
||||
void loop();
|
||||
};
|
||||
@@ -0,0 +1,104 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include "MqttManager.h"
|
||||
#include "Config.h"
|
||||
|
||||
|
||||
WiFiClient wifiClient;
|
||||
|
||||
PubSubClient mqttClient(wifiClient);
|
||||
|
||||
|
||||
|
||||
void MqttManager::begin()
|
||||
{
|
||||
mqttClient.setServer(
|
||||
Config::MQTT_HOST,
|
||||
Config::MQTT_PORT
|
||||
);
|
||||
|
||||
mqttClient.setCallback(
|
||||
[this](char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
callback(topic, payload, length);
|
||||
}
|
||||
);
|
||||
|
||||
reconnect();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MqttManager::loop()
|
||||
{
|
||||
if (!mqttClient.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
mqttClient.loop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MqttManager::reconnect()
|
||||
{
|
||||
Serial.println("MQTT verbinden...");
|
||||
|
||||
while (!mqttClient.connected())
|
||||
{
|
||||
if (mqttClient.connect(Config::MQTT_CLIENT))
|
||||
{
|
||||
Serial.println("MQTT verbunden");
|
||||
|
||||
mqttClient.publish(
|
||||
Config::MQTT_STATUS_TOPIC,
|
||||
"online"
|
||||
);
|
||||
|
||||
mqttClient.subscribe(
|
||||
Config::MQTT_COMMAND_TOPIC
|
||||
);
|
||||
|
||||
_connected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(".");
|
||||
|
||||
delay(3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MqttManager::callback(
|
||||
char* topic,
|
||||
byte* payload,
|
||||
unsigned int length
|
||||
)
|
||||
{
|
||||
Serial.print("MQTT Nachricht: ");
|
||||
Serial.println(topic);
|
||||
|
||||
Serial.print("Payload: ");
|
||||
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
Serial.print(
|
||||
(char)payload[i]
|
||||
);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool MqttManager::connected()
|
||||
{
|
||||
return mqttClient.connected();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
|
||||
class MqttManager
|
||||
{
|
||||
public:
|
||||
|
||||
void begin();
|
||||
void loop();
|
||||
|
||||
bool connected();
|
||||
|
||||
private:
|
||||
|
||||
void reconnect();
|
||||
void callback(
|
||||
char* topic,
|
||||
byte* payload,
|
||||
unsigned int length
|
||||
);
|
||||
|
||||
bool _connected = false;
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "WifiManager.h"
|
||||
#include "Config.h"
|
||||
|
||||
void WifiManager::begin()
|
||||
{
|
||||
Serial.println("WLAN verbinden...");
|
||||
|
||||
WiFi.setHostname(Config::HOSTNAME);
|
||||
|
||||
WiFi.begin(
|
||||
Config::WIFI_SSID,
|
||||
Config::WIFI_PASSWORD
|
||||
);
|
||||
|
||||
uint8_t counter = 0;
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
|
||||
counter++;
|
||||
|
||||
if (counter > 40)
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println("WLAN Timeout");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_connected = true;
|
||||
|
||||
Serial.println();
|
||||
Serial.println("WLAN verbunden");
|
||||
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
|
||||
void WifiManager::loop()
|
||||
{
|
||||
bool state = (WiFi.status() == WL_CONNECTED);
|
||||
|
||||
if (state != _connected)
|
||||
{
|
||||
_connected = state;
|
||||
|
||||
if (_connected)
|
||||
{
|
||||
Serial.println("WLAN wieder verbunden");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("WLAN getrennt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool WifiManager::connected()
|
||||
{
|
||||
return _connected;
|
||||
}
|
||||
|
||||
|
||||
String WifiManager::ip()
|
||||
{
|
||||
if (_connected)
|
||||
return WiFi.localIP().toString();
|
||||
|
||||
return "0.0.0.0";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
class WifiManager
|
||||
{
|
||||
public:
|
||||
void begin();
|
||||
void loop();
|
||||
|
||||
bool connected();
|
||||
String ip();
|
||||
|
||||
private:
|
||||
bool _connected = false;
|
||||
};
|
||||
+5
-5
@@ -1,17 +1,17 @@
|
||||
#include <Arduino.h>
|
||||
#include "Application.h"
|
||||
|
||||
Application app;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(1000);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("=====================");
|
||||
Serial.println(" MQTTPanel32");
|
||||
Serial.println("=====================");
|
||||
app.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
app.loop();
|
||||
}
|
||||
Reference in New Issue
Block a user