k64f_OneNET

Dependencies:   EthernetInterface FXOS8700Q eCompass_FPU_Lib mbed-rtos mbed mbed

Fork of K64F_eCompass by Jim Carver

Files at this revision

API Documentation at this revision

Comitter:
robert_jw
Date:
Mon Jun 20 01:40:34 2016 +0000
Parent:
3:d6404f10bd3b
Child:
5:15c4a67df599
Commit message:
OneNET;

Changed in this revision

EthernetInterface.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-rtos.lib 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
mbed.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Jun 20 01:40:34 2016 +0000
@@ -0,0 +1,1 @@
+EthernetInterface#b2805b6888dc
--- a/main.cpp	Fri May 09 16:30:47 2014 +0000
+++ b/main.cpp	Mon Jun 20 01:40:34 2016 +0000
@@ -3,21 +3,20 @@
 #include "eCompass_Lib.h"
 #include "rtos.h"
 //#include "MotionSensorDtypes.h"
+#include "EthernetInterface.h"
 
 
 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
 FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
 Serial pc(USBTX, USBRX);
-DigitalOut gpo(D0);
-DigitalOut led(LED_RED);
 eCompass compass;
+DigitalOut red(LED_RED);        //debug led
+DigitalOut green(LED_GREEN);    //debug ledeCompass compass;
 
 //void calibrate_thread(void const *argument);
 //void print_thread(void const *argument);
 //void compass_thread(void const *argument);
 
-
-
 extern axis6_t axis6;
 extern uint32_t seconds;
 extern uint32_t compass_type; // optional, NED compass is default
@@ -26,9 +25,18 @@
 int  l = 0;
 volatile int sflag = 0;
 
+char http_cmd[500];
+char devid[] = "1100227";
+char api_key[] ="Jl4VyAUUG6j4Qdms3ZyOjbAu6J8=";
+char host[] = "api.heclouds.com";
+
+EthernetInterface eth;          //create ethernet
+TCPSocketConnection sock;       
+
 MotionSensorDataCounts mag_raw;
 MotionSensorDataCounts acc_raw;
 
+
 void hal_map( MotionSensorDataCounts * acc_raw, MotionSensorDataCounts * mag_raw)
 {
 int16_t t;
@@ -44,6 +52,27 @@
 mag_raw->z *= -1;
 }
 
+void creat_cmd()
+{
+    char tmp[30];
+    char str[100];
+
+    sprintf(str,",;roll,%d;pitch,%d;yaw,%d", axis6.roll, axis6.pitch, axis6.yaw);
+    http_cmd[0] = 0;
+    strcat(http_cmd,"POST /devices/");
+    strcat(http_cmd,devid);
+    strcat(http_cmd,"/datapoints?type=5 HTTP/1.1\r\n");
+    strcat(http_cmd,"api-key:");
+    strcat(http_cmd,api_key);
+    strcat(http_cmd,"\r\n");
+    strcat(http_cmd,"Host:");
+    strcat(http_cmd,host);
+    strcat(http_cmd,"\r\n");
+    sprintf(tmp,"Content-Length:%d\r\n\r\n", strlen(str));
+    strcat(http_cmd,tmp);
+    strcat(http_cmd,str);
+}
+
 //
 // Print data values for debug
 //
@@ -57,59 +86,86 @@
 }
 
 
-void compass_thread(void const *argument) {
-
+void compass_thread(void const *argument) 
+{
     // get raw data from the sensors
     acc.getAxis( acc_raw);
     mag.getAxis( mag_raw);
     if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass
-    if(l++ >= 50) { // take car of business once a second
+    if(l++ >= 50) 
+    {
+         // take car of business once a second
         seconds++;
         sflag = 1;
         compass.calibrate();
         debug_print();
         l = 0;
-        led = !led;
+        
+        if(sock.is_connected()) 
+        {
+            green = 0;
+            red = 1;
+            creat_cmd();
+            sock.send(http_cmd, strlen(http_cmd));
+            //printf("%s\r\n",http_cmd);
         }
+        else
+        {
+            green = 1;
+            red = 0;
+            sock.close();
+            
+            wait(3);
+            
+            printf("IP Address is %s\r\n", eth.getIPAddress());
+            sock.connect("api.heclouds.com", 80);   //connetct to OneNET
+            
+            printf("reconnect the server\r\n");
+            while(!sock.is_connected());
+        }
+        
+    }
     tcount++;
 }
- 
-/*  
-void calibrate_thread(void const *argument) {
-    while (true) {
-        // Signal flags that are reported as event are automatically cleared.
-        Thread::signal_wait(0x1);
-        compass.calibrate(); // re-calibrate the eCompass every second
-    }
-}
- 
-
-  
-void print_thread(void const *argument) {
-    while (true) {
-        // Signal flags that are reported as event are automatically cleared.
-        Thread::signal_wait(0x1);
-        debug_print(); // re-calibrate the eCompass every second
-    }
-}
- */            
-
-int main() {
 
 
-RtosTimer compass_timer(compass_thread, osTimerPeriodic);
-
-//cdebug = 1;  // uncomment to disable compass
-printf("\r\n\n\n\n\n\n\n");
-printf("Who AM I= %X\r\n", acc.whoAmI());
-acc.enable();
+int main() 
+{
+    RtosTimer compass_timer(compass_thread, osTimerPeriodic);
+    
+    /* ethernet init */
+    eth.init(); //DHCP
+    eth.connect();
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+    sock.connect("api.heclouds.com", 80);   //connetct to OneNET
 
-
-acc.getAxis( acc_raw);
-mag.getAxis( mag_raw);
-
-compass_timer.start(20); // Run the Compass every 20ms
-while(1) {
-    Thread::wait(osWaitForever);
+    printf("\r\n\n\n\n\n\n\n");
+    printf("Who AM I= %X\r\n", acc.whoAmI());
+    acc.enable();
+    
+    acc.getAxis( acc_raw);
+    mag.getAxis( mag_raw);
+    
+    compass_timer.start(20); // Run the Compass every 20ms
+    while(1) 
+    {
+        //Thread::wait(osWaitForever);
+        /**********receive response data*******************/
+        char buffer[300];
+        int ret = 0;
+        buffer[0] = '\0';
+        if(sock.is_connected())
+        {
+            ret = sock.receive(buffer, sizeof(buffer)-1);
+            if(ret >= 0) 
+            {
+                buffer[ret] = '\0';
+            }
+            //printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);
+        }
+        else
+        {
+            wait(3);
+        }
     }  
 }
--- a/mbed-rtos.lib	Fri May 09 16:30:47 2014 +0000
+++ b/mbed-rtos.lib	Mon Jun 20 01:40:34 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d
+http://mbed.org/users/mbed_official/code/mbed-rtos/#162b12aea5f2
--- a/mbed.bld	Fri May 09 16:30:47 2014 +0000
+++ b/mbed.bld	Mon Jun 20 01:40:34 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Mon Jun 20 01:40:34 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbotkinl/code/mbed/#2cc6bb4d7fea