diff --git a/monitor.ino b/monitor.ino index 3652dc7..54fc565 100644 --- a/monitor.ino +++ b/monitor.ino @@ -27,8 +27,17 @@ #define NTP_POOL "uk.pool.ntp.org" +/*----------------------------------------------------------+ +| Need this here until I figure out why I can't instantiate | +| within the Sensors class... | ++----------------------------------------------------------*/ +#include +#include +OneWire oneWire(TEMPERATURE_PIN); +DallasTemperature sensors(&oneWire); + Networking _networking = Networking(HOSTNAME, SSID, PASSWORD ,MQTT_SERVER); -Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, &_networking); +Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, &_networking, &sensors); NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET); Weather _weather = Weather(LATITUDE, LONGITUDE); Lighting _lighting = Lighting(LED_COUNT, &_naturalLight, &_weather, CLOUD_COVER_LIMIT); @@ -72,8 +81,8 @@ void doLighting() { } void publishReadings(){ - EVERY_N_SECONDS(60) { - _sensors.publishpH(); + EVERY_N_SECONDS(2) { + //_sensors.publishpH(); _sensors.publishTemperature(); } } @@ -88,6 +97,6 @@ void loop() { _networking.loop(); syncTime(); doLighting(); - //publishReadings(); + publishReadings(); delay(10); } \ No newline at end of file diff --git a/sensors.cpp b/sensors.cpp index 703fd3b..3e7c3ab 100644 --- a/sensors.cpp +++ b/sensors.cpp @@ -1,9 +1,11 @@ #include "sensors.h" +#include -Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking) { +Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking, DallasTemperature* ds18b20) { _networking = networking; - OneWire oneWire = OneWire(temperaturePin); - _ds18b20 = DallasTemperature(&oneWire); + //OneWire oneWire = OneWire(temperaturePin); + //_ds18b20 = DallasTemperature(&oneWire); + _ds18b20 = ds18b20; _temperaturePin = temperaturePin; _temperatureTopic = temperatureTopic; _phTopic = phTopic; @@ -11,7 +13,7 @@ Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Netw void Sensors::setup() { pinMode(_temperaturePin, INPUT_PULLUP); - _ds18b20.begin(); + _ds18b20->begin(); _ads.setGain(GAIN_TWOTHIRDS); _ads.begin(); } @@ -41,8 +43,15 @@ void Sensors::publishpH() { } void Sensors::publishTemperature() { - _ds18b20.requestTemperatures(); - float temperature = _ds18b20.getTempCByIndex(0); + _ds18b20->requestTemperatures(); + float temperature = _ds18b20->getTempCByIndex(0); + int attemptStart = millis(); + while ((temperature == 85 || temperature == -127)){ + Serial.println(temperature); + if (attemptStart - millis() > 1000) + return; + temperature = _ds18b20->getTempCByIndex(0); + } String temperatureString(temperature, 2); _networking->publish(_temperatureTopic, temperatureString.c_str(), true); } \ No newline at end of file diff --git a/sensors.h b/sensors.h index 38f774e..930c60a 100644 --- a/sensors.h +++ b/sensors.h @@ -17,11 +17,11 @@ class Sensors { char *_phTopic; float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3; Adafruit_ADS1115 _ads; - DallasTemperature _ds18b20; + DallasTemperature* _ds18b20; Networking* _networking; public: - Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking); + Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking *networking, DallasTemperature* ds18b20); void setup(); float readpH(); void publishpH();