Instantiate temperature sensor in entry file because it doesn't want to work in the class.
This commit is contained in:
parent
ccbbf6e4c1
commit
059ebad84e
3 changed files with 30 additions and 12 deletions
21
sensors.cpp
21
sensors.cpp
|
@ -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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue