see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/
Fork of GSwifi_xively by
Diff: main.cpp
- Revision:
- 4:9a2415f2ab07
- Parent:
- 3:1abf2be8b312
--- a/main.cpp Tue Aug 20 04:34:57 2013 +0000 +++ b/main.cpp Wed Nov 27 08:18:45 2013 +0000 @@ -5,7 +5,7 @@ #define INTERVAL 60 #define CT (3000 / 330 / 0.98) -#define SECURE GSwifi::GSSEC_WPA_PSK +#define SECURE GSwifi::SEC_WPA_PSK #define SSID "SSID" #define PASS "passkey" @@ -13,8 +13,8 @@ #define FEED_ID "000000" #define API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -//GSwifi gs(p13, p14, p20); // LPC1768, LPC11U24 -GSwifi gs(PTD3, PTD2, PTA13); // FRDM KL25Z +//GSwifi gs(p13, p14, NC, NC, p20); // LPC1768, LPC11U24 +GSwifi gs(PTD3, PTD2, NC, NC, PTA13); // FRDM KL25Z Serial pc(USBTX, USBRX); DigitalOut led1(LED1), led2(LED2), led3(LED3); AnalogIn ad0(PTB0), ad1(PTB3), ad2(PTC2); @@ -22,8 +22,7 @@ float ref; volatile int count = 0; volatile double sum1 = 0, sum2 = 0; -volatile int busy = 1; -volatile int status = 1; +volatile int busy = 1, status = 1, timeout = 0; extern "C" void HardFault_Handler() { @@ -34,13 +33,12 @@ int xively (char *data) { int i; - Host host; int cid; char buf[128]; + char ip[17]; - host.setName(FEED_HOST); - host.setPort(80); - cid = gs.open(host, GSwifi::GSPROT_TCP); + if (gs.getHostByName(FEED_HOST, ip)) return -1; + cid = gs.open(GSwifi::PROTO_TCP, ip, 80); if (cid < 0) return -1; sprintf(buf, "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n"); @@ -68,17 +66,27 @@ } void isr_ticker () { - float a1, a2; + static int w = 0, x = 0; + double a1, a2; - if (status) { - status ++; - if (status & 0x200) { - led1 = ! led1; + w ++; + if (w >= (SAMPLES / 10)) { + if (status) { + led1 = x < 5 ? 0 : 1; + } else { + led1 = 1; + } + + w = 0; + x ++; + if (x >= 10) { + x = 0; + led1 = (timeout & 1) ? 0 : 1; + if (timeout) timeout --; } } if (busy) return; - a1 = ad1 - ref; a2 = ad2 - ref; sum1 += (a1 * a1); @@ -87,37 +95,38 @@ } int main () { - IpAddr ipaddr, netmask, gateway, nameserver; + char ipaddr[17], netmask[17], gateway[17]; Ticker ticker; - Timer timer; int num = 0; char buf[128]; - led1 = led2 = led3 = 1; + led1 = 0; + led2 = led3 = 1; pc.baud(115200); pc.printf("Xively\r\n"); ticker.attach(isr_ticker, 1.0f / SAMPLES); pc.printf("connect\r\n"); NVIC_SetPriority(UART2_IRQn, 1); - if (gs.connect(SECURE, SSID, PASS, 1, 60)) { + gs.setAddress("wattmeter"); + gs.setSsid(SECURE, SSID, PASS); + if (gs.join()) { return -1; } - gs.getAddress(ipaddr, netmask, gateway, nameserver); - pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); + gs.getAddress(ipaddr, netmask, gateway); + pc.printf("ip %s\r\n", ipaddr); ref = ad0; status = 0; busy = 0; - led1 = 0; + timeout = 2; - timer.start(); for (;;) { gs.poll(); - if (timer.read() >= INTERVAL) { - float a1, a2; - float f1, f2; + if (timeout == 0) { + double a1, a2; + double f1, f2; int c; busy = 1; @@ -127,7 +136,7 @@ count = 0; sum1 = 0; sum2 = 0; - timer.reset(); + timeout = INTERVAL; num ++; ref = ad0; if (c) { @@ -146,7 +155,7 @@ led2 = 0; led3 = 1; - sprintf(buf, "L1,%0.3f\r\nL2,%0.3f\r\nUptime,%d\r\n", f1, f2, num); + sprintf(buf, "Total,%0.0f\r\nL1,%0.2f\r\nL2,%0.2f\r\nUptime,%d\r\n", (f1 + f2) * 100, f1, f2, num); if (xively(buf)) led3 = 0; pc.printf("L1=%0.3f, L2=%0.3f, Uptime=%d, ref=%0.2f\r\n", f1, f2, num, ref); led2 = 1;