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

29
Screen.cpp Normal file
View 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
View 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

View file

@ -39,9 +39,11 @@
#include <FastLED.h>
#include <time.h>
#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();
}
}

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();
}

View file

@ -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