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:
0:f6611c8f453c
Child:
1:518f39df3485
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 07 14:15:21 2014 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+DigitalIn gpPin(p21);
+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()
+{
+    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;
+
+    // Gas Meter Pulse sample timer
+    Timer gasPulseTimer;
+    gasPulseTimer.start();
+    int gasPulseCount = 0;
+    bool gasPulseVals[NUM_GASPULSE_VALS_IN_PACKET];
+    
+    // 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
+        {
+            if (gasPulseTimer.read_ms() > 1000)
+            {
+                gasPulseTimer.reset();
+                
+                // Read from Gas Pulse pin
+                gasPulseVals[gasPulseCount++] = gpPin;
+                led = gpPin;
+                if (gasPulseCount >= NUM_GASPULSE_VALS_IN_PACKET)
+                {
+                    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;
+                    }
+                    sendSocket.sendTo(broadcast, outBuf, sizeof(outBuf)-2);
+                    printf("Sending %s", outBuf);
+                }
+            }
+        }
+    }
+}
\ No newline at end of file