Simple Demo that gets a webpage

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of EECS149_email_notifier by Antonio Iannopollo

main.cpp

Committer:
antoni0
Date:
2014-10-10
Revision:
8:594cb3bc6f0f
Parent:
7:47cd0d3d5e4d
Child:
9:f80b62b60b1c

File content as of revision 8:594cb3bc6f0f:

/* 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.
 */
 
 /**
 *  \brief EECS149 demo
 *  \author Antonio Iannopollo
 */
 
#include "mbed.h"
#include "cc3000.h"
#include "main.h"

#include "HTTPClient.h"

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);
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

HTTPClient http;
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 EECS149 demo
 *  \param  none
 *  \return int
 */
int main() {
    init(); /* board dependent init */
    pc.baud(115200);
    
    led_red = 1;
    led_green = 1;

    printf("EECS149 email notifier. \r\n");
    wifi.init();
    
    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());
                led_red = 1;
                led_green = 0;  
            }
        } 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);
    }
            

}