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