From 20d02638121c9f343e405988bb0fba9c9dcb3af9 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sun, 7 Oct 2018 16:09:43 +0100 Subject: [PATCH] Move network management stuff to own class --- Networking.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ Networking.h | 28 +++++++++++++++++++++ monitor.ino | 62 ++++++---------------------------------------- sensors.cpp | 8 +++--- sensors.h | 5 ++-- 5 files changed, 110 insertions(+), 60 deletions(-) create mode 100644 Networking.cpp create mode 100644 Networking.h diff --git a/Networking.cpp b/Networking.cpp new file mode 100644 index 0000000..06f4df3 --- /dev/null +++ b/Networking.cpp @@ -0,0 +1,67 @@ +#include "Networking.h" +#include +#include + +Networking::Networking(char* hostname, char* ssid, char* password, char* mqttServer){ + _hostname = hostname; + _ssid = ssid; + _password = password; + + auto callback = std::bind(&Networking::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + _mqttClient = PubSubClient(mqttServer, 1883, callback, _wifiClient); +} + +void Networking::setup(){ + WiFi.hostname(_hostname); + WiFi.begin(_ssid, _password); + reconnect(); +} + +void Networking::waitForWiFi() { + if (WiFi.status() == WL_CONNECTED) + return; + Serial.println("Reconnecting WiFi..."); + int waitCount=0; + while (WiFi.status() != WL_CONNECTED) { + delay(100); + waitCount++; + if (waitCount>600) + ESP.restart(); + } + Serial.println("Connected WiFi!"); +} + +void Networking::waitForMQTT() { + if (_mqttClient.connected()) + return; + Serial.println("Reconnecting MQTT..."); + while (!_mqttClient.connected()) + if (!_mqttClient.connect(_hostname)) + delay(50); + Serial.println("Connected MQTT!"); +} + +void Networking::reconnect() { + waitForWiFi(); + waitForMQTT(); +} + +void Networking::loop(){ + reconnect(); + _mqttClient.loop(); +} + +void Networking::mqttCallback(char *topic, byte *payload, unsigned int length) { + Serial.print("Got payload: "); + for (int i = 0; i < length; i++) + Serial.print((char)payload[i]); + Serial.println(); +} + +void Networking::publish(const char* topic, const char* payload){ + publish(topic, payload, false) +} + +void Networking::publish(const char* topic, const char* payload, bool retained){ + _mqttClient.publish(topic, payload, retained); +} \ No newline at end of file diff --git a/Networking.h b/Networking.h new file mode 100644 index 0000000..89e709c --- /dev/null +++ b/Networking.h @@ -0,0 +1,28 @@ +#ifndef Networking_h +#define Networking_h + +#include +#include + +class Networking{ + char* _hostname; + char* _ssid; + char* _password; + + WiFiClient _wifiClient; + PubSubClient _mqttClient; + + void waitForWiFi(); + void waitForMQTT(); + void mqttCallback(char *topic, byte *payload, unsigned int length); + + public: + Networking(char* hostname, char* ssid, char* password, char* mqttServer); + void setup(); + void reconnect(); + void loop(); + void publish(const char* topic, const char* payload); + void publish(const char* topic, const char* payload, bool retained); +}; + +#endif \ No newline at end of file diff --git a/monitor.ino b/monitor.ino index 87ff171..6495568 100644 --- a/monitor.ino +++ b/monitor.ino @@ -1,8 +1,8 @@ -#include -#include + #include #include +#include "Networking.h" #include "sensors.h" #include "NaturalLight.h" #include "Weather.h" @@ -11,6 +11,7 @@ #define MQTT_SERVER "192.168.1.3" #define SSID "GCHQ Surveillance Van" #define PASSWORD "cocklol." + #define PH_TOPIC "/home/sensors/fishtank/ph" #define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature" #define TEMPERATURE_PIN D5 @@ -26,54 +27,19 @@ #define NTP_POOL "uk.pool.ntp.org" -void mqttCallback(char *topic, byte *payload, unsigned int length); - -WiFiClient _wifiClient; -PubSubClient _mqttClient = PubSubClient(MQTT_SERVER, 1883, mqttCallback, _wifiClient); - -Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, _mqttClient); +Networking _networking = Networking(HOSTNAME, SSID, PASSWORD ,MQTT_SERVER); +Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, &_networking); NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET); Weather _weather = Weather(LATITUDE, LONGITUDE); Lighting _lighting = Lighting(LED_COUNT, &_naturalLight, &_weather, CLOUD_COVER_LIMIT); -void waitForWiFi() { - if (WiFi.status() == WL_CONNECTED) - return; - Serial.println("Reconnecting WiFi..."); - int waitCount=0; - while (WiFi.status() != WL_CONNECTED) { - delay(100); - waitCount++; - if (waitCount>600) - ESP.restart(); - } - Serial.println("Connected WiFi!"); -} - -void waitForMQTT() { - if (_mqttClient.connected()) - return; - Serial.println("Reconnecting MQTT..."); - while (!_mqttClient.connected()) - if (!_mqttClient.connect(HOSTNAME)) - delay(50); - Serial.println("Connected MQTT!"); -} - -void reconnect() { - waitForWiFi(); - waitForMQTT(); -} void setup() { Serial.begin(115200); + _networking.setup(); _sensors.setup(); - WiFi.hostname(HOSTNAME); - WiFi.begin(SSID, PASSWORD); - reconnect(); - delay(2000); configTime(0, 0, NTP_POOL); @@ -85,7 +51,7 @@ void setup() { void publishFloat(char* topic, float value){ char output[6]; dtostrf(value, 0, 2, output); - _mqttClient.publish(topic, output, true); + _networking.publish(topic, output, true); } void doLighting() { @@ -118,21 +84,9 @@ void syncTime(){ } void loop() { - reconnect(); - _mqttClient.loop(); - + _networking.loop(); syncTime(); - doLighting(); - //publishReadings(); - delay(10); -} - -void mqttCallback(char *topic, byte *payload, unsigned int length) { - Serial.print("Got payload: "); - for (int i = 0; i < length; i++) - Serial.print((char)payload[i]); - Serial.println(); } \ No newline at end of file diff --git a/sensors.cpp b/sensors.cpp index d5c5503..703fd3b 100644 --- a/sensors.cpp +++ b/sensors.cpp @@ -1,7 +1,7 @@ #include "sensors.h" -Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, PubSubClient mqttClient) { - _mqttClient = mqttClient; +Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking) { + _networking = networking; OneWire oneWire = OneWire(temperaturePin); _ds18b20 = DallasTemperature(&oneWire); _temperaturePin = temperaturePin; @@ -37,12 +37,12 @@ float Sensors::readpH() { void Sensors::publishpH() { float pH = readpH(); String pHString(pH, 2); - _mqttClient.publish(_phTopic, pHString.c_str(), true); + _networking->publish(_phTopic, pHString.c_str(), true); } void Sensors::publishTemperature() { _ds18b20.requestTemperatures(); float temperature = _ds18b20.getTempCByIndex(0); String temperatureString(temperature, 2); - _mqttClient.publish(_temperatureTopic, temperatureString.c_str(), true); + _networking->publish(_temperatureTopic, temperatureString.c_str(), true); } \ No newline at end of file diff --git a/sensors.h b/sensors.h index e709653..38f774e 100644 --- a/sensors.h +++ b/sensors.h @@ -5,6 +5,7 @@ #include #include #include +#include "Networking.h" #define PH_7_VOLTAGE 2.5 #define PH_4_VOLTAGE 3.04 @@ -17,10 +18,10 @@ class Sensors { float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3; Adafruit_ADS1115 _ads; DallasTemperature _ds18b20; - PubSubClient _mqttClient; + Networking* _networking; public: - Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, PubSubClient mqttClient); + Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking); void setup(); float readpH(); void publishpH();