Make variable naming consistent. Organise things a bit.

This commit is contained in:
Robert Marshall 2018-09-20 17:36:30 +01:00
parent 386f405f15
commit f383baae95

View file

@ -9,10 +9,10 @@
#include <ArduinoJson.h>
#include <time.h>
#define HOSTNAME "Fishtank"
#define MQTT_SERVER "192.168.1.3"
#define SSID "GCHQ Surveillance Van"
#define PASSWORD "cocklol."
#define LIGHT_TOPIC "/home/switches/kitchen/fishtank"
#define LIGHT_PIN D2
#define LED_COUNT 36
#define PH_TOPIC "/home/sensors/fishtank/ph"
@ -27,21 +27,21 @@
#define SOLAR_EVENT_URL "http://api.sunrise-sunset.org/json?lat=20.548103&lng=96.916835&formatted=0"
#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);
Adafruit_ADS1115 ads;
OneWire oneWire(TEMPERATURE_PIN);
DallasTemperature DS18B20(&oneWire);
WiFiClient wifiClient;
PubSubClient client = PubSubClient(MQTT_SERVER, 1883, mqttCallback, wifiClient);
CRGB leds[LED_COUNT];
bool lightsOn = true;
WiFiClient _wifiClient;
PubSubClient _mqttClient = PubSubClient(MQTT_SERVER, 1883, mqttCallback, _wifiClient);
static const float heatIndexMax = 241.0; // After 241 the pallet seems to go dim
Adafruit_ADS1115 _ads;
OneWire _oneWire(TEMPERATURE_PIN);
DallasTemperature _ds18b20(&_oneWire);
CRGB _leds[LED_COUNT];
int _lightsOnStartTime, _lightsOnEndTime, _lightsOffStartTime, _lightsOffEndTime;
float pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
void waitForWiFi() {
if (WiFi.status() == WL_CONNECTED)
@ -58,14 +58,12 @@ void waitForWiFi() {
}
void waitForMQTT() {
if (client.connected())
if (_mqttClient.connected())
return;
Serial.println("Reconnecting MQTT...");
while (!client.connected())
if (!client.connect(PH_TOPIC))
while (!_mqttClient.connected())
if (!_mqttClient.connect(HOSTNAME))
delay(50);
client.subscribe(LIGHT_TOPIC);
Serial.println(LIGHT_TOPIC);
Serial.println("Connected MQTT!");
}
@ -124,12 +122,13 @@ void parseEvents(String json){
void setup() {
Serial.begin(115200);
ads.setGain(GAIN_TWOTHIRDS);
ads.begin();
_ads.setGain(GAIN_TWOTHIRDS);
_ads.begin();
pinMode(TEMPERATURE_PIN, INPUT_PULLUP);
DS18B20.begin();
_ds18b20.begin();
WiFi.hostname(HOSTNAME);
WiFi.begin(SSID, PASSWORD);
reconnect();
@ -139,10 +138,7 @@ void setup() {
String json = getSolarEventJson();
parseEvents(json);
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
Serial.print("Light interval: ");
Serial.println(lightChangeInterval);
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(_leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
}
float readpH() {
@ -152,7 +148,7 @@ float readpH() {
for (int i = 0; i < readCount; i++)
{
sum += ads.readADC_SingleEnded(0);
sum += _ads.readADC_SingleEnded(0);
delay(10);
}
@ -163,7 +159,7 @@ float readpH() {
float voltage = 6.144 / 32768.0 * averageRead;
voltage -= VOLTAGE_OFFSET;
Serial.println(voltage);
float pH = 7 - ((PH_7_VOLTAGE - voltage) / pHStep);
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
return pH;
}
@ -171,15 +167,15 @@ void publishpH() {
float pH = readpH();
String pHString(pH, 2);
Serial.println(pHString);
client.publish(PH_TOPIC, pHString.c_str(), true);
_mqttClient.publish(PH_TOPIC, pHString.c_str(), true);
}
void publishTemperature() {
DS18B20.requestTemperatures();
float temperature = DS18B20.getTempCByIndex(0);
_ds18b20.requestTemperatures();
float temperature = _ds18b20.getTempCByIndex(0);
String temperatureString(temperature, 2);
Serial.println(temperatureString);
client.publish(TEMPERATURE_TOPIC, temperatureString.c_str(), true);
_mqttClient.publish(TEMPERATURE_TOPIC, temperatureString.c_str(), true);
}
float getIndexMultiplier(int now, int startTime, int endTime, bool swap){
@ -197,13 +193,13 @@ float getIndexMultiplier(int now, int startTime, int endTime, bool swap){
float getHeatIndex() {
time_t now = time(nullptr);
float indexMultiplier = now >= _lightsOffStartTime ? getIndexMultiplier(now, _lightsOffStartTime, _lightsOffEndTime, true) : getIndexMultiplier(now, _lightsOnStartTime, _lightsOnEndTime, false);
return heatIndexMax / indexMultiplier;
return HEAT_INDEX_MAX / indexMultiplier;
}
void doLighting() {
EVERY_N_SECONDS(1) {
CRGB colour = ColorFromPalette(HeatColors_p, getHeatIndex());
fill_solid(leds, LED_COUNT, colour);
fill_solid(_leds, LED_COUNT, colour);
FastLED.show();
}
}
@ -223,7 +219,7 @@ void syncTime(){
void loop() {
reconnect();
client.loop();
_mqttClient.loop();
syncTime();
@ -241,6 +237,4 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
Serial.print((char)payload[i]);
}
Serial.println();
lightsOn = payload[0] == '1';
}