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@1:518f39df3485, 2014-11-07 (annotated)
- Committer:
- Bobty
- Date:
- Fri Nov 07 14:44:23 2014 +0000
- Revision:
- 1:518f39df3485
- Parent:
- 0:f6611c8f453c
- Child:
- 2:6bfef0839102
Ethernet reconnects ok
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 | 0:f6611c8f453c | 3 | |
Bobty | 0:f6611c8f453c | 4 | DigitalIn gpPin(p21); |
Bobty | 0:f6611c8f453c | 5 | DigitalOut led(LED1); |
Bobty | 0:f6611c8f453c | 6 | |
Bobty | 0:f6611c8f453c | 7 | const int BROADCAST_PORT = 42853; // Arbitrarily chosen port number |
Bobty | 0:f6611c8f453c | 8 | const int NUM_GASPULSE_VALS_IN_PACKET = 10; |
Bobty | 0:f6611c8f453c | 9 | Serial pc(USBTX, USBRX); |
Bobty | 0:f6611c8f453c | 10 | |
Bobty | 0:f6611c8f453c | 11 | int main() |
Bobty | 0:f6611c8f453c | 12 | { |
Bobty | 0:f6611c8f453c | 13 | pc.baud(115200); |
Bobty | 0:f6611c8f453c | 14 | printf("Gas Monitor - Rob Dobson 2014\n"); |
Bobty | 0:f6611c8f453c | 15 | |
Bobty | 0:f6611c8f453c | 16 | EthernetInterface eth; |
Bobty | 0:f6611c8f453c | 17 | eth.init(); //Use DHCP |
Bobty | 0:f6611c8f453c | 18 | UDPSocket sendSocket; |
Bobty | 0:f6611c8f453c | 19 | Endpoint broadcast; |
Bobty | 0:f6611c8f453c | 20 | |
Bobty | 0:f6611c8f453c | 21 | // Connection establishment/re-establishment |
Bobty | 0:f6611c8f453c | 22 | Timer connectRetryTimer; |
Bobty | 0:f6611c8f453c | 23 | connectRetryTimer.start(); |
Bobty | 0:f6611c8f453c | 24 | bool isConnected = false; |
Bobty | 0:f6611c8f453c | 25 | |
Bobty | 0:f6611c8f453c | 26 | // Gas Meter Pulse sample timer |
Bobty | 0:f6611c8f453c | 27 | Timer gasPulseTimer; |
Bobty | 0:f6611c8f453c | 28 | gasPulseTimer.start(); |
Bobty | 0:f6611c8f453c | 29 | int gasPulseCount = 0; |
Bobty | 0:f6611c8f453c | 30 | bool gasPulseVals[NUM_GASPULSE_VALS_IN_PACKET]; |
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 | 0:f6611c8f453c | 57 | if (gasPulseTimer.read_ms() > 1000) |
Bobty | 0:f6611c8f453c | 58 | { |
Bobty | 0:f6611c8f453c | 59 | gasPulseTimer.reset(); |
Bobty | 0:f6611c8f453c | 60 | |
Bobty | 0:f6611c8f453c | 61 | // Read from Gas Pulse pin |
Bobty | 0:f6611c8f453c | 62 | gasPulseVals[gasPulseCount++] = gpPin; |
Bobty | 0:f6611c8f453c | 63 | led = gpPin; |
Bobty | 0:f6611c8f453c | 64 | if (gasPulseCount >= NUM_GASPULSE_VALS_IN_PACKET) |
Bobty | 0:f6611c8f453c | 65 | { |
Bobty | 0:f6611c8f453c | 66 | gasPulseCount = 0; |
Bobty | 0:f6611c8f453c | 67 | |
Bobty | 0:f6611c8f453c | 68 | // Send the packet |
Bobty | 0:f6611c8f453c | 69 | char outBuf[] = "{\"gaspulseval\":[0,0,0,0,0,0,0,0,0,0]}\n"; |
Bobty | 0:f6611c8f453c | 70 | char* pOutBuf = strchr(outBuf, '[') + 1; |
Bobty | 0:f6611c8f453c | 71 | for (int i = 0; i < NUM_GASPULSE_VALS_IN_PACKET; i++) |
Bobty | 0:f6611c8f453c | 72 | { |
Bobty | 0:f6611c8f453c | 73 | *pOutBuf = gasPulseVals[i] ? '1' : '0'; |
Bobty | 0:f6611c8f453c | 74 | pOutBuf+=2; |
Bobty | 0:f6611c8f453c | 75 | } |
Bobty | 1:518f39df3485 | 76 | int bytesToSend = sizeof(outBuf) - 2; |
Bobty | 1:518f39df3485 | 77 | int rslt = sendSocket.sendTo(broadcast, outBuf, bytesToSend); |
Bobty | 1:518f39df3485 | 78 | if (rslt == bytesToSend) |
Bobty | 1:518f39df3485 | 79 | { |
Bobty | 1:518f39df3485 | 80 | printf("Sent ok %s", outBuf); |
Bobty | 1:518f39df3485 | 81 | } |
Bobty | 1:518f39df3485 | 82 | else if (rslt == -1) |
Bobty | 1:518f39df3485 | 83 | { |
Bobty | 1:518f39df3485 | 84 | printf("Failed to send %s", outBuf); |
Bobty | 1:518f39df3485 | 85 | isConnected = false; |
Bobty | 1:518f39df3485 | 86 | } |
Bobty | 1:518f39df3485 | 87 | else |
Bobty | 1:518f39df3485 | 88 | { |
Bobty | 1:518f39df3485 | 89 | printf("Didn't send all of %s", outBuf); |
Bobty | 1:518f39df3485 | 90 | isConnected = false; |
Bobty | 1:518f39df3485 | 91 | } |
Bobty | 0:f6611c8f453c | 92 | } |
Bobty | 0:f6611c8f453c | 93 | } |
Bobty | 1:518f39df3485 | 94 | |
Bobty | 1:518f39df3485 | 95 | // See if anything has failed |
Bobty | 1:518f39df3485 | 96 | if (!isConnected) |
Bobty | 1:518f39df3485 | 97 | { |
Bobty | 1:518f39df3485 | 98 | sendSocket.close(); |
Bobty | 1:518f39df3485 | 99 | eth.disconnect(); |
Bobty | 1:518f39df3485 | 100 | Thread::wait(1000); |
Bobty | 1:518f39df3485 | 101 | } |
Bobty | 0:f6611c8f453c | 102 | } |
Bobty | 0:f6611c8f453c | 103 | } |
Bobty | 0:f6611c8f453c | 104 | } |