Move sensors in to own code file
This commit is contained in:
parent
9c01dc2435
commit
9ebb45cdbe
4 changed files with 84 additions and 59 deletions
5
.vscode/c_cpp_properties.json
vendored
5
.vscode/c_cpp_properties.json
vendored
|
@ -3,11 +3,12 @@
|
||||||
{
|
{
|
||||||
"name": "Linux",
|
"name": "Linux",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"/home/rob/Arduino/libraries/Adafruit_ADS1X15",
|
"/home/rob/Arduino/libraries/",
|
||||||
"/home/rob/.arduino15/packages/esp8266/tools/**",
|
"/home/rob/.arduino15/packages/esp8266/tools/**",
|
||||||
"/home/rob/.arduino15/packages/esp8266/hardware/esp8266/2.4.0/**",
|
"/home/rob/.arduino15/packages/esp8266/hardware/esp8266/2.4.0/**",
|
||||||
"/home/rob/Apps/arduino-1.8.5/tools/**",
|
"/home/rob/Apps/arduino-1.8.5/tools/**",
|
||||||
"/home/rob/Apps/arduino-1.8.5/hardware/arduino/avr/**"
|
"/home/rob/Apps/arduino-1.8.5/hardware/arduino/avr/**",
|
||||||
|
"/home/rob/.arduino15/staging/libraries"
|
||||||
],
|
],
|
||||||
"intelliSenseMode": "clang-x64",
|
"intelliSenseMode": "clang-x64",
|
||||||
"compilerPath": "/usr/bin/gcc",
|
"compilerPath": "/usr/bin/gcc",
|
||||||
|
|
63
monitor.ino
63
monitor.ino
|
@ -1,14 +1,12 @@
|
||||||
#include <Wire.h>
|
|
||||||
#include <Adafruit_ADS1015.h>
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <OneWire.h>
|
|
||||||
#include <DallasTemperature.h>
|
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
#include <ESP8266HTTPClient.h>
|
#include <ESP8266HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "sensors.h"
|
||||||
|
|
||||||
#define HOSTNAME "Fishtank"
|
#define HOSTNAME "Fishtank"
|
||||||
#define MQTT_SERVER "192.168.1.3"
|
#define MQTT_SERVER "192.168.1.3"
|
||||||
#define SSID "GCHQ Surveillance Van"
|
#define SSID "GCHQ Surveillance Van"
|
||||||
|
@ -19,11 +17,6 @@
|
||||||
#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature"
|
#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature"
|
||||||
#define TEMPERATURE_PIN D5
|
#define TEMPERATURE_PIN D5
|
||||||
|
|
||||||
#define PH_7_VOLTAGE 2.5
|
|
||||||
#define PH_4_VOLTAGE 3.04
|
|
||||||
|
|
||||||
#define VOLTAGE_OFFSET 0.03
|
|
||||||
|
|
||||||
#define SOLAR_EVENT_URL "http://api.sunrise-sunset.org/json?lat=20.548103&lng=96.916835&formatted=0"
|
#define SOLAR_EVENT_URL "http://api.sunrise-sunset.org/json?lat=20.548103&lng=96.916835&formatted=0"
|
||||||
#define TIMEZONE_OFFSET 27000 // 7.5 hours in seconds
|
#define TIMEZONE_OFFSET 27000 // 7.5 hours in seconds
|
||||||
#define NTP_POOL "uk.pool.ntp.org"
|
#define NTP_POOL "uk.pool.ntp.org"
|
||||||
|
@ -34,14 +27,11 @@ void mqttCallback(char *topic, byte *payload, unsigned int length);
|
||||||
WiFiClient _wifiClient;
|
WiFiClient _wifiClient;
|
||||||
PubSubClient _mqttClient = PubSubClient(MQTT_SERVER, 1883, mqttCallback, _wifiClient);
|
PubSubClient _mqttClient = PubSubClient(MQTT_SERVER, 1883, mqttCallback, _wifiClient);
|
||||||
|
|
||||||
Adafruit_ADS1115 _ads;
|
Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, _mqttClient);
|
||||||
OneWire _oneWire(TEMPERATURE_PIN);
|
|
||||||
DallasTemperature _ds18b20(&_oneWire);
|
|
||||||
|
|
||||||
CRGB _leds[LED_COUNT];
|
CRGB _leds[LED_COUNT];
|
||||||
int _lightsOnStartTime, _lightsOnEndTime, _lightsOffStartTime, _lightsOffEndTime;
|
int _lightsOnStartTime, _lightsOnEndTime, _lightsOffStartTime, _lightsOffEndTime;
|
||||||
|
|
||||||
float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
|
|
||||||
|
|
||||||
void waitForWiFi() {
|
void waitForWiFi() {
|
||||||
if (WiFi.status() == WL_CONNECTED)
|
if (WiFi.status() == WL_CONNECTED)
|
||||||
|
@ -123,11 +113,7 @@ void parseEvents(){
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
_ads.setGain(GAIN_TWOTHIRDS);
|
_sensors.setup();
|
||||||
_ads.begin();
|
|
||||||
|
|
||||||
pinMode(TEMPERATURE_PIN, INPUT_PULLUP);
|
|
||||||
_ds18b20.begin();
|
|
||||||
|
|
||||||
WiFi.hostname(HOSTNAME);
|
WiFi.hostname(HOSTNAME);
|
||||||
WiFi.begin(SSID, PASSWORD);
|
WiFi.begin(SSID, PASSWORD);
|
||||||
|
@ -141,43 +127,6 @@ void setup() {
|
||||||
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(_leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
|
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(_leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
|
||||||
}
|
}
|
||||||
|
|
||||||
float readpH() {
|
|
||||||
Serial.println("Reading pH...");
|
|
||||||
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;
|
|
||||||
Serial.print("Average value: ");
|
|
||||||
Serial.println(averageRead);
|
|
||||||
|
|
||||||
float voltage = 6.144 / 32768.0 * averageRead;
|
|
||||||
voltage -= VOLTAGE_OFFSET;
|
|
||||||
Serial.println(voltage);
|
|
||||||
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
|
|
||||||
return pH;
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishpH() {
|
|
||||||
float pH = readpH();
|
|
||||||
String pHString(pH, 2);
|
|
||||||
Serial.println(pHString);
|
|
||||||
_mqttClient.publish(PH_TOPIC, pHString.c_str(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishTemperature() {
|
|
||||||
_ds18b20.requestTemperatures();
|
|
||||||
float temperature = _ds18b20.getTempCByIndex(0);
|
|
||||||
String temperatureString(temperature, 2);
|
|
||||||
Serial.println(temperatureString);
|
|
||||||
_mqttClient.publish(TEMPERATURE_TOPIC, temperatureString.c_str(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
float getIndexMultiplier(int now, int startTime, int endTime, bool swap){
|
float getIndexMultiplier(int now, int startTime, int endTime, bool swap){
|
||||||
float indexMultiplier = 1;
|
float indexMultiplier = 1;
|
||||||
float a = swap ? endTime - now : now - startTime;
|
float a = swap ? endTime - now : now - startTime;
|
||||||
|
@ -205,8 +154,8 @@ void doLighting() {
|
||||||
|
|
||||||
void publishReadings(){
|
void publishReadings(){
|
||||||
EVERY_N_SECONDS(60) {
|
EVERY_N_SECONDS(60) {
|
||||||
publishpH();
|
_sensors.publishpH();
|
||||||
publishTemperature();
|
_sensors.publishTemperature();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
sensors.cpp
Normal file
48
sensors.cpp
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#include "sensors.h"
|
||||||
|
|
||||||
|
Sensors::Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, PubSubClient mqttClient) {
|
||||||
|
_mqttClient = mqttClient;
|
||||||
|
OneWire oneWire = OneWire(temperaturePin);
|
||||||
|
_ds18b20 = DallasTemperature(&oneWire);
|
||||||
|
_temperaturePin = temperaturePin;
|
||||||
|
_temperatureTopic = temperatureTopic;
|
||||||
|
_phTopic = phTopic;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
Serial.println(voltage);
|
||||||
|
float pH = 7 - ((PH_7_VOLTAGE - voltage) / _pHStep);
|
||||||
|
return pH;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensors::publishpH() {
|
||||||
|
float pH = readpH();
|
||||||
|
String pHString(pH, 2);
|
||||||
|
_mqttClient.publish(_phTopic, pHString.c_str(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensors::publishTemperature() {
|
||||||
|
_ds18b20.requestTemperatures();
|
||||||
|
float temperature = _ds18b20.getTempCByIndex(0);
|
||||||
|
String temperatureString(temperature, 2);
|
||||||
|
_mqttClient.publish(_temperatureTopic, temperatureString.c_str(), true);
|
||||||
|
}
|
27
sensors.h
Normal file
27
sensors.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_ADS1015.h>
|
||||||
|
#include <OneWire.h>
|
||||||
|
#include <DallasTemperature.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
#define PH_7_VOLTAGE 2.5
|
||||||
|
#define PH_4_VOLTAGE 3.04
|
||||||
|
#define VOLTAGE_OFFSET 0.03
|
||||||
|
|
||||||
|
class Sensors {
|
||||||
|
int _temperaturePin;
|
||||||
|
char *_temperatureTopic;
|
||||||
|
char *_phTopic;
|
||||||
|
float _pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
|
||||||
|
Adafruit_ADS1115 _ads;
|
||||||
|
//OneWire _oneWire;
|
||||||
|
DallasTemperature _ds18b20;
|
||||||
|
PubSubClient _mqttClient;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Sensors(int temperaturePin, char *temperatureTopic, char *phTopic, PubSubClient mqttClient);
|
||||||
|
void setup();
|
||||||
|
float readpH();
|
||||||
|
void publishpH();
|
||||||
|
void publishTemperature();
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue