インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラム。   CQ publishing Interface 2014.10 issue, C027 3G test program.

Dependencies:   C027_Support C027_SupportTest mbed picojson

Fork of C027_SupportTest by u-blox

インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラムです。

Files at this revision

API Documentation at this revision

Comitter:
ntaka206
Date:
Thu Jul 17 10:06:02 2014 +0000
Parent:
29:1f18e74ff972
Child:
31:22a4e87217f8
Commit message:
Xively?3G?lat,lon?POST?????????

Changed in this revision

libxively.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
xi_user_config.h Show diff for this revision Revisions of this file
--- a/libxively.lib	Thu Jul 17 02:52:30 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/xively/code/libxively/#1208875310d3
--- a/main.cpp	Thu Jul 17 02:52:30 2014 +0000
+++ b/main.cpp	Thu Jul 17 10:06:02 2014 +0000
@@ -34,12 +34,17 @@
 #define PASSWORD    "nnku62"
 
 //---- Xively --------------------------------------------------------------
-#define XI_FEED_ID 1016719601 // set Xively Feed ID (numerical, no quoutes)
+#define XI_FEED_ID "1016719601" // set Xively Feed ID (numerical, no quoutes)
 #define XI_API_KEY "Y1mN05SzFgxjwiB9xFj42KYaTzdhohBLaKN8a9juznDrGsvK" // set Xively API key (double-quoted string)
 
-#include "xively.h"
-#include "xi_err.h"
+#define XI_SERVER   "api.xively.com"
+//#define XI_PATH     "POST /v2/feeds/" XI_FEED_ID ".csv?_method=put "
+//#define XI_HEADER   "X-ApiKey: " XI_API_KEY "\r\nContent-Type: text/csv\r\n"
 
+#define XI_PATH     "/v2/feeds/" XI_FEED_ID ".csv"
+#define XI_HEADER   "X-ApiKey: " XI_API_KEY
+
+#define XI_UPLOAD_INTERVAL   30  // sec
 //------------------------------------------------------------------------------------
 
 int main(void)
@@ -122,6 +127,7 @@
                 }
                 mdm.socketFree(socket);
             }
+#if 0
             
             int port = 7;
             const char* host = "echo.u-blox.com";
@@ -190,63 +196,23 @@
                 }
                 mdm.socketFree(socket);
             }
-            
+#endif
+           
+// no disconnect
             // disconnect  
-            mdm.disconnect();
+//            mdm.disconnect();
         }
     }
 #endif
-#if 0
-    EthernetInterface eth;
-    
-    int s = eth.init(); //Use DHCP
-    if( s != NULL ) {
-        printf( "Could not initialise. Will halt!\n" );        
-        exit( 0 );
-    }    
-        
-    s = eth.connect();
-    if( s != NULL ) {
-        printf( "Could not connect. Will halt!\n" );
-        exit( 0 );
-    } else {
-        printf( "IP: %s\n", eth.getIPAddress() );    
-    }
-#endif
 
 // Xively
-    xi_feed_t feed;
-    memset( &feed, NULL, sizeof( xi_feed_t ) );
-    
-    feed.feed_id = XI_FEED_ID;
-    feed.datastream_count = 2;
-
-    feed.datastreams[0].datapoint_count = 1;
-    xi_datastream_t* lat_datastream = &feed.datastreams[0];
-    strcpy( lat_datastream->datastream_id, "lat" );
-    xi_datapoint_t* current_lat = &lat_datastream->datapoints[0];
-
-    feed.datastreams[1].datapoint_count = 1;
-    xi_datastream_t* lon_datastream = &feed.datastreams[1];
-    strcpy( lon_datastream->datastream_id, "lon" );
-    xi_datapoint_t* current_lon = &lon_datastream->datapoints[0];
-    
-    // create the cosm library context
-    xi_context_t* xi_context
-        = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
-
-    // check if everything works
-    if( xi_context == NULL )
-    {
-        printf("xi_context NULL!\n");
-        return -1;
-    }
 
     printf("SMS and GPS Loop\r\n");
     char link[128] = "";
     unsigned int i = 0xFFFFFFFF;
     const int wait = 100;
     bool abort = false;
+    int xv_upload_cnt = XI_UPLOAD_INTERVAL;
     //DigitalOut led(LED1);
     while (!abort) {
     //    led = !led;
@@ -266,11 +232,34 @@
                         printf("GPS Location: %.5f %.5f\r\n", la, lo); 
                         sprintf(link, "I am here!\n"
                                       "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
-                        // Xively
-                        xi_set_value_f32( current_lat, la );
-                        xi_set_value_f32( current_lon, lo );
-                        xi_feed_update( xi_context, &feed );
+// Xively
+        if (xv_upload_cnt++ >= XI_UPLOAD_INTERVAL) {
+            xv_upload_cnt = 0;
+            int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
+            if (socket >= 0)
+            {
+                mdm.socketSetBlocking(socket, 10000);
+                if (mdm.socketConnect(socket, XI_SERVER, 80))
+                {
+                    
+//                    const char http[] = "POST /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
+//                    mdm.socketSend(socket, XI_PATH, sizeof(XI_PATH)-1);
+//                    mdm.socketSend(socket, XI_HEADER, sizeof(XI_HEADER)-1);
+                    char http[512];
+                    char data[128];
+                    sprintf(data, "lat,%f\r\nlon,%f\r\n", la, lo);
+                    sprintf(http, "PUT %s HTTP/1.1\r\nHost: %s\r\n%s\r\nContent-Length: %d\r\n\r\n%s", XI_PATH, XI_SERVER, XI_HEADER, strlen(data), data);
+printf(http);
+                    mdm.socketSend(socket, http, strlen(http));
 
+                    ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
+                    if (ret > 0)
+                        printf("Socket Recv \"%*s\"\r\n", ret, buf);
+                    mdm.socketClose(socket);
+                }
+                mdm.socketFree(socket);
+            }
+        }
                     }
                 } else if (!strncmp("$GPGGA", buf, 6)) {
                     double a = 0; 
--- a/xi_user_config.h	Thu Jul 17 02:52:30 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-#ifndef __XI_USER_CONFIG_H__
-#define __XI_USER_CONFIG_H__
-
-// The following settings should lower memory footprint.
-// The library currently allows one to send batch datapoint
-// and feed updates, but it's not needed in most use cases
-#define XI_MAX_DATAPOINTS         1
-// The number of channels can be increased if needed
-#define XI_MAX_DATASTREAMS        5
-
-// Below are optimisations that reduce some minor functionality
-#define XI_OPT_NO_ERROR_STRINGS
-
-// If you wish to enable assertions, set this to 1
-#define XI_DEBUG_ASSERT 1
-// If you wish to disable debug output, set this to 0
-#define XI_DEBUG_OUTPUT 1
-
-// On the mbed app board we can use the LCD for debug output,
-// but one may wish to modify this and write to file instead
-//#include "app_board_io.h"
-//#define XI_DEBUG_PRINTF lcd_printf //TODO: use serial port...
-#define XI_DEBUG_PRINTF printf //TODO: use serial port...
-
-#endif /* __XI_USER_CONFIG_H__ */