![](/media/cache/profiles/de713869d1db126cff30a1fc0e990962.50x50_q85.jpg)
Meter Reader for mbed 1768
main.cpp@0:c98a5d4d4855, 2012-04-29 (annotated)
- Committer:
- harrisjunaid
- Date:
- Sun Apr 29 13:37:35 2012 +0000
- Revision:
- 0:c98a5d4d4855
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harrisjunaid | 0:c98a5d4d4855 | 1 | |
harrisjunaid | 0:c98a5d4d4855 | 2 | #include "mbed.h" |
harrisjunaid | 0:c98a5d4d4855 | 3 | #include "Serial.h" |
harrisjunaid | 0:c98a5d4d4855 | 4 | |
harrisjunaid | 0:c98a5d4d4855 | 5 | //PC Serial for DEBUG Messages |
harrisjunaid | 0:c98a5d4d4855 | 6 | Serial pc(USBTX, USBRX); |
harrisjunaid | 0:c98a5d4d4855 | 7 | |
harrisjunaid | 0:c98a5d4d4855 | 8 | //Sensor Input |
harrisjunaid | 0:c98a5d4d4855 | 9 | InterruptIn SensorIn(p21);//Sensor Sensed Needle and sends voltaga (minimum > 2V) |
harrisjunaid | 0:c98a5d4d4855 | 10 | Timer SensorIn_Filter; |
harrisjunaid | 0:c98a5d4d4855 | 11 | //mbed Pin Wake |
harrisjunaid | 0:c98a5d4d4855 | 12 | //InterruptIn wake(p22); |
harrisjunaid | 0:c98a5d4d4855 | 13 | |
harrisjunaid | 0:c98a5d4d4855 | 14 | |
harrisjunaid | 0:c98a5d4d4855 | 15 | //Battery Voltage |
harrisjunaid | 0:c98a5d4d4855 | 16 | AnalogIn supplyin(p15);//Connection From XBEE Power Supply |
harrisjunaid | 0:c98a5d4d4855 | 17 | |
harrisjunaid | 0:c98a5d4d4855 | 18 | //Gas Consumption Variable |
harrisjunaid | 0:c98a5d4d4855 | 19 | unsigned GasCounter = 0; |
harrisjunaid | 0:c98a5d4d4855 | 20 | //unsigned GasCounter = 1;//Increment at ev reset/////////.......DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 21 | |
harrisjunaid | 0:c98a5d4d4855 | 22 | //Meter Reader Data Storage |
harrisjunaid | 0:c98a5d4d4855 | 23 | LocalFileSystem fs("fs");//mbed internal flash storage |
harrisjunaid | 0:c98a5d4d4855 | 24 | //FILE *previous;// Pointer for previous_readings.txt (Gas consumption data not yet transmitted ) |
harrisjunaid | 0:c98a5d4d4855 | 25 | //unsigned previous_consumption = 0;//Variable for storing previous consumption (unsigned => only positive value) |
harrisjunaid | 0:c98a5d4d4855 | 26 | |
harrisjunaid | 0:c98a5d4d4855 | 27 | //Indicators |
harrisjunaid | 0:c98a5d4d4855 | 28 | DigitalOut SafeShutdown(LED1);//User can now power off |
harrisjunaid | 0:c98a5d4d4855 | 29 | DigitalOut BoilerOn(LED2);//Indicate to turn on boiler |
harrisjunaid | 0:c98a5d4d4855 | 30 | DigitalOut XbeeOff(LED3);//XBEE can not get power |
harrisjunaid | 0:c98a5d4d4855 | 31 | DigitalOut EndDeviceOff(LED3);//Flash LED2 when End Device is not accessable |
harrisjunaid | 0:c98a5d4d4855 | 32 | DigitalOut PreviousData(LED4);//Previous shutdown was not safe or coordinator was not present |
harrisjunaid | 0:c98a5d4d4855 | 33 | DigitalOut CounterIncrement(LED4);//Indicates Counter Increment |
harrisjunaid | 0:c98a5d4d4855 | 34 | BusOut BatteryLow(LED1, LED2, LED3, LED4);// Battery is Low (Flash from LED1 to LED4) |
harrisjunaid | 0:c98a5d4d4855 | 35 | |
harrisjunaid | 0:c98a5d4d4855 | 36 | //XBEE Connection |
harrisjunaid | 0:c98a5d4d4855 | 37 | Serial xbee(p9, p10);//XBEE Dout (xbee p2) <--> mbed rx p10, XBEE Din (xbee p3) <--> mbed tx p9 |
harrisjunaid | 0:c98a5d4d4855 | 38 | DigitalOut xbee_reset(p8);//XBEE Reset (xbee p5) <--> mbed digital out p8 |
harrisjunaid | 0:c98a5d4d4855 | 39 | DigitalIn xbee_power(p11);//XBEE Power Indicator (xbee p13) Digital In |
harrisjunaid | 0:c98a5d4d4855 | 40 | //DigitalIn xbee_associate(p12);//XBEE Association (xbee p15) Digital In |
harrisjunaid | 0:c98a5d4d4855 | 41 | DigitalIn xbee_CTS(p13);//XBEE pin 12 CTS High = Signal to stop data transfer over UART |
harrisjunaid | 0:c98a5d4d4855 | 42 | DigitalOut xbee_sleep(p14);//XBEE SLEEP_RQ (p9) |
harrisjunaid | 0:c98a5d4d4855 | 43 | unsigned xbeeon_off = 0;//Default OFF |
harrisjunaid | 0:c98a5d4d4855 | 44 | |
harrisjunaid | 0:c98a5d4d4855 | 45 | /*************************************************************************************************** |
harrisjunaid | 0:c98a5d4d4855 | 46 | Functions |
harrisjunaid | 0:c98a5d4d4855 | 47 | ***************************************************************************************************/ |
harrisjunaid | 0:c98a5d4d4855 | 48 | |
harrisjunaid | 0:c98a5d4d4855 | 49 | //Event Based Functions |
harrisjunaid | 0:c98a5d4d4855 | 50 | void MeterReader_On();//Called at module power on |
harrisjunaid | 0:c98a5d4d4855 | 51 | void Reading_Handler();//Manage "GasCounter" Transfer and File Update |
harrisjunaid | 0:c98a5d4d4855 | 52 | void SensorInput();//Called at sensor input |
harrisjunaid | 0:c98a5d4d4855 | 53 | |
harrisjunaid | 0:c98a5d4d4855 | 54 | //File Handling Functions |
harrisjunaid | 0:c98a5d4d4855 | 55 | unsigned CheckCounterFile();//Check reading counter file |
harrisjunaid | 0:c98a5d4d4855 | 56 | void UpdateCounterFile();//Keeps Record of GasCounter |
harrisjunaid | 0:c98a5d4d4855 | 57 | void PreviousData_Indication();//Called when improper module shutdown or coordinator not found |
harrisjunaid | 0:c98a5d4d4855 | 58 | |
harrisjunaid | 0:c98a5d4d4855 | 59 | //XBee Handling Functions |
harrisjunaid | 0:c98a5d4d4855 | 60 | void TransmitGasCounter( unsigned forced);//Transmit Readings |
harrisjunaid | 0:c98a5d4d4855 | 61 | void Xbee_Check();//Xbee Power and Xbee Range |
harrisjunaid | 0:c98a5d4d4855 | 62 | unsigned Xbee_RangeCheck();//XBEE Range Check (Check for END DEVICE) |
harrisjunaid | 0:c98a5d4d4855 | 63 | unsigned Xbee_On_Off();//Return XBEE Power Status |
harrisjunaid | 0:c98a5d4d4855 | 64 | void XbeeSleep(unsigned sleep);//XBEE Pin Sleep |
harrisjunaid | 0:c98a5d4d4855 | 65 | void Xbee_Reset();//Reset xbee |
harrisjunaid | 0:c98a5d4d4855 | 66 | unsigned CTS();//returns 1 when mbed is Clear To Send |
harrisjunaid | 0:c98a5d4d4855 | 67 | void EndDevice_Indication();//Flash LED2 |
harrisjunaid | 0:c98a5d4d4855 | 68 | |
harrisjunaid | 0:c98a5d4d4855 | 69 | //Battery Check Functions |
harrisjunaid | 0:c98a5d4d4855 | 70 | unsigned BatteryCheck();//Check Battery Voltage |
harrisjunaid | 0:c98a5d4d4855 | 71 | void BatteryLow_Indication();//Flash all LEDs indicating Low Battery |
harrisjunaid | 0:c98a5d4d4855 | 72 | |
harrisjunaid | 0:c98a5d4d4855 | 73 | //Boiler On Indication |
harrisjunaid | 0:c98a5d4d4855 | 74 | void BoilerSafeOn(); |
harrisjunaid | 0:c98a5d4d4855 | 75 | /**************************************************************************************************** |
harrisjunaid | 0:c98a5d4d4855 | 76 | Battery Check |
harrisjunaid | 0:c98a5d4d4855 | 77 | ****************************************************************************************************/ |
harrisjunaid | 0:c98a5d4d4855 | 78 | |
harrisjunaid | 0:c98a5d4d4855 | 79 | unsigned BatteryCheck() { |
harrisjunaid | 0:c98a5d4d4855 | 80 | |
harrisjunaid | 0:c98a5d4d4855 | 81 | float battery = 0.0;//Stores sum of three supplyin values |
harrisjunaid | 0:c98a5d4d4855 | 82 | float supply = 0.0;//Stores real battery voltage |
harrisjunaid | 0:c98a5d4d4855 | 83 | |
harrisjunaid | 0:c98a5d4d4855 | 84 | for (int i=0; i<3; i++) {//Takes three values 0-2 |
harrisjunaid | 0:c98a5d4d4855 | 85 | battery = battery + supplyin.read(); |
harrisjunaid | 0:c98a5d4d4855 | 86 | //pc.printf("Loop %d\t", i);//////////////.......DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 87 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 88 | } |
harrisjunaid | 0:c98a5d4d4855 | 89 | supply = (battery /3) * 3.5 ;//Reading Avg * Logic High (battery '1' => 100% '3.5') |
harrisjunaid | 0:c98a5d4d4855 | 90 | //pc.printf("%f\n", supply);/////////.......DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 91 | battery = 0.0; |
harrisjunaid | 0:c98a5d4d4855 | 92 | if ( supply < 3.1 )//Voltage dropped is greater than 0.4V (max of 0.5V drop is acceptable for operation) |
harrisjunaid | 0:c98a5d4d4855 | 93 | return 1; |
harrisjunaid | 0:c98a5d4d4855 | 94 | else |
harrisjunaid | 0:c98a5d4d4855 | 95 | return 0; |
harrisjunaid | 0:c98a5d4d4855 | 96 | } |
harrisjunaid | 0:c98a5d4d4855 | 97 | |
harrisjunaid | 0:c98a5d4d4855 | 98 | void BatteryLow_Indication() {////Flash from LED1 to LED4 |
harrisjunaid | 0:c98a5d4d4855 | 99 | for (int i=0; i<4; i++) { |
harrisjunaid | 0:c98a5d4d4855 | 100 | BatteryLow = 1 << i; |
harrisjunaid | 0:c98a5d4d4855 | 101 | wait(0.25); |
harrisjunaid | 0:c98a5d4d4855 | 102 | } |
harrisjunaid | 0:c98a5d4d4855 | 103 | } |
harrisjunaid | 0:c98a5d4d4855 | 104 | |
harrisjunaid | 0:c98a5d4d4855 | 105 | /**************************************************************** |
harrisjunaid | 0:c98a5d4d4855 | 106 | XBEE Control Functios |
harrisjunaid | 0:c98a5d4d4855 | 107 | ****************************************************************/ |
harrisjunaid | 0:c98a5d4d4855 | 108 | |
harrisjunaid | 0:c98a5d4d4855 | 109 | void TransmitGasCounter(unsigned forced) { |
harrisjunaid | 0:c98a5d4d4855 | 110 | pc.printf("\nTransmitGasCounter called GasCounter = %u",GasCounter);//.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 111 | if ( (GasCounter >= 5) || (forced == 1) ) {//Previous Gas Counter (Due To Improper Shut Down) (Forced used for previous GasCounter) |
harrisjunaid | 0:c98a5d4d4855 | 112 | while ( !(CTS()) );//Wait Till mbed is not Clear To Send |
harrisjunaid | 0:c98a5d4d4855 | 113 | xbee.putc('*');//Special Character Identifying Reading Transmit |
harrisjunaid | 0:c98a5d4d4855 | 114 | xbee.putc('\r'); |
harrisjunaid | 0:c98a5d4d4855 | 115 | //xbee.printf("%u\r", GasCounter);//%u", GasCounter); |
harrisjunaid | 0:c98a5d4d4855 | 116 | xbee.printf("%u", GasCounter);//***TRANSMIT GAS COUNTER*** |
harrisjunaid | 0:c98a5d4d4855 | 117 | GasCounter = 0; |
harrisjunaid | 0:c98a5d4d4855 | 118 | //UpdateCounterFile(); |
harrisjunaid | 0:c98a5d4d4855 | 119 | } |
harrisjunaid | 0:c98a5d4d4855 | 120 | } |
harrisjunaid | 0:c98a5d4d4855 | 121 | |
harrisjunaid | 0:c98a5d4d4855 | 122 | void Xbee_Check() {//Check Xbee Power and Range |
harrisjunaid | 0:c98a5d4d4855 | 123 | while (Xbee_On_Off() );//Execute Till XBEE is Powered OFF |
harrisjunaid | 0:c98a5d4d4855 | 124 | while (Xbee_RangeCheck() );//Execute Till End DEVICE is not Powered ON |
harrisjunaid | 0:c98a5d4d4855 | 125 | } |
harrisjunaid | 0:c98a5d4d4855 | 126 | |
harrisjunaid | 0:c98a5d4d4855 | 127 | unsigned Xbee_On_Off() {//Return XBEE Power Status |
harrisjunaid | 0:c98a5d4d4855 | 128 | if (!xbee_power) {//XBEE Powered On |
harrisjunaid | 0:c98a5d4d4855 | 129 | XbeeOff = 1;//XBEE OFF Indicator |
harrisjunaid | 0:c98a5d4d4855 | 130 | xbeeon_off = 1; |
harrisjunaid | 0:c98a5d4d4855 | 131 | return 1;//Retrun XBEE is OFF |
harrisjunaid | 0:c98a5d4d4855 | 132 | } else {//Xbee is On |
harrisjunaid | 0:c98a5d4d4855 | 133 | XbeeOff = 0;//Will Send a Ground Signal (LED will Blink) |
harrisjunaid | 0:c98a5d4d4855 | 134 | xbeeon_off = 0; |
harrisjunaid | 0:c98a5d4d4855 | 135 | return 0;//Return XBEE is ON |
harrisjunaid | 0:c98a5d4d4855 | 136 | } |
harrisjunaid | 0:c98a5d4d4855 | 137 | } |
harrisjunaid | 0:c98a5d4d4855 | 138 | |
harrisjunaid | 0:c98a5d4d4855 | 139 | unsigned Xbee_RangeCheck() { |
harrisjunaid | 0:c98a5d4d4855 | 140 | |
harrisjunaid | 0:c98a5d4d4855 | 141 | char response[5]; |
harrisjunaid | 0:c98a5d4d4855 | 142 | wait(10);//Allow Time For Communication |
harrisjunaid | 0:c98a5d4d4855 | 143 | for (int i=1 ; i<=3 ; i++) {//Enter XBEE in AT Command Mode |
harrisjunaid | 0:c98a5d4d4855 | 144 | xbee.putc('+'); |
harrisjunaid | 0:c98a5d4d4855 | 145 | wait(0.05); |
harrisjunaid | 0:c98a5d4d4855 | 146 | } |
harrisjunaid | 0:c98a5d4d4855 | 147 | xbee.scanf("%s",response);//Get Xbee Response |
harrisjunaid | 0:c98a5d4d4855 | 148 | //pc.printf("\n%s\n",response);//////////////...........DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 149 | |
harrisjunaid | 0:c98a5d4d4855 | 150 | //XBEE IS IN COMMAND MODE AND OK RESPONSE IS READ |
harrisjunaid | 0:c98a5d4d4855 | 151 | xbee.printf("ATNDLOGGER");//AT(AT Command Mode) DN(Destination Node Command) LOGGER(Node Identifier "NI" for END DEVICE) |
harrisjunaid | 0:c98a5d4d4855 | 152 | xbee.putc('\r');//Carriage Return |
harrisjunaid | 0:c98a5d4d4855 | 153 | xbee.scanf("%s",response);//Get Response |
harrisjunaid | 0:c98a5d4d4855 | 154 | //pc.printf("\n%s\n",response);/////////////.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 155 | xbee.printf("ATCN"); |
harrisjunaid | 0:c98a5d4d4855 | 156 | xbee.putc('\r'); |
harrisjunaid | 0:c98a5d4d4855 | 157 | if (response[0] == 'E') {//Could Not Find End Device ("ERROR"\r returned) |
harrisjunaid | 0:c98a5d4d4855 | 158 | pc.printf("\nCound not find END DEVICE\n");/////////.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 159 | EndDevice_Indication(); |
harrisjunaid | 0:c98a5d4d4855 | 160 | return 1;//Return No End Device |
harrisjunaid | 0:c98a5d4d4855 | 161 | |
harrisjunaid | 0:c98a5d4d4855 | 162 | } else {//End Device is Active ("OK"\r returned) |
harrisjunaid | 0:c98a5d4d4855 | 163 | //pc.printf("\nEnd Device Responded\n");/////////.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 164 | return 0;//Return End Device is ON |
harrisjunaid | 0:c98a5d4d4855 | 165 | } |
harrisjunaid | 0:c98a5d4d4855 | 166 | } |
harrisjunaid | 0:c98a5d4d4855 | 167 | |
harrisjunaid | 0:c98a5d4d4855 | 168 | unsigned CTS() {//returns 1 when mbed is Clear To Send |
harrisjunaid | 0:c98a5d4d4855 | 169 | if (xbee_CTS == 0) return 1;//mbed is Clear To Send |
harrisjunaid | 0:c98a5d4d4855 | 170 | else return 0; |
harrisjunaid | 0:c98a5d4d4855 | 171 | } |
harrisjunaid | 0:c98a5d4d4855 | 172 | |
harrisjunaid | 0:c98a5d4d4855 | 173 | void Xbee_Reset() { |
harrisjunaid | 0:c98a5d4d4855 | 174 | xbee_reset = 0;//RESET |
harrisjunaid | 0:c98a5d4d4855 | 175 | wait_ms(1); |
harrisjunaid | 0:c98a5d4d4855 | 176 | xbee_reset = 1;//Back To Operation |
harrisjunaid | 0:c98a5d4d4855 | 177 | wait_ms(1); |
harrisjunaid | 0:c98a5d4d4855 | 178 | } |
harrisjunaid | 0:c98a5d4d4855 | 179 | |
harrisjunaid | 0:c98a5d4d4855 | 180 | void XbeeSleep(unsigned sleep) { |
harrisjunaid | 0:c98a5d4d4855 | 181 | //pc.printf("\nSleep Called\n");////////////............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 182 | if (sleep == 1) xbee_sleep = 1;//SLEEP_RQ pin 9 |
harrisjunaid | 0:c98a5d4d4855 | 183 | else xbee_sleep = 0; |
harrisjunaid | 0:c98a5d4d4855 | 184 | wait(2);//Time for XBEE to Respond |
harrisjunaid | 0:c98a5d4d4855 | 185 | } |
harrisjunaid | 0:c98a5d4d4855 | 186 | |
harrisjunaid | 0:c98a5d4d4855 | 187 | void EndDevice_Indication() {//Flash LED2 => NO END DEVICE |
harrisjunaid | 0:c98a5d4d4855 | 188 | for (unsigned i=0; i<=5 ; i++) { |
harrisjunaid | 0:c98a5d4d4855 | 189 | EndDeviceOff = 1; |
harrisjunaid | 0:c98a5d4d4855 | 190 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 191 | EndDeviceOff = 0; |
harrisjunaid | 0:c98a5d4d4855 | 192 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 193 | EndDeviceOff = 1; |
harrisjunaid | 0:c98a5d4d4855 | 194 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 195 | EndDeviceOff = 0; |
harrisjunaid | 0:c98a5d4d4855 | 196 | } |
harrisjunaid | 0:c98a5d4d4855 | 197 | } |
harrisjunaid | 0:c98a5d4d4855 | 198 | |
harrisjunaid | 0:c98a5d4d4855 | 199 | void BoilerSafeOn() {//LED2 |
harrisjunaid | 0:c98a5d4d4855 | 200 | BoilerOn = 1; |
harrisjunaid | 0:c98a5d4d4855 | 201 | wait(4); |
harrisjunaid | 0:c98a5d4d4855 | 202 | BoilerOn = 0; |
harrisjunaid | 0:c98a5d4d4855 | 203 | } |
harrisjunaid | 0:c98a5d4d4855 | 204 | /**************************************************************** |
harrisjunaid | 0:c98a5d4d4855 | 205 | FILE MANIPULATION FUNCTIONS |
harrisjunaid | 0:c98a5d4d4855 | 206 | |
harrisjunaid | 0:c98a5d4d4855 | 207 | Checks if reading record file is present. File is created if not |
harrisjunaid | 0:c98a5d4d4855 | 208 | found or else it will be read for previous untransmitted counter. |
harrisjunaid | 0:c98a5d4d4855 | 209 | Variable previous_consumption has global scope. |
harrisjunaid | 0:c98a5d4d4855 | 210 | ****************************************************************/ |
harrisjunaid | 0:c98a5d4d4855 | 211 | |
harrisjunaid | 0:c98a5d4d4855 | 212 | unsigned CheckCounterFile() {// Checks For Previous Readings |
harrisjunaid | 0:c98a5d4d4855 | 213 | |
harrisjunaid | 0:c98a5d4d4855 | 214 | FILE *counter;// Pointer for COUNTER.txt (Gas consumption data) |
harrisjunaid | 0:c98a5d4d4855 | 215 | |
harrisjunaid | 0:c98a5d4d4855 | 216 | if ( (counter = fopen("/fs/COUNTER.txt", "r")) == NULL) {// Check if the file exist |
harrisjunaid | 0:c98a5d4d4855 | 217 | pc.printf("File not found so created\n");//////////////////////................DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 218 | counter = fopen("/fs/COUNTER.txt", "w");//File created mode = w |
harrisjunaid | 0:c98a5d4d4855 | 219 | |
harrisjunaid | 0:c98a5d4d4855 | 220 | } else {//File exist |
harrisjunaid | 0:c98a5d4d4855 | 221 | |
harrisjunaid | 0:c98a5d4d4855 | 222 | fscanf( counter,"%u", &GasCounter) ;//Update GasCounter |
harrisjunaid | 0:c98a5d4d4855 | 223 | pc.printf( "previous value %u\n", GasCounter);////////................DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 224 | |
harrisjunaid | 0:c98a5d4d4855 | 225 | |
harrisjunaid | 0:c98a5d4d4855 | 226 | } |
harrisjunaid | 0:c98a5d4d4855 | 227 | fclose(counter);//close file handler |
harrisjunaid | 0:c98a5d4d4855 | 228 | |
harrisjunaid | 0:c98a5d4d4855 | 229 | //Return status for indication activation |
harrisjunaid | 0:c98a5d4d4855 | 230 | if (GasCounter > 0) { |
harrisjunaid | 0:c98a5d4d4855 | 231 | |
harrisjunaid | 0:c98a5d4d4855 | 232 | return 1;//Return There Were Previous Readings |
harrisjunaid | 0:c98a5d4d4855 | 233 | } else return 0;//Return No Previous Reading |
harrisjunaid | 0:c98a5d4d4855 | 234 | |
harrisjunaid | 0:c98a5d4d4855 | 235 | } |
harrisjunaid | 0:c98a5d4d4855 | 236 | |
harrisjunaid | 0:c98a5d4d4855 | 237 | void UpdateCounterFile() { |
harrisjunaid | 0:c98a5d4d4855 | 238 | pc.printf("\nUpdateCounterFile called GasCounter = %u",GasCounter);//.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 239 | unsigned PreviousCounter = 0; |
harrisjunaid | 0:c98a5d4d4855 | 240 | FILE *counter;// Pointer for COUNTER.txt (Gas consumption data) |
harrisjunaid | 0:c98a5d4d4855 | 241 | if (GasCounter == 0) |
harrisjunaid | 0:c98a5d4d4855 | 242 | counter = fopen("/fs/COUNTER.txt", "w");//Contents Discarded |
harrisjunaid | 0:c98a5d4d4855 | 243 | else { |
harrisjunaid | 0:c98a5d4d4855 | 244 | counter = fopen("/fs/COUNTER.txt", "r"); |
harrisjunaid | 0:c98a5d4d4855 | 245 | fscanf( counter,"%u", &PreviousCounter); |
harrisjunaid | 0:c98a5d4d4855 | 246 | fclose(counter); |
harrisjunaid | 0:c98a5d4d4855 | 247 | counter = fopen("/fs/COUNTER.txt", "w");//Contents Discarded |
harrisjunaid | 0:c98a5d4d4855 | 248 | GasCounter = GasCounter + PreviousCounter; |
harrisjunaid | 0:c98a5d4d4855 | 249 | fprintf( counter, "%u", GasCounter ); |
harrisjunaid | 0:c98a5d4d4855 | 250 | pc.printf("\rCounter file update: %u\r");///////......DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 251 | GasCounter = 0; |
harrisjunaid | 0:c98a5d4d4855 | 252 | } |
harrisjunaid | 0:c98a5d4d4855 | 253 | fclose(counter); |
harrisjunaid | 0:c98a5d4d4855 | 254 | } |
harrisjunaid | 0:c98a5d4d4855 | 255 | |
harrisjunaid | 0:c98a5d4d4855 | 256 | |
harrisjunaid | 0:c98a5d4d4855 | 257 | |
harrisjunaid | 0:c98a5d4d4855 | 258 | void PreviousData_Indication() {//LED4 is Flashed (Indicating previous shut down was not safe) |
harrisjunaid | 0:c98a5d4d4855 | 259 | for (unsigned i=0; i<=5 ; i++) { |
harrisjunaid | 0:c98a5d4d4855 | 260 | PreviousData = 1; |
harrisjunaid | 0:c98a5d4d4855 | 261 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 262 | PreviousData = 0; |
harrisjunaid | 0:c98a5d4d4855 | 263 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 264 | PreviousData = 1; |
harrisjunaid | 0:c98a5d4d4855 | 265 | wait(0.2); |
harrisjunaid | 0:c98a5d4d4855 | 266 | PreviousData = 0; |
harrisjunaid | 0:c98a5d4d4855 | 267 | |
harrisjunaid | 0:c98a5d4d4855 | 268 | } |
harrisjunaid | 0:c98a5d4d4855 | 269 | } |
harrisjunaid | 0:c98a5d4d4855 | 270 | |
harrisjunaid | 0:c98a5d4d4855 | 271 | ///////////////////////////////////////////////////////////////// |
harrisjunaid | 0:c98a5d4d4855 | 272 | void MeterReader_On() { |
harrisjunaid | 0:c98a5d4d4855 | 273 | pc.printf("\nMeterReader_On called GasCounter = %u",GasCounter);//.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 274 | //Battery Voltage Check |
harrisjunaid | 0:c98a5d4d4855 | 275 | while ( BatteryCheck() )//Till Battery is not sufficiently charged |
harrisjunaid | 0:c98a5d4d4855 | 276 | BatteryLow_Indication();//Battery Low Indication |
harrisjunaid | 0:c98a5d4d4855 | 277 | |
harrisjunaid | 0:c98a5d4d4855 | 278 | //Last Shutdown Check |
harrisjunaid | 0:c98a5d4d4855 | 279 | if ( CheckCounterFile() )//GasCounter update from previous untransmitted record and return status |
harrisjunaid | 0:c98a5d4d4855 | 280 | PreviousData_Indication();//LED 4 Flash |
harrisjunaid | 0:c98a5d4d4855 | 281 | |
harrisjunaid | 0:c98a5d4d4855 | 282 | |
harrisjunaid | 0:c98a5d4d4855 | 283 | //Reset XBEE |
harrisjunaid | 0:c98a5d4d4855 | 284 | Xbee_Reset(); |
harrisjunaid | 0:c98a5d4d4855 | 285 | |
harrisjunaid | 0:c98a5d4d4855 | 286 | //Check XBEE Power and Communication |
harrisjunaid | 0:c98a5d4d4855 | 287 | Xbee_Check();//Checks for XBEE and provides LED indication |
harrisjunaid | 0:c98a5d4d4855 | 288 | //Will not move forward in case of an error |
harrisjunaid | 0:c98a5d4d4855 | 289 | |
harrisjunaid | 0:c98a5d4d4855 | 290 | //Check For Previous Counter and Transmit if any |
harrisjunaid | 0:c98a5d4d4855 | 291 | if (GasCounter > 0)TransmitGasCounter(1);//FORCE TO TRANSMIT PREVIOUS READINGS IF ANY |
harrisjunaid | 0:c98a5d4d4855 | 292 | //TransmitGasCounter will reset GasCounter after reading transmition |
harrisjunaid | 0:c98a5d4d4855 | 293 | |
harrisjunaid | 0:c98a5d4d4855 | 294 | //XBEE to sleep |
harrisjunaid | 0:c98a5d4d4855 | 295 | XbeeSleep(1); |
harrisjunaid | 0:c98a5d4d4855 | 296 | |
harrisjunaid | 0:c98a5d4d4855 | 297 | //Boiler Can Be Turned On |
harrisjunaid | 0:c98a5d4d4855 | 298 | BoilerSafeOn(); |
harrisjunaid | 0:c98a5d4d4855 | 299 | |
harrisjunaid | 0:c98a5d4d4855 | 300 | // Xbee_Check();////XBee should be powered off//////..........DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 301 | // SafeShutdown = 1;//////////////.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 302 | // deepsleep(); |
harrisjunaid | 0:c98a5d4d4855 | 303 | //SafeShutdown = 0;//////////////////..........DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 304 | |
harrisjunaid | 0:c98a5d4d4855 | 305 | } |
harrisjunaid | 0:c98a5d4d4855 | 306 | |
harrisjunaid | 0:c98a5d4d4855 | 307 | void Reading_Handler() { |
harrisjunaid | 0:c98a5d4d4855 | 308 | pc.printf("\nReading_Handler called GasCounter = %u",GasCounter);//.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 309 | if (GasCounter >= 4) {//Function Internal Check |
harrisjunaid | 0:c98a5d4d4855 | 310 | XbeeSleep(0); |
harrisjunaid | 0:c98a5d4d4855 | 311 | wait(5); |
harrisjunaid | 0:c98a5d4d4855 | 312 | TransmitGasCounter(1);//Readings are above limit SO FORCE TRANSMIT |
harrisjunaid | 0:c98a5d4d4855 | 313 | |
harrisjunaid | 0:c98a5d4d4855 | 314 | UpdateCounterFile();//Update GasCounter in file (Record is maintained in file to prevent power off data loss) |
harrisjunaid | 0:c98a5d4d4855 | 315 | } |
harrisjunaid | 0:c98a5d4d4855 | 316 | |
harrisjunaid | 0:c98a5d4d4855 | 317 | |
harrisjunaid | 0:c98a5d4d4855 | 318 | XbeeSleep(1); |
harrisjunaid | 0:c98a5d4d4855 | 319 | //SafeShutdown = !SafeShutdown;////LED1//////////.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 320 | //deepsleep(); |
harrisjunaid | 0:c98a5d4d4855 | 321 | } |
harrisjunaid | 0:c98a5d4d4855 | 322 | |
harrisjunaid | 0:c98a5d4d4855 | 323 | void SensorInput() { |
harrisjunaid | 0:c98a5d4d4855 | 324 | if ((SensorIn_Filter.read()) == 0) { |
harrisjunaid | 0:c98a5d4d4855 | 325 | SensorIn_Filter.start(); |
harrisjunaid | 0:c98a5d4d4855 | 326 | CounterIncrement = 1;///////LED2.............DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 327 | } |
harrisjunaid | 0:c98a5d4d4855 | 328 | while (1) { |
harrisjunaid | 0:c98a5d4d4855 | 329 | if ( (SensorIn_Filter.read() ) >= 2) {//Readings within last 2 sec from call are ignored as SensorIn noise |
harrisjunaid | 0:c98a5d4d4855 | 330 | |
harrisjunaid | 0:c98a5d4d4855 | 331 | GasCounter = GasCounter + 1;//Update Gas Counter as a result of sensor input |
harrisjunaid | 0:c98a5d4d4855 | 332 | pc.printf("\nSensorInput incremented GasCountre: %u", GasCounter);//////////.........DEBUG |
harrisjunaid | 0:c98a5d4d4855 | 333 | |
harrisjunaid | 0:c98a5d4d4855 | 334 | SensorIn_Filter.stop(); |
harrisjunaid | 0:c98a5d4d4855 | 335 | SensorIn_Filter.reset(); |
harrisjunaid | 0:c98a5d4d4855 | 336 | CounterIncrement = 0; |
harrisjunaid | 0:c98a5d4d4855 | 337 | break; |
harrisjunaid | 0:c98a5d4d4855 | 338 | } |
harrisjunaid | 0:c98a5d4d4855 | 339 | } |
harrisjunaid | 0:c98a5d4d4855 | 340 | } |
harrisjunaid | 0:c98a5d4d4855 | 341 | |
harrisjunaid | 0:c98a5d4d4855 | 342 | int main() { |
harrisjunaid | 0:c98a5d4d4855 | 343 | |
harrisjunaid | 0:c98a5d4d4855 | 344 | SensorIn.rise(&SensorInput); |
harrisjunaid | 0:c98a5d4d4855 | 345 | MeterReader_On(); |
harrisjunaid | 0:c98a5d4d4855 | 346 | while (1) { |
harrisjunaid | 0:c98a5d4d4855 | 347 | |
harrisjunaid | 0:c98a5d4d4855 | 348 | if (GasCounter>=5) { |
harrisjunaid | 0:c98a5d4d4855 | 349 | |
harrisjunaid | 0:c98a5d4d4855 | 350 | Reading_Handler(); |
harrisjunaid | 0:c98a5d4d4855 | 351 | } |
harrisjunaid | 0:c98a5d4d4855 | 352 | } |
harrisjunaid | 0:c98a5d4d4855 | 353 | |
harrisjunaid | 0:c98a5d4d4855 | 354 | } |