Monitor for central heating system (e.g. 2zones+hw) Supports up to 15 temp probes (DS18B20/DS18S20) 3 valve monitors Gas pulse meter recording Use stand-alone or with nodeEnergyServer See http://robdobson.com/2015/09/central-heating-monitor
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
Revision 2:6bfef0839102, committed 2014-11-07
- Comitter:
- Bobty
- Date:
- Fri Nov 07 16:09:15 2014 +0000
- Parent:
- 1:518f39df3485
- Child:
- 3:9f00be404f8f
- Commit message:
- Working - detects between pulse time and counts total pulses
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PulsePin.cpp Fri Nov 07 16:09:15 2014 +0000 @@ -0,0 +1,53 @@ +// Handles a pin that has a slow pulse applied +// Written for a gas meter monitor + +#include "PulsePin.h" + +PulsePin::PulsePin(DigitalIn& pin, bool risingEdge, int waitForPinStabilisationMs) : + _pin(pin) +{ + _risingEdge = risingEdge; + _waitForPinStabilisationMs = waitForPinStabilisationMs; + _pinTimer.start(); + _curPinState = _pin; + _lastStableTimeMs = _pinTimer.read_ms(); + _firstEdgeDetected = false; + _timeBetweenEdgesMs = 0; +} + +bool PulsePin::Service() +{ + // Check time since last edge - looking for stability + int timeNowMs = _pinTimer.read_ms(); + if (timeNowMs < _lastStableTimeMs + _waitForPinStabilisationMs) + return false; + + // Check for a change of state + bool pinState = _pin; + if (pinState == _curPinState) + return false; + + _curPinState = pinState; + _lastStableTimeMs = timeNowMs; + + // Check if this is the direction of edge we're looking for + if (pinState != _risingEdge) + return false; + + // Reset the timer to avoid wrap around problems + bool firstEdgeDetected = _firstEdgeDetected; + _pinTimer.reset(); + _firstEdgeDetected = true; + _lastStableTimeMs = 0; + + // Check if this should be returned + if (!firstEdgeDetected) + return false; + _timeBetweenEdgesMs = timeNowMs; + return true; +} + +int PulsePin::GetLastCycleTimeMs() +{ + return _timeBetweenEdgesMs; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PulsePin.h Fri Nov 07 16:09:15 2014 +0000 @@ -0,0 +1,24 @@ +#ifndef __PULSEPIN__H +#define __PULSEPIN__H +#include "mbed.h" + +class PulsePin +{ + public: + PulsePin(DigitalIn& pin, bool risingEdge, int pinStableTimeMs); + bool Service(); + int GetLastCycleTimeMs(); + + private: + DigitalIn& _pin; + Timer _pinTimer; + bool _curPinState; + bool _firstEdgeDetected; + int _lastStableTimeMs; + int _waitForPinStabilisationMs; + bool _risingEdge; + int _timeBetweenEdgesMs; +}; + + +#endif \ No newline at end of file
--- a/main.cpp Fri Nov 07 14:44:23 2014 +0000 +++ b/main.cpp Fri Nov 07 16:09:15 2014 +0000 @@ -1,11 +1,12 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "PulsePin.h" DigitalIn gpPin(p21); +PulsePin pulsePin(gpPin, false, 200); DigitalOut led(LED1); const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number -const int NUM_GASPULSE_VALS_IN_PACKET = 10; Serial pc(USBTX, USBRX); int main() @@ -22,12 +23,9 @@ Timer connectRetryTimer; connectRetryTimer.start(); bool isConnected = false; - - // Gas Meter Pulse sample timer - Timer gasPulseTimer; - gasPulseTimer.start(); - int gasPulseCount = 0; - bool gasPulseVals[NUM_GASPULSE_VALS_IN_PACKET]; + + // Count of gas pulses + int gasCount = 0; // Forever while (true) @@ -54,41 +52,30 @@ } else { - if (gasPulseTimer.read_ms() > 1000) + led = gpPin; + // Check for an edge + bool edgeDetected = pulsePin.Service(); + if (edgeDetected) { - gasPulseTimer.reset(); - - // Read from Gas Pulse pin - gasPulseVals[gasPulseCount++] = gpPin; - led = gpPin; - if (gasPulseCount >= NUM_GASPULSE_VALS_IN_PACKET) + gasCount++; + char outBuf[200]; + sprintf(outBuf, "[{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasInterPulse\",\"v\":%d,\"u\":\"ms\"}]", + gasCount, pulsePin.GetLastCycleTimeMs()); + int bytesToSend = strlen(outBuf); + int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); + if (rslt == bytesToSend) { - gasPulseCount = 0; - - // Send the packet - char outBuf[] = "{\"gaspulseval\":[0,0,0,0,0,0,0,0,0,0]}\n"; - char* pOutBuf = strchr(outBuf, '[') + 1; - for (int i = 0; i < NUM_GASPULSE_VALS_IN_PACKET; i++) - { - *pOutBuf = gasPulseVals[i] ? '1' : '0'; - pOutBuf+=2; - } - int bytesToSend = sizeof(outBuf) - 2; - int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); - if (rslt == bytesToSend) - { - printf("Sent ok %s", outBuf); - } - else if (rslt == -1) - { - printf("Failed to send %s", outBuf); - isConnected = false; - } - else - { - printf("Didn't send all of %s", outBuf); - isConnected = false; - } + printf("Sent ok %s\n", outBuf); + } + else if (rslt == -1) + { + printf("Failed to send %s\n", outBuf); + isConnected = false; + } + else + { + printf("Didn't send all of %s\n", outBuf); + isConnected = false; } }