for znrobotics workshop

Dependencies:   DHT22 HTTPClient SDFileSystem WIZnet_Library mbed

Fork of archlink_Temperture_dweetio by Kevin Lee

This program is for seeed arch link, using wiznet w550 ethernet interface. not compatible with mbed ethernet interface.

Revision:
7:98c5b9eba03a
Parent:
6:dbf5cf5ca7b1
Child:
8:0f34e39a6674
diff -r dbf5cf5ca7b1 -r 98c5b9eba03a main.cpp
--- a/main.cpp	Thu Jun 16 03:34:49 2016 +0000
+++ b/main.cpp	Mon Jun 20 11:14:37 2016 +0000
@@ -1,4 +1,4 @@
-/* This is just a demo by Kevin Lee */
+// provided by znrobotics.
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include "WIZnetInterface.h"
@@ -8,6 +8,10 @@
 #define USE_DHCP 0
 #define LOOPBACKPORT  5000
 
+const int USE_HTTP_PROTOCAL = 1;
+const int USE_COAP_PROTOCAL = 2;
+const int USE_MQTT_PROTOCAL = 3;
+
 int W5500_Test(void);
 
 float* temperature_get();         /* Grove - Temperature Sensor V1.2 */
@@ -16,13 +20,26 @@
 SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0);
 WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset
 int ret, dummy, lv = 1;
-const char * IP_Addr      = "192.168.1.123";
+
+const char* http_port = "9090";
+const char* coap_port = "5683";
+const char* mqtt_port = "1883";
+
+// todo:: your settings here.
+//const char * SERVER_REMOTE = "58.214.20.195";
+const char * SERVER_REMOTE = "52.76.14.222";
+const char * IP_Addr      = "192.168.1.223";
 const char * IP_Subnet    = "255.255.255.0";
 const char * IP_Gateway   = "192.168.1.1";
 const char * DEVICE_ID    = "933691ed-1f00-4826-953a-d56841f65240";
 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA};
-
-const int time_interval   = 10; // 10 seconds;
+// which protocal to use
+// 1 => HTTP; 
+// 2 => CoAP;
+// 3 => MQTT
+const int PROTOCAL_INUSE  = 1;
+// unit: seconds;
+const int time_interval   = 15; //seconds
 
 // timer event;
 Ticker flipper;
@@ -36,7 +53,6 @@
 AnalogIn   ain(A0);
 
 int main() {
-    char buffer[256];
     wait(1);
     pc.baud(9600);
     wait(1);
@@ -57,25 +73,45 @@
 void timer_ticked()
 {
     char str[512];
-    char post_data[256]= "";
+    char get_msg[256]= "";
     
     float* th = temperature_get();
     printf("temperature: %.2f -- Humidity: %.2f.\r\n",th[0], th[1]);
     
-    pc.printf("Posting message to znrobotics server.\r\n");
-    // http://machine.address:port/did/temperature?param1=v1&param2=v2
-    sprintf(post_data, "http://192.168.1.222:3000/temperature?deviceid=%s&temperature=%.2f", DEVICE_ID, th[0]);
-    pc.printf("post msg: %s\r\n", post_data);
+    pc.printf("posting message to znrobotics server.\r\n");
     
-    HTTPClient http;
-    ret = http.get(post_data, str, sizeof(str));
-    if(!ret)
+    switch (PROTOCAL_INUSE)
     {
-        pc.printf("Result: %s\r\n", str);
-    }
-    else
-    {
-        pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+        case USE_HTTP_PROTOCAL:
+        {
+//            sprintf(get_msg, "http://%s/sensors/temperature?deviceid=%s&temperature=%.2f", SERVER_REMOTE, DEVICE_ID, th[0]);
+            sprintf(get_msg, "http://52.76.14.222/hello");
+//            sprintf(get_msg,"http://dweet.io/dweet/for/%s?Temperature=%.2f\r\n","ArchLink", th[0]);
+            HTTPClient http;
+        
+            pc.printf("msg : %s\r\n",get_msg);
+            ret = http.get(get_msg, str, sizeof(str));
+            if(!ret)
+            {
+                pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
+                pc.printf("Result: %s\r\n", str);
+            }
+            else
+            {
+                pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+            }                 
+        }
+        break;
+        case USE_COAP_PROTOCAL:
+        {
+            
+        }
+        break;
+        case USE_MQTT_PROTOCAL:
+        {
+            
+        }
+        break;
     }
 }
 
@@ -97,8 +133,7 @@
         pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
         ret = ethernet.connect();
         if (!ret) {
-            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
-                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+            pc.printf("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
             return 0;
         } else {
             pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);