fishtankmonitor/monitor.ino

140 lines
No EOL
3.4 KiB
C++

/*-----------------+
| Network settings |
+-----------------*/
#define HOSTNAME "72L_Aquarium"
#define NETWORK_NAME "GCHQ Surveillance Van"
#define PASSWORD "cocklol."
#define MQTT_SERVER "192.168.1.3"
/*------------------+
| Lighting settings |
+------------------*/
#define LED_USE_STRIP
#define LED_COUNT 41
#define LED_USE_PWM
#define CLOUD_COVER_LIMIT 50.0 // percent;
/*----------------+
| Pin allocations |
+----------------*/
#define TEMPERATURE_PIN D5
#define WHITE_PIN D2
#define RGB_PIN D3
#define R_PIN D8
#define G_PIN D7
#define B_PIN D6
#define SCL_PIN D4
#define SDA_PIN D1
/*---------------------------+
| Location and time settings |
+---------------------------*/
#define LATITUDE "20.548103"
#define LONGITUDE "96.916835"
#define TIMEZONE_OFFSET 28800 // 8 hours in seconds
#define NTP_POOL "uk.pool.ntp.org"
/*============================================================================================================================*/
#include <FastLED.h>
#include <time.h>
#include "Networking.h"
#include "Sensors.h"
#include "NaturalLight.h"
#include "Lighting.h"
#include "Weather.h"
#include "Screen.h"
#define BASE_TOPIC "/home/sensors/" HOSTNAME
#define PH_TOPIC BASE_TOPIC "/ph"
#define TEMPERATURE_TOPIC BASE_TOPIC "/temperature"
#define LIGHT_INDEX_TOPIC BASE_TOPIC "/lightindex"
#define BRIGHTNESS_TOPIC BASE_TOPIC "/brightness"
/*----------------------------------------------------------+
| Need this here until I figure out why I can't instantiate |
| within the Sensors class... |
+----------------------------------------------------------*/
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(TEMPERATURE_PIN);
DallasTemperature temperatureSensor(&oneWire);
Networking _networking = Networking(HOSTNAME, NETWORK_NAME, PASSWORD ,MQTT_SERVER);
Sensors _sensors = Sensors(TEMPERATURE_PIN, &_networking, &temperatureSensor);
NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET);
Weather _weather = Weather(LATITUDE, LONGITUDE);
Lighting _lighting = Lighting(&_naturalLight, &_weather, CLOUD_COVER_LIMIT);
Screen _display = Screen(&_sensors, SDA_PIN, SCL_PIN);
void setup() {
Serial.begin(115200);
#ifdef LED_USE_STRIP
_lighting.setupStrip<RGB_PIN, LED_COUNT, WHITE_PIN>();
#endif
#ifdef LED_USE_PWM
_lighting.setupPWM<R_PIN, G_PIN, B_PIN, WHITE_PIN>();
#endif
_networking.setup();
_sensors.setup();
_display.setup();
delay(2000);
configTime(0, 0, NTP_POOL);
_naturalLight.update();
_weather.update();
}
void publishFloat(char* topic, float value){
char output[6];
dtostrf(value, 0, 2, output);
_networking.publish(topic, output, true);
}
void doLighting() {
EVERY_N_SECONDS(10800){
_naturalLight.update();
}
EVERY_N_SECONDS(60){
_weather.update();
}
EVERY_N_SECONDS(1) {
publishFloat(LIGHT_INDEX_TOPIC, _lighting.getCurrentHeatIndex());
publishFloat(BRIGHTNESS_TOPIC, _lighting.getCurrentBrightness());
}
_lighting.update(time(nullptr));
}
void publishTemperature(){
String temperature = _sensors.getTemperature();
if (temperature != "Pending...")
_networking.publish(TEMPERATURE_TOPIC, temperature.c_str(), true);
}
void publishReadings(){
EVERY_N_SECONDS(2) {
publishTemperature();
_display.update();
}
}
void syncTime(){
EVERY_N_SECONDS(3600) {
configTime(0, 0, NTP_POOL);
}
}
void loop() {
_networking.loop();
syncTime();
doLighting();
publishReadings();
delay(10);
}