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:
Wed Sep 12 19:32:17 2018 +0000
Revision:
19:74f78a777ffa
Parent:
18:9434e06223c7
Child:
20:4f3a45fd4bbb
arrays

Who changed what in which revision?

UserRevisionLine numberNew 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 17:718e41e971f8 11
andrewklmn 12:187e02ab447f 12 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 13 void rxCallback() {
andrewklmn 12:187e02ab447f 14 char c;
andrewklmn 14:d954558361b9 15 c = pc.getc();
andrewklmn 12:187e02ab447f 16 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 17 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 12:187e02ab447f 18 recieved = 1;
andrewklmn 12:187e02ab447f 19 } else {
andrewklmn 12:187e02ab447f 20 if (i==16){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 21 a[0] = c;
andrewklmn 12:187e02ab447f 22 a[1] = '\0';
andrewklmn 12:187e02ab447f 23 i = 0;
andrewklmn 12:187e02ab447f 24 } else {
andrewklmn 12:187e02ab447f 25 a[i] = c;
andrewklmn 12:187e02ab447f 26 i++;
andrewklmn 12:187e02ab447f 27 a[i] = '\0';
andrewklmn 12:187e02ab447f 28 };
andrewklmn 14:d954558361b9 29 };
andrewklmn 12:187e02ab447f 30 };
andrewklmn 12:187e02ab447f 31 };
andrewklmn 12:187e02ab447f 32
hudakz 10:4b88be251088 33
hudakz 0:ab218237069e 34 int main() {
andrewklmn 12:187e02ab447f 35
hudakz 10:4b88be251088 36 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 12:187e02ab447f 37 DigitalOut myled(LED1);
andrewklmn 19:74f78a777ffa 38
andrewklmn 19:74f78a777ffa 39 string labels[5] = {
andrewklmn 19:74f78a777ffa 40 "OUTDOOR",
andrewklmn 19:74f78a777ffa 41 "LITOS",
andrewklmn 19:74f78a777ffa 42 "MEBEL",
andrewklmn 19:74f78a777ffa 43 "HOT WATER",
andrewklmn 19:74f78a777ffa 44 "BACK WATER"
andrewklmn 19:74f78a777ffa 45 };
hudakz 10:4b88be251088 46
andrewklmn 19:74f78a777ffa 47 DS1820 ds1820[5] = {
andrewklmn 19:74f78a777ffa 48 DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
andrewklmn 19:74f78a777ffa 49 DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS
andrewklmn 19:74f78a777ffa 50 DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
andrewklmn 19:74f78a777ffa 51 DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 19:74f78a777ffa 52 DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 19:74f78a777ffa 53 };
andrewklmn 19:74f78a777ffa 54
andrewklmn 17:718e41e971f8 55 float temp = 0;
andrewklmn 17:718e41e971f8 56 int error = 0;
andrewklmn 17:718e41e971f8 57
andrewklmn 12:187e02ab447f 58 pc.attach(&rxCallback, Serial::RxIrq);
andrewklmn 15:721546347507 59 pc.baud(115200);
andrewklmn 19:74f78a777ffa 60 pc.printf("Hello world! Mbed version\r\n");
andrewklmn 19:74f78a777ffa 61
andrewklmn 19:74f78a777ffa 62 for ( int j=0; j < 5; j++ ) {
andrewklmn 19:74f78a777ffa 63 if(ds1820[j].begin()) {
andrewklmn 19:74f78a777ffa 64 pc.printf("%s sensor present!\r\n", labels[j].c_str());
andrewklmn 19:74f78a777ffa 65 } else {
andrewklmn 19:74f78a777ffa 66 pc.printf("No %s sensor found!\r\n", labels[j].c_str());
andrewklmn 19:74f78a777ffa 67 };
andrewklmn 17:718e41e971f8 68 };
andrewklmn 17:718e41e971f8 69
hudakz 0:ab218237069e 70 while(1) {
hudakz 5:3c3ef17a17a6 71 // The on-board LED is connected, via a resistor, to +3.3V (not to GND).
hudakz 3:c6a589f444b9 72 // So to turn the LED on or off we have to set it to 0 or 1 respectively
hudakz 7:accb2c83a007 73 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 74 wait_ms(50); // 200 millisecond
andrewklmn 12:187e02ab447f 75 myled = 1; // turn the LED off
andrewklmn 12:187e02ab447f 76 wait_ms(50); // 1000 millisecond
andrewklmn 12:187e02ab447f 77 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 78 wait_ms(50); // 200 millisecond
andrewklmn 12:187e02ab447f 79 myled = 1; // turn the LED off
andrewklmn 12:187e02ab447f 80 wait_ms(50); // 1000 millisecond
andrewklmn 12:187e02ab447f 81 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 82 wait_ms(50); // 200 millisecond
hudakz 7:accb2c83a007 83 myled = 1; // turn the LED off
andrewklmn 17:718e41e971f8 84
andrewklmn 17:718e41e971f8 85
andrewklmn 19:74f78a777ffa 86 // start temperature conversion from analog to digital
andrewklmn 19:74f78a777ffa 87 for ( int j=0; j < 5; j++ ) {
andrewklmn 19:74f78a777ffa 88 ds1820[j].startConversion();
andrewklmn 19:74f78a777ffa 89 };
andrewklmn 19:74f78a777ffa 90
andrewklmn 17:718e41e971f8 91
andrewklmn 19:74f78a777ffa 92 wait(0.750); // let DS1820 complete the temperature conversion
andrewklmn 19:74f78a777ffa 93
andrewklmn 19:74f78a777ffa 94 for ( int j=0; j < 5; j++ ) {
andrewklmn 19:74f78a777ffa 95 error = ds1820[j].read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 17:718e41e971f8 96 switch(error) {
andrewklmn 17:718e41e971f8 97 case 0: // no errors -> 'temp' contains the value of measured temperature
andrewklmn 19:74f78a777ffa 98 pc.printf("temp%d = %3.1fC \r\n", j, temp);
andrewklmn 17:718e41e971f8 99 break;
andrewklmn 17:718e41e971f8 100 case 1: // no sensor present -> 'temp' is not updated
andrewklmn 19:74f78a777ffa 101 pc.printf("no sensor%d present \r\n", j);
andrewklmn 17:718e41e971f8 102 break;
andrewklmn 17:718e41e971f8 103 case 2: // CRC error -> 'temp' is not updated
andrewklmn 19:74f78a777ffa 104 pc.printf("sensor%d CRC error \r\n", j);
andrewklmn 18:9434e06223c7 105 };
andrewklmn 19:74f78a777ffa 106 };
andrewklmn 18:9434e06223c7 107
andrewklmn 17:718e41e971f8 108
andrewklmn 17:718e41e971f8 109
andrewklmn 17:718e41e971f8 110
andrewklmn 12:187e02ab447f 111 if (recieved == 1) {
andrewklmn 14:d954558361b9 112
andrewklmn 12:187e02ab447f 113 // command was recieved
andrewklmn 12:187e02ab447f 114 pc.printf(a);
andrewklmn 12:187e02ab447f 115
andrewklmn 12:187e02ab447f 116 // ready for new command
andrewklmn 14:d954558361b9 117 recieved = 0;
andrewklmn 12:187e02ab447f 118 a[0] = '\0';
andrewklmn 12:187e02ab447f 119 i = 0;
andrewklmn 14:d954558361b9 120 };
andrewklmn 14:d954558361b9 121
hudakz 0:ab218237069e 122 }
hudakz 0:ab218237069e 123 }
hudakz 8:f1432e9af6c8 124