Antonio Iannopollo / Mbed 2 deprecated EECS149_email_notifier

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of cc3000_http_client_demo by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017  /**
00018  *  \brief EECS149 demo
00019  *  \author Antonio Iannopollo
00020  *
00021  * This demo illustrates how to connect the mbed KL25Z platform to internet 
00022  * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469).
00023  * Connections between the KL25Z and the CC3000 are made according to the 
00024  * guide at https://learn.adafruit.com/adafruit-cc3000-wifi -- KL25Z and arduino
00025  * UNO are pin to pin compatible --
00026  *
00027  * This application will read a number from a remote text file and change the 
00028  * color of the onboard LED from green to red if the number is >0, and vice 
00029  * versa.
00030  *
00031  * The number in the remote file represents the number of unread emails with
00032  * subject equal to [EECS 149/249] in Antonio's account.
00033  * The remote text file is written by a server script 
00034  * (https://github.com/ianno/eecs149_notifier).
00035  *
00036  * This application uses the following libraries:
00037  * - cc3000_hostdriver_mbedsocket 
00038  *   (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
00039  * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
00040  */
00041  
00042 #include "mbed.h"
00043 #include "cc3000.h"
00044 
00045 #include "HTTPClient.h"
00046 
00047 #define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
00048 
00049 
00050 using namespace mbed_cc3000;
00051 
00052 //KL25Z wifi connection
00053 //we need to define connection pins for:
00054 //- IRQ             (pin D3)
00055 //- Enable          (pin D5)
00056 //- SPI CS (pin D10) 
00057 //- SPI configuration:
00058 //  - MOSI (pin D11)
00059 //  - MISO (pin D12)
00060 //  - SCLK (pin D13)
00061 //plus wifi network SSID, password, security level and smart-configuration flag.
00062 cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false);
00063 
00064 //setup the serial connection to the host machine (used to print debug info)
00065 Serial pc(USBTX, USBRX);
00066 
00067 //global vars
00068 HTTPClient http;
00069 char str[10];
00070 int num_emails;
00071 
00072 //directly control the green and red onboard LEDs
00073 //LEDs are internally pulled-up. To turn on a LED, set its pin to 0.
00074 DigitalOut led_red(LED_RED);
00075 DigitalOut led_green(LED_GREEN); 
00076 
00077 
00078 int main() {
00079     
00080     //initially turn off both the leds.
00081     //Blue LED will be always on because it shares the connection with the SPI clock
00082     led_red = 1;
00083     led_green = 1;
00084 
00085     printf("EECS149 email notifier. \r\n");
00086     
00087     wifi.init();
00088     
00089     while(1) {
00090         
00091         //continuosly check connection status
00092         //If not connected, try to set up a connection
00093         if(wifi.is_connected() == false) {
00094             
00095             //try to connect
00096             if (wifi.connect() == -1) {
00097                 printf("Failed to connect. Please verify connection details and try again. \r\n");
00098             } else {
00099                 printf("IP address: %s \r\n",wifi.getIPAddress());
00100                 
00101                 //once connected, turn green LED on and red LED off
00102                 led_red = 1;
00103                 led_green = 0;  
00104             }
00105         } else {
00106             //if the board is connected, fetch the remote file
00107             
00108             printf("\r\nTrying to fetch mail info... \r\n");
00109             
00110             //GET remote file data
00111             int ret = http.get(MAIL_URL, str, 128);
00112             //analyze ret code
00113             if (!ret) {
00114                 printf("Page fetched successfully - read %d characters \r\n", strlen(str));
00115                 printf("Result: %s \r\n", str);
00116                 
00117                 num_emails = atoi(str);
00118                 
00119                 if(num_emails == 0) {
00120                     //no unread emails, turn the green LED on and red LED off
00121                     led_red = 1;
00122                     led_green = 0;    
00123                 } else {
00124                     //unread emails. Turn the green LED off and the red one on
00125                     led_red = 0;
00126                     led_green = 1;    
00127                 }
00128                 
00129                 
00130             } else {
00131                 //error fetching remote file
00132                 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
00133             }
00134         }
00135         //poll the remote file every 5 sec
00136         wait(5.0);
00137     }
00138             
00139 
00140 }