From 520370fb5921fa3cfabab7dcaaf4a7d393db90d8 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sat, 15 Jun 2019 13:52:14 +0100 Subject: [PATCH] Return Arduino String object because of a wierd bug where the char* wasn't passing properly --- Sensors.cpp | 104 ++++++++++++++++++++++++++-------------------------- Sensors.h | 54 +++++++++++++-------------- monitor.ino | 12 +++--- 3 files changed, 85 insertions(+), 85 deletions(-) diff --git a/Sensors.cpp b/Sensors.cpp index 39e8608..12e3e1c 100644 --- a/Sensors.cpp +++ b/Sensors.cpp @@ -1,53 +1,53 @@ -#include "Sensors.h" - -Sensors::Sensors(int temperaturePin, Networking* networking, DallasTemperature* ds18b20) { - _networking = networking; - //OneWire oneWire = OneWire(temperaturePin); - //_ds18b20 = DallasTemperature(&oneWire); - _ds18b20 = ds18b20; - _temperaturePin = temperaturePin; -} - -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; - float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep); - return pH; -} - -const char* Sensors::getTemperature(){ - _ds18b20->requestTemperatures(); - float temperature = _ds18b20->getTempCByIndex(0); - int attemptStart = millis(); - while ((temperature == 85 || temperature == -127)) - { - if (attemptStart - millis() > 1000) - return "Pending..."; - temperature = _ds18b20->getTempCByIndex(0); - } - String temperatureString(temperature, 2); - return temperatureString.c_str(); -} - -const char* Sensors::getpH() { - float pH = readpH(); - String pHString(pH, 2); - return pHString.c_str(); +#include "Sensors.h" + +Sensors::Sensors(int temperaturePin, Networking* networking, DallasTemperature* ds18b20) { + _networking = networking; + //OneWire oneWire = OneWire(temperaturePin); + //_ds18b20 = DallasTemperature(&oneWire); + _ds18b20 = ds18b20; + _temperaturePin = temperaturePin; +} + +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; + float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep); + return pH; +} + +const String Sensors::getTemperature(){ + _ds18b20->requestTemperatures(); + float temperature = _ds18b20->getTempCByIndex(0); + int attemptStart = millis(); + while ((temperature == 85 || temperature == -127)) + { + if (attemptStart - millis() > 1000) + return "Pending..."; + temperature = _ds18b20->getTempCByIndex(0); + } + + return String(temperature, 2); +} + +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 dbd3143..9e8f64c 100644 --- a/Sensors.h +++ b/Sensors.h @@ -1,28 +1,28 @@ -#ifndef Sensors_h -#define Sensors_h -#include -#include -#include -#include -#include -#include "Networking.h" - -#define PH_7_VOLTAGE 2.5 -#define PH_4_VOLTAGE 3.04 -#define VOLTAGE_OFFSET 0.03 - -class Sensors { - int _temperaturePin; - float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3; - Adafruit_ADS1115 _ads; - DallasTemperature* _ds18b20; - Networking *_networking; - - public: - Sensors(int temperaturePin, Networking *networking, DallasTemperature* ds18b20); - void setup(); - float readpH(); - const char *getpH(); - const char *getTemperature(); -}; +#ifndef Sensors_h +#define Sensors_h +#include +#include +#include +#include +#include +#include "Networking.h" + +#define PH_7_VOLTAGE 2.5 +#define PH_4_VOLTAGE 3.04 +#define VOLTAGE_OFFSET 0.03 + +class Sensors { + int _temperaturePin; + float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3; + Adafruit_ADS1115 _ads; + DallasTemperature* _ds18b20; + Networking *_networking; + + public: + Sensors(int temperaturePin, Networking *networking, DallasTemperature* ds18b20); + void setup(); + float readpH(); + const char* getpH(); + const String getTemperature(); +}; #endif \ No newline at end of file diff --git a/monitor.ino b/monitor.ino index 0c39453..c904358 100644 --- a/monitor.ino +++ b/monitor.ino @@ -23,15 +23,15 @@ #define R_PIN D8 #define G_PIN D7 #define B_PIN D6 -#define SCL_PIN D1 -#define SDA_PIN D4 +#define SCL_PIN D4 +#define SDA_PIN D1 /*---------------------------+ | Location and time settings | +---------------------------*/ #define LATITUDE "20.548103" #define LONGITUDE "96.916835" -#define TIMEZONE_OFFSET 30600 // 8.5 hours in seconds +#define TIMEZONE_OFFSET 28800 // 8 hours in seconds #define NTP_POOL "uk.pool.ntp.org" /*============================================================================================================================*/ @@ -75,7 +75,7 @@ void setup() { #endif #ifdef LED_USE_PWM - _lighting.setupPWM(); + _lighting.setupPWM(); #endif _networking.setup(); @@ -113,9 +113,9 @@ void doLighting() { } void publishTemperature(){ - const char *temperature = _sensors.getTemperature(); + String temperature = _sensors.getTemperature(); if (temperature != "Pending...") - _networking.publish(TEMPERATURE_TOPIC, temperature, true); + _networking.publish(TEMPERATURE_TOPIC, temperature.c_str(), true); } void publishReadings(){