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 18:42:31 2018 +0000
Revision:
33:2c54746f7518
Parent:
32:aebbfe391bbc
Child:
34:09d8edcce624
123

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