Central Heating controller using the real time clock, PHY module for internet, 1-wire interface for temperature sensors, a system log and a configuration file
Dependencies: net 1-wire lpc1768 crypto clock web fram log
/media/uploads/andrewboyson/heating.sch
/media/uploads/andrewboyson/heating.brd
/media/uploads/andrewboyson/eagle.epf
heating/values.c@106:41ed3ea0bbba, 2021-04-23 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Apr 23 08:36:42 2021 +0000
- Revision:
- 106:41ed3ea0bbba
- Parent:
- 105:1899f7ed17ec
Not working, crashes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:3c04f4b47041 | 1 | #include <stdint.h> |
andrewboyson | 0:3c04f4b47041 | 2 | #include <string.h> |
andrewboyson | 0:3c04f4b47041 | 3 | #include <stdbool.h> |
andrewboyson | 0:3c04f4b47041 | 4 | |
andrewboyson | 0:3c04f4b47041 | 5 | #include "tftp.h" |
andrewboyson | 38:2bfeefa8709a | 6 | #include "dnslabel.h" |
andrewboyson | 106:41ed3ea0bbba | 7 | //#include "fram.h" |
andrewboyson | 20:904a4f043f2c | 8 | #include "clk.h" |
andrewboyson | 48:6eac12df3ad5 | 9 | #include "clktime.h" |
andrewboyson | 11:fa01ea25b62d | 10 | #include "mstimer.h" |
andrewboyson | 11:fa01ea25b62d | 11 | #include "clktm.h" |
andrewboyson | 0:3c04f4b47041 | 12 | #include "ds18b20.h" |
andrewboyson | 0:3c04f4b47041 | 13 | #include "radiator.h" |
andrewboyson | 0:3c04f4b47041 | 14 | #include "boiler.h" |
andrewboyson | 94:ded7c74d49fb | 15 | #include "hot-water.h" |
andrewboyson | 0:3c04f4b47041 | 16 | #include "log.h" |
andrewboyson | 0:3c04f4b47041 | 17 | #include "net.h" |
andrewboyson | 106:41ed3ea0bbba | 18 | #include "settings.h" |
andrewboyson | 0:3c04f4b47041 | 19 | |
andrewboyson | 0:3c04f4b47041 | 20 | bool ValuesTrace = false; |
andrewboyson | 0:3c04f4b47041 | 21 | |
andrewboyson | 106:41ed3ea0bbba | 22 | static char serverName[DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 106:41ed3ea0bbba | 23 | static char fileName [DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 106:41ed3ea0bbba | 24 | static int32_t writeSize; |
andrewboyson | 106:41ed3ea0bbba | 25 | static int32_t readInterval; |
andrewboyson | 106:41ed3ea0bbba | 26 | static int64_t startTime; |
andrewboyson | 106:41ed3ea0bbba | 27 | static int32_t count; |
andrewboyson | 0:3c04f4b47041 | 28 | |
andrewboyson | 48:6eac12df3ad5 | 29 | char* ValuesGetServerName ( ) { return serverName; } |
andrewboyson | 106:41ed3ea0bbba | 30 | char* ValuesGetFileName ( ) { return fileName; } |
andrewboyson | 106:41ed3ea0bbba | 31 | int ValuesGetWriteSize ( ) { return (int) writeSize; } |
andrewboyson | 106:41ed3ea0bbba | 32 | int ValuesGetReadInterval ( ) { return (int) readInterval; } |
andrewboyson | 48:6eac12df3ad5 | 33 | void ValuesGetStartTm (struct tm* ptm) { ClkTimeToTmUtc(startTime, ptm); } |
andrewboyson | 48:6eac12df3ad5 | 34 | int64_t ValuesGetStartTime ( ) { return startTime >> CLK_TIME_ONE_SECOND_SHIFT;} |
andrewboyson | 106:41ed3ea0bbba | 35 | int ValuesGetCount ( ) { return (int) count; } |
andrewboyson | 0:3c04f4b47041 | 36 | |
andrewboyson | 106:41ed3ea0bbba | 37 | void ValuesSetServerName (char* value) { DnsLabelCopy(serverName, value); SetValuesServerName ( value); } |
andrewboyson | 106:41ed3ea0bbba | 38 | void ValuesSetFileName (char* value) { DnsLabelCopy( fileName, value); SetValuesFileName ( value); } |
andrewboyson | 106:41ed3ea0bbba | 39 | void ValuesSetWriteSize (int value) { writeSize = value ; SetValuesWriteSize (&value); } |
andrewboyson | 106:41ed3ea0bbba | 40 | void ValuesSetReadInterval (int value) { readInterval = value ; SetValuesReadInterval(&value); } |
andrewboyson | 106:41ed3ea0bbba | 41 | static void setStartTime (int64_t value) { startTime = value ; SetValuesStartTime (&value); } |
andrewboyson | 106:41ed3ea0bbba | 42 | static void setCount (int value) { count = value ; SetValuesCount (&value); } |
andrewboyson | 0:3c04f4b47041 | 43 | |
andrewboyson | 0:3c04f4b47041 | 44 | static int readValuesFromFram() |
andrewboyson | 0:3c04f4b47041 | 45 | { |
andrewboyson | 0:3c04f4b47041 | 46 | int address; |
andrewboyson | 0:3c04f4b47041 | 47 | int32_t def4; |
andrewboyson | 106:41ed3ea0bbba | 48 | GetValuesServerName ( serverName); |
andrewboyson | 106:41ed3ea0bbba | 49 | GetValuesFileName ( fileName); |
andrewboyson | 106:41ed3ea0bbba | 50 | GetValuesWriteSize ( &writeSize); |
andrewboyson | 106:41ed3ea0bbba | 51 | GetValuesReadInterval(&readInterval); |
andrewboyson | 106:41ed3ea0bbba | 52 | GetValuesStartTime ( &startTime); |
andrewboyson | 106:41ed3ea0bbba | 53 | GetValuesCount ( &count); |
andrewboyson | 0:3c04f4b47041 | 54 | return 0; |
andrewboyson | 0:3c04f4b47041 | 55 | } |
andrewboyson | 0:3c04f4b47041 | 56 | |
andrewboyson | 0:3c04f4b47041 | 57 | |
andrewboyson | 11:fa01ea25b62d | 58 | static uint32_t readStartMs; |
andrewboyson | 0:3c04f4b47041 | 59 | |
andrewboyson | 0:3c04f4b47041 | 60 | static int writeIndex; |
andrewboyson | 0:3c04f4b47041 | 61 | |
andrewboyson | 0:3c04f4b47041 | 62 | static int nextByteOfWriteStream() |
andrewboyson | 0:3c04f4b47041 | 63 | { |
andrewboyson | 0:3c04f4b47041 | 64 | int byteAfterData = count * 8; |
andrewboyson | 106:41ed3ea0bbba | 65 | if (writeIndex >= byteAfterData) |
andrewboyson | 0:3c04f4b47041 | 66 | { |
andrewboyson | 0:3c04f4b47041 | 67 | setCount(0); |
andrewboyson | 0:3c04f4b47041 | 68 | return -1; |
andrewboyson | 0:3c04f4b47041 | 69 | } |
andrewboyson | 0:3c04f4b47041 | 70 | char c; |
andrewboyson | 106:41ed3ea0bbba | 71 | GetValuesData(writeIndex, &c); |
andrewboyson | 0:3c04f4b47041 | 72 | writeIndex++; |
andrewboyson | 0:3c04f4b47041 | 73 | return c; |
andrewboyson | 0:3c04f4b47041 | 74 | } |
andrewboyson | 0:3c04f4b47041 | 75 | |
andrewboyson | 0:3c04f4b47041 | 76 | static void readValues() |
andrewboyson | 11:fa01ea25b62d | 77 | { |
andrewboyson | 0:3c04f4b47041 | 78 | uint64_t record = 0; |
andrewboyson | 0:3c04f4b47041 | 79 | uint16_t value; |
andrewboyson | 48:6eac12df3ad5 | 80 | value = RadiatorGetHallDS18B20Value(); |
andrewboyson | 0:3c04f4b47041 | 81 | value &= 0x0FFF; |
andrewboyson | 0:3c04f4b47041 | 82 | record |= value; //0000 0000 0000 0AAA |
andrewboyson | 0:3c04f4b47041 | 83 | record <<= 12; //0000 0000 00AA A000 |
andrewboyson | 48:6eac12df3ad5 | 84 | value = BoilerGetTankDS18B20Value(); |
andrewboyson | 0:3c04f4b47041 | 85 | value &= 0x0FFF; |
andrewboyson | 0:3c04f4b47041 | 86 | record |= value; //0000 0000 00AA ABBB |
andrewboyson | 0:3c04f4b47041 | 87 | record <<= 12; //0000 000A AABB B000 |
andrewboyson | 48:6eac12df3ad5 | 88 | value = BoilerGetOutputDS18B20Value(); |
andrewboyson | 0:3c04f4b47041 | 89 | value &= 0x0FFF; |
andrewboyson | 0:3c04f4b47041 | 90 | record |= value; //0000 000A AABB BCCC |
andrewboyson | 0:3c04f4b47041 | 91 | record <<= 12; //0000 AAAB BBCC C000 |
andrewboyson | 105:1899f7ed17ec | 92 | //value = BoilerGetReturnDS18B20Value(); |
andrewboyson | 105:1899f7ed17ec | 93 | value = BoilerGetRtnDelDS18B20Value(); |
andrewboyson | 0:3c04f4b47041 | 94 | value &= 0x0FFF; |
andrewboyson | 0:3c04f4b47041 | 95 | record |= value; //0000 AAAB BBCC CDDD |
andrewboyson | 94:ded7c74d49fb | 96 | record <<= 12; //0AAA BBBC CCDD D000 |
andrewboyson | 94:ded7c74d49fb | 97 | value = HotWaterGetDS18B20Value(); |
andrewboyson | 94:ded7c74d49fb | 98 | value &= 0x0FFF; |
andrewboyson | 94:ded7c74d49fb | 99 | record |= value; //0AAA BBBC CCDD DEEE |
andrewboyson | 94:ded7c74d49fb | 100 | record <<= 4; //AAAB BBCC CDDD EEE0 |
andrewboyson | 0:3c04f4b47041 | 101 | |
andrewboyson | 94:ded7c74d49fb | 102 | record |= RadiatorPump << 0; |
andrewboyson | 94:ded7c74d49fb | 103 | record |= BoilerCall << 1; |
andrewboyson | 94:ded7c74d49fb | 104 | record |= BoilerPump << 2; //AAAB BBCC CDDD EEEF |
andrewboyson | 0:3c04f4b47041 | 105 | |
andrewboyson | 106:41ed3ea0bbba | 106 | if (count <= 0) |
andrewboyson | 106:41ed3ea0bbba | 107 | { |
andrewboyson | 106:41ed3ea0bbba | 108 | count = 0; |
andrewboyson | 106:41ed3ea0bbba | 109 | setStartTime(ClkNowTai()); |
andrewboyson | 106:41ed3ea0bbba | 110 | } |
andrewboyson | 0:3c04f4b47041 | 111 | |
andrewboyson | 106:41ed3ea0bbba | 112 | SetValuesData(count, &record); |
andrewboyson | 0:3c04f4b47041 | 113 | setCount(count + 1); |
andrewboyson | 0:3c04f4b47041 | 114 | } |
andrewboyson | 0:3c04f4b47041 | 115 | |
andrewboyson | 0:3c04f4b47041 | 116 | static void writeValues() |
andrewboyson | 0:3c04f4b47041 | 117 | { |
andrewboyson | 0:3c04f4b47041 | 118 | if (!serverName[0] ) return; //Do nothing if have no server name |
andrewboyson | 0:3c04f4b47041 | 119 | if ( !fileName[0] ) return; //Do nothing if have no file name |
andrewboyson | 0:3c04f4b47041 | 120 | if (TftpWriteStatus) return; //Do nothing if the TFTP client is busy |
andrewboyson | 0:3c04f4b47041 | 121 | |
andrewboyson | 0:3c04f4b47041 | 122 | strcpy(TftpServerName, serverName); |
andrewboyson | 0:3c04f4b47041 | 123 | struct tm tm; |
andrewboyson | 13:2ca12dd42e91 | 124 | ClkTimeToTmUtc(startTime, &tm); |
andrewboyson | 90:1c504a7b465e | 125 | int len = strftime(TftpFileName, DNS_MAX_LABEL_LENGTH+1, fileName, &tm); |
andrewboyson | 90:1c504a7b465e | 126 | if (len == 0) |
andrewboyson | 90:1c504a7b465e | 127 | { |
andrewboyson | 90:1c504a7b465e | 128 | LogTimeF("Values - cannot make filename from template '%s'\r\n", count, fileName); |
andrewboyson | 90:1c504a7b465e | 129 | return; |
andrewboyson | 90:1c504a7b465e | 130 | } |
andrewboyson | 0:3c04f4b47041 | 131 | |
andrewboyson | 0:3c04f4b47041 | 132 | if (ValuesTrace) |
andrewboyson | 0:3c04f4b47041 | 133 | { |
andrewboyson | 0:3c04f4b47041 | 134 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 0:3c04f4b47041 | 135 | LogTimeF("Values - requesting backup of %d values to %s\r\n", count, TftpFileName); |
andrewboyson | 0:3c04f4b47041 | 136 | } |
andrewboyson | 28:bb55def47737 | 137 | |
andrewboyson | 0:3c04f4b47041 | 138 | writeIndex = 0; |
andrewboyson | 0:3c04f4b47041 | 139 | TftpWriteStatus = TFTP_WRITE_STATUS_REQUEST; //This is reset by TFTP when finished |
andrewboyson | 0:3c04f4b47041 | 140 | } |
andrewboyson | 0:3c04f4b47041 | 141 | |
andrewboyson | 0:3c04f4b47041 | 142 | void ValuesMain() |
andrewboyson | 0:3c04f4b47041 | 143 | { |
andrewboyson | 11:fa01ea25b62d | 144 | if (!readInterval) readStartMs = MsTimerCount; |
andrewboyson | 28:bb55def47737 | 145 | if (writeSize && count < writeSize && readInterval) |
andrewboyson | 0:3c04f4b47041 | 146 | { |
andrewboyson | 41:6413522ed343 | 147 | if (MsTimerRelative(readStartMs, readInterval * 1000)) |
andrewboyson | 11:fa01ea25b62d | 148 | { |
andrewboyson | 11:fa01ea25b62d | 149 | readValues(); //Only read values if they are going to be backed up |
andrewboyson | 11:fa01ea25b62d | 150 | readStartMs = MsTimerCount; |
andrewboyson | 11:fa01ea25b62d | 151 | } |
andrewboyson | 0:3c04f4b47041 | 152 | } |
andrewboyson | 11:fa01ea25b62d | 153 | else |
andrewboyson | 11:fa01ea25b62d | 154 | { |
andrewboyson | 11:fa01ea25b62d | 155 | readStartMs = MsTimerCount; |
andrewboyson | 11:fa01ea25b62d | 156 | } |
andrewboyson | 28:bb55def47737 | 157 | if (writeSize && count >= writeSize) writeValues(); //Backup the values once the backup size is reached |
andrewboyson | 0:3c04f4b47041 | 158 | } |
andrewboyson | 0:3c04f4b47041 | 159 | |
andrewboyson | 0:3c04f4b47041 | 160 | int ValuesInit() |
andrewboyson | 0:3c04f4b47041 | 161 | { |
andrewboyson | 0:3c04f4b47041 | 162 | if (readValuesFromFram()) return -1; |
andrewboyson | 11:fa01ea25b62d | 163 | readStartMs = MsTimerCount; |
andrewboyson | 0:3c04f4b47041 | 164 | TftpGetNextByteFunction = nextByteOfWriteStream; |
andrewboyson | 0:3c04f4b47041 | 165 | if (count > 0) writeValues(); //Backup the values if there are any |
andrewboyson | 0:3c04f4b47041 | 166 | return 0; |
andrewboyson | 0:3c04f4b47041 | 167 | } |