Instantiate temperature sensor in entry file because it doesn't want to work in the class.

This commit is contained in:
Robert Marshall 2019-02-02 08:36:07 +00:00
parent ccbbf6e4c1
commit 059ebad84e
3 changed files with 30 additions and 12 deletions

View file

@ -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 <OneWire.h>
#include <DallasTemperature.h>
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);
}

View file

@ -1,9 +1,11 @@
#include "sensors.h"
#include <Arduino.h>
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);
}

View file

@ -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();