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
main.cpp@11:e68fb69de2c6, 2014-10-14 (annotated)
- Committer:
- antoni0
- Date:
- Tue Oct 14 21:34:25 2014 +0000
- Revision:
- 11:e68fb69de2c6
- Parent:
- 10:1804a9dbaee0
- Child:
- 12:9e383f611c45
typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:fe1445e57b7d | 1 | /* mbed Microcontroller Library |
Kojto | 0:fe1445e57b7d | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 0:fe1445e57b7d | 3 | * |
Kojto | 0:fe1445e57b7d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 0:fe1445e57b7d | 5 | * you may not use this file except in compliance with the License. |
Kojto | 0:fe1445e57b7d | 6 | * You may obtain a copy of the License at |
Kojto | 0:fe1445e57b7d | 7 | * |
Kojto | 0:fe1445e57b7d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 0:fe1445e57b7d | 9 | * |
Kojto | 0:fe1445e57b7d | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 0:fe1445e57b7d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 0:fe1445e57b7d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 0:fe1445e57b7d | 13 | * See the License for the specific language governing permissions and |
Kojto | 0:fe1445e57b7d | 14 | * limitations under the License. |
Kojto | 0:fe1445e57b7d | 15 | */ |
antoni0 | 8:594cb3bc6f0f | 16 | |
antoni0 | 8:594cb3bc6f0f | 17 | /** |
antoni0 | 8:594cb3bc6f0f | 18 | * \brief EECS149 demo |
antoni0 | 8:594cb3bc6f0f | 19 | * \author Antonio Iannopollo |
antoni0 | 10:1804a9dbaee0 | 20 | * |
antoni0 | 11:e68fb69de2c6 | 21 | * This demo illustrates how to connect the mbed KL25Z platform to internet |
antoni0 | 10:1804a9dbaee0 | 22 | * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469). |
antoni0 | 10:1804a9dbaee0 | 23 | * Connections between the KL25Z and the CC3000 are made according to the |
antoni0 | 11:e68fb69de2c6 | 24 | * guide at https://learn.adafruit.com/adafruit-cc3000-wifi -- KL25Z and arduino |
antoni0 | 10:1804a9dbaee0 | 25 | * UNO are pin to pin compatible -- |
antoni0 | 10:1804a9dbaee0 | 26 | * |
antoni0 | 10:1804a9dbaee0 | 27 | * This application will read a number from a remote text file and change the |
antoni0 | 10:1804a9dbaee0 | 28 | * color of the onboard LED from green to red if the number is >0, and vice |
antoni0 | 10:1804a9dbaee0 | 29 | * versa. |
antoni0 | 10:1804a9dbaee0 | 30 | * |
antoni0 | 10:1804a9dbaee0 | 31 | * The number in the remote file represents the number of unread emails with |
antoni0 | 10:1804a9dbaee0 | 32 | * subject equal to [EECS 149/249] in Antonio's account. |
antoni0 | 10:1804a9dbaee0 | 33 | * The remote text file is written by a server script |
antoni0 | 10:1804a9dbaee0 | 34 | * (https://github.com/ianno/eecs149_notifier). |
antoni0 | 10:1804a9dbaee0 | 35 | * |
antoni0 | 10:1804a9dbaee0 | 36 | * This application uses the following libraries: |
antoni0 | 10:1804a9dbaee0 | 37 | * - cc3000_hostdriver_mbedsocket |
antoni0 | 10:1804a9dbaee0 | 38 | * (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/) |
antoni0 | 10:1804a9dbaee0 | 39 | * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/) |
antoni0 | 8:594cb3bc6f0f | 40 | */ |
antoni0 | 8:594cb3bc6f0f | 41 | |
Kojto | 0:fe1445e57b7d | 42 | #include "mbed.h" |
Kojto | 0:fe1445e57b7d | 43 | #include "cc3000.h" |
Kojto | 0:fe1445e57b7d | 44 | #include "main.h" |
Kojto | 0:fe1445e57b7d | 45 | |
Kojto | 0:fe1445e57b7d | 46 | #include "HTTPClient.h" |
Kojto | 0:fe1445e57b7d | 47 | |
antoni0 | 10:1804a9dbaee0 | 48 | #define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt" |
antoni0 | 10:1804a9dbaee0 | 49 | |
antoni0 | 10:1804a9dbaee0 | 50 | |
Kojto | 0:fe1445e57b7d | 51 | using namespace mbed_cc3000; |
Kojto | 0:fe1445e57b7d | 52 | |
antoni0 | 10:1804a9dbaee0 | 53 | //KL25Z wifi connection |
antoni0 | 10:1804a9dbaee0 | 54 | //we need to define connection pins for: |
antoni0 | 10:1804a9dbaee0 | 55 | //- IRQ (pin D3) |
antoni0 | 10:1804a9dbaee0 | 56 | //- Enable (pin D5) |
antoni0 | 10:1804a9dbaee0 | 57 | //- SPI CS (pin D10) |
antoni0 | 10:1804a9dbaee0 | 58 | //- SPI configuration: |
antoni0 | 10:1804a9dbaee0 | 59 | // - MOSI (pin D11) |
antoni0 | 10:1804a9dbaee0 | 60 | // - MISO (pin D12) |
antoni0 | 10:1804a9dbaee0 | 61 | // - SCLK (pin D13) |
antoni0 | 10:1804a9dbaee0 | 62 | //plus wifi network SSID, password, security level and smart-configuration flag. |
antoni0 | 10:1804a9dbaee0 | 63 | cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false); |
antoni0 | 10:1804a9dbaee0 | 64 | |
antoni0 | 10:1804a9dbaee0 | 65 | //setup the serial connection to the host machine (used to print debug info) |
Kojto | 0:fe1445e57b7d | 66 | Serial pc(USBTX, USBRX); |
Kojto | 0:fe1445e57b7d | 67 | |
antoni0 | 10:1804a9dbaee0 | 68 | //global vars |
Kojto | 0:fe1445e57b7d | 69 | HTTPClient http; |
antoni0 | 7:47cd0d3d5e4d | 70 | char str[10]; |
antoni0 | 7:47cd0d3d5e4d | 71 | int num_emails; |
antoni0 | 7:47cd0d3d5e4d | 72 | |
antoni0 | 10:1804a9dbaee0 | 73 | //directly control the green and red onboard LEDs |
antoni0 | 10:1804a9dbaee0 | 74 | //LEDs are internally pulled-up. To turn on a LED, set its pin to 0. |
antoni0 | 7:47cd0d3d5e4d | 75 | DigitalOut led_red(LED_RED); |
antoni0 | 7:47cd0d3d5e4d | 76 | DigitalOut led_green(LED_GREEN); |
antoni0 | 7:47cd0d3d5e4d | 77 | |
antoni0 | 7:47cd0d3d5e4d | 78 | |
Kojto | 0:fe1445e57b7d | 79 | int main() { |
antoni0 | 7:47cd0d3d5e4d | 80 | |
antoni0 | 10:1804a9dbaee0 | 81 | //initially turn off both the leds. |
antoni0 | 10:1804a9dbaee0 | 82 | //Blue LED will be always on because it shares the connection with the SPI clock |
antoni0 | 7:47cd0d3d5e4d | 83 | led_red = 1; |
antoni0 | 8:594cb3bc6f0f | 84 | led_green = 1; |
Kojto | 0:fe1445e57b7d | 85 | |
antoni0 | 7:47cd0d3d5e4d | 86 | printf("EECS149 email notifier. \r\n"); |
antoni0 | 10:1804a9dbaee0 | 87 | |
Kojto | 4:b61a6b6190be | 88 | wifi.init(); |
Kojto | 4:b61a6b6190be | 89 | |
antoni0 | 7:47cd0d3d5e4d | 90 | while(1) { |
antoni0 | 10:1804a9dbaee0 | 91 | |
antoni0 | 10:1804a9dbaee0 | 92 | //continuosly check connection status |
antoni0 | 10:1804a9dbaee0 | 93 | //If not connected, try to set up a connection |
antoni0 | 7:47cd0d3d5e4d | 94 | if(wifi.is_connected() == false) { |
antoni0 | 10:1804a9dbaee0 | 95 | |
antoni0 | 10:1804a9dbaee0 | 96 | //try to connect |
antoni0 | 7:47cd0d3d5e4d | 97 | if (wifi.connect() == -1) { |
antoni0 | 7:47cd0d3d5e4d | 98 | printf("Failed to connect. Please verify connection details and try again. \r\n"); |
antoni0 | 7:47cd0d3d5e4d | 99 | } else { |
antoni0 | 7:47cd0d3d5e4d | 100 | printf("IP address: %s \r\n",wifi.getIPAddress()); |
antoni0 | 10:1804a9dbaee0 | 101 | |
antoni0 | 10:1804a9dbaee0 | 102 | //once connected, turn green LED on and red LED off |
antoni0 | 8:594cb3bc6f0f | 103 | led_red = 1; |
antoni0 | 8:594cb3bc6f0f | 104 | led_green = 0; |
antoni0 | 7:47cd0d3d5e4d | 105 | } |
antoni0 | 7:47cd0d3d5e4d | 106 | } else { |
antoni0 | 10:1804a9dbaee0 | 107 | //if the board is connected, fetch the remote file |
antoni0 | 10:1804a9dbaee0 | 108 | |
antoni0 | 7:47cd0d3d5e4d | 109 | printf("\r\nTrying to fetch mail info... \r\n"); |
antoni0 | 10:1804a9dbaee0 | 110 | |
antoni0 | 10:1804a9dbaee0 | 111 | //GET remote file data |
antoni0 | 7:47cd0d3d5e4d | 112 | int ret = http.get(MAIL_URL, str, 128); |
antoni0 | 10:1804a9dbaee0 | 113 | //analyze ret code |
antoni0 | 7:47cd0d3d5e4d | 114 | if (!ret) { |
antoni0 | 7:47cd0d3d5e4d | 115 | printf("Page fetched successfully - read %d characters \r\n", strlen(str)); |
antoni0 | 7:47cd0d3d5e4d | 116 | printf("Result: %s \r\n", str); |
antoni0 | 7:47cd0d3d5e4d | 117 | |
antoni0 | 7:47cd0d3d5e4d | 118 | num_emails = atoi(str); |
antoni0 | 7:47cd0d3d5e4d | 119 | |
antoni0 | 7:47cd0d3d5e4d | 120 | if(num_emails == 0) { |
antoni0 | 10:1804a9dbaee0 | 121 | //no unread emails, turn the green LED on and red LED off |
antoni0 | 7:47cd0d3d5e4d | 122 | led_red = 1; |
antoni0 | 7:47cd0d3d5e4d | 123 | led_green = 0; |
antoni0 | 7:47cd0d3d5e4d | 124 | } else { |
antoni0 | 10:1804a9dbaee0 | 125 | //unread emails. Turn the green LED off and the red one on |
antoni0 | 7:47cd0d3d5e4d | 126 | led_red = 0; |
antoni0 | 7:47cd0d3d5e4d | 127 | led_green = 1; |
antoni0 | 7:47cd0d3d5e4d | 128 | } |
antoni0 | 7:47cd0d3d5e4d | 129 | |
antoni0 | 7:47cd0d3d5e4d | 130 | |
antoni0 | 7:47cd0d3d5e4d | 131 | } else { |
antoni0 | 10:1804a9dbaee0 | 132 | //error fetching remote file |
antoni0 | 7:47cd0d3d5e4d | 133 | printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); |
antoni0 | 7:47cd0d3d5e4d | 134 | } |
antoni0 | 7:47cd0d3d5e4d | 135 | } |
antoni0 | 10:1804a9dbaee0 | 136 | //poll the remote file every 5 sec |
antoni0 | 9:f80b62b60b1c | 137 | wait(5.0); |
Kojto | 0:fe1445e57b7d | 138 | } |
antoni0 | 7:47cd0d3d5e4d | 139 | |
Kojto | 1:58cd57bddd3a | 140 | |
Kojto | 0:fe1445e57b7d | 141 | } |