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 22 08:32:20 2018 +0000
Revision:
42:03cbc2a6f63b
Parent:
41:978c2d85a0e7
Child:
43:cc47a2e6ae07
AT get command 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 27:007e17df5ba0 21
andrewklmn 39:aa5f95061409 22 Serial pc(PA_2, PA_3); // (TX, RX) Pin definition
andrewklmn 12:187e02ab447f 23 char a[128] = ""; // RX command buffer
andrewklmn 12:187e02ab447f 24 char i = 0; // RX char pointer
andrewklmn 14:d954558361b9 25 static char recieved = 0;
andrewklmn 12:187e02ab447f 26
andrewklmn 38:a0753c2a4497 27
andrewklmn 23:33d6689834c6 28 DigitalOut myled(LED1);
andrewklmn 17:718e41e971f8 29
andrewklmn 34:09d8edcce624 30 DigitalOut pomp_OFF(PA_12); // pomp off
andrewklmn 34:09d8edcce624 31 DigitalOut heater_OFF(PA_15); // heater off
andrewklmn 34:09d8edcce624 32
andrewklmn 12:187e02ab447f 33 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 34 void rxCallback() {
andrewklmn 12:187e02ab447f 35 char c;
andrewklmn 14:d954558361b9 36 c = pc.getc();
andrewklmn 12:187e02ab447f 37 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 38 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 29:5940a80717e4 39 while ( pc.readable() ) c = pc.getc();
andrewklmn 12:187e02ab447f 40 recieved = 1;
andrewklmn 12:187e02ab447f 41 } else {
andrewklmn 12:187e02ab447f 42 if (i==16){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 43 a[0] = c;
andrewklmn 12:187e02ab447f 44 a[1] = '\0';
andrewklmn 12:187e02ab447f 45 i = 0;
andrewklmn 12:187e02ab447f 46 } else {
andrewklmn 12:187e02ab447f 47 a[i] = c;
andrewklmn 12:187e02ab447f 48 i++;
andrewklmn 12:187e02ab447f 49 a[i] = '\0';
andrewklmn 12:187e02ab447f 50 };
andrewklmn 14:d954558361b9 51 };
andrewklmn 12:187e02ab447f 52 };
andrewklmn 12:187e02ab447f 53 };
andrewklmn 12:187e02ab447f 54
andrewklmn 23:33d6689834c6 55
andrewklmn 23:33d6689834c6 56 void pb_hit_interrupt (void) {
andrewklmn 38:a0753c2a4497 57
andrewklmn 37:c9b96afec535 58 //writeEEPROMWord(0, 666 );
andrewklmn 38:a0753c2a4497 59 //epprom_config_value++;
andrewklmn 41:978c2d85a0e7 60 working_mode ++;
andrewklmn 41:978c2d85a0e7 61 if (working_mode == 6) working_mode=0;
andrewklmn 41:978c2d85a0e7 62
andrewklmn 41:978c2d85a0e7 63 pc.printf("working_mode %d \r\n", working_mode);
andrewklmn 41:978c2d85a0e7 64 //pc.printf("Button pressed \r\n");
andrewklmn 23:33d6689834c6 65 };
andrewklmn 25:49e5d653d789 66
andrewklmn 25:49e5d653d789 67 void pb_out_interrupt (void) {
andrewklmn 27:007e17df5ba0 68 pc.printf("Button unpressed\r\n");
andrewklmn 25:49e5d653d789 69 };
andrewklmn 27:007e17df5ba0 70
andrewklmn 38:a0753c2a4497 71
andrewklmn 27:007e17df5ba0 72 void at_command(){
andrewklmn 27:007e17df5ba0 73 if (recieved == 1) {
andrewklmn 27:007e17df5ba0 74
andrewklmn 32:aebbfe391bbc 75 __disable_irq();
andrewklmn 32:aebbfe391bbc 76
andrewklmn 42:03cbc2a6f63b 77 if (strcmp(a,"AT check setup")==0) {
andrewklmn 42:03cbc2a6f63b 78 pc.printf("RAM=%u FLASH=%u OK\r\n", eeprom_config_value, readEEPROMWord(0));
andrewklmn 42:03cbc2a6f63b 79 } else if (strcmp(a,"AT get status")==0) {
andrewklmn 42:03cbc2a6f63b 80 pc.printf("%d|%d|%d|%d|%d\r\n", temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4]);
andrewklmn 42:03cbc2a6f63b 81 } else if (strcmp(a,"AT get temp")==0) {
andrewklmn 42:03cbc2a6f63b 82 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 42:03cbc2a6f63b 83 } else if (strcmp(a,"AT get mode")==0) {
andrewklmn 42:03cbc2a6f63b 84 pc.printf("%d\r\n", working_mode);
andrewklmn 42:03cbc2a6f63b 85 } else {
andrewklmn 42:03cbc2a6f63b 86 pc.printf("Bad request\r\n");
andrewklmn 42:03cbc2a6f63b 87 };
andrewklmn 42:03cbc2a6f63b 88
andrewklmn 27:007e17df5ba0 89 // command was recieved
andrewklmn 42:03cbc2a6f63b 90 /*
andrewklmn 32:aebbfe391bbc 91 if (a[0]=='A' && a[1]=='T') {
andrewklmn 32:aebbfe391bbc 92
andrewklmn 32:aebbfe391bbc 93 if ( a[2]=='\0') {
andrewklmn 32:aebbfe391bbc 94 //pc.printf(a);
andrewklmn 38:a0753c2a4497 95 pc.printf("RAM=%u FLASH=%u OK\r\n", eeprom_config_value, readEEPROMWord(0));
andrewklmn 33:2c54746f7518 96 } else {
andrewklmn 39:aa5f95061409 97 pc.printf("Wrong command\r\n");
andrewklmn 32:aebbfe391bbc 98 };
andrewklmn 37:c9b96afec535 99
andrewklmn 37:c9b96afec535 100 //readEEPROMWord(0x01);
andrewklmn 32:aebbfe391bbc 101 } else {
andrewklmn 39:aa5f95061409 102 pc.printf("Bad request\r\n");
andrewklmn 32:aebbfe391bbc 103 };
andrewklmn 27:007e17df5ba0 104
andrewklmn 27:007e17df5ba0 105 // process_command(a);
andrewklmn 42:03cbc2a6f63b 106 */
andrewklmn 32:aebbfe391bbc 107 __enable_irq();
andrewklmn 32:aebbfe391bbc 108
andrewklmn 27:007e17df5ba0 109 // ready for new command
andrewklmn 27:007e17df5ba0 110 recieved = 0;
andrewklmn 27:007e17df5ba0 111 a[0] = '\0';
andrewklmn 27:007e17df5ba0 112 i = 0;
andrewklmn 27:007e17df5ba0 113 };
andrewklmn 27:007e17df5ba0 114 };
andrewklmn 39:aa5f95061409 115
hudakz 10:4b88be251088 116
hudakz 0:ab218237069e 117 int main() {
andrewklmn 12:187e02ab447f 118
hudakz 10:4b88be251088 119 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 37:c9b96afec535 120
andrewklmn 38:a0753c2a4497 121 eeprom_config_value = get_temp_config_value();
andrewklmn 38:a0753c2a4497 122
andrewklmn 37:c9b96afec535 123 wd.Configure(10.0);
andrewklmn 32:aebbfe391bbc 124
andrewklmn 26:750f21025bb9 125 PinDetect pb(PA_11);
andrewklmn 23:33d6689834c6 126 pb.mode(PullUp);
andrewklmn 26:750f21025bb9 127
andrewklmn 26:750f21025bb9 128 // Delay for initial pullup to take effect
andrewklmn 26:750f21025bb9 129 wait(.005);
andrewklmn 26:750f21025bb9 130
andrewklmn 26:750f21025bb9 131 pb.attach_deasserted(&pb_hit_interrupt);
andrewklmn 26:750f21025bb9 132 pb.attach_asserted(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 133 //pb.rise(&pb_out_interrupt);
andrewklmn 26:750f21025bb9 134 pb.setSampleFrequency();
andrewklmn 22:32beca745706 135
andrewklmn 12:187e02ab447f 136 pc.attach(&rxCallback, Serial::RxIrq);
andrewklmn 15:721546347507 137 pc.baud(115200);
andrewklmn 39:aa5f95061409 138 pc.printf("\r\nNaumovich 2.0\r\n");
andrewklmn 19:74f78a777ffa 139
andrewklmn 27:007e17df5ba0 140
andrewklmn 34:09d8edcce624 141 pomp_OFF = 0;
andrewklmn 34:09d8edcce624 142 heater_OFF = 0;
andrewklmn 34:09d8edcce624 143 wait(0.25);
andrewklmn 34:09d8edcce624 144 pomp_OFF = 1;
andrewklmn 34:09d8edcce624 145 heater_OFF = 1;
andrewklmn 34:09d8edcce624 146 wait(0.25);
andrewklmn 34:09d8edcce624 147 pomp_OFF = 0;
andrewklmn 34:09d8edcce624 148 heater_OFF = 0;
andrewklmn 34:09d8edcce624 149 wait(0.25);
andrewklmn 34:09d8edcce624 150 pomp_OFF = 1;
andrewklmn 34:09d8edcce624 151 heater_OFF = 1;
andrewklmn 34:09d8edcce624 152 wait(0.25);
andrewklmn 34:09d8edcce624 153
andrewklmn 17:718e41e971f8 154
andrewklmn 27:007e17df5ba0 155 queue.call( start_temp );
andrewklmn 27:007e17df5ba0 156 queue.call_every(100, at_command);
andrewklmn 37:c9b96afec535 157 queue.call_every(2000, check_temp);
andrewklmn 41:978c2d85a0e7 158 queue.call_every(500, process_temp);
andrewklmn 27:007e17df5ba0 159
andrewklmn 27:007e17df5ba0 160 // Start queue
andrewklmn 27:007e17df5ba0 161 queue.dispatch();
andrewklmn 22:32beca745706 162 };
hudakz 8:f1432e9af6c8 163