see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/
Fork of GSwifi_xively by
main.cpp@4:9a2415f2ab07, 2013-11-27 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 27 08:18:45 2013 +0000
- Revision:
- 4:9a2415f2ab07
- Parent:
- 3:1abf2be8b312
update GSwifiInterface library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 0:0360c49c3bbe | 1 | #include "mbed.h" |
gsfan | 0:0360c49c3bbe | 2 | #include "GSwifi.h" |
gsfan | 0:0360c49c3bbe | 3 | |
okini3939 | 3:1abf2be8b312 | 4 | #define SAMPLES 1250 |
okini3939 | 3:1abf2be8b312 | 5 | #define INTERVAL 60 |
okini3939 | 3:1abf2be8b312 | 6 | #define CT (3000 / 330 / 0.98) |
okini3939 | 3:1abf2be8b312 | 7 | |
okini3939 | 4:9a2415f2ab07 | 8 | #define SECURE GSwifi::SEC_WPA_PSK |
gsfan | 0:0360c49c3bbe | 9 | #define SSID "SSID" |
gsfan | 0:0360c49c3bbe | 10 | #define PASS "passkey" |
gsfan | 0:0360c49c3bbe | 11 | |
gsfan | 2:a8ab83e57440 | 12 | #define FEED_HOST "api.xively.com" |
gsfan | 0:0360c49c3bbe | 13 | #define FEED_ID "000000" |
gsfan | 0:0360c49c3bbe | 14 | #define API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
gsfan | 0:0360c49c3bbe | 15 | |
okini3939 | 4:9a2415f2ab07 | 16 | //GSwifi gs(p13, p14, NC, NC, p20); // LPC1768, LPC11U24 |
okini3939 | 4:9a2415f2ab07 | 17 | GSwifi gs(PTD3, PTD2, NC, NC, PTA13); // FRDM KL25Z |
gsfan | 0:0360c49c3bbe | 18 | Serial pc(USBTX, USBRX); |
okini3939 | 3:1abf2be8b312 | 19 | DigitalOut led1(LED1), led2(LED2), led3(LED3); |
okini3939 | 3:1abf2be8b312 | 20 | AnalogIn ad0(PTB0), ad1(PTB3), ad2(PTC2); |
gsfan | 0:0360c49c3bbe | 21 | |
okini3939 | 3:1abf2be8b312 | 22 | float ref; |
okini3939 | 3:1abf2be8b312 | 23 | volatile int count = 0; |
okini3939 | 3:1abf2be8b312 | 24 | volatile double sum1 = 0, sum2 = 0; |
okini3939 | 4:9a2415f2ab07 | 25 | volatile int busy = 1, status = 1, timeout = 0; |
okini3939 | 3:1abf2be8b312 | 26 | |
okini3939 | 3:1abf2be8b312 | 27 | |
okini3939 | 3:1abf2be8b312 | 28 | extern "C" void HardFault_Handler() { |
okini3939 | 3:1abf2be8b312 | 29 | register unsigned int _msp __asm("msp"); |
okini3939 | 3:1abf2be8b312 | 30 | printf("Hard Fault! address: %08x\r\n", *((unsigned int *)(_msp + 24))); |
okini3939 | 3:1abf2be8b312 | 31 | exit(-1); |
okini3939 | 3:1abf2be8b312 | 32 | } |
gsfan | 0:0360c49c3bbe | 33 | |
gsfan | 2:a8ab83e57440 | 34 | int xively (char *data) { |
gsfan | 1:0852a9bd5365 | 35 | int i; |
gsfan | 0:0360c49c3bbe | 36 | int cid; |
okini3939 | 3:1abf2be8b312 | 37 | char buf[128]; |
okini3939 | 4:9a2415f2ab07 | 38 | char ip[17]; |
gsfan | 0:0360c49c3bbe | 39 | |
okini3939 | 4:9a2415f2ab07 | 40 | if (gs.getHostByName(FEED_HOST, ip)) return -1; |
okini3939 | 4:9a2415f2ab07 | 41 | cid = gs.open(GSwifi::PROTO_TCP, ip, 80); |
gsfan | 0:0360c49c3bbe | 42 | if (cid < 0) return -1; |
gsfan | 0:0360c49c3bbe | 43 | |
gsfan | 0:0360c49c3bbe | 44 | sprintf(buf, "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n"); |
gsfan | 0:0360c49c3bbe | 45 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 46 | sprintf(buf, "Host: " FEED_HOST "\r\n"); |
gsfan | 0:0360c49c3bbe | 47 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 48 | sprintf(buf, "Connection: close\r\n"); |
gsfan | 0:0360c49c3bbe | 49 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 50 | sprintf(buf, "X-ApiKey: " API_KEY "\r\n"); |
gsfan | 0:0360c49c3bbe | 51 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 52 | sprintf(buf, "Content-Type: text/csv\r\n"); |
gsfan | 0:0360c49c3bbe | 53 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 54 | sprintf(buf, "Content-Length: %d\r\n", strlen(data)); |
gsfan | 0:0360c49c3bbe | 55 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 56 | gs.send(cid, "\r\n", 2); |
gsfan | 0:0360c49c3bbe | 57 | gs.send(cid, data, strlen(data)); |
gsfan | 0:0360c49c3bbe | 58 | |
gsfan | 1:0852a9bd5365 | 59 | for (i = 0; i < 10; i ++) { |
okini3939 | 3:1abf2be8b312 | 60 | gs.poll(); |
gsfan | 1:0852a9bd5365 | 61 | if (! gs.isConnected(cid)) break; |
okini3939 | 3:1abf2be8b312 | 62 | wait_ms(100); |
gsfan | 1:0852a9bd5365 | 63 | } |
gsfan | 0:0360c49c3bbe | 64 | gs.close(cid); |
gsfan | 0:0360c49c3bbe | 65 | return 0; |
gsfan | 0:0360c49c3bbe | 66 | } |
gsfan | 0:0360c49c3bbe | 67 | |
okini3939 | 3:1abf2be8b312 | 68 | void isr_ticker () { |
okini3939 | 4:9a2415f2ab07 | 69 | static int w = 0, x = 0; |
okini3939 | 4:9a2415f2ab07 | 70 | double a1, a2; |
okini3939 | 3:1abf2be8b312 | 71 | |
okini3939 | 4:9a2415f2ab07 | 72 | w ++; |
okini3939 | 4:9a2415f2ab07 | 73 | if (w >= (SAMPLES / 10)) { |
okini3939 | 4:9a2415f2ab07 | 74 | if (status) { |
okini3939 | 4:9a2415f2ab07 | 75 | led1 = x < 5 ? 0 : 1; |
okini3939 | 4:9a2415f2ab07 | 76 | } else { |
okini3939 | 4:9a2415f2ab07 | 77 | led1 = 1; |
okini3939 | 4:9a2415f2ab07 | 78 | } |
okini3939 | 4:9a2415f2ab07 | 79 | |
okini3939 | 4:9a2415f2ab07 | 80 | w = 0; |
okini3939 | 4:9a2415f2ab07 | 81 | x ++; |
okini3939 | 4:9a2415f2ab07 | 82 | if (x >= 10) { |
okini3939 | 4:9a2415f2ab07 | 83 | x = 0; |
okini3939 | 4:9a2415f2ab07 | 84 | led1 = (timeout & 1) ? 0 : 1; |
okini3939 | 4:9a2415f2ab07 | 85 | if (timeout) timeout --; |
okini3939 | 3:1abf2be8b312 | 86 | } |
okini3939 | 3:1abf2be8b312 | 87 | } |
okini3939 | 3:1abf2be8b312 | 88 | |
okini3939 | 3:1abf2be8b312 | 89 | if (busy) return; |
okini3939 | 3:1abf2be8b312 | 90 | a1 = ad1 - ref; |
okini3939 | 3:1abf2be8b312 | 91 | a2 = ad2 - ref; |
okini3939 | 3:1abf2be8b312 | 92 | sum1 += (a1 * a1); |
okini3939 | 3:1abf2be8b312 | 93 | sum2 += (a2 * a2); |
okini3939 | 3:1abf2be8b312 | 94 | count ++; |
okini3939 | 3:1abf2be8b312 | 95 | } |
okini3939 | 3:1abf2be8b312 | 96 | |
gsfan | 0:0360c49c3bbe | 97 | int main () { |
okini3939 | 4:9a2415f2ab07 | 98 | char ipaddr[17], netmask[17], gateway[17]; |
okini3939 | 3:1abf2be8b312 | 99 | Ticker ticker; |
okini3939 | 3:1abf2be8b312 | 100 | int num = 0; |
okini3939 | 3:1abf2be8b312 | 101 | char buf[128]; |
gsfan | 0:0360c49c3bbe | 102 | |
okini3939 | 4:9a2415f2ab07 | 103 | led1 = 0; |
okini3939 | 4:9a2415f2ab07 | 104 | led2 = led3 = 1; |
gsfan | 0:0360c49c3bbe | 105 | pc.baud(115200); |
okini3939 | 3:1abf2be8b312 | 106 | pc.printf("Xively\r\n"); |
okini3939 | 3:1abf2be8b312 | 107 | ticker.attach(isr_ticker, 1.0f / SAMPLES); |
okini3939 | 3:1abf2be8b312 | 108 | |
gsfan | 0:0360c49c3bbe | 109 | pc.printf("connect\r\n"); |
okini3939 | 3:1abf2be8b312 | 110 | NVIC_SetPriority(UART2_IRQn, 1); |
okini3939 | 4:9a2415f2ab07 | 111 | gs.setAddress("wattmeter"); |
okini3939 | 4:9a2415f2ab07 | 112 | gs.setSsid(SECURE, SSID, PASS); |
okini3939 | 4:9a2415f2ab07 | 113 | if (gs.join()) { |
gsfan | 0:0360c49c3bbe | 114 | return -1; |
gsfan | 0:0360c49c3bbe | 115 | } |
okini3939 | 4:9a2415f2ab07 | 116 | gs.getAddress(ipaddr, netmask, gateway); |
okini3939 | 4:9a2415f2ab07 | 117 | pc.printf("ip %s\r\n", ipaddr); |
okini3939 | 3:1abf2be8b312 | 118 | |
okini3939 | 3:1abf2be8b312 | 119 | ref = ad0; |
okini3939 | 3:1abf2be8b312 | 120 | status = 0; |
okini3939 | 3:1abf2be8b312 | 121 | busy = 0; |
okini3939 | 4:9a2415f2ab07 | 122 | timeout = 2; |
okini3939 | 3:1abf2be8b312 | 123 | |
okini3939 | 3:1abf2be8b312 | 124 | for (;;) { |
okini3939 | 3:1abf2be8b312 | 125 | gs.poll(); |
okini3939 | 3:1abf2be8b312 | 126 | |
okini3939 | 4:9a2415f2ab07 | 127 | if (timeout == 0) { |
okini3939 | 4:9a2415f2ab07 | 128 | double a1, a2; |
okini3939 | 4:9a2415f2ab07 | 129 | double f1, f2; |
okini3939 | 3:1abf2be8b312 | 130 | int c; |
gsfan | 0:0360c49c3bbe | 131 | |
okini3939 | 3:1abf2be8b312 | 132 | busy = 1; |
okini3939 | 3:1abf2be8b312 | 133 | a1 = sum1; |
okini3939 | 3:1abf2be8b312 | 134 | a2 = sum2; |
okini3939 | 3:1abf2be8b312 | 135 | c = count; |
okini3939 | 3:1abf2be8b312 | 136 | count = 0; |
okini3939 | 3:1abf2be8b312 | 137 | sum1 = 0; |
okini3939 | 3:1abf2be8b312 | 138 | sum2 = 0; |
okini3939 | 4:9a2415f2ab07 | 139 | timeout = INTERVAL; |
okini3939 | 3:1abf2be8b312 | 140 | num ++; |
okini3939 | 3:1abf2be8b312 | 141 | ref = ad0; |
okini3939 | 3:1abf2be8b312 | 142 | if (c) { |
okini3939 | 3:1abf2be8b312 | 143 | f1 = sqrt(a1 / c); |
okini3939 | 3:1abf2be8b312 | 144 | f2 = sqrt(a2 / c); |
okini3939 | 3:1abf2be8b312 | 145 | if (f1 < 0.001 && f1 > -0.001) { |
okini3939 | 3:1abf2be8b312 | 146 | f1 = 0; |
okini3939 | 3:1abf2be8b312 | 147 | } else { |
okini3939 | 3:1abf2be8b312 | 148 | f1 = f1 * 3.3 * CT; |
okini3939 | 3:1abf2be8b312 | 149 | } |
okini3939 | 3:1abf2be8b312 | 150 | if (f2 < 0.001 && f2 > -0.001) { |
okini3939 | 3:1abf2be8b312 | 151 | f2 = 0; |
okini3939 | 3:1abf2be8b312 | 152 | } else { |
okini3939 | 3:1abf2be8b312 | 153 | f2 = f2 * 3.3 * CT; |
okini3939 | 3:1abf2be8b312 | 154 | } |
okini3939 | 3:1abf2be8b312 | 155 | |
okini3939 | 3:1abf2be8b312 | 156 | led2 = 0; |
okini3939 | 3:1abf2be8b312 | 157 | led3 = 1; |
okini3939 | 4:9a2415f2ab07 | 158 | sprintf(buf, "Total,%0.0f\r\nL1,%0.2f\r\nL2,%0.2f\r\nUptime,%d\r\n", (f1 + f2) * 100, f1, f2, num); |
okini3939 | 3:1abf2be8b312 | 159 | if (xively(buf)) led3 = 0; |
okini3939 | 3:1abf2be8b312 | 160 | pc.printf("L1=%0.3f, L2=%0.3f, Uptime=%d, ref=%0.2f\r\n", f1, f2, num, ref); |
okini3939 | 3:1abf2be8b312 | 161 | led2 = 1; |
okini3939 | 3:1abf2be8b312 | 162 | } |
okini3939 | 3:1abf2be8b312 | 163 | busy = 0; |
okini3939 | 3:1abf2be8b312 | 164 | } |
okini3939 | 3:1abf2be8b312 | 165 | } |
gsfan | 0:0360c49c3bbe | 166 | } |