diff --git a/Screen.cpp b/Screen.cpp new file mode 100644 index 0000000..a8bad70 --- /dev/null +++ b/Screen.cpp @@ -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(); +} \ No newline at end of file diff --git a/Screen.h b/Screen.h new file mode 100644 index 0000000..b87615b --- /dev/null +++ b/Screen.h @@ -0,0 +1,21 @@ +#ifndef Screen_h +#define Screen_h + +#include +#include +#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 \ No newline at end of file diff --git a/monitor.ino b/monitor.ino index ee08e35..0c39453 100644 --- a/monitor.ino +++ b/monitor.ino @@ -39,9 +39,11 @@ #include #include #include "Networking.h" -#include "sensors.h" +#include "Sensors.h" #include "NaturalLight.h" +#include "Lighting.h" #include "Weather.h" +#include "Screen.h" #define BASE_TOPIC "/home/sensors/" HOSTNAME #define PH_TOPIC BASE_TOPIC "/ph" @@ -59,10 +61,11 @@ OneWire oneWire(TEMPERATURE_PIN); DallasTemperature temperatureSensor(&oneWire); 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); Weather _weather = Weather(LATITUDE, LONGITUDE); Lighting _lighting = Lighting(&_naturalLight, &_weather, CLOUD_COVER_LIMIT); +Screen _display = Screen(&_sensors, SDA_PIN, SCL_PIN); void setup() { Serial.begin(115200); @@ -77,6 +80,7 @@ void setup() { _networking.setup(); _sensors.setup(); + _display.setup(); delay(2000); configTime(0, 0, NTP_POOL); @@ -108,10 +112,16 @@ void doLighting() { _lighting.update(time(nullptr)); } +void publishTemperature(){ + const char *temperature = _sensors.getTemperature(); + if (temperature != "Pending...") + _networking.publish(TEMPERATURE_TOPIC, temperature, true); +} + void publishReadings(){ EVERY_N_SECONDS(2) { - //_sensors.publishpH(); - _sensors.publishTemperature(); + publishTemperature(); + _display.update(); } } diff --git a/sensors.cpp b/sensors.cpp index 3e7c3ab..39e8608 100644 --- a/sensors.cpp +++ b/sensors.cpp @@ -1,14 +1,11 @@ -#include "sensors.h" -#include +#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(); } \ No newline at end of file diff --git a/sensors.h b/sensors.h index 930c60a..dbd3143 100644 --- a/sensors.h +++ b/sensors.h @@ -13,18 +13,16 @@ class Sensors { int _temperaturePin; - char *_temperatureTopic; - char *_phTopic; float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3; Adafruit_ADS1115 _ads; DallasTemperature* _ds18b20; - Networking* _networking; + Networking *_networking; public: - Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, Networking *networking, DallasTemperature* ds18b20); + Sensors(int temperaturePin, Networking *networking, DallasTemperature* ds18b20); void setup(); float readpH(); - void publishpH(); - void publishTemperature(); + const char *getpH(); + const char *getTemperature(); }; #endif \ No newline at end of file