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:
Sat Sep 29 23:54:21 2018 +0000
Revision:
54:61ccc1036944
Parent:
53:b9620bb0608e
Child:
55:3378972851fd
error_count  reset on AT SET MODE 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 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 12:187e02ab447f 35 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 36 void rxCallback() {
andrewklmn 12:187e02ab447f 37 char c;
andrewklmn 14:d954558361b9 38 c = pc.getc();
andrewklmn 12:187e02ab447f 39 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 40 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 29:5940a80717e4 41 while ( pc.readable() ) c = pc.getc();
andrewklmn 12:187e02ab447f 42 recieved = 1;
andrewklmn 12:187e02ab447f 43 } else {
andrewklmn 43:cc47a2e6ae07 44 if (i==127){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 45 a[0] = c;
andrewklmn 12:187e02ab447f 46 a[1] = '\0';
andrewklmn 12:187e02ab447f 47 i = 0;
andrewklmn 12:187e02ab447f 48 } else {
andrewklmn 12:187e02ab447f 49 a[i] = c;
andrewklmn 12:187e02ab447f 50 i++;
andrewklmn 12:187e02ab447f 51 a[i] = '\0';
andrewklmn 12:187e02ab447f 52 };
andrewklmn 14:d954558361b9 53 };
andrewklmn 12:187e02ab447f 54 };
andrewklmn 12:187e02ab447f 55 };
andrewklmn 12:187e02ab447f 56
andrewklmn 48:4ec055c066b4 57 void blink_myled(char amount) {
andrewklmn 48:4ec055c066b4 58 while (amount > 0) {
andrewklmn 48:4ec055c066b4 59 myled = 0;
andrewklmn 48:4ec055c066b4 60 wait(0.1);
andrewklmn 48:4ec055c066b4 61 myled = 1;
andrewklmn 48:4ec055c066b4 62 wait(0.2);
andrewklmn 48:4ec055c066b4 63 amount--;
andrewklmn 48:4ec055c066b4 64 };
andrewklmn 48:4ec055c066b4 65 };
andrewklmn 23:33d6689834c6 66
andrewklmn 23:33d6689834c6 67 void pb_hit_interrupt (void) {
andrewklmn 38:a0753c2a4497 68
andrewklmn 37:c9b96afec535 69 //writeEEPROMWord(0, 666 );
andrewklmn 38:a0753c2a4497 70 //epprom_config_value++;
andrewklmn 41:978c2d85a0e7 71 working_mode ++;
andrewklmn 48:4ec055c066b4 72 blink_myled(working_mode);
andrewklmn 46:0d60fbcfb245 73 if (working_mode == 7) working_mode=0;
andrewklmn 41:978c2d85a0e7 74
andrewklmn 48:4ec055c066b4 75 //pc.printf("working_mode %d \r\n", working_mode);
andrewklmn 41:978c2d85a0e7 76 //pc.printf("Button pressed \r\n");
andrewklmn 23:33d6689834c6 77 };
andrewklmn 25:49e5d653d789 78
andrewklmn 25:49e5d653d789 79 void pb_out_interrupt (void) {
andrewklmn 45:abc682827659 80 //queue.call_in(2000, printf, "called in 2 seconds\n");
andrewklmn 45:abc682827659 81 //pc.printf("Button unpressed\r\n");
andrewklmn 25:49e5d653d789 82 };
andrewklmn 27:007e17df5ba0 83
andrewklmn 38:a0753c2a4497 84
andrewklmn 27:007e17df5ba0 85 void at_command(){
andrewklmn 44:e6efa8be1f9e 86 int x;
andrewklmn 44:e6efa8be1f9e 87 int ii;
andrewklmn 44:e6efa8be1f9e 88 int jj;
andrewklmn 44:e6efa8be1f9e 89 char num[10];
andrewklmn 44:e6efa8be1f9e 90
andrewklmn 44:e6efa8be1f9e 91
andrewklmn 27:007e17df5ba0 92 if (recieved == 1) {
andrewklmn 27:007e17df5ba0 93
andrewklmn 32:aebbfe391bbc 94 __disable_irq();
andrewklmn 44:e6efa8be1f9e 95
andrewklmn 44:e6efa8be1f9e 96 if (strcmp(a,"AT get all")==0) {
andrewklmn 44:e6efa8be1f9e 97 // config_ram|config_rom|mode|status0|status1|status2|status3|status4|temp0|temp1|temp2|temp3|temp4
andrewklmn 44:e6efa8be1f9e 98 pc.printf("%u|%u|", eeprom_config_value, readEEPROMWord(0));
andrewklmn 47:4a955f381a1e 99 if (working_mode==3 || working_mode==4) {
andrewklmn 44:e6efa8be1f9e 100 pc.printf("%d|%d|%d|%d|%d|%d|", working_mode, 0, 0, 0, 0, 0);
andrewklmn 50:94730b99ea41 101 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f|%d|%d\r\n",
andrewklmn 50:94730b99ea41 102 simulated_temp[0],
andrewklmn 50:94730b99ea41 103 simulated_temp[1],
andrewklmn 50:94730b99ea41 104 simulated_temp[2],
andrewklmn 50:94730b99ea41 105 simulated_temp[3],
andrewklmn 50:94730b99ea41 106 simulated_temp[4],
andrewklmn 50:94730b99ea41 107 (uint8_t)pomp_OFF,
andrewklmn 50:94730b99ea41 108 (uint8_t)heater_OFF
andrewklmn 50:94730b99ea41 109 );
andrewklmn 44:e6efa8be1f9e 110 } else {
andrewklmn 44:e6efa8be1f9e 111 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 112 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 113 };
andrewklmn 44:e6efa8be1f9e 114 } else if (strcmp(a,"AT")==0) {
andrewklmn 43:cc47a2e6ae07 115 pc.printf("OK\r\n");
andrewklmn 44:e6efa8be1f9e 116 } else if (strcmp(a,"AT get config")==0) {
andrewklmn 43:cc47a2e6ae07 117 pc.printf("%u|%u\r\n", eeprom_config_value, readEEPROMWord(0));
andrewklmn 42:03cbc2a6f63b 118 } else if (strcmp(a,"AT get status")==0) {
andrewklmn 50:94730b99ea41 119 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 120 } else if (strcmp(a,"AT get temp")==0) {
andrewklmn 42:03cbc2a6f63b 121 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 122 } else if (strcmp(a,"AT get sim temp")==0) {
andrewklmn 43:cc47a2e6ae07 123 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 124 } else if (strcmp(a,"AT get mode")==0) {
andrewklmn 42:03cbc2a6f63b 125 pc.printf("%d\r\n", working_mode);
andrewklmn 44:e6efa8be1f9e 126 } else if (strncmp(a,"AT set mode ",12) == 0) {
andrewklmn 44:e6efa8be1f9e 127 x= (int)a[12] - 48;
andrewklmn 50:94730b99ea41 128 working_mode = ( x >= 0 && x <=6 ) ? x : working_mode;
andrewklmn 54:61ccc1036944 129 error_count = 0;
andrewklmn 44:e6efa8be1f9e 130 pc.printf("%d\r\n", working_mode);
andrewklmn 44:e6efa8be1f9e 131 } else if (strncmp(a,"AT set sim temp ", 16) == 0) {
andrewklmn 44:e6efa8be1f9e 132 // from a[16] "t0|t1|t2|t3|t4\0"
andrewklmn 44:e6efa8be1f9e 133 ii = 16;
andrewklmn 44:e6efa8be1f9e 134 for (int j=0; j<6; j++) {
andrewklmn 44:e6efa8be1f9e 135 jj = 0;
andrewklmn 44:e6efa8be1f9e 136 while ( a[ii]!='|' && a[ii]!='\0') {
andrewklmn 44:e6efa8be1f9e 137 num[jj] = a[ii];
andrewklmn 44:e6efa8be1f9e 138 ii++;
andrewklmn 44:e6efa8be1f9e 139 jj++;
andrewklmn 44:e6efa8be1f9e 140 };
andrewklmn 44:e6efa8be1f9e 141 ii++;
andrewklmn 44:e6efa8be1f9e 142 num[jj] = '\0';
andrewklmn 44:e6efa8be1f9e 143 simulated_temp[j] = (float)atol(num);
andrewklmn 44:e6efa8be1f9e 144 };
andrewklmn 44:e6efa8be1f9e 145 pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n",
andrewklmn 44:e6efa8be1f9e 146 simulated_temp[0],
andrewklmn 44:e6efa8be1f9e 147 simulated_temp[1],
andrewklmn 44:e6efa8be1f9e 148 simulated_temp[2],
andrewklmn 44:e6efa8be1f9e 149 simulated_temp[3],
andrewklmn 44:e6efa8be1f9e 150 simulated_temp[4]
andrewklmn 44:e6efa8be1f9e 151 );
andrewklmn 42:03cbc2a6f63b 152 } else {
andrewklmn 42:03cbc2a6f63b 153 pc.printf("Bad request\r\n");
andrewklmn 42:03cbc2a6f63b 154 };
andrewklmn 42:03cbc2a6f63b 155
andrewklmn 32:aebbfe391bbc 156 __enable_irq();
andrewklmn 32:aebbfe391bbc 157
andrewklmn 27:007e17df5ba0 158 // ready for new command
andrewklmn 27:007e17df5ba0 159 recieved = 0;
andrewklmn 27:007e17df5ba0 160 a[0] = '\0';
andrewklmn 27:007e17df5ba0 161 i = 0;
andrewklmn 27:007e17df5ba0 162 };
andrewklmn 27:007e17df5ba0 163 };
andrewklmn 39:aa5f95061409 164
hudakz 10:4b88be251088 165
hudakz 0:ab218237069e 166 int main() {
andrewklmn 12:187e02ab447f 167
hudakz 10:4b88be251088 168 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 37:c9b96afec535 169
andrewklmn 48:4ec055c066b4 170 heater_OFF = 1;
andrewklmn 48:4ec055c066b4 171 pomp_OFF = 1;
andrewklmn 48:4ec055c066b4 172
andrewklmn 38:a0753c2a4497 173 eeprom_config_value = get_temp_config_value();
andrewklmn 50:94730b99ea41 174 working_mode = (char)get_mode_config_value();
andrewklmn 38:a0753c2a4497 175
andrewklmn 37:c9b96afec535 176 wd.Configure(10.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 51:881a30217532 190 pc.baud(19200);
andrewklmn 39:aa5f95061409 191 pc.printf("\r\nNaumovich 2.0\r\n");
andrewklmn 48:4ec055c066b4 192
andrewklmn 48:4ec055c066b4 193 blink_myled(3);
andrewklmn 48:4ec055c066b4 194
andrewklmn 48:4ec055c066b4 195
andrewklmn 17:718e41e971f8 196
andrewklmn 27:007e17df5ba0 197 queue.call( start_temp );
andrewklmn 27:007e17df5ba0 198 queue.call_every(100, at_command);
andrewklmn 53:b9620bb0608e 199 queue.call_every(1000, check_temp);
andrewklmn 53:b9620bb0608e 200 queue.call_every(1000, process_temp);
andrewklmn 27:007e17df5ba0 201
andrewklmn 27:007e17df5ba0 202 // Start queue
andrewklmn 27:007e17df5ba0 203 queue.dispatch();
andrewklmn 22:32beca745706 204 };
hudakz 8:f1432e9af6c8 205