Reading Utility metering data using MBED

Dependencies:   mbed

Committer:
pwheels
Date:
Fri May 16 21:44:01 2014 +0000
Revision:
4:1ebd1aabaa16
Parent:
3:5b5166815cd4
unfortunnally the driver had some odd gps test code in it, this has been corrected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pwheels 0:bacf1c9b9afc 1 #if 1
pwheels 0:bacf1c9b9afc 2 /*
pwheels 0:bacf1c9b9afc 3 * Copyright (c) 2012 Paul van der Wielen, Pro-Serv
pwheels 0:bacf1c9b9afc 4 *
pwheels 0:bacf1c9b9afc 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
pwheels 0:bacf1c9b9afc 6 * of this software and associated documentation files (the "Software"), to use
pwheels 0:bacf1c9b9afc 7 * and implement the software for none commercial reason and usage only and
pwheels 0:bacf1c9b9afc 8 * subject to the following conditions:
pwheels 0:bacf1c9b9afc 9 *
pwheels 0:bacf1c9b9afc 10 * The above copyright notice and this permission notice shall be included in
pwheels 0:bacf1c9b9afc 11 * all copies or substantial portions of the Software.
pwheels 0:bacf1c9b9afc 12 *
pwheels 0:bacf1c9b9afc 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pwheels 0:bacf1c9b9afc 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pwheels 0:bacf1c9b9afc 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pwheels 0:bacf1c9b9afc 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pwheels 0:bacf1c9b9afc 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pwheels 0:bacf1c9b9afc 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pwheels 0:bacf1c9b9afc 19 * THE SOFTWARE.
pwheels 0:bacf1c9b9afc 20 */
pwheels 0:bacf1c9b9afc 21 #include "mbed.h"
pwheels 0:bacf1c9b9afc 22 #include <stdlib.h>
pwheels 0:bacf1c9b9afc 23 #include <string>
pwheels 0:bacf1c9b9afc 24
pwheels 4:1ebd1aabaa16 25 #include "mt382.h"
pwheels 0:bacf1c9b9afc 26
pwheels 4:1ebd1aabaa16 27 MT382 mt382 ( p21, p28, p27);
pwheels 0:bacf1c9b9afc 28
pwheels 0:bacf1c9b9afc 29 Serial pc(USBTX, USBRX);
pwheels 3:5b5166815cd4 30 Serial db(p28, p27);
pwheels 3:5b5166815cd4 31 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
pwheels 0:bacf1c9b9afc 32
pwheels 4:1ebd1aabaa16 33 Ticker tx;
pwheels 0:bacf1c9b9afc 34
pwheels 4:1ebd1aabaa16 35 void mt_ticker() {
pwheels 4:1ebd1aabaa16 36 if (mt382.getReading() != 0) {
pwheels 4:1ebd1aabaa16 37 printf("\nNo data found or time-out!\n");
pwheels 4:1ebd1aabaa16 38 } else {
pwheels 4:1ebd1aabaa16 39 printf("Val_0 %.3lf, Val_1 %.3lf, Val_2 %.3lf, Val_3 %.3lf, Val_4 %.3lf, Val_5 %.3lf, Val_6 %.3lf\n", mt382.getValue(0), mt382.getValue(1), mt382.getValue(2), mt382.getValue(3), mt382.getValue(4), mt382.getValue(5), mt382.getValue(6));
pwheels 4:1ebd1aabaa16 40 }
pwheels 4:1ebd1aabaa16 41 }
pwheels 3:5b5166815cd4 42 unsigned char tmpValue[128] = {0};
pwheels 3:5b5166815cd4 43
pwheels 3:5b5166815cd4 44 void dis_cnt (int x_cnt);
pwheels 0:bacf1c9b9afc 45
pwheels 0:bacf1c9b9afc 46 int main() {
pwheels 0:bacf1c9b9afc 47
pwheels 3:5b5166815cd4 48 pc.baud(9600);
pwheels 3:5b5166815cd4 49 db.baud(9600);
pwheels 4:1ebd1aabaa16 50 //init device driver
pwheels 4:1ebd1aabaa16 51 mt382.setComm(9600, 7, 2, 1);
pwheels 4:1ebd1aabaa16 52 mt382.setTime(5.0);
pwheels 4:1ebd1aabaa16 53 mt382.setMatch("1.8.1","1.8.2","2.8.1","2.8.2","1.7.0","2.7.0","24.2.1");
pwheels 0:bacf1c9b9afc 54 // get first pass of MT382 'Smart Meter' data
pwheels 4:1ebd1aabaa16 55 if (mt382.getReading() != 0) {
pwheels 4:1ebd1aabaa16 56 printf("\nNo data found or time-out!\n");
pwheels 4:1ebd1aabaa16 57 } else {
pwheels 4:1ebd1aabaa16 58 printf("\nDone, char cnt %d !!\n", mt382.getCount());
pwheels 4:1ebd1aabaa16 59 printf("Get data value by element\n");
pwheels 4:1ebd1aabaa16 60 printf("Val_0 %.3lf, Val_1 %.3lf, Val_2 %.3lf, Val_3 %.3lf, Val_4 %.3lf, Val_5 %.3lf, Val_6 %.3lf\n", mt382.getValue(0), mt382.getValue(1), mt382.getValue(2), mt382.getValue(3), mt382.getValue(4), mt382.getValue(5), mt382.getValue(6));
pwheels 4:1ebd1aabaa16 61 double * arr2 = mt382.getArray();
pwheels 4:1ebd1aabaa16 62 printf("Get array with data by pointer\n");
pwheels 4:1ebd1aabaa16 63 printf("Val_0 %.3lf, Val_1 %.3lf, Val_2 %.3lf, Val_3 %.3lf, Val_4 %.3lf, Val_5 %.3lf, Val_6 %.3lf\n", arr2[0], arr2[1], arr2[2], arr2[3], arr2[4], arr2[5], arr2[6]);
pwheels 4:1ebd1aabaa16 64 printf("Raw data was: \n%s\n\n", mt382.getRaw());
pwheels 4:1ebd1aabaa16 65 }
pwheels 0:bacf1c9b9afc 66 // now setup collection interval
pwheels 4:1ebd1aabaa16 67 tx.attach(&mt_ticker, 10.0);
pwheels 3:5b5166815cd4 68
pwheels 3:5b5166815cd4 69 int i = 0;
pwheels 0:bacf1c9b9afc 70 while(1) {
pwheels 3:5b5166815cd4 71
pwheels 4:1ebd1aabaa16 72 while (pc.readable())
pwheels 4:1ebd1aabaa16 73 {
pwheels 4:1ebd1aabaa16 74 tmpValue[i++] = pc.getc();
pwheels 4:1ebd1aabaa16 75 led1 = !led1;
pwheels 4:1ebd1aabaa16 76 }
pwheels 3:5b5166815cd4 77 wait(1);
pwheels 4:1ebd1aabaa16 78 dis_cnt (i);
pwheels 4:1ebd1aabaa16 79 if ((tmpValue[0] != 0x00) && (i == 5)) // or (i == 11)
pwheels 4:1ebd1aabaa16 80 {
pwheels 4:1ebd1aabaa16 81 pc.printf("%c%c%c%c%c" , 0x02,0x00,0x0C,0x00,0x03);
pwheels 4:1ebd1aabaa16 82 for(int j=0; j<i; j++) {
pwheels 4:1ebd1aabaa16 83 db.printf("%c" , tmpValue[j]);
pwheels 4:1ebd1aabaa16 84 }
pwheels 4:1ebd1aabaa16 85 tmpValue[0] = 0x00;
pwheels 4:1ebd1aabaa16 86 i = 0;
pwheels 4:1ebd1aabaa16 87 wait(1);
pwheels 4:1ebd1aabaa16 88 led1 = 0;
pwheels 4:1ebd1aabaa16 89 wait(2);
pwheels 4:1ebd1aabaa16 90 led2 = led3 = led4 = 0;
pwheels 4:1ebd1aabaa16 91 }
pwheels 3:5b5166815cd4 92
pwheels 3:5b5166815cd4 93 led1 = 1;
pwheels 3:5b5166815cd4 94 wait(60.2);
pwheels 3:5b5166815cd4 95 led1 = 0;
pwheels 3:5b5166815cd4 96 wait(.2);
pwheels 0:bacf1c9b9afc 97 }
pwheels 0:bacf1c9b9afc 98 }
pwheels 3:5b5166815cd4 99
pwheels 3:5b5166815cd4 100 void dis_cnt (int x_cnt)
pwheels 3:5b5166815cd4 101 {
pwheels 3:5b5166815cd4 102 switch (x_cnt)
pwheels 3:5b5166815cd4 103 {
pwheels 3:5b5166815cd4 104 case 0:
pwheels 3:5b5166815cd4 105 led2 = led3 = led4 = 0;
pwheels 3:5b5166815cd4 106 break;
pwheels 3:5b5166815cd4 107 case 1:
pwheels 3:5b5166815cd4 108 led2 = led3 = 0;
pwheels 3:5b5166815cd4 109 led4 = 1;
pwheels 3:5b5166815cd4 110 break;
pwheels 3:5b5166815cd4 111 case 2:
pwheels 3:5b5166815cd4 112 led2 = led4 = 0;
pwheels 3:5b5166815cd4 113 led3 = 1;
pwheels 3:5b5166815cd4 114 break;
pwheels 3:5b5166815cd4 115 case 3:
pwheels 3:5b5166815cd4 116 led2 = 0;
pwheels 3:5b5166815cd4 117 led3 = led4 = 1;
pwheels 3:5b5166815cd4 118 break;
pwheels 3:5b5166815cd4 119 case 4:
pwheels 3:5b5166815cd4 120 led2 = 1;
pwheels 3:5b5166815cd4 121 led3 = led4 = 0;
pwheels 3:5b5166815cd4 122 break;
pwheels 3:5b5166815cd4 123 case 5:
pwheels 3:5b5166815cd4 124 led2 = led4 = 1;
pwheels 3:5b5166815cd4 125 led3 = 0;
pwheels 3:5b5166815cd4 126 break;
pwheels 3:5b5166815cd4 127 case 6:
pwheels 3:5b5166815cd4 128 led2 = led3 = 1;
pwheels 3:5b5166815cd4 129 led4 = 0;
pwheels 3:5b5166815cd4 130 break;
pwheels 3:5b5166815cd4 131 case 7:
pwheels 3:5b5166815cd4 132 led2 = led3 = led4 = 1;
pwheels 3:5b5166815cd4 133 break;
pwheels 3:5b5166815cd4 134 default:
pwheels 3:5b5166815cd4 135 break;
pwheels 3:5b5166815cd4 136 }
pwheels 3:5b5166815cd4 137 }
pwheels 0:bacf1c9b9afc 138 #endif