Work with nodejs server: https://github.com/bjo3rn/idd-examples/tree/ben-node-demo/web/nodeServerHTTPSocket

Dependencies:   HTTPClient WebSocketClient cc3000_hostdriver_mbedsocket mbed

Fork of CC3000_demo by Ben Zhang

Files at this revision

API Documentation at this revision

Comitter:
antoni0
Date:
Tue Oct 14 21:20:53 2014 +0000
Parent:
9:f80b62b60b1c
Child:
11:e68fb69de2c6
Commit message:
simple demo for UC Berkeley EECS 149/249. The KL25Z connects to internet through the CC3000 breakout board and requests a text file, changing color of the onboard LEDs based on the read content

Changed in this revision

NVIC_set_all_priorities.lib Show diff for this revision Revisions of this file
init.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/NVIC_set_all_priorities.lib	Fri Oct 10 23:55:33 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/frankvnk/code/NVIC_set_all_priorities/#6c8e950d8037
--- a/init.cpp	Fri Oct 10 23:55:33 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "main.h"
-#include "mbed.h"
-
-#if (MY_BOARD == WIGO)
-
-#include "NVIC_set_all_priorities.h"
-
-/**
- *  \brief Wi-Go initialization
- *  \param none
- *  \return none
- */
-void init() {
-    DigitalOut PWR_EN1(PTB2);
-    DigitalOut PWR_EN2(PTB3);
-
-    // Wi-Go set current to 500mA since we're turning on the Wi-Fi
-    PWR_EN1 = 0;
-    PWR_EN2 = 1;
-
-    NVIC_set_all_irq_priorities(3);
-    NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
-    NVIC_SetPriority(PORTA_IRQn, 0x1);
-    NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    PORTA->PCR[16] |=PORT_PCR_ISF_MASK;
-    PORTA->ISFR |= (1 << 16);
-}
-
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-
-/**
- *  \brief Wifi DipCortex initialization
- *  \param none
- *  \return none
- */
-void init() {
-    NVIC_SetPriority(SSP1_IRQn, 0x0);
-    NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
-
-    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    NVIC_SetPriority(SysTick_IRQn, 0x2);
-}
-
-#else
-
-/**
- *  \brief Place here init routine for your board
- *  \param none
- *  \return none
- */
-void init() {
-
-}
-
-#endif
--- a/main.cpp	Fri Oct 10 23:55:33 2014 +0000
+++ b/main.cpp	Tue Oct 14 21:20:53 2014 +0000
@@ -17,6 +17,26 @@
  /**
  *  \brief EECS149 demo
  *  \author Antonio Iannopollo
+ *
+ * This demo illustrate how to connect the mbed KL25Z platform to internet 
+ * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469).
+ * Connections between the KL25Z and the CC3000 are made according to the 
+ * guide (https://learn.adafruit.com/adafruit-cc3000-wifi) -- KL25Z and arduino
+ * UNO are pin to pin compatible --
+ *
+ * This application will read a number from a remote text file and change the 
+ * color of the onboard LED from green to red if the number is >0, and vice 
+ * versa.
+ *
+ * The number in the remote file represents the number of unread emails with
+ * subject equal to [EECS 149/249] in Antonio's account.
+ * The remote text file is written by a server script 
+ * (https://github.com/ianno/eecs149_notifier).
+ *
+ * This application uses the following libraries:
+ * - cc3000_hostdriver_mbedsocket 
+ *   (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
+ * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
  */
  
 #include "mbed.h"
@@ -25,62 +45,72 @@
 
 #include "HTTPClient.h"
 
+#define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
+
+
 using namespace mbed_cc3000;
 
-/* cc3000 module declaration specific for user's board. Check also init() */
-#if (MY_BOARD == WIGO)
-cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
+//KL25Z wifi connection
+//we need to define connection pins for:
+//- IRQ             (pin D3)
+//- Enable          (pin D5)
+//- SPI CS (pin D10) 
+//- SPI configuration:
+//  - MOSI (pin D11)
+//  - MISO (pin D12)
+//  - SCLK (pin D13)
+//plus wifi network SSID, password, security level and smart-configuration flag.
+cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false);
+
+//setup the serial connection to the host machine (used to print debug info)
 Serial pc(USBTX, USBRX);
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
-Serial pc(UART_TX, UART_RX);
-#elif (MY_BOARD == MBED_BOARD_EXAMPLE)
-cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
-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
-
+//global vars
 HTTPClient http;
 char str[10];
 int num_emails;
 
+//directly control the green and red onboard LEDs
+//LEDs are internally pulled-up. To turn on a LED, set its pin to 0.
 DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN); 
 
-#define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
 
-/**
- *  \brief EECS149 demo
- *  \param  none
- *  \return int
- */
 int main() {
-    init(); /* board dependent init */
-    pc.baud(115200);
     
+    //initially turn off both the leds.
+    //Blue LED will be always on because it shares the connection with the SPI clock
     led_red = 1;
     led_green = 1;
 
     printf("EECS149 email notifier. \r\n");
+    
     wifi.init();
     
     while(1) {
+        
+        //continuosly check connection status
+        //If not connected, try to set up a connection
         if(wifi.is_connected() == false) {
+            
+            //try to connect
             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());
+                
+                //once connected, turn green LED on and red LED off
                 led_red = 1;
                 led_green = 0;  
             }
         } else {
-            //GET mail data
+            //if the board is connected, fetch the remote file
+            
             printf("\r\nTrying to fetch mail info... \r\n");
+            
+            //GET remote file data
             int ret = http.get(MAIL_URL, str, 128);
+            //analyze ret code
             if (!ret) {
                 printf("Page fetched successfully - read %d characters \r\n", strlen(str));
                 printf("Result: %s \r\n", str);
@@ -88,18 +118,22 @@
                 num_emails = atoi(str);
                 
                 if(num_emails == 0) {
+                    //no unread emails, turn the green LED on and red LED off
                     led_red = 1;
                     led_green = 0;    
                 } else {
+                    //unread emails. Turn the green LED off and the red one on
                     led_red = 0;
                     led_green = 1;    
                 }
                 
                 
             } else {
+                //error fetching remote file
                 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
             }
         }
+        //poll the remote file every 5 sec
         wait(5.0);
     }
             
--- a/main.h	Fri Oct 10 23:55:33 2014 +0000
+++ b/main.h	Tue Oct 14 21:20:53 2014 +0000
@@ -16,13 +16,6 @@
 #ifndef MAIN_H
 #define MAIN_H
 
-#define WIGO               1
-#define WIFI_DIPCORTEX     2
-#define MBED_BOARD_EXAMPLE 3
-#define UNDEFINED          4
 
-#define MY_BOARD UNDEFINED
-
-void init();
 
 #endif