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 20:15:38 2018 +0000
Revision:
35:74a546766adb
Parent:
34:09d8edcce624
Child:
37:c9b96afec535
epprom_flash updated

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