see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/

Dependencies:   mbed

Fork of GSwifi_xively by gs fan

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Tue Aug 20 04:34:57 2013 +0000
Parent:
2:a8ab83e57440
Child:
4:9a2415f2ab07
Commit message:
1st build

Changed in this revision

GSwifi.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/GSwifi.lib	Thu May 23 00:59:52 2013 +0000
+++ b/GSwifi.lib	Tue Aug 20 04:34:57 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/gsfan/code/GSwifi/#f5f40c92af00
+http://mbed.org/users/gsfan/code/GSwifi/#72f70748f6e8
--- a/main.cpp	Thu May 23 00:59:52 2013 +0000
+++ b/main.cpp	Tue Aug 20 04:34:57 2013 +0000
@@ -1,6 +1,10 @@
 #include "mbed.h"
 #include "GSwifi.h"
 
+#define SAMPLES 1250
+#define INTERVAL 60
+#define CT (3000 / 330 / 0.98)
+
 #define SECURE GSwifi::GSSEC_WPA_PSK
 #define SSID "SSID"
 #define PASS "passkey"
@@ -9,30 +13,34 @@
 #define FEED_ID "000000"
 #define API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 
-GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
-//GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
+//GSwifi gs(p13, p14, p20); // LPC1768, LPC11U24
+GSwifi gs(PTD3, PTD2, PTA13); // FRDM KL25Z
 Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1), led2(LED2);
+DigitalOut led1(LED1), led2(LED2), led3(LED3);
+AnalogIn ad0(PTB0), ad1(PTB3), ad2(PTC2);
 
-void onGsReceive (int cid, int len) {
-    int i;
-    char buf[GS_DATA_SIZE + 1];
-    
-    led2 = 1;
-    i = gs.recv(cid, buf, len);
-    buf[i] = 0;
-    pc.printf(buf);
-}
+float ref;
+volatile int count = 0;
+volatile double sum1 = 0, sum2 = 0;
+volatile int busy = 1;
+volatile int status = 1;
+
+
+extern "C" void HardFault_Handler() {
+    register unsigned int _msp __asm("msp");
+    printf("Hard Fault! address: %08x\r\n", *((unsigned int *)(_msp + 24)));
+    exit(-1);
+}  
 
 int xively (char *data) {
   int i;
   Host host;
   int cid;
-  char buf[100];
+  char buf[128];
 
   host.setName(FEED_HOST);
   host.setPort(80);
-  cid = gs.open(host, GSwifi::GSPROT_TCP, &onGsReceive);
+  cid = gs.open(host, GSwifi::GSPROT_TCP);
   if (cid < 0) return -1;
   
   sprintf(buf, "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n");
@@ -51,29 +59,99 @@
   gs.send(cid, data, strlen(data));
 
   for (i = 0; i < 10; i ++) {
+    gs.poll();
     if (! gs.isConnected(cid)) break;
-    wait(100);
+    wait_ms(100);
   }
   gs.close(cid);
   return 0;
 }
 
+void isr_ticker () {
+    float a1, a2;
+
+    if (status) {
+        status ++;
+        if (status & 0x200) {
+            led1 = ! led1;
+        }
+    }
+
+    if (busy) return;
+
+    a1 = ad1 - ref;
+    a2 = ad2 - ref;
+    sum1 += (a1 * a1);
+    sum2 += (a2 * a2);
+    count ++;
+}
+
 int main () {
     IpAddr ipaddr, netmask, gateway, nameserver;
-    char data[] = "1,123\r\n2,345\r\n";
+    Ticker ticker;
+    Timer timer;
+    int num = 0;
+    char buf[128];
 
-    led1 = 1;
+    led1 = led2 = led3 = 1;
     pc.baud(115200);
-    
+    pc.printf("Xively\r\n");
+    ticker.attach(isr_ticker, 1.0f / SAMPLES);
+
     pc.printf("connect\r\n");
-    if (gs.connect(SECURE, SSID, PASS, 0)) {
+    NVIC_SetPriority(UART2_IRQn, 1);
+    if (gs.connect(SECURE, SSID, PASS, 1, 60)) {
         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]);
-    
-    pc.printf("Xively\r\n");
-    xively(data);
+
+    ref = ad0;
+    status = 0;
+    busy = 0;
+    led1 = 0;
+
+    timer.start();
+    for (;;) {
+        gs.poll();
+
+        if (timer.read() >= INTERVAL) {
+            float a1, a2;
+            float f1, f2;
+            int c;
 
-    pc.printf("exit\r\n");
+            busy = 1;
+            a1 = sum1;
+            a2 = sum2;
+            c = count;
+            count = 0;
+            sum1 = 0;
+            sum2 = 0;
+            timer.reset();
+            num ++;
+            ref = ad0;
+            if (c) {
+                f1 = sqrt(a1 / c);
+                f2 = sqrt(a2 / c);
+                if (f1 < 0.001 && f1 > -0.001) {
+                    f1 = 0;
+                } else {
+                    f1 = f1 * 3.3 * CT;
+                }
+                if (f2 < 0.001 && f2 > -0.001) {
+                    f2 = 0;
+                } else {
+                    f2 = f2 * 3.3 * CT;
+                }
+
+                led2 = 0;
+                led3 = 1;
+                sprintf(buf, "L1,%0.3f\r\nL2,%0.3f\r\nUptime,%d\r\n", 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;
+            }
+            busy = 0;
+        }
+    }
 }
--- a/mbed.bld	Thu May 23 00:59:52 2013 +0000
+++ b/mbed.bld	Tue Aug 20 04:34:57 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/5e5da4a5990b
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb
\ No newline at end of file