Move network management stuff to own class
This commit is contained in:
parent
a3068a2d15
commit
20d0263812
5 changed files with 110 additions and 60 deletions
67
Networking.cpp
Normal file
67
Networking.cpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include "Networking.h"
|
||||
#include <PubSubClient.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
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);
|
||||
}
|
28
Networking.h
Normal file
28
Networking.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef Networking_h
|
||||
#define Networking_h
|
||||
|
||||
#include <PubSubClient.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
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
|
62
monitor.ino
62
monitor.ino
|
@ -1,8 +1,8 @@
|
|||
#include <PubSubClient.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include <FastLED.h>
|
||||
#include <time.h>
|
||||
|
||||
#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();
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
#include <PubSubClient.h>
|
||||
#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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue