Skip to content

Commit

Permalink
Moved to my DS18B20 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 15, 2020
1 parent b8367d1 commit 14de5fd
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 63 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extra_scripts = ; pre:python tools\name_firmware.py
lib_deps =
ArduinoJson
https://github.com/lbussy/OneWire.git
; 5544 ; DS18B20
https://github.com/lbussy/DS18B20.git
DallasTemperature
ArduinoLog
DoubleResetDetect
Expand Down
90 changes: 37 additions & 53 deletions src/bubbles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,41 @@ Bubbles* Bubbles::single = NULL; // Holds pointer to class
Bubbles* Bubbles::getInstance() {
if (!single) {
single = new Bubbles();
single->start();
pinMode(COUNTPIN, INPUT); // Change pinmode to input
attachInterrupt(digitalPinToInterrupt(COUNTPIN), HandleInterruptsStatic, RISING); // FALLING, RISING or CHANGE
pBubbles = single; // Assign current instance to pointer
single->ulLastReport = millis();// Store the last report timer
single->ulMicroLast = millis(); // Starting point for debouce timer
single->pulse = 0; // Reset pulse counter
single->doBub = false;
// Set starting values
unsigned long ulNow = millis();
single->ulStart = ulNow;
single->lastBpm = 0.0;
single->lastAmb = 0.0;
single->lastVes = 0.0;

// Set starting time
single->lastTime = getDTS();
}
return single;
}

void Bubbles::start() {
pinMode(COUNTPIN, INPUT); // Change pinmode to input
attachInterrupt(digitalPinToInterrupt(COUNTPIN), HandleInterruptsStatic, RISING); // FALLING, RISING or CHANGE
pBubbles = single; // Assign current instance to pointer
single->ulLastReport = millis();// Store the last report timer
single->pulse = 0; // Reset pulse counter
single->doBub = false;

// Set starting values
unsigned long ulNow = millis();
single->ulStart = ulNow;
single->lastBpm = 0.0;
single->lastAmb = 0.0;
single->lastVes = 0.0;

// Set starting time
strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime()));
}

void Bubbles::update() { // Regular update loop, once per minute
// Handle NTP Time
strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime()));
// Get NTP Time
single->lastTime = getDTS();

// Store last values
single->lastBpm = single->getRawBpm();
single->lastAmb = single->getTemp(AMBSENSOR);
single->lastVes = single->getTemp(VESSENSOR);
single->lastAmb = getTemp(AMBSENSOR);
single->lastVes = getTemp(VESSENSOR);

// Push values to circular buffers
tempAmbAvg.push(single->lastAmb);
tempVesAvg.push(single->lastVes);
bubAvg.push(single->lastBpm);

