Move sensors in to own code file
This commit is contained in:
parent
9c01dc2435
commit
9ebb45cdbe
4 changed files with 84 additions and 59 deletions
48
sensors.cpp
Normal file
48
sensors.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "sensors.h"
|
||||
|
||||
Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, PubSubClient mqttClient) {
|
||||
_mqttClient = mqttClient;
|
||||
OneWire oneWire = OneWire(temperaturePin);
|
||||
_ds18b20 = DallasTemperature(&oneWire);
|
||||
_temperaturePin = temperaturePin;
|
||||
_temperatureTopic = temperatureTopic;
|
||||
_phTopic = phTopic;
|
||||
}
|
||||
|
||||
void Sensors::setup() {
|
||||
pinMode(_temperaturePin, INPUT_PULLUP);
|
||||
_ds18b20.begin();
|
||||
_ads.setGain(GAIN_TWOTHIRDS);
|
||||
_ads.begin();
|
||||
}
|
||||
|
||||
float Sensors::readpH() {
|
||||
int sum = 0;
|
||||
const int readCount = 10;
|
||||
|
||||
for (int i = 0; i < readCount; i++) {
|
||||
sum += _ads.readADC_SingleEnded(0);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
float averageRead = float(sum) / readCount;
|
||||
|
||||
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);
|
||||
_mqttClient.publish(_phTopic, pHString.c_str(), true);
|
||||
}
|
||||
|
||||
void Sensors::publishTemperature() {
|
||||
_ds18b20.requestTemperatures();
|
||||
float temperature = _ds18b20.getTempCByIndex(0);
|
||||
String temperatureString(temperature, 2);
|
||||
_mqttClient.publish(_temperatureTopic, temperatureString.c_str(), true);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue