Refactor Sensors to remove publish responsibility and instead return values. Add Screen class to output to OLED screen.
This commit is contained in:
parent
12e756fef1
commit
d51b140050
5 changed files with 81 additions and 27 deletions
29
Screen.cpp
Normal file
29
Screen.cpp
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#include "Screen.h"
|
||||||
|
|
||||||
|
Screen::Screen(Sensors *sensors, int sdaPin, int sclPin){
|
||||||
|
_sensors = sensors;
|
||||||
|
_sdaPin = sdaPin;
|
||||||
|
_sclPin = sclPin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::setup() {
|
||||||
|
Wire.begin(_sdaPin, _sclPin);
|
||||||
|
_screen.begin(SSD1306_SWITCHCAPVCC, 0x3C);
|
||||||
|
_screen.clearDisplay();
|
||||||
|
_screen.setTextWrap(false);
|
||||||
|
_screen.setTextColor(WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::writeTemperature(){
|
||||||
|
_screen.setTextSize(1);
|
||||||
|
_screen.setCursor(0, 0);
|
||||||
|
_screen.println("Temperature:");
|
||||||
|
_screen.setTextSize(2);
|
||||||
|
_screen.setCursor(0, 10);
|
||||||
|
_screen.println(_sensors->getTemperature());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::update(){
|
||||||
|
writeTemperature();
|
||||||
|
_screen.display();
|
||||||
|
}
|
21
Screen.h
Normal file
21
Screen.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef Screen_h
|
||||||
|
#define Screen_h
|
||||||
|
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_SSD1306.h>
|
||||||
|
#include "Sensors.h"
|
||||||
|
|
||||||
|
class Screen {
|
||||||
|
Adafruit_SSD1306 _screen=Adafruit_SSD1306(128, 64, &Wire, -1);
|
||||||
|
Sensors *_sensors;
|
||||||
|
int _sdaPin, _sclPin;
|
||||||
|
|
||||||
|
void writeTemperature();
|
||||||
|
|
||||||
|
public:
|
||||||
|
Screen(Sensors *sensors, int sdaPin, int sclPin);
|
||||||
|
void setup();
|
||||||
|
void update();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
18
monitor.ino
18
monitor.ino
|
@ -39,9 +39,11 @@
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "Networking.h"
|
#include "Networking.h"
|
||||||
#include "sensors.h"
|
#include "Sensors.h"
|
||||||
#include "NaturalLight.h"
|
#include "NaturalLight.h"
|
||||||
|
#include "Lighting.h"
|
||||||
#include "Weather.h"
|
#include "Weather.h"
|
||||||
|
#include "Screen.h"
|
||||||
|
|
||||||
#define BASE_TOPIC "/home/sensors/" HOSTNAME
|
#define BASE_TOPIC "/home/sensors/" HOSTNAME
|
||||||
#define PH_TOPIC BASE_TOPIC "/ph"
|
#define PH_TOPIC BASE_TOPIC "/ph"
|
||||||
|
@ -59,10 +61,11 @@ OneWire oneWire(TEMPERATURE_PIN);
|
||||||
DallasTemperature temperatureSensor(&oneWire);
|
DallasTemperature temperatureSensor(&oneWire);
|
||||||
|
|
||||||
Networking _networking = Networking(HOSTNAME, NETWORK_NAME, PASSWORD ,MQTT_SERVER);
|
Networking _networking = Networking(HOSTNAME, NETWORK_NAME, PASSWORD ,MQTT_SERVER);
|
||||||
Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, &_networking, &temperatureSensor);
|
Sensors _sensors = Sensors(TEMPERATURE_PIN, &_networking, &temperatureSensor);
|
||||||
NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET);
|
NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET);
|
||||||
Weather _weather = Weather(LATITUDE, LONGITUDE);
|
Weather _weather = Weather(LATITUDE, LONGITUDE);
|
||||||
Lighting _lighting = Lighting(&_naturalLight, &_weather, CLOUD_COVER_LIMIT);
|
Lighting _lighting = Lighting(&_naturalLight, &_weather, CLOUD_COVER_LIMIT);
|
||||||
|
Screen _display = Screen(&_sensors, SDA_PIN, SCL_PIN);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
@ -77,6 +80,7 @@ void setup() {
|
||||||
|
|
||||||
_networking.setup();
|
_networking.setup();
|
||||||
_sensors.setup();
|
_sensors.setup();
|
||||||
|
_display.setup();
|
||||||
|
|
||||||
delay(2000);
|
delay(2000);
|
||||||
configTime(0, 0, NTP_POOL);
|
configTime(0, 0, NTP_POOL);
|
||||||
|
@ -108,10 +112,16 @@ void doLighting() {
|
||||||
_lighting.update(time(nullptr));
|
_lighting.update(time(nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void publishTemperature(){
|
||||||
|
const char *temperature = _sensors.getTemperature();
|
||||||
|
if (temperature != "Pending...")
|
||||||
|
_networking.publish(TEMPERATURE_TOPIC, temperature, true);
|
||||||
|
}
|
||||||
|
|
||||||
void publishReadings(){
|
void publishReadings(){
|
||||||
EVERY_N_SECONDS(2) {
|
EVERY_N_SECONDS(2) {
|
||||||
//_sensors.publishpH();
|
publishTemperature();
|
||||||
_sensors.publishTemperature();
|
_display.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
sensors.cpp
30
sensors.cpp
|
@ -1,14 +1,11 @@
|
||||||
#include "sensors.h"
|
#include "Sensors.h"
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking* networking, DallasTemperature* ds18b20) {
|
Sensors::Sensors(int temperaturePin, Networking* networking, DallasTemperature* ds18b20) {
|
||||||
_networking = networking;
|
_networking = networking;
|
||||||
//OneWire oneWire = OneWire(temperaturePin);
|
//OneWire oneWire = OneWire(temperaturePin);
|
||||||
//_ds18b20 = DallasTemperature(&oneWire);
|
//_ds18b20 = DallasTemperature(&oneWire);
|
||||||
_ds18b20 = ds18b20;
|
_ds18b20 = ds18b20;
|
||||||
_temperaturePin = temperaturePin;
|
_temperaturePin = temperaturePin;
|
||||||
_temperatureTopic = temperatureTopic;
|
|
||||||
_phTopic = phTopic;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sensors::setup() {
|
void Sensors::setup() {
|
||||||
|
@ -31,27 +28,26 @@ float Sensors::readpH() {
|
||||||
|
|
||||||
float voltage = 6.144 / 32768.0 * averageRead;
|
float voltage = 6.144 / 32768.0 * averageRead;
|
||||||
voltage -= VOLTAGE_OFFSET;
|
voltage -= VOLTAGE_OFFSET;
|
||||||
Serial.println(voltage);
|
|
||||||
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
|
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
|
||||||
return pH;
|
return pH;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sensors::publishpH() {
|
const char* Sensors::getTemperature(){
|
||||||
float pH = readpH();
|
|
||||||
String pHString(pH, 2);
|
|
||||||
_networking->publish(_phTopic, pHString.c_str(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Sensors::publishTemperature() {
|
|
||||||
_ds18b20->requestTemperatures();
|
_ds18b20->requestTemperatures();
|
||||||
float temperature = _ds18b20->getTempCByIndex(0);
|
float temperature = _ds18b20->getTempCByIndex(0);
|
||||||
int attemptStart = millis();
|
int attemptStart = millis();
|
||||||
while ((temperature == 85 || temperature == -127)){
|
while ((temperature == 85 || temperature == -127))
|
||||||
Serial.println(temperature);
|
{
|
||||||
if (attemptStart - millis() > 1000)
|
if (attemptStart - millis() > 1000)
|
||||||
return;
|
return "Pending...";
|
||||||
temperature = _ds18b20->getTempCByIndex(0);
|
temperature = _ds18b20->getTempCByIndex(0);
|
||||||
}
|
}
|
||||||
String temperatureString(temperature, 2);
|
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();
|
||||||
}
|
}
|
|
@ -13,18 +13,16 @@
|
||||||
|
|
||||||
class Sensors {
|
class Sensors {
|
||||||
int _temperaturePin;
|
int _temperaturePin;
|
||||||
char *_temperatureTopic;
|
|
||||||
char *_phTopic;
|
|
||||||
float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
|
float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
|
||||||
Adafruit_ADS1115 _ads;
|
Adafruit_ADS1115 _ads;
|
||||||
DallasTemperature* _ds18b20;
|
DallasTemperature* _ds18b20;
|
||||||
Networking *_networking;
|
Networking *_networking;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking *networking, DallasTemperature* ds18b20);
|
Sensors(int temperaturePin, Networking *networking, DallasTemperature* ds18b20);
|
||||||
void setup();
|
void setup();
|
||||||
float readpH();
|
float readpH();
|
||||||
void publishpH();
|
const char *getpH();
|
||||||
void publishTemperature();
|
const char *getTemperature();
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue