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 07:54:57 2018 +0000
Revision:
31:0a516ceaec4d
Parent:
30:b696b5799507
Child:
32:aebbfe391bbc
Watchdog added one more version...

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