electronic water meter system

Dependencies:   mbed Comms

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*##########################################################################################
00002 
00003 Program Name    : ex1_HC06_leds
00004 Author          : Grant Phillips
00005 Date Modified   : 12/05/2016
00006 Compiler        : ARMmbed
00007 Tested On       : STM32F4-Discovery
00008 Discription     : I used some of the program to develop mine Elecrtonic Householder Water Meter
00009 ##############################################################################################*/
00010 ///////////////////////DECLARING LIBRIES////////////////////////////////////////////////////////
00011 
00012 #include "mbed.h"
00013 //////////////////////////////////////////////////////////////////////////////////////////////
00014 
00015 /////////////////////////////////DECLARING BLUETOOTH MODULE PINS//////////////////////////////////////////////////////
00016 
00017 Serial bt(PC_10, PC_11);      //create a Serial object to connect to the HC05 Bluetooth module
00018 //        tx  , rx 
00019 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00020 
00021 ///////////////////////////DECLARING SOLAR PANEL TRANSISTOR'S OPARATIONAL PINS///////////////////////////////////////////////////////////////////////////////////
00022 
00023 DigitalOut Q1(PA_10);       // Solar Panel Forward bias (Q1)transistor for the posistive leg
00024 DigitalOut Q2(PB_3);        // Solar Panel Voltage testing loop forward bias Q2 for the positive leg of the loop
00025 DigitalOut Q3(PB_5);        // Solar Panel Voltage testing loop forward bias Q3 for the negetive leg of the loop back to the Solar
00026 DigitalOut Q4(PB_4);        // Solar Panel Forward bias (Q4)transistor for the negetive leg
00027 AnalogIn SolarVolt(PC_0);   // Solar Panel supply Voltage
00028 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00029     
00030  //////////////////////////////DECLARING HYDRO GENERATION TRANSISTOR'S OPARATIONAL PINS///////////////////////////////////////////
00031  
00032 DigitalOut Q5(PB_13);       //Hydro Generator Forward bias Q5 transistor for the posistive leg
00033 DigitalOut Q6(PB_14);       //Hydro Generator Voltage testing loop bias Q6 for the positive leg of the loop
00034 DigitalOut Q7(PB_15);       //Hydro Generator Voltage testing loop bias Q7 for the negetive leg of the loop back to the hydro
00035 DigitalOut Q8(PB_2);        //Hydro Generator Forward bias Q8 transistor nfor the egetive leg
00036 AnalogIn HydroVolt(PC_1);   //Hydro Generator supply Voltage
00037 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00038    
00039 //////////////////////////////DECLARING FAN TRANSISTOR'S PIN///////////////////////////////////////////
00040 
00041 DigitalOut Q9(PC_6);        // Forward bias to keep the fan on
00042 ////////////////////////////////////////////////////////////////////////////////
00043 
00044 ///////////////////////////DECLARING WATER VALVE TRANSISTOR'S PIN//////////////
00045 
00046 DigitalOut Q10(PC_8);       // Forward bias to keep the valve open
00047 /////////////////////////////////////////////////////////////////////////////////
00048 
00049 //////////////////////////////DECLARING BATTERY TRANSISTOR'S OPARATIONAL PINS/////////////////////////////////////////////////
00050 
00051 DigitalOut Q11(PB_8);       // Battery Voltage testing loop bias Q11 for the positive leg of the loop
00052 DigitalOut Q12(PC_9);       // Battery Voltage testing loop biasing Q12 for the negative leg of the loop down to nuetral of the load
00053 AnalogIn BatVolt(PA_4);   // Battery Panel supply Voltage
00054 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00055 
00056 /////////////////////////////DECLARING WATER METER TRANSISTOR'S OPARATIONAL PIN///////////////////////////////////
00057 
00058 AnalogIn Met(PA_1);     // Water meter
00059 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00060 
00061 ////////DECLARING WATER SENSOR TRANSISTOR'S OPARATIONAL PIN///////////////////////////////
00062 AnalogIn Sen(PA_0);     //Water Sensor
00063 //////////////////////////////////////////////////////////////////////////////////////////
00064 
00065 /////////////////////////////DECLARING WATER METER VARABLE//////////////////////////////////
00066 
00067 double METER;          // Water Meter varable that posses a the current Kiloliter value
00068 
00069 //////////////////////////////////////////////////////////////////////////////////////////
00070 
00071 
00072 ////////////IT'S A SUB PROGRAM THAT CLOSES THE WATER VALVE/////////////
00073 void ClosedValve()
00074       { 
00075       Q10 = 0;
00076       }
00077 ////////////////////////////////////////////////////////////////////
00078 
00079 ///////////////////////IT'S A SUB PROGRAM THAT OPENS THE WATER VALVE////////////////////////
00080 void OpenValve ()
00081                 {
00082                Q10 = 1;
00083                 }
00084 ////////////////////////////////////////////////////////////////////////
00085 
00086 /////////////////IT'S A SUB PROGRAM THAT COMPARES THE WATER METER AND THE WATER SENSOR VALUES THAT ARE EQUAL///////////////
00087 void Water_Valve ()
00088   {
00089      while (1)
00090             {
00091                 if (Met.read_u16() != Sen.read_u16())
00092                 {
00093                     ClosedValve();
00094                     wait (0.2);
00095                 }
00096                 else 
00097                 {
00098                         OpenValve ();
00099                 }
00100             }
00101    }
00102 ///////////////////////////////////////////////////////////////////////////////////////////////
00103 
00104 ///////////////////////////////////NORMAL OPARATIONS SUB PROGRAM WHICH BIAS THE TRANSISTORS//////////////////
00105 
00106 void Normal()               // When every thing is running normal
00107 {
00108     Q1 = 1;
00109     Q2 = 0;
00110     Q3 = 0;
00111     Q4 = 1;
00112     Q5 = 1;
00113     Q6 = 0;
00114     Q7 = 0;
00115     Q8 = 1;
00116     Q9 = 1;
00117   Water_Valve ();
00118     Q11 = 0;
00119     Q12 = 0;
00120 }
00121 ////////////////////////////////////////////////////////////////////////////////////////////////////
00122 
00123 ////////////////////////////////IT'S A SUB PROGRAM OF THE WATER SENSOR OPARATION////////////////////////////////////////////////////////////////
00124 void Water_Sensor()
00125 {
00126 
00127 uint16_t senval;     //Declare variable to store the water analog value after conversion - 16-bit
00128 uint32_t senval_sum; //Declare variable to store the water analog value sum in loop - 32-bit
00129 double senkvolt;      //Declare 64-bit floating point data type to store water analog killovoltage value
00130    
00131     while(1)    
00132     {
00133     //read the 16-bit value from the analog pin,  The STM32F4-Trainer only supports 12-bit A/D (0 - 4095),
00134         senval = Sen.read_u16();     //but the read_u16 function converts it to a 16-bit value (0 - 65535)
00135         senval_sum = senval_sum + senval;      //Add every analog reading to the sum variable
00136 
00137     //calculate the voltage from the 16-bit value
00138          senkvolt = ((double)senval_sum* 0.00158) / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double
00139         // 1 Volt = 1.58 litres then one kilolitres is 1580 acording to kyleconvert.com
00140         wait (0.2);
00141         }
00142 }
00143 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00144 
00145 ////////////////////////////////IT'S A SUB PROGRAM OF THE WATER METER OPARATION///////////////////////////////////////////////////////////////
00146 void Water_Readings()
00147 {
00148 
00149 uint16_t watval;     //Declare variable to store the water analog value after conversion - 16-bit
00150 uint32_t watval_sum; //Declare variable to store the water analog value sum in loop - 32-bit
00151 double watkvolt;      //Declare 64-bit floating point data type to store water analog killovoltage value
00152  
00153 while(1)
00154     {
00155     //read the 16-bit value from the analog pin,  The STM32F4-Trainer only supports 12-bit A/D (0 - 4095),
00156         watval = Met.read_u16();     //but the read_u16 function converts it to a 16-bit value (0 - 65535)
00157         watval_sum = watval_sum + watval;      //Add every analog reading to the sum variable
00158    
00159     //calculate the voltage from the 16-bit value
00160          watkvolt = ((double)watval_sum * 0.00158) / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double
00161         // 1 Volt = 1.58 litres then one kilolitres is 1580 acording to kyleconvert.com
00162         METER = watkvolt;    
00163         bt.printf(" %2.3f Volts \n", watkvolt); 
00164         wait (0.2);
00165         Normal ();
00166     }
00167 }
00168 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00169 
00170 /////////////////////////////////////IT'S A SUB PROGRAM THAT TEST THE SOLAR PANNEL'S VOLTAGE VALUE/////////////////////////////////////////////
00171 
00172 void Test_Solar()       //The loop to test the Solar Panel's voltage
00173 {
00174     Q1 = 0;
00175     Q2 = 1;
00176     Q3 = 1;
00177     Q4 = 0;
00178     Q5 = 1;
00179     Q6 = 0;
00180     Q7 = 0;
00181     Q8 = 1;
00182     Q9 = 1;
00183    
00184     Q11 = 0;
00185     Q12 = 0;
00186     
00187     uint16_t solval;     //Declare variable to store analog value after conversion - 16-bit
00188     double solvolt;      //Declare 64-bit floating point data type to store analog voltage value
00189     
00190         while (1)
00191         
00192         {
00193             //read the 16-bit value from the analog pin,  The STM32F4-Trainer only supports 12-bit A/D (0 - 4095),
00194             solval = SolarVolt.read_u16();     //but the read_u16 function converts it to a 16-bit value (0 - 65535)
00195           
00196             //calculate the voltage from the 16-bit value
00197             solvolt = ((double)solval*5.9613) / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double
00198             //5.9613 convert the voltage value to equeal the supply
00199             bt.printf("%2.3f Volts \n", solvolt);  
00200             wait (5);
00201             Normal ();
00202         }
00203 }
00204 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00205 
00206 ////////////////////////////////IT'S A SUB PROGRAM THAT TEST THE HYDRO GENERATOR'S VOLTAGE VALUE///////////////////////////////////////////////
00207 
00208 void Test_Hydro()       // the loop to test the Hydro Generator's voltage
00209 {
00210     Q1 = 1;
00211     Q2 = 0;
00212     Q3 = 0;
00213     Q4 = 1;
00214     Q5 = 0;
00215     Q6 = 1;
00216     Q7 = 1;
00217     Q8 = 0; 
00218     Q9 = 1;
00219     
00220     Q11 = 0;
00221     Q12 = 0;
00222     
00223     uint16_t hydval;     //Declare variable to store analog value after conversion - 16-bit
00224     double hydvolt;      //Declare 64-bit floating point data type to store analog voltage value
00225     
00226         while (1)
00227         
00228         {
00229             //read the 16-bit value from the analog pin,  The STM32F4-Trainer only supports 12-bit A/D (0 - 4095),
00230             hydval = HydroVolt.read_u16();     //but the read_u16 function converts it to a 16-bit value (0 - 65535)
00231           
00232             //calculate the voltage from the 16-bit value
00233             hydvolt = ((double)hydval * 5.9613) / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double
00234             //5.9613 convert the voltage value to equeal the supply
00235             bt.printf("%2.3f Volts \n", hydvolt); 
00236             wait (5);
00237             Normal();
00238         }          
00239 }
00240 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00241 
00242 /////////////////////////////////IT'S A SUB PROGRAM THAT TEST THE 12 VOLTS BATTERY'S VOLTAGE VALUE/////////////////////////////////////////////
00243 void Test_Battery()         // The loop to test the battery's voltage
00244 {
00245     Q1 = 0;
00246     Q2 = 0;
00247     Q3 = 0;
00248     Q4 = 0;
00249     Q5 = 0;
00250     Q6 = 0;
00251     Q7 = 0;
00252     Q8 = 0;
00253    
00254     Q9 = 0;
00255     Q11 = 1;
00256     Q12 = 1;
00257     
00258 uint16_t battval;     //Declare variable to store analog value after conversion - 16-bit
00259 double battvolt;      //Declare 64-bit floating point data type to store analog voltage value
00260 while (1)
00261     {
00262     //read the 16-bit value from the analog pin,  The STM32F4-Trainer only supports 12-bit A/D (0 - 4095),
00263         battval = BatVolt.read_u16();     //but the read_u16 function converts it to a 16-bit value (0 - 65535)
00264 
00265     //calculate the voltage from the 16-bit value
00266          battvolt = ((double)battval * 5.9613) / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double
00267         // 5.9613 convert the voltage value to equeal the supply
00268         bt.printf(" %2.3f Volts \n", battvolt); 
00269         wait (5);
00270         Normal ();
00271     } 
00272 }
00273 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00274 
00275 ////////////////////////////DECLARATION OF INCOME MESSAGE AS A CHARACTORS THAT COMES FROM THE PHONE APP//////////////////////////////////////
00276 
00277 char btmsg[50];             //incoming message string
00278 int btmsg_counter = 0;      //character position counter in the message string
00279 int new_btmsg = 0;          //flag to indicate if a new complete message was received
00280 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00281 
00282 
00283 
00284 ////////////////////////////////IT'S A SUB PROGRAM THAT INTERRUPT THE ROUTINE WHEN A NEW CHARACTER ARRIVES/////////////////////////////////////////////
00285 void btinterrupt() 
00286 {    
00287     char c;                                     // declare the c as a charactor
00288     
00289     c = bt.getc();                              //read the incoming character
00290     if(c == '\n')                              //if it is the terminating character
00291        {
00292         if(btmsg[btmsg_counter - 1] == '\r')
00293             btmsg[btmsg_counter - 1] = '\0';
00294         else
00295             btmsg[btmsg_counter] = '\0';
00296         new_btmsg = 1;                          //enable the new message flag to indicate a COMPLETE message was received
00297         btmsg_counter = 0;                      //clear the message string
00298         }
00299     else 
00300         {
00301         btmsg[btmsg_counter] = c;               //add the character to the message string
00302         btmsg_counter++;                        //move to the next character in the string
00303         }   
00304 }
00305 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00306 
00307 /////////////////////////////////////////THE MAIN PROGRAM WHERE IT WILL CALL ALL THE SUB PROGRAM//////////////////////////////////////////////
00308 int main() {
00309     bt.attach(&btinterrupt);        //if a new character arrives, call the interrupt service routine
00310    
00311     while(1) { 
00312         Normal ();
00313         Water_Readings ();
00314         Water_Valve ();
00315         if(new_btmsg) {                                     //check if there is a new message
00316             if(strcmp(btmsg, "SolarON") == 0)              //message for SolarOn is ON
00317                 Test_Solar();  
00318             else if(strcmp(btmsg, "HydrON") == 0)            //message for HydroOn is ON
00319                Test_Hydro() ;
00320             else if(strcmp(btmsg, "BatteryON") == 0)           //message for BatteryON is ON
00321                 Test_Battery();
00322             else if(strcmp(btmsg, "WaterON") == 0)          //message for WaterOn is ON
00323               bt.printf("%2.3f Kl \n",METER);
00324             else if(strcmp(btmsg, "ValveOPEN") == 0)         //message for for ValveOpen is ON
00325                OpenValve();
00326             else if(strcmp(btmsg, "ValveClosed") == 0)         //message for for ValveClosed is ON
00327                ClosedValve();
00328             else
00329                 bt.printf("Incorrect command\n");           //Reply incorrect command
00330                 
00331             new_btmsg=0;                                    //clear message flag
00332         }
00333     }
00334 }
00335 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////