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)

Revision:
27:007e17df5ba0
Parent:
26:750f21025bb9
Child:
29:5940a80717e4
--- a/main.cpp	Sat Sep 15 20:00:16 2018 +0000
+++ b/main.cpp	Sun Sep 16 08:39:56 2018 +0000
@@ -1,9 +1,17 @@
 #include "stm32f103c8t6.h"
 #include "mbed.h"
+#include "mbed_events.h"
+#include <stdio.h>
 #include "DS1820.h"
 #include "PinDetect.h"
 #include <string> 
 
+// Create a queue that can hold a maximum of 32 events
+EventQueue queue(32 * EVENTS_EVENT_SIZE);
+// Create a thread that'll run the event queue's dispatch function
+//Thread t;
+
+
 char a[128] = "";       // RX command buffer
 char i = 0;             // RX char pointer
 static char recieved = 0;
@@ -11,6 +19,32 @@
 Serial  pc(PA_2, PA_3);
 DigitalOut  myled(LED1);
 
+
+float temp[5] = {
+                    85,85,85,85,85  // initial temperature
+                };
+                
+int temp_error[5] = {
+                    0,0,0,0,0       // initial state
+                };
+                        
+string labels[5] = {
+                        "OUTDOOR", 
+                        "LITOS", 
+                        "MEBEL", 
+                        "HOT WATER", 
+                        "BACK WATER" 
+                    };
+    
+DS1820  ds1820[5] = { 
+                            DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
+                            DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS    
+                            DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
+                            DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
+                            DS1820(PA_5)  // substitute PA_6 with actual mbed pin name connected to the HOT WATER
+                        };
+
+
 // This function is called when a character goes into the RX buffer.
 void rxCallback() {
     char c;
@@ -34,12 +68,70 @@
  
  
 void pb_hit_interrupt (void) {
-    //pc.printf("Button pressed\r\n"); 
+    pc.printf("Button pressed\r\n"); 
 }; 
 
 void pb_out_interrupt (void) {
-    //pc.printf("Button unpressed\r\n"); 
+    pc.printf("Button unpressed\r\n"); 
 }; 
+
+void start_temp(){
+    for ( int j=0; j < 5; j++ ) {
+        if(ds1820[j].begin()) { 
+            ds1820[j].startConversion();
+            pc.printf("%s sensor present!\r\n", labels[j].c_str()); 
+        } else {
+            pc.printf("No %s sensor found!\r\n", labels[j].c_str());         
+        };
+    };
+};
+
+void at_command(){
+        if (recieved == 1) {
+            
+            // command was recieved
+            pc.printf(a);
+            pc.printf("\r\n");
+            
+            // process_command(a);
+            
+            // ready for new command
+            recieved = 0;
+            a[0] = '\0';
+            i = 0;
+        }; 
+};
+
+
+void check_temp(){
+       
+       myled = 0;      // turn the LED on
+       
+       for ( int j=0; j < 5; j++ ) {
+            
+            temp_error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
+            
+            switch(temp_error[j]) {
+            case 0:    // no errors -> 'temp' contains the value of measured temperature
+                pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]);
+                break;
+            case 1:    // no sensor present -> 'temp' is not updated
+                pc.printf("no %s sensor present \r\n", labels[j].c_str() );
+                break;
+            case 2:    // CRC error -> 'temp' is not updated
+                pc.printf("%s sensor CRC error \r\n", labels[j].c_str() );
+            };
+            // start temperature conversion from analog to digital
+            ds1820[j].startConversion();             
+        };
+        
+        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] );
+        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] );
+        pc.printf("=======================================");
+        myled = 1;      // turn the LED off
+};
+
+
   
 int main() {
         
@@ -56,103 +148,17 @@
     //pb.rise(&pb_out_interrupt);
     pb.setSampleFrequency();
     
-    float temp[5] = {
-                    85,85,85,85,85  // initial temperature
-                };
-                
-    int error[5] = {
-                    0,0,0,0,0       // initial state
-                };
-                        
-    string labels[5] = {
-                        "OUTDOOR", 
-                        "LITOS", 
-                        "MEBEL", 
-                        "HOT WATER", 
-                        "BACK WATER" 
-                    };
-    
-    DS1820  ds1820[5] = { 
-                            DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
-                            DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS    
-                            DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
-                            DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
-                            DS1820(PA_5)  // substitute PA_6 with actual mbed pin name connected to the HOT WATER
-                        };
-    
     pc.attach(&rxCallback, Serial::RxIrq);
     pc.baud(115200);  
     pc.printf("Wifi Heating system\r\n"); 
 
-    for ( int j=0; j < 5; j++ ) {
-        if(ds1820[j].begin()) { 
-            pc.printf("%s sensor present!\r\n", labels[j].c_str()); 
-        } else {
-            //pc.printf("No %s sensor found!\r\n", labels[j].c_str());         
-        };
-    };
+
     
-    while(1) {
-        // The on-board LED is connected, via a resistor, to +3.3V (not to GND). 
-        // So to turn the LED on or off we have to set it to 0 or 1 respectively
-        myled = 0;      // turn the LED on
-        wait_ms(50);   // 200 millisecond
-        myled = 1;      // turn the LED off
-        wait_ms(50);  // 1000 millisecond
-        myled = 0;      // turn the LED on
-        wait_ms(50);   // 200 millisecond
-        myled = 1;      // turn the LED off
-        wait_ms(50);  // 1000 millisecond
-        myled = 0;      // turn the LED on
-        wait_ms(50);   // 200 millisecond
-        myled = 1;      // turn the LED off
-        
-        
-        // start temperature conversion from analog to digital
-        for ( int j=0; j < 5; j++ ) {
-            ds1820[j].startConversion();
-        };
-
-        
-        wait(1.0);                 // let DS1820 complete the temperature conversion 
-            
-        if (recieved == 1) {
-            
-            // command was recieved
-            pc.printf(a);
-            pc.printf("\r\n");
-            
-            // process_command(a);
-            
-            // ready for new command
-            recieved = 0;
-            a[0] = '\0';
-            i = 0;
-        };  
-        
-        
-
-        for ( int j=0; j < 5; j++ ) {
-            
-            error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
-            
-            switch(error[j]) {
-            case 0:    // no errors -> 'temp' contains the value of measured temperature
-                pc.printf("%s = %3.1fC \r\n", labels[j].c_str() , temp[j]);
-                break;
-            case 1:    // no sensor present -> 'temp' is not updated
-                //pc.printf("no %s sensor present \r\n", labels[j].c_str() );
-                break;
-            case 2:    // CRC error -> 'temp' is not updated
-                pc.printf("%s sensor CRC error \r\n", labels[j].c_str() );
-            };
-             
-        };
-        
-        pc.printf("State %d|%d|%d|%d|%d\r\n", error[0], error[1], error[2], error[3], error[4] );
-        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] );
-        pc.printf("=======================================");
-
-    };
+    queue.call( start_temp );
+    queue.call_every(100, at_command);
+    queue.call_every(1000, check_temp);
+    
+    // Start queue 
+    queue.dispatch();
 };
  
\ No newline at end of file