STM32F103C8T6_WIFI_Heating_system
Dependencies: mbed mbed-STM32F103C8T6 eeprom_flash Watchdog PinDetect DS1820
- Bluepill STM32F103C8T6 Heating system
- _This project is core part of bigger heating system project!_
Features
- Reading temperature from four DS18B20 sensors
- Making a decision about switching on/off heater and pomp
- Executing simple user commands from UART
- Storing state parameters to program memory (EEPROM emulation)
temp_controller/temp_controller.cpp@39:aa5f95061409, 2018-09-21 (annotated)
- Committer:
- andrewklmn
- Date:
- Fri Sep 21 17:56:25 2018 +0000
- Revision:
- 39:aa5f95061409
- Parent:
- 38:a0753c2a4497
- Child:
- 40:6168609574d0
modes added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewklmn | 38:a0753c2a4497 | 1 | #include "temp_controller.h" |
andrewklmn | 38:a0753c2a4497 | 2 | |
andrewklmn | 38:a0753c2a4497 | 3 | extern Serial pc; |
andrewklmn | 38:a0753c2a4497 | 4 | extern Watchdog wd; |
andrewklmn | 38:a0753c2a4497 | 5 | extern DigitalOut myled; |
andrewklmn | 39:aa5f95061409 | 6 | extern DigitalOut pomp_OFF; |
andrewklmn | 39:aa5f95061409 | 7 | extern DigitalOut heater_OFF; |
andrewklmn | 39:aa5f95061409 | 8 | |
andrewklmn | 38:a0753c2a4497 | 9 | extern unsigned int eeprom_config_value; |
andrewklmn | 38:a0753c2a4497 | 10 | |
andrewklmn | 38:a0753c2a4497 | 11 | #define MIN_MEBEL_TEMP 15 |
andrewklmn | 38:a0753c2a4497 | 12 | #define MIN_LITOS_TEMP 10 |
andrewklmn | 38:a0753c2a4497 | 13 | #define MIN_BACK_WATER_TEMP 10 |
andrewklmn | 38:a0753c2a4497 | 14 | #define MAX_HOT_WATER_TEMP 35 |
andrewklmn | 38:a0753c2a4497 | 15 | |
andrewklmn | 38:a0753c2a4497 | 16 | //unsigned int epprom_config_value = 0x0F0A0A23; |
andrewklmn | 38:a0753c2a4497 | 17 | /* |
andrewklmn | 38:a0753c2a4497 | 18 | epprom_config_value: |
andrewklmn | 38:a0753c2a4497 | 19 | 1 unsigned char - Min mebel temp (default +15) |
andrewklmn | 38:a0753c2a4497 | 20 | 2 unsigned char Min litos temp (default +10) |
andrewklmn | 38:a0753c2a4497 | 21 | 3 unsigned char Min back water temp (default +10) |
andrewklmn | 38:a0753c2a4497 | 22 | 4 unsigned char Max hot water temp (default +35) |
andrewklmn | 38:a0753c2a4497 | 23 | */ |
andrewklmn | 38:a0753c2a4497 | 24 | |
andrewklmn | 38:a0753c2a4497 | 25 | float temp[5] = { |
andrewklmn | 39:aa5f95061409 | 26 | // initial temperature is maximum |
andrewklmn | 39:aa5f95061409 | 27 | 85, // OUTDOOR |
andrewklmn | 39:aa5f95061409 | 28 | 85, // LITOS |
andrewklmn | 39:aa5f95061409 | 29 | 85, // MEBEL |
andrewklmn | 39:aa5f95061409 | 30 | 85, // HOT WATER |
andrewklmn | 39:aa5f95061409 | 31 | 85 // BACK WATER |
andrewklmn | 39:aa5f95061409 | 32 | }; |
andrewklmn | 39:aa5f95061409 | 33 | |
andrewklmn | 39:aa5f95061409 | 34 | float simulated_temp[5] = { |
andrewklmn | 39:aa5f95061409 | 35 | // initial temperature for simulator |
andrewklmn | 39:aa5f95061409 | 36 | 7, // OUTDOOR |
andrewklmn | 39:aa5f95061409 | 37 | 10, // LITOS |
andrewklmn | 39:aa5f95061409 | 38 | 14, // MEBEL |
andrewklmn | 39:aa5f95061409 | 39 | 33, // HOT WATER |
andrewklmn | 39:aa5f95061409 | 40 | 18 // BACK WATER |
andrewklmn | 38:a0753c2a4497 | 41 | }; |
andrewklmn | 38:a0753c2a4497 | 42 | |
andrewklmn | 38:a0753c2a4497 | 43 | int temp_error[5] = { |
andrewklmn | 39:aa5f95061409 | 44 | 1,1,1,1,1 // initial state is no sensors |
andrewklmn | 39:aa5f95061409 | 45 | // 0 - sensor present |
andrewklmn | 39:aa5f95061409 | 46 | // 1 - no sensor |
andrewklmn | 39:aa5f95061409 | 47 | // 2 - sensor CRC error |
andrewklmn | 39:aa5f95061409 | 48 | }; |
andrewklmn | 38:a0753c2a4497 | 49 | |
andrewklmn | 38:a0753c2a4497 | 50 | string labels[5] = { |
andrewklmn | 38:a0753c2a4497 | 51 | "OUTDOOR", |
andrewklmn | 38:a0753c2a4497 | 52 | "LITOS", |
andrewklmn | 38:a0753c2a4497 | 53 | "MEBEL", |
andrewklmn | 38:a0753c2a4497 | 54 | "HOT WATER", |
andrewklmn | 38:a0753c2a4497 | 55 | "BACK WATER" |
andrewklmn | 38:a0753c2a4497 | 56 | }; |
andrewklmn | 38:a0753c2a4497 | 57 | |
andrewklmn | 38:a0753c2a4497 | 58 | DS1820 ds1820[5] = { |
andrewklmn | 39:aa5f95061409 | 59 | DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR |
andrewklmn | 39:aa5f95061409 | 60 | DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS |
andrewklmn | 39:aa5f95061409 | 61 | DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL |
andrewklmn | 39:aa5f95061409 | 62 | DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER |
andrewklmn | 39:aa5f95061409 | 63 | DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER |
andrewklmn | 39:aa5f95061409 | 64 | }; |
andrewklmn | 38:a0753c2a4497 | 65 | |
andrewklmn | 39:aa5f95061409 | 66 | unsigned char working_mode = 1; // default mode after powerup is ECO |
andrewklmn | 39:aa5f95061409 | 67 | // 0 - OFF - heating off, pomp and heater are OFF |
andrewklmn | 39:aa5f95061409 | 68 | // 1 - ECO - eco heating |
andrewklmn | 39:aa5f95061409 | 69 | // 2 - STANDART - standart heating |
andrewklmn | 39:aa5f95061409 | 70 | // 3 - SIMULATOR - simulator mode - work on simulated_temp |
andrewklmn | 39:aa5f95061409 | 71 | // 4 - POMP - pomp is ON, heater is OFF |
andrewklmn | 39:aa5f95061409 | 72 | // 5 - FULL - pomp and heater are ON |
andrewklmn | 38:a0753c2a4497 | 73 | |
andrewklmn | 38:a0753c2a4497 | 74 | void start_temp(){ |
andrewklmn | 38:a0753c2a4497 | 75 | |
andrewklmn | 38:a0753c2a4497 | 76 | __disable_irq(); |
andrewklmn | 38:a0753c2a4497 | 77 | |
andrewklmn | 38:a0753c2a4497 | 78 | for ( int j=0; j < 5; j++ ) { |
andrewklmn | 38:a0753c2a4497 | 79 | if(ds1820[j].begin()) { |
andrewklmn | 38:a0753c2a4497 | 80 | ds1820[j].startConversion(); |
andrewklmn | 39:aa5f95061409 | 81 | //pc.printf("%s sensor present!\r\n", labels[j].c_str()); |
andrewklmn | 39:aa5f95061409 | 82 | };// else { |
andrewklmn | 39:aa5f95061409 | 83 | //pc.printf("No %s sensor found!\r\n", labels[j].c_str()); |
andrewklmn | 39:aa5f95061409 | 84 | //}; |
andrewklmn | 38:a0753c2a4497 | 85 | }; |
andrewklmn | 38:a0753c2a4497 | 86 | __enable_irq(); |
andrewklmn | 38:a0753c2a4497 | 87 | |
andrewklmn | 38:a0753c2a4497 | 88 | }; |
andrewklmn | 38:a0753c2a4497 | 89 | |
andrewklmn | 38:a0753c2a4497 | 90 | void check_temp(){ |
andrewklmn | 38:a0753c2a4497 | 91 | |
andrewklmn | 38:a0753c2a4497 | 92 | myled = 0; // turn the LED on |
andrewklmn | 38:a0753c2a4497 | 93 | |
andrewklmn | 38:a0753c2a4497 | 94 | // kick the watchdog |
andrewklmn | 38:a0753c2a4497 | 95 | wd.Service(); |
andrewklmn | 38:a0753c2a4497 | 96 | |
andrewklmn | 38:a0753c2a4497 | 97 | __disable_irq(); |
andrewklmn | 38:a0753c2a4497 | 98 | |
andrewklmn | 38:a0753c2a4497 | 99 | for ( int j=0; j < 5; j++ ) { |
andrewklmn | 38:a0753c2a4497 | 100 | |
andrewklmn | 38:a0753c2a4497 | 101 | temp_error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
andrewklmn | 38:a0753c2a4497 | 102 | |
andrewklmn | 39:aa5f95061409 | 103 | /* |
andrewklmn | 38:a0753c2a4497 | 104 | switch(temp_error[j]) { |
andrewklmn | 39:aa5f95061409 | 105 | case 0: // no errors -> 'temp' contains the value of measured temperature |
andrewklmn | 39:aa5f95061409 | 106 | //pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]); |
andrewklmn | 39:aa5f95061409 | 107 | break; |
andrewklmn | 39:aa5f95061409 | 108 | case 1: // no sensor present -> 'temp' is not updated |
andrewklmn | 39:aa5f95061409 | 109 | //pc.printf("no %s sensor present \r\n", labels[j].c_str() ); |
andrewklmn | 39:aa5f95061409 | 110 | break; |
andrewklmn | 39:aa5f95061409 | 111 | case 2: // CRC error -> 'temp' is not updated |
andrewklmn | 39:aa5f95061409 | 112 | //pc.printf("%s sensor CRC error \r\n", labels[j].c_str() ); |
andrewklmn | 39:aa5f95061409 | 113 | default: |
andrewklmn | 39:aa5f95061409 | 114 | |
andrewklmn | 38:a0753c2a4497 | 115 | }; |
andrewklmn | 39:aa5f95061409 | 116 | */ |
andrewklmn | 39:aa5f95061409 | 117 | // start temperature conversion from analog to digital before next reading |
andrewklmn | 38:a0753c2a4497 | 118 | ds1820[j].startConversion(); |
andrewklmn | 39:aa5f95061409 | 119 | }; |
andrewklmn | 38:a0753c2a4497 | 120 | |
andrewklmn | 38:a0753c2a4497 | 121 | |
andrewklmn | 38:a0753c2a4497 | 122 | // save new config if it was changed |
andrewklmn | 38:a0753c2a4497 | 123 | if (readEEPROMWord(0)!= eeprom_config_value) { |
andrewklmn | 39:aa5f95061409 | 124 | save_new_config(); |
andrewklmn | 38:a0753c2a4497 | 125 | }; |
andrewklmn | 38:a0753c2a4497 | 126 | |
andrewklmn | 38:a0753c2a4497 | 127 | |
andrewklmn | 38:a0753c2a4497 | 128 | |
andrewklmn | 38:a0753c2a4497 | 129 | __enable_irq(); |
andrewklmn | 38:a0753c2a4497 | 130 | |
andrewklmn | 38:a0753c2a4497 | 131 | myled = 1; // turn the LED off |
andrewklmn | 38:a0753c2a4497 | 132 | }; |
andrewklmn | 38:a0753c2a4497 | 133 | |
andrewklmn | 39:aa5f95061409 | 134 | |
andrewklmn | 39:aa5f95061409 | 135 | void process_temp() { |
andrewklmn | 39:aa5f95061409 | 136 | switch(working_mode) { |
andrewklmn | 39:aa5f95061409 | 137 | case 0: |
andrewklmn | 39:aa5f95061409 | 138 | // 0 - OFF - heating off, pomp and heater are OFF |
andrewklmn | 39:aa5f95061409 | 139 | pomp_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 140 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 141 | break; |
andrewklmn | 39:aa5f95061409 | 142 | case 1: |
andrewklmn | 39:aa5f95061409 | 143 | // 1 - ECO - eco heating |
andrewklmn | 39:aa5f95061409 | 144 | pc.printf("State %d|%d|%d|%d|%d\r\n", temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4] ); |
andrewklmn | 39:aa5f95061409 | 145 | pc.printf("Temp %3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4] ); |
andrewklmn | 39:aa5f95061409 | 146 | pc.printf("RAM_config=%X, FLASH_config=%X\r\n", eeprom_config_value ,readEEPROMWord(0)); |
andrewklmn | 39:aa5f95061409 | 147 | pc.printf("======================================="); |
andrewklmn | 39:aa5f95061409 | 148 | pomp_OFF = !pomp_OFF; |
andrewklmn | 39:aa5f95061409 | 149 | heater_OFF = !heater_OFF; |
andrewklmn | 39:aa5f95061409 | 150 | break; |
andrewklmn | 39:aa5f95061409 | 151 | case 2: |
andrewklmn | 39:aa5f95061409 | 152 | // 2 - STANDART - standart heating |
andrewklmn | 39:aa5f95061409 | 153 | pomp_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 154 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 155 | break; |
andrewklmn | 39:aa5f95061409 | 156 | case 3: |
andrewklmn | 39:aa5f95061409 | 157 | // 3 - SIMULATOR - simulator mode - work on simulated_temp |
andrewklmn | 39:aa5f95061409 | 158 | pomp_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 159 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 160 | break; |
andrewklmn | 39:aa5f95061409 | 161 | case 4: |
andrewklmn | 39:aa5f95061409 | 162 | // 4 - POMP - pomp is ON, heater is OFF |
andrewklmn | 39:aa5f95061409 | 163 | pomp_OFF = 0 |
andrewklmn | 39:aa5f95061409 | 164 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 165 | break; |
andrewklmn | 39:aa5f95061409 | 166 | case 5: |
andrewklmn | 39:aa5f95061409 | 167 | // 5 - FULL - pomp and heater are ON |
andrewklmn | 39:aa5f95061409 | 168 | pomp_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 169 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 170 | break; |
andrewklmn | 39:aa5f95061409 | 171 | default: |
andrewklmn | 39:aa5f95061409 | 172 | pomp_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 173 | heater_OFF = 1; |
andrewklmn | 39:aa5f95061409 | 174 | }; |
andrewklmn | 39:aa5f95061409 | 175 | |
andrewklmn | 39:aa5f95061409 | 176 | }; |
andrewklmn | 39:aa5f95061409 | 177 | |
andrewklmn | 39:aa5f95061409 | 178 | |
andrewklmn | 39:aa5f95061409 | 179 | void save_new_config() { |
andrewklmn | 39:aa5f95061409 | 180 | |
andrewklmn | 39:aa5f95061409 | 181 | enableEEPROMWriting(); |
andrewklmn | 39:aa5f95061409 | 182 | writeEEPROMWord(0,eeprom_config_value); |
andrewklmn | 39:aa5f95061409 | 183 | disableEEPROMWriting(); |
andrewklmn | 39:aa5f95061409 | 184 | |
andrewklmn | 39:aa5f95061409 | 185 | }; |
andrewklmn | 39:aa5f95061409 | 186 | |
andrewklmn | 39:aa5f95061409 | 187 | |
andrewklmn | 38:a0753c2a4497 | 188 | unsigned int get_temp_config_value(){ |
andrewklmn | 38:a0753c2a4497 | 189 | |
andrewklmn | 38:a0753c2a4497 | 190 | unsigned int v = readEEPROMWord(0); |
andrewklmn | 38:a0753c2a4497 | 191 | if ( v == 0xFFFFFFFF ) { |
andrewklmn | 38:a0753c2a4497 | 192 | // if eeprom config value is not initialized |
andrewklmn | 38:a0753c2a4497 | 193 | // set default value 0x0F0A0A23; |
andrewklmn | 38:a0753c2a4497 | 194 | v = MIN_MEBEL_TEMP; |
andrewklmn | 38:a0753c2a4497 | 195 | v = ( v << 8 ) | MIN_LITOS_TEMP; |
andrewklmn | 38:a0753c2a4497 | 196 | v = ( v << 8 ) | MIN_BACK_WATER_TEMP; |
andrewklmn | 38:a0753c2a4497 | 197 | v = ( v << 8 ) | MAX_HOT_WATER_TEMP; |
andrewklmn | 38:a0753c2a4497 | 198 | }; |
andrewklmn | 38:a0753c2a4497 | 199 | return v; |
andrewklmn | 38:a0753c2a4497 | 200 | }; |