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

main.cpp

Committer:
Bobty
Date:
2014-11-07
Revision:
3:9f00be404f8f
Parent:
2:6bfef0839102
Child:
4:0d3a207680b0

File content as of revision 3:9f00be404f8f:

#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
Serial pc(USBTX, USBRX);

int main()
{
    pc.baud(115200);
    printf("Gas Monitor - Rob Dobson 2014\n");

    EthernetInterface eth;
    eth.init(); //Use DHCP
    UDPSocket sendSocket;
    Endpoint broadcast;
    
    // Connection establishment/re-establishment
    Timer connectRetryTimer;
    connectRetryTimer.start();
    bool isConnected = false;
    
    // Count of gas pulses
    int gasCount = 0;
    
    // Forever    
    while (true)
    {
        // Check if already connected to ethernet
        if (!isConnected)
        {
            if (connectRetryTimer.read_ms() > 1000)
            {
                isConnected = eth.connect() == 0;
                connectRetryTimer.reset();
                if (isConnected)
                {
                    printf("Eth Connected - IP Address is %s - MAC is %s\n", eth.getIPAddress(), eth.getMACAddress());
                    sendSocket.init();
                    sendSocket.set_broadcasting();
                    broadcast.set_address("255.255.255.255", BROADCAST_PORT);
                    }
                else
                {
                    printf("Eth Connect Attempt Failed\n");
                }
            }
        }
        else
        {
            led = gpPin;
            // Check for an edge
            bool edgeDetected = pulsePin.Service();
            if (edgeDetected)
            {
                gasCount++;
                char outBuf[200];
                sprintf(outBuf, "{\"e\":[{\"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)
                {
                    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;
                }
            }
            
            // See if anything has failed
            if (!isConnected)
            {
                sendSocket.close();
                eth.disconnect();
                Thread::wait(1000);
            }
        }
    }
}