fishtankmonitor/monitor.ino

141 lines
No EOL
3.2 KiB
C++

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <FastLED.h>
#include <time.h>
#include "sensors.h"
#include "NaturalLight.h"
#define HOSTNAME "Fishtank"
#define MQTT_SERVER "192.168.1.3"
#define SSID "GCHQ Surveillance Van"
#define PASSWORD "cocklol."
#define LIGHT_PIN D2
#define LED_COUNT 36
#define PH_TOPIC "/home/sensors/fishtank/ph"
#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature"
#define TEMPERATURE_PIN D5
#define LATITUDE "20.548103"
#define LONGITUDE "96.916835"
#define TIMEZONE_OFFSET 27000 // 7.5 hours in seconds
#define NTP_POOL "uk.pool.ntp.org"
#define HEAT_INDEX_MAX 241.0 // After 241 the pallet seems to go dim
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);
NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET);
CRGB _leds[LED_COUNT];
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);
_sensors.setup();
WiFi.hostname(HOSTNAME);
WiFi.begin(SSID, PASSWORD);
reconnect();
delay(2000);
configTime(0, 0, NTP_POOL);
_naturalLight.update();
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(_leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
}
float getIndexMultiplier(int now, int startTime, int endTime, bool swap){
float indexMultiplier = 1;
float a = swap ? endTime - now : now - startTime;
float b = endTime - startTime;
return constrain(a / b, 0, 1);
}
float getHeatIndex() {
time_t now = time(nullptr);
float indexMultiplier = now >= _naturalLight.getAstronomicalTwilightEnd() ?
getIndexMultiplier(now, _naturalLight.getSunset(), _naturalLight.getAstronomicalTwilightEnd(), true) :
getIndexMultiplier(now, _naturalLight.getAstronomicalTwilightBegin(), _naturalLight.getSunrise(), false);
return HEAT_INDEX_MAX * indexMultiplier;
}
void doLighting() {
EVERY_N_SECONDS(1) {
CRGB colour = ColorFromPalette(HeatColors_p, getHeatIndex());
fill_solid(_leds, LED_COUNT, colour);
FastLED.show();
}
EVERY_N_SECONDS(10800){
_naturalLight.update();
}
}
void publishReadings(){
EVERY_N_SECONDS(60) {
_sensors.publishpH();
_sensors.publishTemperature();
}
}
void syncTime(){
EVERY_N_SECONDS(3600) {
configTime(0, 0, NTP_POOL);
}
}
void loop() {
reconnect();
_mqttClient.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();
}