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)
main.cpp@24:4c5909becda6, 2018-09-15 (annotated)
- Committer:
- andrewklmn
- Date:
- Sat Sep 15 19:43:38 2018 +0000
- Revision:
- 24:4c5909becda6
- Parent:
- 23:33d6689834c6
- Child:
- 25:49e5d653d789
Interrupt In
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 5:3c3ef17a17a6 | 1 | #include "stm32f103c8t6.h" |
hudakz | 0:ab218237069e | 2 | #include "mbed.h" |
andrewklmn | 17:718e41e971f8 | 3 | #include "DS1820.h" |
andrewklmn | 19:74f78a777ffa | 4 | #include <string> |
andrewklmn | 12:187e02ab447f | 5 | |
andrewklmn | 12:187e02ab447f | 6 | char a[128] = ""; // RX command buffer |
andrewklmn | 12:187e02ab447f | 7 | char i = 0; // RX char pointer |
andrewklmn | 14:d954558361b9 | 8 | static char recieved = 0; |
andrewklmn | 12:187e02ab447f | 9 | |
andrewklmn | 17:718e41e971f8 | 10 | Serial pc(PA_2, PA_3); |
andrewklmn | 23:33d6689834c6 | 11 | DigitalOut myled(LED1); |
andrewklmn | 24:4c5909becda6 | 12 | InterruptIn pb(PA_11); |
andrewklmn | 17:718e41e971f8 | 13 | |
andrewklmn | 12:187e02ab447f | 14 | // This function is called when a character goes into the RX buffer. |
andrewklmn | 12:187e02ab447f | 15 | void rxCallback() { |
andrewklmn | 12:187e02ab447f | 16 | char c; |
andrewklmn | 14:d954558361b9 | 17 | c = pc.getc(); |
andrewklmn | 12:187e02ab447f | 18 | if (recieved == 0){ // skip if command was already received |
andrewklmn | 12:187e02ab447f | 19 | if ( c == 0x0d || c == 0x0a ) { |
andrewklmn | 12:187e02ab447f | 20 | recieved = 1; |
andrewklmn | 12:187e02ab447f | 21 | } else { |
andrewklmn | 12:187e02ab447f | 22 | if (i==16){ // max length of command from SERIAL |
andrewklmn | 12:187e02ab447f | 23 | a[0] = c; |
andrewklmn | 12:187e02ab447f | 24 | a[1] = '\0'; |
andrewklmn | 12:187e02ab447f | 25 | i = 0; |
andrewklmn | 12:187e02ab447f | 26 | } else { |
andrewklmn | 12:187e02ab447f | 27 | a[i] = c; |
andrewklmn | 12:187e02ab447f | 28 | i++; |
andrewklmn | 12:187e02ab447f | 29 | a[i] = '\0'; |
andrewklmn | 12:187e02ab447f | 30 | }; |
andrewklmn | 14:d954558361b9 | 31 | }; |
andrewklmn | 12:187e02ab447f | 32 | }; |
andrewklmn | 12:187e02ab447f | 33 | }; |
andrewklmn | 12:187e02ab447f | 34 | |
andrewklmn | 23:33d6689834c6 | 35 | |
andrewklmn | 23:33d6689834c6 | 36 | void pb_hit_interrupt (void) { |
andrewklmn | 23:33d6689834c6 | 37 | pc.printf("Button pressed\r\n"); |
andrewklmn | 23:33d6689834c6 | 38 | }; |
hudakz | 10:4b88be251088 | 39 | |
hudakz | 0:ab218237069e | 40 | int main() { |
andrewklmn | 12:187e02ab447f | 41 | |
hudakz | 10:4b88be251088 | 42 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
andrewklmn | 22:32beca745706 | 43 | |
andrewklmn | 23:33d6689834c6 | 44 | pb.mode(PullUp); |
andrewklmn | 23:33d6689834c6 | 45 | pb.fall(&pb_hit_interrupt); |
andrewklmn | 22:32beca745706 | 46 | |
andrewklmn | 22:32beca745706 | 47 | float temp[5] = { |
andrewklmn | 22:32beca745706 | 48 | 85,85,85,85,85 // initial temperature |
andrewklmn | 22:32beca745706 | 49 | }; |
andrewklmn | 22:32beca745706 | 50 | |
andrewklmn | 22:32beca745706 | 51 | int error[5] = { |
andrewklmn | 22:32beca745706 | 52 | 0,0,0,0,0 // initial state |
andrewklmn | 22:32beca745706 | 53 | }; |
andrewklmn | 22:32beca745706 | 54 | |
andrewklmn | 19:74f78a777ffa | 55 | string labels[5] = { |
andrewklmn | 19:74f78a777ffa | 56 | "OUTDOOR", |
andrewklmn | 19:74f78a777ffa | 57 | "LITOS", |
andrewklmn | 19:74f78a777ffa | 58 | "MEBEL", |
andrewklmn | 19:74f78a777ffa | 59 | "HOT WATER", |
andrewklmn | 19:74f78a777ffa | 60 | "BACK WATER" |
andrewklmn | 19:74f78a777ffa | 61 | }; |
hudakz | 10:4b88be251088 | 62 | |
andrewklmn | 19:74f78a777ffa | 63 | DS1820 ds1820[5] = { |
andrewklmn | 19:74f78a777ffa | 64 | DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR |
andrewklmn | 19:74f78a777ffa | 65 | DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS |
andrewklmn | 19:74f78a777ffa | 66 | DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL |
andrewklmn | 19:74f78a777ffa | 67 | DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER |
andrewklmn | 19:74f78a777ffa | 68 | DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER |
andrewklmn | 19:74f78a777ffa | 69 | }; |
andrewklmn | 19:74f78a777ffa | 70 | |
andrewklmn | 12:187e02ab447f | 71 | pc.attach(&rxCallback, Serial::RxIrq); |
andrewklmn | 15:721546347507 | 72 | pc.baud(115200); |
andrewklmn | 21:819277430a55 | 73 | pc.printf("Wifi Heating system\r\n"); |
andrewklmn | 19:74f78a777ffa | 74 | |
andrewklmn | 19:74f78a777ffa | 75 | for ( int j=0; j < 5; j++ ) { |
andrewklmn | 19:74f78a777ffa | 76 | if(ds1820[j].begin()) { |
andrewklmn | 19:74f78a777ffa | 77 | pc.printf("%s sensor present!\r\n", labels[j].c_str()); |
andrewklmn | 19:74f78a777ffa | 78 | } else { |
andrewklmn | 20:4f3a45fd4bbb | 79 | //pc.printf("No %s sensor found!\r\n", labels[j].c_str()); |
andrewklmn | 19:74f78a777ffa | 80 | }; |
andrewklmn | 17:718e41e971f8 | 81 | }; |
andrewklmn | 17:718e41e971f8 | 82 | |
hudakz | 0:ab218237069e | 83 | while(1) { |
hudakz | 5:3c3ef17a17a6 | 84 | // The on-board LED is connected, via a resistor, to +3.3V (not to GND). |
hudakz | 3:c6a589f444b9 | 85 | // So to turn the LED on or off we have to set it to 0 or 1 respectively |
hudakz | 7:accb2c83a007 | 86 | myled = 0; // turn the LED on |
andrewklmn | 12:187e02ab447f | 87 | wait_ms(50); // 200 millisecond |
andrewklmn | 12:187e02ab447f | 88 | myled = 1; // turn the LED off |
andrewklmn | 12:187e02ab447f | 89 | wait_ms(50); // 1000 millisecond |
andrewklmn | 12:187e02ab447f | 90 | myled = 0; // turn the LED on |
andrewklmn | 12:187e02ab447f | 91 | wait_ms(50); // 200 millisecond |
andrewklmn | 12:187e02ab447f | 92 | myled = 1; // turn the LED off |
andrewklmn | 12:187e02ab447f | 93 | wait_ms(50); // 1000 millisecond |
andrewklmn | 12:187e02ab447f | 94 | myled = 0; // turn the LED on |
andrewklmn | 12:187e02ab447f | 95 | wait_ms(50); // 200 millisecond |
hudakz | 7:accb2c83a007 | 96 | myled = 1; // turn the LED off |
andrewklmn | 17:718e41e971f8 | 97 | |
andrewklmn | 17:718e41e971f8 | 98 | |
andrewklmn | 19:74f78a777ffa | 99 | // start temperature conversion from analog to digital |
andrewklmn | 19:74f78a777ffa | 100 | for ( int j=0; j < 5; j++ ) { |
andrewklmn | 19:74f78a777ffa | 101 | ds1820[j].startConversion(); |
andrewklmn | 19:74f78a777ffa | 102 | }; |
andrewklmn | 19:74f78a777ffa | 103 | |
andrewklmn | 17:718e41e971f8 | 104 | |
andrewklmn | 21:819277430a55 | 105 | wait(1.0); // let DS1820 complete the temperature conversion |
andrewklmn | 22:32beca745706 | 106 | |
andrewklmn | 12:187e02ab447f | 107 | if (recieved == 1) { |
andrewklmn | 14:d954558361b9 | 108 | |
andrewklmn | 12:187e02ab447f | 109 | // command was recieved |
andrewklmn | 12:187e02ab447f | 110 | pc.printf(a); |
andrewklmn | 22:32beca745706 | 111 | pc.printf("\r\n"); |
andrewklmn | 22:32beca745706 | 112 | |
andrewklmn | 22:32beca745706 | 113 | // process_command(a); |
andrewklmn | 12:187e02ab447f | 114 | |
andrewklmn | 12:187e02ab447f | 115 | // ready for new command |
andrewklmn | 14:d954558361b9 | 116 | recieved = 0; |
andrewklmn | 12:187e02ab447f | 117 | a[0] = '\0'; |
andrewklmn | 12:187e02ab447f | 118 | i = 0; |
andrewklmn | 14:d954558361b9 | 119 | }; |
andrewklmn | 22:32beca745706 | 120 | |
andrewklmn | 22:32beca745706 | 121 | |
andrewklmn | 22:32beca745706 | 122 | |
andrewklmn | 22:32beca745706 | 123 | for ( int j=0; j < 5; j++ ) { |
andrewklmn | 14:d954558361b9 | 124 | |
andrewklmn | 22:32beca745706 | 125 | error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
andrewklmn | 22:32beca745706 | 126 | |
andrewklmn | 22:32beca745706 | 127 | switch(error[j]) { |
andrewklmn | 22:32beca745706 | 128 | case 0: // no errors -> 'temp' contains the value of measured temperature |
andrewklmn | 22:32beca745706 | 129 | pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]); |
andrewklmn | 22:32beca745706 | 130 | break; |
andrewklmn | 22:32beca745706 | 131 | case 1: // no sensor present -> 'temp' is not updated |
andrewklmn | 22:32beca745706 | 132 | //pc.printf("no %s sensor present \r\n", labels[j].c_str() ); |
andrewklmn | 22:32beca745706 | 133 | break; |
andrewklmn | 22:32beca745706 | 134 | case 2: // CRC error -> 'temp' is not updated |
andrewklmn | 22:32beca745706 | 135 | pc.printf("%s sensor CRC error \r\n", labels[j].c_str() ); |
andrewklmn | 22:32beca745706 | 136 | }; |
andrewklmn | 22:32beca745706 | 137 | |
andrewklmn | 22:32beca745706 | 138 | }; |
andrewklmn | 22:32beca745706 | 139 | |
andrewklmn | 22:32beca745706 | 140 | pc.printf("State %d|%d|%d|%d|%d\r\n", error[0], error[1], error[2], error[3], error[4] ); |
andrewklmn | 22:32beca745706 | 141 | 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 | 22:32beca745706 | 142 | pc.printf("======================================="); |
andrewklmn | 22:32beca745706 | 143 | |
andrewklmn | 22:32beca745706 | 144 | }; |
andrewklmn | 22:32beca745706 | 145 | }; |
hudakz | 8:f1432e9af6c8 | 146 |