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:
Sun Oct 07 08:36:56 2018 +0000
Revision:
57:9bf4c8142958
Parent:
56:522ad08941bb
Child:
58:70930f201f97
production release

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 17:718e41e971f8 4 #include "DS1820.h"
andrewklmn 26:750f21025bb9 5 #include "PinDetect.h"
andrewklmn 31:0a516ceaec4d 6 #include "Watchdog.h"
andrewklmn 35:74a546766adb 7 #include "eeprom_flash.h"
andrewklmn 19:74f78a777ffa 8 #include <string>
andrewklmn 38:a0753c2a4497 9 #include "temp_controller.h"
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 38:a0753c2a4497 17 unsigned int eeprom_config_value;
andrewklmn 41:978c2d85a0e7 18 extern char working_mode;
andrewklmn 42:03cbc2a6f63b 19 extern int temp_error[];
andrewklmn 42:03cbc2a6f63b 20 extern float temp[];
andrewklmn 43:cc47a2e6ae07 21 extern float simulated_temp[];
andrewklmn 54:61ccc1036944 22 extern int error_count;
andrewklmn 27:007e17df5ba0 23
andrewklmn 39:aa5f95061409 24 Serial pc(PA_2, PA_3); // (TX, RX) Pin definition
andrewklmn 12:187e02ab447f 25 char a[128] = ""; // RX command buffer
andrewklmn 12:187e02ab447f 26 char i = 0; // RX char pointer
andrewklmn 14:d954558361b9 27 static char recieved = 0;
andrewklmn 12:187e02ab447f 28
andrewklmn 38:a0753c2a4497 29
andrewklmn 23:33d6689834c6 30 DigitalOut myled(LED1);
andrewklmn 17:718e41e971f8 31
andrewklmn 34:09d8edcce624 32 DigitalOut pomp_OFF(PA_12); // pomp off
andrewklmn 34:09d8edcce624 33 DigitalOut heater_OFF(PA_15); // heater off
andrewklmn 34:09d8edcce624 34
andrewklmn 56:522ad08941bb 35 DigitalOut reset_wifi(PA_4); // reset WIFI
andrewklmn 56:522ad08941bb 36
andrewklmn 12:187e02ab447f 37 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 38 void rxCallback() {
andrewklmn 12:187e02ab447f 39 char c;
andrewklmn 14:d954558361b9 40 c = pc.getc();
andrewklmn 12:187e02ab447f 41 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 42 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 29:5940a80717e4 43 while ( pc.readable() ) c = pc.getc();
andrewklmn 12:187e02ab447f 44 recieved = 1;
andrewklmn 12:187e02ab447f 45 } else {
andrewklmn 43:cc47a2e6ae07 46 if (i==127){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 47 a[0] = c;
andrewklmn 12:187e02ab447f 48 a[1] = '\0';
andrewklmn 12:187e02ab447f 49 i = 0;
andrewklmn 12:187e02ab447f 50 } else {
andrewklmn 12:187e02ab447f 51 a[i] = c;
andrewklmn 12:187e02ab447f 52 i++;
andrewklmn 12:187e02ab447f 53 a[i] = '\0';
andrewklmn 12:187e02ab447f 54 };
andrewklmn 14:d954558361b9 55 };
andrewklmn 12:187e02ab447f 56 };
andrewklmn 12:187e02ab447f 57 };
andrewklmn 12:187e02ab447f 58
andrewklmn 48:4ec055c066b4 59 void blink_myled(char amount) {
andrewklmn 48:4ec055c066b4 60 while (amount > 0) {
andrewklmn 48:4ec055c066b4 61 myled = 0;
andrewklmn 48:4ec055c066b4 62 wait(0.1);
andrewklmn 48:4ec055c066b4 63 myled = 1;
andrewklmn 48:4ec055c066b4 64 wait(0.2);
andrewklmn 48:4ec055c066b4 65 amount--;
andrewklmn 48:4ec055c066b4 66 };
andrewklmn 48:4ec055c066b4 67 };
andrewklmn 23:33d6689834c6 68
andrewklmn 23:33d6689834c6 69 void pb_hit_interrupt (void) {
andrewklmn 38:a0753c2a4497 70
andrewklmn 37:c9b96afec535 71 //writeEEPROMWord(0, 666 );
andrewklmn 38:a0753c2a4497 72 //epprom_config_value++;
andrewklmn 41:978c2d85a0e7 73 working_mode ++;
andrewklmn 48:4ec055c066b4 74 blink_myled(working_mode);
andrewklmn 46:0d60fbcfb245 75 if (working_mode == 7) working_mode=0;
andrewklmn 41:978c2d85a0e7 76
andrewklmn 48:4ec055c066b4 77 //pc.printf("working_mode %d \r\n", working_mode);
andrewklmn 41:978c2d85a0e7 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 45:abc682827659 82 //queue.call_in(2000, printf, "called in 2 seconds\n");
andrewklmn 45:abc682827659 83 //pc.printf("Button unpressed\r\n");
andrewklmn 25:49e5d653d789 84 };
andrewklmn 27:007e17df5ba0 85
andrewklmn 38:a0753c2a4497 86
andrewklmn 27:007e17df5ba0 87 void at_command(){
andrewklmn 44:e6efa8be1f9e 88 int x;
andrewklmn 44:e6efa8be1f9e 89 int ii;
andrewklmn 44:e6efa8be1f9e 90 int jj;
andrewklmn 55:3378972851fd 91
andrewklmn 55:3378972851fd 92 char given_min_mebel;
andrewklmn 55:3378972851fd 93 char given_min_litos;
andrewklmn 55:3378972851fd 94 char given_min_back;
andrewklmn 55:3378972851fd 95 char given_max_hot;
andrewklmn 55:3378972851fd 96
andrewklmn 44:e6efa8be1f9e 97 char num[10];
andrewklmn 55:3378972851fd 98 char config_string[20];
andrewklmn 44:e6efa8be1f9e 99
andrewklmn 44:e6efa8be1f9e 100
andrewklmn 27:007e17df5ba0 101 if (recieved == 1) {
andrewklmn 27:007e17df5ba0 102
andrewklmn 32:aebbfe391bbc 103 __disable_irq();
andrewklmn 44:e6efa8be1f9e 104
andrewklmn 44:e6efa8be1f9e 105 if (strcmp(a,"AT get all")==0) {
andrewklmn 55:3378972851fd 106 // config_ram|config_rom|mode|status0|status1|status2|status3|status4|temp0|temp1|temp2|temp3|temp4|pomp_OFF|heater_OFF
andrewklmn 44:e6efa8be1f9e 107 pc.printf("%u|%u|", eeprom_config_value, readEEPROMWord(0));
andrewklmn 47:4a955f381a1e 108 if (working_mode==3 || working_mode==4) {
andrewklmn 44:e6efa8be1f9e 109 pc.printf("%d|%d|%d|%d|%d|%d|", working_mode, 0, 0, 0, 0, 0);
andrewklmn 50:94730b99ea41 110 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f|%d|%d\r\n",
andrewklmn 50:94730b99ea41 111 simulated_temp[0],
andrewklmn 50:94730b99ea41 112 simulated_temp[1],
andrewklmn 50:94730b99ea41 113 simulated_temp[2],
andrewklmn 50:94730b99ea41 114 simulated_temp[3],
andrewklmn 50:94730b99ea41 115 simulated_temp[4],
andrewklmn 50:94730b99ea41 116 (uint8_t)pomp_OFF,
andrewklmn 50:94730b99ea41 117 (uint8_t)heater_OFF
andrewklmn 50:94730b99ea41 118 );
andrewklmn 44:e6efa8be1f9e 119 } else {
andrewklmn 44:e6efa8be1f9e 120 pc.printf("%d|%d|%d|%d|%d|%d|", working_mode, temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4]);
andrewklmn 50:94730b99ea41 121 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f|%d|%d\r\n", temp[0], temp[1], temp[2], temp[3], temp[4], (uint8_t)pomp_OFF , (uint8_t)heater_OFF);
andrewklmn 44:e6efa8be1f9e 122 };
andrewklmn 44:e6efa8be1f9e 123 } else if (strcmp(a,"AT")==0) {
andrewklmn 43:cc47a2e6ae07 124 pc.printf("OK\r\n");
andrewklmn 44:e6efa8be1f9e 125 } else if (strcmp(a,"AT get config")==0) {
andrewklmn 43:cc47a2e6ae07 126 pc.printf("%u|%u\r\n", eeprom_config_value, readEEPROMWord(0));
andrewklmn 55:3378972851fd 127 } else if (strncmp(a,"AT set config ",14)==0) {
andrewklmn 55:3378972851fd 128 ii = 14;
andrewklmn 55:3378972851fd 129 jj= 0;
andrewklmn 55:3378972851fd 130 //get rest of AT string for config value
andrewklmn 55:3378972851fd 131 while (a[ii]!='\0') {
andrewklmn 55:3378972851fd 132 config_string[jj] = a[ii];
andrewklmn 55:3378972851fd 133 ii++;
andrewklmn 55:3378972851fd 134 jj++;
andrewklmn 55:3378972851fd 135 };
andrewklmn 55:3378972851fd 136 config_string[jj] = '\0';
andrewklmn 55:3378972851fd 137
andrewklmn 55:3378972851fd 138 // check if given temp in right range
andrewklmn 55:3378972851fd 139 int config_int = atol(config_string);
andrewklmn 55:3378972851fd 140 given_min_mebel = config_int >> 24;
andrewklmn 55:3378972851fd 141 given_min_litos = (config_int >> 16) & 0x000000FF;
andrewklmn 55:3378972851fd 142 given_min_back = (config_int >> 8) & 0x000000FF;
andrewklmn 55:3378972851fd 143 given_max_hot = config_int & 0x000000FF;
andrewklmn 55:3378972851fd 144
andrewklmn 55:3378972851fd 145 if ( given_min_mebel > 9 && given_min_mebel < 21
andrewklmn 55:3378972851fd 146 && given_min_litos > 4 && given_min_litos < 16
andrewklmn 55:3378972851fd 147 && given_min_back > 4 && given_min_back < 21
andrewklmn 55:3378972851fd 148 && given_max_hot > 14 && given_max_hot < 51) {
andrewklmn 55:3378972851fd 149 // all given temps are in right range
andrewklmn 55:3378972851fd 150 eeprom_config_value = config_int;
andrewklmn 55:3378972851fd 151 pc.printf("%u\r\n", eeprom_config_value);
andrewklmn 55:3378972851fd 152 } else {
andrewklmn 55:3378972851fd 153 pc.printf("Out of range\r\n");
andrewklmn 55:3378972851fd 154 };
andrewklmn 55:3378972851fd 155
andrewklmn 42:03cbc2a6f63b 156 } else if (strcmp(a,"AT get status")==0) {
andrewklmn 50:94730b99ea41 157 pc.printf("%d|%d|%d|%d|%d|%d|%d\r\n", temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4], (uint8_t)pomp_OFF , (uint8_t)heater_OFF);
andrewklmn 42:03cbc2a6f63b 158 } else if (strcmp(a,"AT get temp")==0) {
andrewklmn 42:03cbc2a6f63b 159 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4]);
andrewklmn 44:e6efa8be1f9e 160 } else if (strcmp(a,"AT get sim temp")==0) {
andrewklmn 43:cc47a2e6ae07 161 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", simulated_temp[0], simulated_temp[1], simulated_temp[2], simulated_temp[3], simulated_temp[4]);
andrewklmn 42:03cbc2a6f63b 162 } else if (strcmp(a,"AT get mode")==0) {
andrewklmn 42:03cbc2a6f63b 163 pc.printf("%d\r\n", working_mode);
andrewklmn 44:e6efa8be1f9e 164 } else if (strncmp(a,"AT set mode ",12) == 0) {
andrewklmn 44:e6efa8be1f9e 165 x= (int)a[12] - 48;
andrewklmn 50:94730b99ea41 166 working_mode = ( x >= 0 && x <=6 ) ? x : working_mode;
andrewklmn 54:61ccc1036944 167 error_count = 0;
andrewklmn 44:e6efa8be1f9e 168 pc.printf("%d\r\n", working_mode);
andrewklmn 44:e6efa8be1f9e 169 } else if (strncmp(a,"AT set sim temp ", 16) == 0) {
andrewklmn 44:e6efa8be1f9e 170 // from a[16] "t0|t1|t2|t3|t4\0"
andrewklmn 44:e6efa8be1f9e 171 ii = 16;
andrewklmn 44:e6efa8be1f9e 172 for (int j=0; j<6; j++) {
andrewklmn 44:e6efa8be1f9e 173 jj = 0;
andrewklmn 44:e6efa8be1f9e 174 while ( a[ii]!='|' && a[ii]!='\0') {
andrewklmn 44:e6efa8be1f9e 175 num[jj] = a[ii];
andrewklmn 44:e6efa8be1f9e 176 ii++;
andrewklmn 44:e6efa8be1f9e 177 jj++;
andrewklmn 44:e6efa8be1f9e 178 };
andrewklmn 44:e6efa8be1f9e 179 ii++;
andrewklmn 44:e6efa8be1f9e 180 num[jj] = '\0';
andrewklmn 44:e6efa8be1f9e 181 simulated_temp[j] = (float)atol(num);
andrewklmn 44:e6efa8be1f9e 182 };
andrewklmn 44:e6efa8be1f9e 183 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n",
andrewklmn 44:e6efa8be1f9e 184 simulated_temp[0],
andrewklmn 44:e6efa8be1f9e 185 simulated_temp[1],
andrewklmn 44:e6efa8be1f9e 186 simulated_temp[2],
andrewklmn 44:e6efa8be1f9e 187 simulated_temp[3],
andrewklmn 44:e6efa8be1f9e 188 simulated_temp[4]
andrewklmn 44:e6efa8be1f9e 189 );
andrewklmn 42:03cbc2a6f63b 190 } else {
andrewklmn 42:03cbc2a6f63b 191 pc.printf("Bad request\r\n");
andrewklmn 42:03cbc2a6f63b 192 };
andrewklmn 42:03cbc2a6f63b 193
andrewklmn 32:aebbfe391bbc 194 __enable_irq();
andrewklmn 32:aebbfe391bbc 195
andrewklmn 27:007e17df5ba0 196 // ready for new command
andrewklmn 27:007e17df5ba0 197 recieved = 0;
andrewklmn 27:007e17df5ba0 198 a[0] = '\0';
andrewklmn 27:007e17df5ba0 199 i = 0;
andrewklmn 27:007e17df5ba0 200 };
andrewklmn 27:007e17df5ba0 201 };
andrewklmn 39:aa5f95061409 202
andrewklmn 56:522ad08941bb 203 void flushSerialBuffer(void) {
andrewklmn 56:522ad08941bb 204 char char1 = 0;
andrewklmn 56:522ad08941bb 205 while (pc.readable()) {
andrewklmn 56:522ad08941bb 206 char1 = pc.getc();
andrewklmn 56:522ad08941bb 207 };
andrewklmn 56:522ad08941bb 208 return;
andrewklmn 56:522ad08941bb 209 };
andrewklmn 56:522ad08941bb 210
hudakz 10:4b88be251088 211
hudakz 0:ab218237069e 212 int main() {
andrewklmn 12:187e02ab447f 213
hudakz 10:4b88be251088 214 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 37:c9b96afec535 215
andrewklmn 48:4ec055c066b4 216 heater_OFF = 1;
andrewklmn 48:4ec055c066b4 217 pomp_OFF = 1;
andrewklmn 56:522ad08941bb 218 reset_wifi = 0;
andrewklmn 48:4ec055c066b4 219
andrewklmn 38:a0753c2a4497 220 eeprom_config_value = get_temp_config_value();
andrewklmn 50:94730b99ea41 221 working_mode = (char)get_mode_config_value();
andrewklmn 38:a0753c2a4497 222
andrewklmn 37:c9b96afec535 223 wd.Configure(10.0);
andrewklmn 32:aebbfe391bbc 224
andrewklmn 26:750f21025bb9 225 PinDetect pb(PA_11);
andrewklmn 23:33d6689834c6 226 pb.mode(PullUp);
andrewklmn 26:750f21025bb9 227
andrewklmn 26:750f21025bb9 228 // Delay for initial pullup to take effect
andrewklmn 56:522ad08941bb 229 wait(.15);
andrewklmn 56:522ad08941bb 230
andrewklmn 56:522ad08941bb 231 reset_wifi = 1; // starts esp8266
andrewklmn 26:750f21025bb9 232
andrewklmn 26:750f21025bb9 233 pb.attach_deasserted(&pb_hit_interrupt);
andrewklmn 26:750f21025bb9 234 pb.attach_asserted(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 235 //pb.rise(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 236 pb.setSampleFrequency();
andrewklmn 22:32beca745706 237
andrewklmn 56:522ad08941bb 238
andrewklmn 12:187e02ab447f 239 pc.attach(&rxCallback, Serial::RxIrq);
andrewklmn 51:881a30217532 240 pc.baud(19200);
andrewklmn 56:522ad08941bb 241 flushSerialBuffer();
andrewklmn 57:9bf4c8142958 242 //pc.printf("\r\nNaumovich 2.0\r\n");
andrewklmn 48:4ec055c066b4 243
andrewklmn 48:4ec055c066b4 244 blink_myled(3);
andrewklmn 48:4ec055c066b4 245
andrewklmn 48:4ec055c066b4 246
andrewklmn 17:718e41e971f8 247
andrewklmn 27:007e17df5ba0 248 queue.call( start_temp );
andrewklmn 27:007e17df5ba0 249 queue.call_every(100, at_command);
andrewklmn 53:b9620bb0608e 250 queue.call_every(1000, check_temp);
andrewklmn 53:b9620bb0608e 251 queue.call_every(1000, process_temp);
andrewklmn 27:007e17df5ba0 252
andrewklmn 27:007e17df5ba0 253 // Start queue
andrewklmn 27:007e17df5ba0 254 queue.dispatch();
andrewklmn 22:32beca745706 255 };
hudakz 8:f1432e9af6c8 256