Ben Zhang / Mbed 2 deprecated CC3000_demo

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of EECS149_email_notifier by Antonio Iannopollo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  *  \brief CS294-84 demo \author Ben Zhang, Antonio Iannopollo
00003  *
00004  * This sampel code illustrates how to connect the mbed KL25Z platform to internet
00005  * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469).
00006  * Connections between the KL25Z and the CC3000 are made according to the
00007  * guide at https://learn.adafruit.com/adafruit-cc3000-wifi -- KL25Z and arduino
00008  * UNO are pin to pin compatible --
00009  *
00010  * This application uses the following libraries:
00011  * - cc3000_hostdriver_mbedsocket
00012  *   (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
00013  * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
00014  */
00015 
00016 #include "mbed.h"
00017 #include "cc3000.h"
00018 #include "HTTPClient.h"
00019 
00020 // KL25Z wifi connection
00021 // we need to define connection pins for:
00022 // - IRQ      => (pin D3)
00023 // - Enable   => (pin D5)
00024 // - SPI CS   => (pin D10)
00025 // - SPI MOSI => (pin D11)
00026 // - SPI MISO => (pin D12)
00027 // - SPI CLK  => (pin D13)
00028 // plus wifi network SSID, password, security level and smart-configuration flag.
00029 mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13),
00030                          "SSID", "PASSWORD", WPA2, false);
00031 
00032 // create an http instance
00033 HTTPClient http;
00034 
00035 // str is used to hold the response data
00036 char str[512];
00037 char url[80];
00038 
00039 // setup the serial connection, and LEDs
00040 Serial pc(USBTX, USBRX);
00041 DigitalOut led_red(LED_RED);
00042 DigitalOut led_green(LED_GREEN);
00043 
00044 int main() {
00045   // by default, it's red
00046   led_red = 0;
00047   led_green = 1;
00048 
00049   // print message to indicate the program has started
00050   pc.printf("CC3000 Sample Program\r\n");
00051   wifi.init();
00052 
00053   while(1) {
00054     // continuosly check connection status
00055     if(wifi.is_connected() == false) {
00056       // try to connect
00057       if (wifi.connect() == -1) {
00058         pc.printf("Failed to connect."
00059                   "Please verify connection details and try again.\r\n");
00060       } else {
00061         pc.printf("IP address: %s \r\n", wifi.getIPAddress());
00062 
00063         //once connected, turn green LED on and red LED off
00064         led_red = 1;
00065         led_green = 0;
00066       }
00067     } else {
00068       // get input url and then return the value
00069       pc.printf("> ");
00070       pc.scanf("%s", url);
00071 
00072       int ret = http.get(url, str, 128);
00073       if (!ret) {
00074         pc.printf("Requested %s\r\n", url);
00075         pc.printf("Page fetched successfully - read %d characters\r\n",
00076                   strlen(str));
00077         pc.printf("Result: %s\r\n", str);
00078       } else {
00079         pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
00080                   ret,
00081                   http.getHTTPResponseCode());
00082       }
00083     }
00084   }
00085 }