Code for an ethernet-enabled mbed that calls to a web api, and reasons with JSON resonses

Dependencies:   NetServices RPCInterface mbed picojson xbee_api

Files at this revision

API Documentation at this revision

Comitter:
ammanvedi
Date:
Sun Jan 26 20:49:03 2014 +0000
Parent:
3:2f263c4517e2
Commit message:
v1, works with server and msp430 with xbee chip

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
picojson.lib Show annotated file Show diff for this revision Revisions of this file
xbee_lib.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Jan 26 11:12:36 2014 +0000
+++ b/main.cpp	Sun Jan 26 20:49:03 2014 +0000
@@ -5,6 +5,9 @@
 #include "HTTPServer.h"
 #include "RPCFunction.h"
 #include <sstream>
+#include "picojson.h"
+#include "xbee.h"
+#include "xbeeFrame.h"
 
 
 
@@ -16,29 +19,105 @@
 EthernetNetIf eth(HOSTNAME);
 HTTPClient http;
 NTPClient ntp;
+Ticker pull_ticker;
+xbeeFrame xbee(p9,p10,p11);
+
+const char dest_address[8] = {0x00, 0x13, 0xA2, 0x00, 0x40, 0x9B, 0x6D, 0xB0};
+const char payload[] = "connection_from_host";
+
+char send_data[50] = "xbee string";
+
 
 using namespace std;
 
+
+void pull_updates(){
+    
+    HTTPText a_txt;
+    
+    HTTPResult rs = http.get("192.168.0.4:3000/pull_updates", &a_txt);
+    if (rs==HTTP_OK) {
+        printf("Result ok : %s (code : %d )\n\r", a_txt.gets(), rs);
+    } else {
+        printf("Error %s\n\r", a_txt.gets());
+    }
+    
+    char id[30] = "";
+    char new_msg[50] = "";
+    
+    sscanf((char *)a_txt.gets(), "%s %s", id, new_msg);
+    if(strlen(new_msg) == 0){
+        printf("no data\n\r");
+        return;
+        }else{
+            printf("update device %s with string %s\n\r", id, new_msg);
+            }
+        
+        char to_send[100];
+        char* p = to_send;
+        char* r = send_data;
+        while(*r)
+            *p++ = *r++;
+        *p++ = ' ';
+        r = new_msg;
+        while(*r)
+            *p++ = *r++;
+        *p = '\0';
+        
+        
+ 
+        char data_buf[50];    
+        xbee.InitFrame();
+        xbee.SetDestination((unsigned char *)dest_address);
+        xbee.SetPayload(to_send);
+        xbee.AssembleFrame();
+        xbee.SendFrame();
+   
+   
+           for(int i = 0; i<2; i++)
+        {
+            xbee.ReceiveFrame(data_buf, 500);
+            if(xbee.frameReceived)
+            {
+                xbee.frameReceived = 0;
+                if(xbee.GetType() == TX_STATUS)
+                {
+                    if(xbee.GetStatus() == 0)
+                        printf("Send success!\n\r");
+                    else
+                        printf("Send failed :(\n\r");
+                }
+                else if (xbee.GetType() == RX_PACKET_64)
+                    printf("Received data: %s\n\r", data_buf);
+            }
+        }        
+        
+
+   
+   
+    
+}
+
 int main() {
 
    
-    printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
+    printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n\r");
     EthernetErr ethErr;
     int count = 0;
     do {
-        printf("Setting up %d...\n", ++count);
+        printf("Setting up %d...\n\r", ++count);
         ethErr = eth.setup();
-        if (ethErr) printf("Timeout\n", ethErr);
+        if (ethErr) printf("Timeout\n\r", ethErr);
     } while (ethErr != ETH_OK);
 
-    printf("Connected OK\n");
+    printf("Connected OK\n\r");
     const char* hwAddr = eth.getHwAddr();
-    printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
+    printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n\r",
            hwAddr[0], hwAddr[1], hwAddr[2],
            hwAddr[3], hwAddr[4], hwAddr[5]);
 
     IpAddr ethIp = eth.getIp();
-    printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
+    printf("IP address : %d.%d.%d.%d\n\r", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
     
     char ip_buffer[20];
     sprintf(ip_buffer, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
@@ -50,16 +129,16 @@
      url << "192.168.0.4:3000/hubconnect?h_id=" << HOSTNAME << "&h_name=" << DEVICENAME << "&h_ip=" << ip_buffer;
      const std::string uri = url.str();
      
-     printf("query server : %s \n", uri);
+     printf("query server : %s \n\r", uri);
   
    HTTPResult r1 = http.post(uri.c_str(),msg,NULL); 
   if( r1 == HTTP_OK )
   {
-    printf("Hub %s registered with server (code : %d )\n", HOSTNAME, r1);
+    printf("Hub %s registered with server (code : %d )\n\r", HOSTNAME, r1);
   }
   else
   {
-    printf("Could not register, error : %d\n", r1);
+    printf("Could not register, error : %d\n\r", r1);
   }
   
  
@@ -69,10 +148,14 @@
     HTTPText txt;
     HTTPResult r = http.get("192.168.0.4:3000/devices", &txt);
     if (r==HTTP_OK) {
-        printf("Result ok : %s (code : %d )\n", txt.gets(), r);
+        printf("Result ok : %s (code : %d )\n\r", txt.gets(), r);
     } else {
         printf("Error %s\n", txt.gets());
     }
+    
+    //begin polling the server for updates to the devices
+    
+    pull_ticker.attach(&pull_updates, 5.0);
 
 
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/picojson.lib	Sun Jan 26 20:49:03 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mimil/code/picojson/#2bb500b021e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee_lib.lib	Sun Jan 26 20:49:03 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/noname77/code/xbee_api/#61e607fa8621