Simple Demo that gets a webpage
Dependencies: HTTPClient cc3000_hostdriver_mbedsocket mbed
Fork of EECS149_email_notifier by
main.cpp@13:26905142e97b, 2014-10-19 (annotated)
- Committer:
- nebgnahz
- Date:
- Sun Oct 19 07:05:29 2014 +0000
- Revision:
- 13:26905142e97b
- Parent:
- 12:9e383f611c45
- Child:
- 14:efd42910791b
a simple demo now
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nebgnahz | 13:26905142e97b | 1 | /** |
nebgnahz | 13:26905142e97b | 2 | * \brief CS294-84 demo |
nebgnahz | 13:26905142e97b | 3 | * \author Ben Zhang, Antonio Iannopollo |
nebgnahz | 13:26905142e97b | 4 | * |
nebgnahz | 13:26905142e97b | 5 | * This sampel code illustrates how to connect the mbed KL25Z platform to internet |
nebgnahz | 13:26905142e97b | 6 | * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469). |
nebgnahz | 13:26905142e97b | 7 | * Connections between the KL25Z and the CC3000 are made according to the |
nebgnahz | 13:26905142e97b | 8 | * guide at https://learn.adafruit.com/adafruit-cc3000-wifi -- KL25Z and arduino |
nebgnahz | 13:26905142e97b | 9 | * UNO are pin to pin compatible -- |
nebgnahz | 13:26905142e97b | 10 | * |
nebgnahz | 13:26905142e97b | 11 | * This application uses the following libraries: |
nebgnahz | 13:26905142e97b | 12 | * - cc3000_hostdriver_mbedsocket |
nebgnahz | 13:26905142e97b | 13 | * (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/) |
nebgnahz | 13:26905142e97b | 14 | * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/) |
nebgnahz | 13:26905142e97b | 15 | */ |
nebgnahz | 13:26905142e97b | 16 | |
Kojto | 0:fe1445e57b7d | 17 | #include "mbed.h" |
Kojto | 0:fe1445e57b7d | 18 | #include "cc3000.h" |
Kojto | 0:fe1445e57b7d | 19 | #include "HTTPClient.h" |
Kojto | 0:fe1445e57b7d | 20 | |
nebgnahz | 13:26905142e97b | 21 | // KL25Z wifi connection |
nebgnahz | 13:26905142e97b | 22 | // we need to define connection pins for: |
nebgnahz | 13:26905142e97b | 23 | // - IRQ => (pin D3) |
nebgnahz | 13:26905142e97b | 24 | // - Enable => (pin D5) |
nebgnahz | 13:26905142e97b | 25 | // - SPI CS => (pin D10) |
nebgnahz | 13:26905142e97b | 26 | // - SPI MOSI => (pin D11) |
nebgnahz | 13:26905142e97b | 27 | // - SPI MISO => (pin D12) |
nebgnahz | 13:26905142e97b | 28 | // - SPI CLK => (pin D13) |
nebgnahz | 13:26905142e97b | 29 | // plus wifi network SSID, password, security level and smart-configuration flag. |
nebgnahz | 13:26905142e97b | 30 | mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "SSID", "PASSWORD", WPA2, false); |
antoni0 | 10:1804a9dbaee0 | 31 | |
nebgnahz | 13:26905142e97b | 32 | // create an http instance |
nebgnahz | 13:26905142e97b | 33 | HTTPClient http; |
Kojto | 0:fe1445e57b7d | 34 | |
nebgnahz | 13:26905142e97b | 35 | // str is used to hold the response data |
nebgnahz | 13:26905142e97b | 36 | char str[512]; |
antoni0 | 7:47cd0d3d5e4d | 37 | |
nebgnahz | 13:26905142e97b | 38 | // setup the serial connection, and LEDs |
nebgnahz | 13:26905142e97b | 39 | Serial pc(USBTX, USBRX); |
antoni0 | 7:47cd0d3d5e4d | 40 | DigitalOut led_red(LED_RED); |
nebgnahz | 13:26905142e97b | 41 | DigitalOut led_green(LED_GREEN); |
antoni0 | 7:47cd0d3d5e4d | 42 | |
nebgnahz | 13:26905142e97b | 43 | int main() |
nebgnahz | 13:26905142e97b | 44 | { |
nebgnahz | 13:26905142e97b | 45 | // by default, it's red |
nebgnahz | 13:26905142e97b | 46 | led_red = 0; |
antoni0 | 8:594cb3bc6f0f | 47 | led_green = 1; |
Kojto | 0:fe1445e57b7d | 48 | |
nebgnahz | 13:26905142e97b | 49 | // print message to indicate the program has started |
nebgnahz | 13:26905142e97b | 50 | pc.printf("CC3000 Sample Program\r\n"); |
Kojto | 4:b61a6b6190be | 51 | wifi.init(); |
nebgnahz | 13:26905142e97b | 52 | |
antoni0 | 7:47cd0d3d5e4d | 53 | while(1) { |
nebgnahz | 13:26905142e97b | 54 | // continuosly check connection status |
antoni0 | 7:47cd0d3d5e4d | 55 | if(wifi.is_connected() == false) { |
nebgnahz | 13:26905142e97b | 56 | // try to connect |
antoni0 | 7:47cd0d3d5e4d | 57 | if (wifi.connect() == -1) { |
antoni0 | 7:47cd0d3d5e4d | 58 | printf("Failed to connect. Please verify connection details and try again. \r\n"); |
antoni0 | 7:47cd0d3d5e4d | 59 | } else { |
nebgnahz | 13:26905142e97b | 60 | printf("IP address: %s \r\n", wifi.getIPAddress()); |
nebgnahz | 13:26905142e97b | 61 | |
antoni0 | 10:1804a9dbaee0 | 62 | //once connected, turn green LED on and red LED off |
antoni0 | 8:594cb3bc6f0f | 63 | led_red = 1; |
nebgnahz | 13:26905142e97b | 64 | led_green = 0; |
antoni0 | 7:47cd0d3d5e4d | 65 | } |
antoni0 | 7:47cd0d3d5e4d | 66 | } else { |
nebgnahz | 13:26905142e97b | 67 | int ret = http.get("http://www.eecs.berkeley.edu/~benzh/hello.txt", str, 128); |
antoni0 | 7:47cd0d3d5e4d | 68 | if (!ret) { |
nebgnahz | 13:26905142e97b | 69 | printf("Page fetched successfully - read %d characters\r\n", strlen(str)); |
nebgnahz | 13:26905142e97b | 70 | printf("Result: %s\r\n", str); |
antoni0 | 7:47cd0d3d5e4d | 71 | } else { |
nebgnahz | 13:26905142e97b | 72 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
antoni0 | 7:47cd0d3d5e4d | 73 | } |
antoni0 | 7:47cd0d3d5e4d | 74 | } |
nebgnahz | 13:26905142e97b | 75 | // repeat after waiting for 1 sec |
nebgnahz | 13:26905142e97b | 76 | wait(1.0); |
Kojto | 0:fe1445e57b7d | 77 | } |
nebgnahz | 13:26905142e97b | 78 | } |