Log.verbose(F("Time is %s, BPM is %D." CR), single->lastTime, single->lastBpm);
Log.verbose(F("Time is %s, BPM is %D." CR), single->lastTime.c_str(), single->lastBpm);
Log.verbose(F("Averages: BPM = %D (%l in sample), Ambient = %D (%l in sample), Vessel = %D (%l in sample)." CR),
single->getAvgBpm(), bubAvg.size(),
single->getAvgAmbient(), tempAmbAvg.size(),
Expand All @@ -82,10 +78,11 @@ void Bubbles::update() { // Regular update loop, once per minute
void Bubbles::handleInterrupts(void) { // Bubble Interrupt handler
digitalWrite(LED, LOW);
unsigned long now = micros();
if ((now - ulMicroLast) > RESOLUTION) { // Filter noise/bounce
if ((now - single->ulMicroLast) > RESOLUTION) { // Filter noise/bounce
single->pulse++; // Increment pulse count
single->ulMicroLast = now;
}
doBub = true;
single->doBub = true;
}

float Bubbles::getRawBpm() { // Return raw pulses per minute (resets counter)
Expand All @@ -99,32 +96,6 @@ float Bubbles::getRawBpm() { // Return raw pulses per minute (resets counter)
return ppm; // Return pulses per minute
}

float Bubbles::getTemp(uint8_t pin) {
OneWire oneWire(pin);
DS18B20 sensor(&oneWire);
sensor.begin();
float retVal = -100.00;
sensor.setResolution(12);
sensor.requestTemperatures();
while (!sensor.isConversionComplete());
// Get Temps
JsonConfig *config = JsonConfig::getInstance();
if (config->tempinf) {
retVal = sensor.getTempC();
retVal = retVal * 9/5 + 32;
} else {
retVal = sensor.getTempC();
}
// Apply calibration
if (pin == AMBSENSOR) {
retVal = retVal + config->calAmbient;
} else if (pin == VESSENSOR) {
retVal = retVal + config->calVessel;
}

return retVal;
}

float Bubbles::getAvgAmbient() {
// Retrieve TEMPAVG readings to calculate average
float avg = 0.0;
Expand Down Expand Up @@ -161,3 +132,16 @@ float Bubbles::getAvgBpm() {
void Bubbles::setLast(double last) {
bubAvg.push(last);
}

void setDoBubUpdate() {
doBubUpdate = true; // Semaphore required for Ticker + radio event
}

void bubLoop() {
Bubbles *bubble = Bubbles::getInstance();
// Do Bubble update post
if (doBubUpdate) {
doBubUpdate = false;
bubble->update();
}
}
26 changes: 17 additions & 9 deletions src/bubbles.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ SOFTWARE. */
#define _BUBBLES_H

#include "config.h"
#include "sensors.h"
#include "jsonconfig.h"
#include "ntp.h"
#include "DS18B20.h"
#include "OneWire.h"
#include <ArduinoLog.h>
#include <CircularBuffer.h>
#include <Arduino.h>
Expand All @@ -37,8 +36,11 @@ class Bubbles {
// Singleton Declarations
Bubbles() {};
static Bubbles *single;
// Other Declarations
void start();

// Private Methods
float getRawBpm();

// Private Properties
CircularBuffer<float, TEMPAVG> tempAmbAvg;
CircularBuffer<float, TEMPAVG> tempVesAvg;
CircularBuffer<float, BUBAVG> bubAvg;
Expand All @@ -49,22 +51,28 @@ class Bubbles {
float lastBpm; // Holds most recent count
float lastAmb;
float lastVes;
float getRawBpm();
float getTemp(uint8_t);

public:
// Singleton Declarations
static Bubbles* getInstance();
~Bubbles() {single = NULL;}
// Other Declarations

// Public Methods
void handleInterrupts(void);
void update(); // Call every 60 seconds
char lastTime[22];
float getAvgAmbient();
float getAvgVessel();
float getAvgBpm();
void setLast(double); // Push last reading on reboot

// Public Properties
String lastTime;
bool doBub;
void setLast(double); // Push last reading on reboot
};

void setDoBubUpdate();
void bubLoop();

static bool __attribute__((unused)) doBubUpdate = false; // Semaphore for Bubble timer

#endif // _BUBBLES_H
57 changes: 57 additions & 0 deletions src/sensors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)

This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#include "sensors.h"

double getTemp(uint8_t pin) {
float retVal;
JsonConfig *config = JsonConfig::getInstance();
OneWire oneWire(pin);
DS18B20 sensor(&oneWire);
sensor.begin();
sensor.setResolution(TEMP_12_BIT);
sensor.requestTemperatures();
while (!sensor.isConversionComplete());
retVal = sensor.getTempC();

if (config->tempinf) {
retVal = sensor.getTempF();
if (retVal == float(DEVICE_DISCONNECTED_F)) {
retVal = -100.0;
} else if (pin == AMBSENSOR) {
retVal = retVal + config->calAmbient;
} else if (pin == VESSENSOR) {
retVal = retVal + config->calVessel;
}
} else {
retVal = sensor.getTempC();
if (retVal == float(DEVICE_DISCONNECTED_C)) {
retVal = -100.0;
} else if (pin == AMBSENSOR) {
retVal = retVal + config->calAmbient;
} else if (pin == VESSENSOR) {
retVal = retVal + config->calVessel;
}
}

return retVal;
}
36 changes: 36 additions & 0 deletions src/sensors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)
This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#ifndef _SENSORS_H
#define _SENSORS_H

#include "config.h"
#include "jsonconfig.h"
#include <OneWire.h>
#include <DS18B20.h>

#define DEVICE_DISCONNECTED_C -127
#define DEVICE_DISCONNECTED_F -196.6

double getTemp(uint8_t);

#endif // _SENSORS_H

0 comments on commit 14de5fd

Please sign in to comment.