Refactor Sensors to remove publish responsibility and instead return values. Add Screen class to output to OLED screen.

This commit is contained in:
Robert Marshall 2019-03-03 21:37:31 +00:00
parent 12e756fef1
commit d51b140050
5 changed files with 81 additions and 27 deletions

View file

@ -1,14 +1,11 @@
#include "sensors.h"
#include <Arduino.h>
#include "Sensors.h"
Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking, DallasTemperature* ds18b20) {
Sensors::Sensors(int temperaturePin, Networking* networking, DallasTemperature* ds18b20) {
_networking = networking;
//OneWire oneWire = OneWire(temperaturePin);
//_ds18b20 = DallasTemperature(&oneWire);
_ds18b20 = ds18b20;
_temperaturePin = temperaturePin;
_temperatureTopic = temperatureTopic;
_phTopic = phTopic;
}
void Sensors::setup() {
@ -31,27 +28,26 @@ float Sensors::readpH() {
float voltage = 6.144 / 32768.0 * averageRead;
voltage -= VOLTAGE_OFFSET;
Serial.println(voltage);
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
return pH;
}
void Sensors::publishpH() {
float pH = readpH();
String pHString(pH, 2);
_networking->publish(_phTopic, pHString.c_str(), true);
}
void Sensors::publishTemperature() {
const char* Sensors::getTemperature(){
_ds18b20->requestTemperatures();
float temperature = _ds18b20->getTempCByIndex(0);
int attemptStart = millis();
while ((temperature == 85 || temperature == -127)){
Serial.println(temperature);
while ((temperature == 85 || temperature == -127))
{
if (attemptStart - millis() > 1000)
return;
return "Pending...";
temperature = _ds18b20->getTempCByIndex(0);
}
String temperatureString(temperature, 2);
_networking->publish(_temperatureTopic, temperatureString.c_str(), true);
return temperatureString.c_str();
}
const char* Sensors::getpH() {
float pH = readpH();
String pHString(pH, 2);
return pHString.c_str();
}