Simple Demo that gets a webpage

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of EECS149_email_notifier by Antonio Iannopollo

Revision:
7:47cd0d3d5e4d
Parent:
5:236e750e4202
Child:
8:594cb3bc6f0f
--- a/main.cpp	Tue Jun 03 09:32:29 2014 +0000
+++ b/main.cpp	Fri Oct 10 23:38:09 2014 +0000
@@ -33,88 +33,67 @@
 Serial pc(USBTX, USBRX);
 #else
 
+//KL25Z 149 connection
+cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false);
+Serial pc(USBTX, USBRX);
 #endif
 
 HTTPClient http;
-char str[512];
+char str[10];
+int num_emails;
+
+DigitalOut led_red(LED_RED);
+DigitalOut led_green(LED_GREEN); 
+
+#define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
+
 /**
- *  \brief HTTP client demo
+ *  \brief EECS149 demo
  *  \param  none
  *  \return int
  */
 int main() {
     init(); /* board dependent init */
     pc.baud(115200);
+    
+    led_red = 1;
+    led_green = 0;
 
-    printf("cc3000 HTTP client demo. \r\n");
+    printf("EECS149 email notifier. \r\n");
     wifi.init();
-    if (wifi.connect() == -1) {
-        printf("Failed to connect. Please verify connection details and try again. \r\n");
-        while(1);
-    } else {
-        printf("IP address: %s \r\n",wifi.getIPAddress());
-    }
     
-    //GET data
-    printf("\r\nTrying to fetch page... \r\n");
-    int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
-    if (!ret)
-    {
-      printf("Page fetched successfully - read %d characters \r\n", strlen(str));
-      printf("Result: %s \r\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
-    }
-
-    //POST data
-    HTTPMap map;
-    HTTPText inText(str, 512);
-    map.put("Hello", "World");
-    map.put("test", "1234");
-    printf(" \r\nTrying to post data... \r\n");
-    ret = http.post("http://httpbin.org/post", map, &inText);
-    if (!ret)
-    {
-      printf("Executed POST successfully - read %d characters \r\n", strlen(str));
-      printf("Result: %s \r\n", str);
+    while(1) {
+        if(wifi.is_connected() == false) {
+            if (wifi.connect() == -1) {
+                printf("Failed to connect. Please verify connection details and try again. \r\n");
+            } else {
+                printf("IP address: %s \r\n",wifi.getIPAddress());
+            }
+        } else {
+            //GET mail data
+            printf("\r\nTrying to fetch mail info... \r\n");
+            int ret = http.get(MAIL_URL, str, 128);
+            if (!ret) {
+                printf("Page fetched successfully - read %d characters \r\n", strlen(str));
+                printf("Result: %s \r\n", str);
+                
+                num_emails = atoi(str);
+                
+                if(num_emails == 0) {
+                    led_red = 1;
+                    led_green = 0;    
+                } else {
+                    led_red = 0;
+                    led_green = 1;    
+                }
+                
+                
+            } else {
+                printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
+            }
+        }
+        wait(10.0);
     }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
-    }
+            
 
-    //PUT data
-    strcpy(str, "This is a PUT test!");
-    HTTPText outText(str);
-    //HTTPText inText(str, 512);
-    printf(" \r\nTrying to put resource... \r\n");
-    ret = http.put("http://httpbin.org/put", outText, &inText);
-    if (!ret)
-    {
-      printf("Executed PUT successfully - read %d characters \r\n", strlen(str));
-      printf("Result: %s \r\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
-    }
-
-    //DELETE data
-    //HTTPText inText(str, 512);
-    printf(" \r\nTrying to delete resource... \r\n");
-    ret = http.del("http://httpbin.org/delete", &inText);
-    if (!ret)
-    {
-      printf("Executed DELETE successfully - read %d characters \r\n", strlen(str));
-      printf("Result: %s \r\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
-    }
-
-    printf("Demo completed. \r\n");
-    wifi.disconnect();
 }