-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JVKran
committed
Aug 3, 2020
1 parent
6833840
commit 178e7ec
Showing
8 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "ClimateManager.hpp" | ||
|
||
ClimateManager::ClimateManager(ForcedClimate & climateSensor, MqttClient & client, const unsigned int updatePeriod): | ||
climateSensor(climateSensor), | ||
updatePeriod(updatePeriod), | ||
client(client) | ||
{} | ||
|
||
void ClimateManager::operator()(){ | ||
if(millis() - lastUpdate > updatePeriod){ | ||
climateSensor.takeForcedMeasurement(); | ||
client.sendMessage("/kas/vochtigheid", String(climateSensor.getRelativeHumidity()).c_str()); | ||
client.sendMessage("/kas/temperatuur", String(climateSensor.getTemperatureCelcius()).c_str()); | ||
client.sendMessage("/kas/luchtdruk", String(climateSensor.getPressure()).c_str()); | ||
lastUpdate = millis(); | ||
if(climateSensor.getTemperatureCelcius(true) > 30){ | ||
// Turn on fan | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef __CLIMATE_MANAGER | ||
#define __CLIMATE_MANAGER | ||
|
||
#include <forcedClimate.h> | ||
#include "MqttClient.hpp" | ||
|
||
#define CLM_10MIN 600000 | ||
|
||
class ClimateManager { | ||
private: | ||
ForcedClimate & climateSensor; | ||
unsigned int updatePeriod; | ||
unsigned int lastUpdate = 0; | ||
|
||
MqttClient & client; | ||
public: | ||
ClimateManager(ForcedClimate & climateSensor, MqttClient & client, const unsigned int updatePeriod = CLM_10MIN); | ||
|
||
void operator()(); | ||
|
||
}; | ||
|
||
#endif //__CLIMATE_MANAGER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <PubSubClient.h> | ||
#include <forcedClimate.h> | ||
|
||
#include "MqttClient.hpp" | ||
#include "ClimateManager.hpp" | ||
#include "PlantManager.hpp" | ||
|
||
WiFiClient espClient; | ||
MqttClient client("KraanBast2.4", "Snip238!", "192.168.178.81", "/raspberrypi/hassio", espClient); | ||
|
||
ForcedClimate climateSensor = ForcedClimate(); | ||
ClimateManager climateManager = ClimateManager(climateSensor, client, CLM_10MIN); | ||
PlantManager plantManager = PlantManager(D5, client); | ||
|
||
void callback(char* topic, byte* payload, unsigned int length) { | ||
static String message; | ||
for (int i = 0; i < length; i++) { | ||
message.concat((char)payload[i]); | ||
} | ||
client.notifyListeners(message, topic); | ||
message = ""; | ||
} | ||
|
||
void setup(){ | ||
Wire.begin(); | ||
Serial.begin(115200); | ||
|
||
climateSensor.begin(); | ||
plantManager.begin(); | ||
|
||
client.setupWifi(); | ||
client.setupConnections(); | ||
|
||
climateSensor.takeForcedMeasurement(); | ||
Serial.print("Temperature: "); | ||
Serial.print(climateSensor.getTemperatureCelcius()); | ||
Serial.print(", Humidity: "); | ||
Serial.print(climateSensor.getRelativeHumidity()); | ||
Serial.print(" and Pressure: "); | ||
Serial.print(climateSensor.getPressure()); | ||
Serial.println(); | ||
} | ||
|
||
void loop(){ | ||
client(); // Handle incoming messages | ||
climateManager(); | ||
plantManager(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "MqttClient.hpp" | ||
|
||
MqttClient::MqttClient(char* ssid, char* password, char* mqttServer, const char* topic, WiFiClient & espClient, const bool retainedMessages, const uint8_t qosLevel): | ||
ssid(ssid), | ||
password(password), | ||
mqttServer(mqttServer), | ||
topic(topic), | ||
client(espClient), | ||
retainedMessages(retainedMessages), | ||
qosLevel(qosLevel) | ||
{ | ||
WiFi.mode(WIFI_STA); | ||
} | ||
|
||
void MqttClient::addListener(MessageListener & listener){ | ||
if(amountOfListeners++ <= 19){ | ||
listeners[amountOfListeners] = &listener; | ||
} | ||
} | ||
|
||
void MqttClient::notifyListeners(const String & message, const char* topic){ | ||
for (int i = 0; i < amountOfListeners; i++){ | ||
listeners[i]->messageReceived(message, topic); | ||
} | ||
} | ||
|
||
void MqttClient::setupWifi() { | ||
delay(10); | ||
WiFi.begin(ssid, password); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
} | ||
} | ||
|
||
void MqttClient::setupConnections(){ | ||
client.setServer(mqttServer, 1883); | ||
client.setCallback(callback); | ||
client.subscribe(topic, qosLevel); | ||
reconnect(); //Establish a connection by signing in with credentials. | ||
} | ||
|
||
void MqttClient::reconnect() { | ||
while (!client.connected()) { | ||
if (client.connect("GreenhouseClient", "Greenhouse", "Snip238!")) { | ||
delay(500); | ||
client.subscribe(topic); | ||
for (int i = 0; i < amountOfListeners; i++){ | ||
listeners[i]->messageReceived("CONNECTED"); | ||
} | ||
delay(500); | ||
break; | ||
} else { | ||
delay(500); | ||
} | ||
} | ||
} | ||
|
||
void MqttClient::operator()(){ | ||
if (!client.connected()) { | ||
reconnect(); | ||
} | ||
client.loop(); | ||
} | ||
|
||
void MqttClient::sendMessage(const char* topic, const char* messageToSend){ | ||
client.publish(topic, messageToSend, retainedMessages); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef __CONNECTIONS_HPP | ||
#define __CONNECTIONS_HPP | ||
|
||
#include "Arduino.h" | ||
#include <ESP8266WiFi.h> | ||
#include <PubSubClient.h> | ||
#include "array.hpp" | ||
|
||
class MessageListener { | ||
public: | ||
virtual void messageReceived(const String & receivedMessage, const char* topic = "") = 0; | ||
}; | ||
|
||
void callback(char* topic, byte* payload, unsigned int length); | ||
|
||
class MqttClient { | ||
private: | ||
char* ssid; | ||
char* password; | ||
char* mqttServer; | ||
|
||
const char* topic; | ||
PubSubClient client; | ||
const bool retainedMessages; | ||
const uint8_t qosLevel; | ||
|
||
MessageListener * listeners[20] = {}; | ||
uint8_t amountOfListeners = 0; | ||
public: | ||
|
||
MqttClient(char* ssid, char* password, char* mqttServer, const char* topic, WiFiClient & espClient, const bool retainedMessages = true, const uint8_t qosLevel = 1); | ||
|
||
void addListener(MessageListener & listener); | ||
void notifyListeners(const String & mesage, const char* topic); | ||
|
||
void setupWifi(); | ||
void setupConnections(); | ||
void reconnect(); | ||
|
||
void operator()(); | ||
void sendMessage(const char* topic, const char* messageToSend); | ||
}; | ||
|
||
#endif //__CONNECTIONS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "PlantManager.hpp" | ||
|
||
PlantManager::PlantManager(const uint8_t fanPin, MqttClient & client, const uint8_t moisturePin, const unsigned int updatePeriod): | ||
fanPin(fanPin), | ||
moisturePin(moisturePin), | ||
client(client), | ||
updatePeriod(updatePeriod) | ||
{} | ||
|
||
void PlantManager::begin(){ | ||
pinMode(fanPin, OUTPUT); | ||
pinMode(moisturePin, INPUT); | ||
} | ||
|
||
void PlantManager::operator()(){ | ||
if(analogRead(moisturePin) < 500){ | ||
// Plants should be watered. | ||
} | ||
if(millis() - lastUpdate > updatePeriod){ | ||
client.sendMessage("/kas/grondvochtigheid", String(analogRead(moisturePin)).c_str()); | ||
lastUpdate = millis(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef __PLANT_MANAGER | ||
#define __PLANT_MANAGER | ||
|
||
#include "MqttClient.hpp" | ||
|
||
class PlantManager { | ||
private: | ||
const uint8_t fanPin; | ||
const uint8_t moisturePin; | ||
|
||
MqttClient & client; | ||
unsigned int updatePeriod; | ||
unsigned int lastUpdate = 0; | ||
public: | ||
PlantManager(const uint8_t fanPin, MqttClient & client, const uint8_t moisturePin = A0, const unsigned int updatePeriod = 600000); | ||
void begin(); | ||
|
||
void operator()(); | ||
}; | ||
|
||
#endif //__PLANT_MANAGER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef __ARRAY_HPP | ||
#define __ARRAY_HPP | ||
|
||
template <class T, size_t N> | ||
struct array { | ||
T data[N]; | ||
|
||
static size_t length() { | ||
return N; | ||
} | ||
|
||
using type = T; | ||
|
||
T &operator[](size_t index) { | ||
return data[index]; | ||
} | ||
|
||
const T &operator[](size_t index) const { | ||
return data[index]; | ||
} | ||
|
||
T *begin() { | ||
return &data[0]; | ||
} | ||
const T *begin() const { | ||
return &data[0]; | ||
} | ||
|
||
T *end() { | ||
return &data[N]; | ||
} | ||
|
||
const T *end() const { | ||
return &data[N]; | ||
} | ||
|
||
bool operator==(const array<T, N> &rhs) const { | ||
if (this == &rhs) | ||
return true; | ||
for (size_t i = 0; i < N; i++) | ||
if ((*this)[i] != rhs[i]) | ||
return false; | ||
return true; | ||
} | ||
|
||
bool operator!=(const array<T, N> &rhs) const { | ||
return !(*this == rhs); | ||
} | ||
}; | ||
|
||
#endif //__ARRAY_HPP |