Return Arduino String object because of a wierd bug where the char* wasn't passing properly

This commit is contained in:
Robert Marshall 2019-06-15 13:52:14 +01:00
parent 2a0b540a4a
commit 520370fb59
3 changed files with 85 additions and 85 deletions

View file

@ -1,53 +1,53 @@
#include "Sensors.h" #include "Sensors.h"
Sensors::Sensors(int temperaturePin, 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;
} }
void Sensors::setup() { void Sensors::setup() {
pinMode(_temperaturePin, INPUT_PULLUP); pinMode(_temperaturePin, INPUT_PULLUP);
_ds18b20->begin(); _ds18b20->begin();
_ads.setGain(GAIN_TWOTHIRDS); _ads.setGain(GAIN_TWOTHIRDS);
_ads.begin(); _ads.begin();
} }
float Sensors::readpH() { float Sensors::readpH() {
int sum = 0; int sum = 0;
const int readCount = 10; const int readCount = 10;
for (int i = 0; i < readCount; i++) { for (int i = 0; i < readCount; i++) {
sum += _ads.readADC_SingleEnded(0); sum += _ads.readADC_SingleEnded(0);
delay(10); delay(10);
} }
float averageRead = float(sum) / readCount; float averageRead = float(sum) / readCount;
float voltage = 6.144 / 32768.0 * averageRead; float voltage = 6.144 / 32768.0 * averageRead;
voltage -= VOLTAGE_OFFSET; voltage -= VOLTAGE_OFFSET;
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep); float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
return pH; return pH;
} }
const char* Sensors::getTemperature(){ const String Sensors::getTemperature(){
_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))
{ {
if (attemptStart - millis() > 1000) if (attemptStart - millis() > 1000)
return "Pending..."; return "Pending...";
temperature = _ds18b20->getTempCByIndex(0); temperature = _ds18b20->getTempCByIndex(0);
} }
String temperatureString(temperature, 2);
return temperatureString.c_str(); return String(temperature, 2);
} }
const char* Sensors::getpH() { const char* Sensors::getpH() {
float pH = readpH(); float pH = readpH();
String pHString(pH, 2); String pHString(pH, 2);
return pHString.c_str(); return pHString.c_str();
} }

View file

@ -1,28 +1,28 @@
#ifndef Sensors_h #ifndef Sensors_h
#define Sensors_h #define Sensors_h
#include <Wire.h> #include <Wire.h>
#include <Adafruit_ADS1015.h> #include <Adafruit_ADS1015.h>
#include <OneWire.h> #include <OneWire.h>
#include <DallasTemperature.h> #include <DallasTemperature.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include "Networking.h" #include "Networking.h"
#define PH_7_VOLTAGE 2.5 #define PH_7_VOLTAGE 2.5
#define PH_4_VOLTAGE 3.04 #define PH_4_VOLTAGE 3.04
#define VOLTAGE_OFFSET 0.03 #define VOLTAGE_OFFSET 0.03
class Sensors { class Sensors {
int _temperaturePin; int _temperaturePin;
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, Networking *networking, DallasTemperature* ds18b20); Sensors(int temperaturePin, Networking *networking, DallasTemperature* ds18b20);
void setup(); void setup();
float readpH(); float readpH();
const char *getpH(); const char* getpH();
const char *getTemperature(); const String getTemperature();
}; };
#endif #endif

View file

@ -23,15 +23,15 @@
#define R_PIN D8 #define R_PIN D8
#define G_PIN D7 #define G_PIN D7
#define B_PIN D6 #define B_PIN D6
#define SCL_PIN D1 #define SCL_PIN D4
#define SDA_PIN D4 #define SDA_PIN D1
/*---------------------------+ /*---------------------------+
| Location and time settings | | Location and time settings |
+---------------------------*/ +---------------------------*/
#define LATITUDE "20.548103" #define LATITUDE "20.548103"
#define LONGITUDE "96.916835" #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" #define NTP_POOL "uk.pool.ntp.org"
/*============================================================================================================================*/ /*============================================================================================================================*/
@ -75,7 +75,7 @@ void setup() {
#endif #endif
#ifdef LED_USE_PWM #ifdef LED_USE_PWM
_lighting.setupPWM<R_PIN, R_PIN, G_PIN, WHITE_PIN>(); _lighting.setupPWM<R_PIN, G_PIN, B_PIN, WHITE_PIN>();
#endif #endif
_networking.setup(); _networking.setup();
@ -113,9 +113,9 @@ void doLighting() {
} }
void publishTemperature(){ void publishTemperature(){
const char *temperature = _sensors.getTemperature(); String temperature = _sensors.getTemperature();
if (temperature != "Pending...") if (temperature != "Pending...")
_networking.publish(TEMPERATURE_TOPIC, temperature, true); _networking.publish(TEMPERATURE_TOPIC, temperature.c_str(), true);
} }
void publishReadings(){ void publishReadings(){