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:
Mon Sep 17 19:52:45 2018 +0000
Revision:
34:09d8edcce624
Parent:
33:2c54746f7518
Child:
35:74a546766adb
eeprom flash added

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 27:007e17df5ba0 3 #include "mbed_events.h"
andrewklmn 27:007e17df5ba0 4 #include <stdio.h>
andrewklmn 17:718e41e971f8 5 #include "DS1820.h"
andrewklmn 26:750f21025bb9 6 #include "PinDetect.h"
andrewklmn 31:0a516ceaec4d 7 #include "Watchdog.h"
andrewklmn 19:74f78a777ffa 8 #include <string>
andrewklmn 12:187e02ab447f 9
andrewklmn 27:007e17df5ba0 10 // Create a queue that can hold a maximum of 32 events
andrewklmn 27:007e17df5ba0 11 EventQueue queue(32 * EVENTS_EVENT_SIZE);
andrewklmn 27:007e17df5ba0 12 // Create a thread that'll run the event queue's dispatch function
andrewklmn 27:007e17df5ba0 13 //Thread t;
andrewklmn 27:007e17df5ba0 14
andrewklmn 32:aebbfe391bbc 15 Watchdog wd;
andrewklmn 27:007e17df5ba0 16
andrewklmn 12:187e02ab447f 17 char a[128] = ""; // RX command buffer
andrewklmn 12:187e02ab447f 18 char i = 0; // RX char pointer
andrewklmn 14:d954558361b9 19 static char recieved = 0;
andrewklmn 12:187e02ab447f 20
andrewklmn 17:718e41e971f8 21 Serial pc(PA_2, PA_3);
andrewklmn 23:33d6689834c6 22 DigitalOut myled(LED1);
andrewklmn 17:718e41e971f8 23
andrewklmn 34:09d8edcce624 24 DigitalOut pomp_OFF(PA_12); // pomp off
andrewklmn 34:09d8edcce624 25 DigitalOut heater_OFF(PA_15); // heater off
andrewklmn 34:09d8edcce624 26
andrewklmn 34:09d8edcce624 27
andrewklmn 27:007e17df5ba0 28 float temp[5] = {
andrewklmn 27:007e17df5ba0 29 85,85,85,85,85 // initial temperature
andrewklmn 27:007e17df5ba0 30 };
andrewklmn 27:007e17df5ba0 31
andrewklmn 27:007e17df5ba0 32 int temp_error[5] = {
andrewklmn 27:007e17df5ba0 33 0,0,0,0,0 // initial state
andrewklmn 27:007e17df5ba0 34 };
andrewklmn 27:007e17df5ba0 35
andrewklmn 27:007e17df5ba0 36 string labels[5] = {
andrewklmn 27:007e17df5ba0 37 "OUTDOOR",
andrewklmn 27:007e17df5ba0 38 "LITOS",
andrewklmn 27:007e17df5ba0 39 "MEBEL",
andrewklmn 27:007e17df5ba0 40 "HOT WATER",
andrewklmn 27:007e17df5ba0 41 "BACK WATER"
andrewklmn 27:007e17df5ba0 42 };
andrewklmn 27:007e17df5ba0 43
andrewklmn 27:007e17df5ba0 44 DS1820 ds1820[5] = {
andrewklmn 27:007e17df5ba0 45 DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
andrewklmn 27:007e17df5ba0 46 DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS
andrewklmn 27:007e17df5ba0 47 DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
andrewklmn 27:007e17df5ba0 48 DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 27:007e17df5ba0 49 DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 27:007e17df5ba0 50 };
andrewklmn 27:007e17df5ba0 51
andrewklmn 27:007e17df5ba0 52
andrewklmn 29:5940a80717e4 53
andrewklmn 12:187e02ab447f 54 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 55 void rxCallback() {
andrewklmn 12:187e02ab447f 56 char c;
andrewklmn 14:d954558361b9 57 c = pc.getc();
andrewklmn 12:187e02ab447f 58 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 59 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 29:5940a80717e4 60 while ( pc.readable() ) c = pc.getc();
andrewklmn 12:187e02ab447f 61 recieved = 1;
andrewklmn 12:187e02ab447f 62 } else {
andrewklmn 12:187e02ab447f 63 if (i==16){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 64 a[0] = c;
andrewklmn 12:187e02ab447f 65 a[1] = '\0';
andrewklmn 12:187e02ab447f 66 i = 0;
andrewklmn 12:187e02ab447f 67 } else {
andrewklmn 12:187e02ab447f 68 a[i] = c;
andrewklmn 12:187e02ab447f 69 i++;
andrewklmn 12:187e02ab447f 70 a[i] = '\0';
andrewklmn 12:187e02ab447f 71 };
andrewklmn 14:d954558361b9 72 };
andrewklmn 12:187e02ab447f 73 };
andrewklmn 12:187e02ab447f 74 };
andrewklmn 12:187e02ab447f 75
andrewklmn 23:33d6689834c6 76
andrewklmn 23:33d6689834c6 77 void pb_hit_interrupt (void) {
andrewklmn 27:007e17df5ba0 78 pc.printf("Button pressed\r\n");
andrewklmn 23:33d6689834c6 79 };
andrewklmn 25:49e5d653d789 80
andrewklmn 25:49e5d653d789 81 void pb_out_interrupt (void) {
andrewklmn 27:007e17df5ba0 82 pc.printf("Button unpressed\r\n");
andrewklmn 25:49e5d653d789 83 };
andrewklmn 27:007e17df5ba0 84
andrewklmn 27:007e17df5ba0 85 void start_temp(){
andrewklmn 29:5940a80717e4 86
andrewklmn 29:5940a80717e4 87 __disable_irq();
andrewklmn 29:5940a80717e4 88
andrewklmn 27:007e17df5ba0 89 for ( int j=0; j < 5; j++ ) {
andrewklmn 27:007e17df5ba0 90 if(ds1820[j].begin()) {
andrewklmn 27:007e17df5ba0 91 ds1820[j].startConversion();
andrewklmn 27:007e17df5ba0 92 pc.printf("%s sensor present!\r\n", labels[j].c_str());
andrewklmn 27:007e17df5ba0 93 } else {
andrewklmn 27:007e17df5ba0 94 pc.printf("No %s sensor found!\r\n", labels[j].c_str());
andrewklmn 27:007e17df5ba0 95 };
andrewklmn 27:007e17df5ba0 96 };
andrewklmn 29:5940a80717e4 97 __enable_irq();
andrewklmn 29:5940a80717e4 98
andrewklmn 27:007e17df5ba0 99 };
andrewklmn 27:007e17df5ba0 100
andrewklmn 27:007e17df5ba0 101 void at_command(){
andrewklmn 27:007e17df5ba0 102 if (recieved == 1) {
andrewklmn 27:007e17df5ba0 103
andrewklmn 32:aebbfe391bbc 104 __disable_irq();
andrewklmn 32:aebbfe391bbc 105
andrewklmn 27:007e17df5ba0 106 // command was recieved
andrewklmn 32:aebbfe391bbc 107
andrewklmn 32:aebbfe391bbc 108 if (a[0]=='A' && a[1]=='T') {
andrewklmn 32:aebbfe391bbc 109
andrewklmn 32:aebbfe391bbc 110 if ( a[2]=='\0') {
andrewklmn 32:aebbfe391bbc 111 //pc.printf(a);
andrewklmn 32:aebbfe391bbc 112 pc.printf("OK\r\n");
andrewklmn 33:2c54746f7518 113 } else {
andrewklmn 33:2c54746f7518 114 pc.printf("Wrong AT command\r\n");
andrewklmn 32:aebbfe391bbc 115 };
andrewklmn 32:aebbfe391bbc 116 } else {
andrewklmn 32:aebbfe391bbc 117 pc.printf("Wrong request\r\n");
andrewklmn 32:aebbfe391bbc 118 };
andrewklmn 27:007e17df5ba0 119
andrewklmn 27:007e17df5ba0 120 // process_command(a);
andrewklmn 27:007e17df5ba0 121
andrewklmn 32:aebbfe391bbc 122 __enable_irq();
andrewklmn 32:aebbfe391bbc 123
andrewklmn 27:007e17df5ba0 124 // ready for new command
andrewklmn 27:007e17df5ba0 125 recieved = 0;
andrewklmn 27:007e17df5ba0 126 a[0] = '\0';
andrewklmn 27:007e17df5ba0 127 i = 0;
andrewklmn 27:007e17df5ba0 128 };
andrewklmn 27:007e17df5ba0 129 };
andrewklmn 27:007e17df5ba0 130
andrewklmn 27:007e17df5ba0 131
andrewklmn 27:007e17df5ba0 132 void check_temp(){
andrewklmn 27:007e17df5ba0 133
andrewklmn 27:007e17df5ba0 134 myled = 0; // turn the LED on
andrewklmn 27:007e17df5ba0 135
andrewklmn 32:aebbfe391bbc 136 // kick the watchdog
andrewklmn 32:aebbfe391bbc 137 wd.Service();
andrewklmn 29:5940a80717e4 138
andrewklmn 29:5940a80717e4 139 __disable_irq();
andrewklmn 29:5940a80717e4 140
andrewklmn 27:007e17df5ba0 141 for ( int j=0; j < 5; j++ ) {
andrewklmn 27:007e17df5ba0 142
andrewklmn 27:007e17df5ba0 143 temp_error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 27:007e17df5ba0 144
andrewklmn 27:007e17df5ba0 145 switch(temp_error[j]) {
andrewklmn 27:007e17df5ba0 146 case 0: // no errors -> 'temp' contains the value of measured temperature
andrewklmn 27:007e17df5ba0 147 pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]);
andrewklmn 27:007e17df5ba0 148 break;
andrewklmn 27:007e17df5ba0 149 case 1: // no sensor present -> 'temp' is not updated
andrewklmn 27:007e17df5ba0 150 pc.printf("no %s sensor present \r\n", labels[j].c_str() );
andrewklmn 27:007e17df5ba0 151 break;
andrewklmn 27:007e17df5ba0 152 case 2: // CRC error -> 'temp' is not updated
andrewklmn 27:007e17df5ba0 153 pc.printf("%s sensor CRC error \r\n", labels[j].c_str() );
andrewklmn 27:007e17df5ba0 154 };
andrewklmn 27:007e17df5ba0 155 // start temperature conversion from analog to digital
andrewklmn 27:007e17df5ba0 156 ds1820[j].startConversion();
andrewklmn 27:007e17df5ba0 157 };
andrewklmn 29:5940a80717e4 158
andrewklmn 27:007e17df5ba0 159 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 27:007e17df5ba0 160 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 27:007e17df5ba0 161 pc.printf("=======================================");
andrewklmn 29:5940a80717e4 162
andrewklmn 29:5940a80717e4 163 __enable_irq();
andrewklmn 29:5940a80717e4 164
andrewklmn 27:007e17df5ba0 165 myled = 1; // turn the LED off
andrewklmn 27:007e17df5ba0 166 };
andrewklmn 27:007e17df5ba0 167
andrewklmn 31:0a516ceaec4d 168
andrewklmn 27:007e17df5ba0 169
hudakz 10:4b88be251088 170
hudakz 0:ab218237069e 171 int main() {
andrewklmn 12:187e02ab447f 172
hudakz 10:4b88be251088 173 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 26:750f21025bb9 174
andrewklmn 32:aebbfe391bbc 175 wd.Configure(5.0);
andrewklmn 32:aebbfe391bbc 176
andrewklmn 26:750f21025bb9 177 PinDetect pb(PA_11);
andrewklmn 23:33d6689834c6 178 pb.mode(PullUp);
andrewklmn 26:750f21025bb9 179
andrewklmn 26:750f21025bb9 180 // Delay for initial pullup to take effect
andrewklmn 26:750f21025bb9 181 wait(.005);
andrewklmn 26:750f21025bb9 182
andrewklmn 26:750f21025bb9 183 pb.attach_deasserted(&pb_hit_interrupt);
andrewklmn 26:750f21025bb9 184 pb.attach_asserted(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 185 //pb.rise(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 186 pb.setSampleFrequency();
andrewklmn 22:32beca745706 187
andrewklmn 12:187e02ab447f 188 pc.attach(&rxCallback, Serial::RxIrq);
andrewklmn 15:721546347507 189 pc.baud(115200);
andrewklmn 32:aebbfe391bbc 190 pc.printf("\r\n\r\nWifi Heating system\r\n");
andrewklmn 19:74f78a777ffa 191
andrewklmn 27:007e17df5ba0 192
andrewklmn 34:09d8edcce624 193 pomp_OFF = 0;
andrewklmn 34:09d8edcce624 194 heater_OFF = 0;
andrewklmn 34:09d8edcce624 195 wait(0.25);
andrewklmn 34:09d8edcce624 196 pomp_OFF = 1;
andrewklmn 34:09d8edcce624 197 heater_OFF = 1;
andrewklmn 34:09d8edcce624 198 wait(0.25);
andrewklmn 34:09d8edcce624 199 pomp_OFF = 0;
andrewklmn 34:09d8edcce624 200 heater_OFF = 0;
andrewklmn 34:09d8edcce624 201 wait(0.25);
andrewklmn 34:09d8edcce624 202 pomp_OFF = 1;
andrewklmn 34:09d8edcce624 203 heater_OFF = 1;
andrewklmn 34:09d8edcce624 204 wait(0.25);
andrewklmn 34:09d8edcce624 205
andrewklmn 17:718e41e971f8 206
andrewklmn 27:007e17df5ba0 207 queue.call( start_temp );
andrewklmn 27:007e17df5ba0 208 queue.call_every(100, at_command);
andrewklmn 27:007e17df5ba0 209 queue.call_every(1000, check_temp);
andrewklmn 27:007e17df5ba0 210
andrewklmn 27:007e17df5ba0 211 // Start queue
andrewklmn 27:007e17df5ba0 212 queue.dispatch();
andrewklmn 22:32beca745706 213 };
hudakz 8:f1432e9af6c8 214