STM32F103C8T6_WIFI_Heating_system

Dependencies:   mbed mbed-STM32F103C8T6 eeprom_flash Watchdog PinDetect DS1820

  1. Bluepill STM32F103C8T6 Heating system
    1. _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)

Committer:
andrewklmn
Date:
Tue Sep 18 20:49:05 2018 +0000
Revision:
38:a0753c2a4497
Child:
39:aa5f95061409
temp_controller function was moved from mian.cpp to own folder

Who changed what in which revision?

UserRevisionLine numberNew 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 38:a0753c2a4497 6 extern unsigned int eeprom_config_value;
andrewklmn 38:a0753c2a4497 7
andrewklmn 38:a0753c2a4497 8 #define MIN_MEBEL_TEMP 15
andrewklmn 38:a0753c2a4497 9 #define MIN_LITOS_TEMP 10
andrewklmn 38:a0753c2a4497 10 #define MIN_BACK_WATER_TEMP 10
andrewklmn 38:a0753c2a4497 11 #define MAX_HOT_WATER_TEMP 35
andrewklmn 38:a0753c2a4497 12
andrewklmn 38:a0753c2a4497 13 //unsigned int epprom_config_value = 0x0F0A0A23;
andrewklmn 38:a0753c2a4497 14 /*
andrewklmn 38:a0753c2a4497 15 epprom_config_value:
andrewklmn 38:a0753c2a4497 16 1 unsigned char - Min mebel temp (default +15)
andrewklmn 38:a0753c2a4497 17 2 unsigned char Min litos temp (default +10)
andrewklmn 38:a0753c2a4497 18 3 unsigned char Min back water temp (default +10)
andrewklmn 38:a0753c2a4497 19 4 unsigned char Max hot water temp (default +35)
andrewklmn 38:a0753c2a4497 20 */
andrewklmn 38:a0753c2a4497 21
andrewklmn 38:a0753c2a4497 22 float temp[5] = {
andrewklmn 38:a0753c2a4497 23 85,85,85,85,85 // initial temperature
andrewklmn 38:a0753c2a4497 24 };
andrewklmn 38:a0753c2a4497 25
andrewklmn 38:a0753c2a4497 26 int temp_error[5] = {
andrewklmn 38:a0753c2a4497 27 0,0,0,0,0 // initial state
andrewklmn 38:a0753c2a4497 28 };
andrewklmn 38:a0753c2a4497 29
andrewklmn 38:a0753c2a4497 30 string labels[5] = {
andrewklmn 38:a0753c2a4497 31 "OUTDOOR",
andrewklmn 38:a0753c2a4497 32 "LITOS",
andrewklmn 38:a0753c2a4497 33 "MEBEL",
andrewklmn 38:a0753c2a4497 34 "HOT WATER",
andrewklmn 38:a0753c2a4497 35 "BACK WATER"
andrewklmn 38:a0753c2a4497 36 };
andrewklmn 38:a0753c2a4497 37
andrewklmn 38:a0753c2a4497 38 DS1820 ds1820[5] = {
andrewklmn 38:a0753c2a4497 39 DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
andrewklmn 38:a0753c2a4497 40 DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS
andrewklmn 38:a0753c2a4497 41 DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
andrewklmn 38:a0753c2a4497 42 DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 38:a0753c2a4497 43 DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 38:a0753c2a4497 44 };
andrewklmn 38:a0753c2a4497 45
andrewklmn 38:a0753c2a4497 46
andrewklmn 38:a0753c2a4497 47 void start_temp(){
andrewklmn 38:a0753c2a4497 48
andrewklmn 38:a0753c2a4497 49 __disable_irq();
andrewklmn 38:a0753c2a4497 50
andrewklmn 38:a0753c2a4497 51 for ( int j=0; j < 5; j++ ) {
andrewklmn 38:a0753c2a4497 52 if(ds1820[j].begin()) {
andrewklmn 38:a0753c2a4497 53 ds1820[j].startConversion();
andrewklmn 38:a0753c2a4497 54 pc.printf("%s sensor present!\r\n", labels[j].c_str());
andrewklmn 38:a0753c2a4497 55 } else {
andrewklmn 38:a0753c2a4497 56 pc.printf("No %s sensor found!\r\n", labels[j].c_str());
andrewklmn 38:a0753c2a4497 57 };
andrewklmn 38:a0753c2a4497 58 };
andrewklmn 38:a0753c2a4497 59 __enable_irq();
andrewklmn 38:a0753c2a4497 60
andrewklmn 38:a0753c2a4497 61 };
andrewklmn 38:a0753c2a4497 62
andrewklmn 38:a0753c2a4497 63 void check_temp(){
andrewklmn 38:a0753c2a4497 64
andrewklmn 38:a0753c2a4497 65 myled = 0; // turn the LED on
andrewklmn 38:a0753c2a4497 66
andrewklmn 38:a0753c2a4497 67 // kick the watchdog
andrewklmn 38:a0753c2a4497 68 wd.Service();
andrewklmn 38:a0753c2a4497 69
andrewklmn 38:a0753c2a4497 70 __disable_irq();
andrewklmn 38:a0753c2a4497 71
andrewklmn 38:a0753c2a4497 72 for ( int j=0; j < 5; j++ ) {
andrewklmn 38:a0753c2a4497 73
andrewklmn 38:a0753c2a4497 74 temp_error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 38:a0753c2a4497 75
andrewklmn 38:a0753c2a4497 76 switch(temp_error[j]) {
andrewklmn 38:a0753c2a4497 77 case 0: // no errors -> 'temp' contains the value of measured temperature
andrewklmn 38:a0753c2a4497 78 pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]);
andrewklmn 38:a0753c2a4497 79 break;
andrewklmn 38:a0753c2a4497 80 case 1: // no sensor present -> 'temp' is not updated
andrewklmn 38:a0753c2a4497 81 pc.printf("no %s sensor present \r\n", labels[j].c_str() );
andrewklmn 38:a0753c2a4497 82 break;
andrewklmn 38:a0753c2a4497 83 case 2: // CRC error -> 'temp' is not updated
andrewklmn 38:a0753c2a4497 84 pc.printf("%s sensor CRC error \r\n", labels[j].c_str() );
andrewklmn 38:a0753c2a4497 85 };
andrewklmn 38:a0753c2a4497 86 // start temperature conversion from analog to digital
andrewklmn 38:a0753c2a4497 87 ds1820[j].startConversion();
andrewklmn 38:a0753c2a4497 88 };
andrewklmn 38:a0753c2a4497 89
andrewklmn 38:a0753c2a4497 90 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 38:a0753c2a4497 91 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 38:a0753c2a4497 92 pc.printf("RAM_config=%X, FLASH_config=%X\r\n", eeprom_config_value ,readEEPROMWord(0));
andrewklmn 38:a0753c2a4497 93 pc.printf("=======================================");
andrewklmn 38:a0753c2a4497 94
andrewklmn 38:a0753c2a4497 95
andrewklmn 38:a0753c2a4497 96 // save new config if it was changed
andrewklmn 38:a0753c2a4497 97 if (readEEPROMWord(0)!= eeprom_config_value) {
andrewklmn 38:a0753c2a4497 98 enableEEPROMWriting();
andrewklmn 38:a0753c2a4497 99 writeEEPROMWord(0,eeprom_config_value);
andrewklmn 38:a0753c2a4497 100 disableEEPROMWriting();
andrewklmn 38:a0753c2a4497 101 };
andrewklmn 38:a0753c2a4497 102
andrewklmn 38:a0753c2a4497 103
andrewklmn 38:a0753c2a4497 104
andrewklmn 38:a0753c2a4497 105 __enable_irq();
andrewklmn 38:a0753c2a4497 106
andrewklmn 38:a0753c2a4497 107 myled = 1; // turn the LED off
andrewklmn 38:a0753c2a4497 108 };
andrewklmn 38:a0753c2a4497 109
andrewklmn 38:a0753c2a4497 110 unsigned int get_temp_config_value(){
andrewklmn 38:a0753c2a4497 111
andrewklmn 38:a0753c2a4497 112 unsigned int v = readEEPROMWord(0);
andrewklmn 38:a0753c2a4497 113 if ( v == 0xFFFFFFFF ) {
andrewklmn 38:a0753c2a4497 114 // if eeprom config value is not initialized
andrewklmn 38:a0753c2a4497 115 // set default value 0x0F0A0A23;
andrewklmn 38:a0753c2a4497 116 v = MIN_MEBEL_TEMP;
andrewklmn 38:a0753c2a4497 117 v = ( v << 8 ) | MIN_LITOS_TEMP;
andrewklmn 38:a0753c2a4497 118 v = ( v << 8 ) | MIN_BACK_WATER_TEMP;
andrewklmn 38:a0753c2a4497 119 v = ( v << 8 ) | MAX_HOT_WATER_TEMP;
andrewklmn 38:a0753c2a4497 120 };
andrewklmn 38:a0753c2a4497 121 return v;
andrewklmn 38:a0753c2a4497 122 };