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@4:0d3a207680b0, 2015-02-02 (annotated)
- Committer:
- Bobty
- Date:
- Mon Feb 02 16:24:30 2015 +0000
- Revision:
- 4:0d3a207680b0
- Parent:
- 3:9f00be404f8f
- Child:
- 5:5bccf48799d4
Starting to add SD File System
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 0:f6611c8f453c | 1 | #include "mbed.h" |
Bobty | 0:f6611c8f453c | 2 | #include "EthernetInterface.h" |
Bobty | 2:6bfef0839102 | 3 | #include "PulsePin.h" |
Bobty | 4:0d3a207680b0 | 4 | #include "SDFileSystem.h" |
Bobty | 4:0d3a207680b0 | 5 | |
Bobty | 4:0d3a207680b0 | 6 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
Bobty | 0:f6611c8f453c | 7 | DigitalIn gpPin(p21); |
Bobty | 2:6bfef0839102 | 8 | PulsePin pulsePin(gpPin, false, 200); |
Bobty | 0:f6611c8f453c | 9 | DigitalOut led(LED1); |
Bobty | 0:f6611c8f453c | 10 | |
Bobty | 0:f6611c8f453c | 11 | const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number |
Bobty | 0:f6611c8f453c | 12 | Serial pc(USBTX, USBRX); |
Bobty | 0:f6611c8f453c | 13 | |
Bobty | 0:f6611c8f453c | 14 | int main() |
Bobty | 0:f6611c8f453c | 15 | { |
Bobty | 0:f6611c8f453c | 16 | pc.baud(115200); |
Bobty | 0:f6611c8f453c | 17 | printf("Gas Monitor - Rob Dobson 2014\n"); |
Bobty | 0:f6611c8f453c | 18 | |
Bobty | 0:f6611c8f453c | 19 | EthernetInterface eth; |
Bobty | 0:f6611c8f453c | 20 | eth.init(); //Use DHCP |
Bobty | 0:f6611c8f453c | 21 | UDPSocket sendSocket; |
Bobty | 0:f6611c8f453c | 22 | Endpoint broadcast; |
Bobty | 0:f6611c8f453c | 23 | |
Bobty | 0:f6611c8f453c | 24 | // Connection establishment/re-establishment |
Bobty | 0:f6611c8f453c | 25 | Timer connectRetryTimer; |
Bobty | 0:f6611c8f453c | 26 | connectRetryTimer.start(); |
Bobty | 0:f6611c8f453c | 27 | bool isConnected = false; |
Bobty | 2:6bfef0839102 | 28 | |
Bobty | 2:6bfef0839102 | 29 | // Count of gas pulses |
Bobty | 2:6bfef0839102 | 30 | int gasCount = 0; |
Bobty | 0:f6611c8f453c | 31 | |
Bobty | 0:f6611c8f453c | 32 | // Forever |
Bobty | 0:f6611c8f453c | 33 | while (true) |
Bobty | 0:f6611c8f453c | 34 | { |
Bobty | 0:f6611c8f453c | 35 | // Check if already connected to ethernet |
Bobty | 0:f6611c8f453c | 36 | if (!isConnected) |
Bobty | 0:f6611c8f453c | 37 | { |
Bobty | 0:f6611c8f453c | 38 | if (connectRetryTimer.read_ms() > 1000) |
Bobty | 0:f6611c8f453c | 39 | { |
Bobty | 0:f6611c8f453c | 40 | isConnected = eth.connect() == 0; |
Bobty | 0:f6611c8f453c | 41 | connectRetryTimer.reset(); |
Bobty | 0:f6611c8f453c | 42 | if (isConnected) |
Bobty | 0:f6611c8f453c | 43 | { |
Bobty | 0:f6611c8f453c | 44 | printf("Eth Connected - IP Address is %s - MAC is %s\n", eth.getIPAddress(), eth.getMACAddress()); |
Bobty | 0:f6611c8f453c | 45 | sendSocket.init(); |
Bobty | 0:f6611c8f453c | 46 | sendSocket.set_broadcasting(); |
Bobty | 0:f6611c8f453c | 47 | broadcast.set_address("255.255.255.255", BROADCAST_PORT); |
Bobty | 0:f6611c8f453c | 48 | } |
Bobty | 0:f6611c8f453c | 49 | else |
Bobty | 0:f6611c8f453c | 50 | { |
Bobty | 0:f6611c8f453c | 51 | printf("Eth Connect Attempt Failed\n"); |
Bobty | 0:f6611c8f453c | 52 | } |
Bobty | 0:f6611c8f453c | 53 | } |
Bobty | 0:f6611c8f453c | 54 | } |
Bobty | 0:f6611c8f453c | 55 | else |
Bobty | 0:f6611c8f453c | 56 | { |
Bobty | 2:6bfef0839102 | 57 | led = gpPin; |
Bobty | 2:6bfef0839102 | 58 | // Check for an edge |
Bobty | 2:6bfef0839102 | 59 | bool edgeDetected = pulsePin.Service(); |
Bobty | 2:6bfef0839102 | 60 | if (edgeDetected) |
Bobty | 0:f6611c8f453c | 61 | { |
Bobty | 2:6bfef0839102 | 62 | gasCount++; |
Bobty | 2:6bfef0839102 | 63 | char outBuf[200]; |
Bobty | 3:9f00be404f8f | 64 | sprintf(outBuf, "{\"e\":[{\"n\":\"gasCount\",\"v\":%d},{\"n\":\"gasInterPulse\",\"v\":%d,\"u\":\"ms\"}]}", |
Bobty | 2:6bfef0839102 | 65 | gasCount, pulsePin.GetLastCycleTimeMs()); |
Bobty | 2:6bfef0839102 | 66 | int bytesToSend = strlen(outBuf); |
Bobty | 2:6bfef0839102 | 67 | int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); |
Bobty | 2:6bfef0839102 | 68 | if (rslt == bytesToSend) |
Bobty | 0:f6611c8f453c | 69 | { |
Bobty | 2:6bfef0839102 | 70 | printf("Sent ok %s\n", outBuf); |
Bobty | 2:6bfef0839102 | 71 | } |
Bobty | 2:6bfef0839102 | 72 | else if (rslt == -1) |
Bobty | 2:6bfef0839102 | 73 | { |
Bobty | 2:6bfef0839102 | 74 | printf("Failed to send %s\n", outBuf); |
Bobty | 2:6bfef0839102 | 75 | isConnected = false; |
Bobty | 2:6bfef0839102 | 76 | } |
Bobty | 2:6bfef0839102 | 77 | else |
Bobty | 2:6bfef0839102 | 78 | { |
Bobty | 2:6bfef0839102 | 79 | printf("Didn't send all of %s\n", outBuf); |
Bobty | 2:6bfef0839102 | 80 | isConnected = false; |
Bobty | 0:f6611c8f453c | 81 | } |
Bobty | 0:f6611c8f453c | 82 | } |
Bobty | 1:518f39df3485 | 83 | |
Bobty | 1:518f39df3485 | 84 | // See if anything has failed |
Bobty | 1:518f39df3485 | 85 | if (!isConnected) |
Bobty | 1:518f39df3485 | 86 | { |
Bobty | 1:518f39df3485 | 87 | sendSocket.close(); |
Bobty | 1:518f39df3485 | 88 | eth.disconnect(); |
Bobty | 1:518f39df3485 | 89 | Thread::wait(1000); |
Bobty | 1:518f39df3485 | 90 | } |
Bobty | 0:f6611c8f453c | 91 | } |
Bobty | 0:f6611c8f453c | 92 | } |
Bobty | 0:f6611c8f453c | 93 | } |