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

View file

@ -1,28 +1,28 @@
#ifndef Sensors_h
#define Sensors_h
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>
#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 <Wire.h>
#include <Adafruit_ADS1015.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>
#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

View file

@ -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<R_PIN, R_PIN, G_PIN, WHITE_PIN>();
_lighting.setupPWM<R_PIN, G_PIN, B_PIN, WHITE_PIN>();
#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(){