Simple demo for UC Berkeley EECS 149/249. The KL25Z connects to internet through the CC3000 breakout board and requests a text file, changing color of the onboard LEDs based on the read content

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of cc3000_http_client_demo by Martin Kojtal

Committer:
antoni0
Date:
Wed Oct 15 00:32:23 2014 +0000
Revision:
12:9e383f611c45
Parent:
11:e68fb69de2c6
deleted main.h

Who changed what in which revision?

UserRevisionLine numberNew 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
Kojto 0:fe1445e57b7d 45 #include "HTTPClient.h"
Kojto 0:fe1445e57b7d 46
antoni0 10:1804a9dbaee0 47 #define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
antoni0 10:1804a9dbaee0 48
antoni0 10:1804a9dbaee0 49
Kojto 0:fe1445e57b7d 50 using namespace mbed_cc3000;
Kojto 0:fe1445e57b7d 51
antoni0 10:1804a9dbaee0 52 //KL25Z wifi connection
antoni0 10:1804a9dbaee0 53 //we need to define connection pins for:
antoni0 10:1804a9dbaee0 54 //- IRQ (pin D3)
antoni0 10:1804a9dbaee0 55 //- Enable (pin D5)
antoni0 10:1804a9dbaee0 56 //- SPI CS (pin D10)
antoni0 10:1804a9dbaee0 57 //- SPI configuration:
antoni0 10:1804a9dbaee0 58 // - MOSI (pin D11)
antoni0 10:1804a9dbaee0 59 // - MISO (pin D12)
antoni0 10:1804a9dbaee0 60 // - SCLK (pin D13)
antoni0 10:1804a9dbaee0 61 //plus wifi network SSID, password, security level and smart-configuration flag.
antoni0 10:1804a9dbaee0 62 cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false);
antoni0 10:1804a9dbaee0 63
antoni0 10:1804a9dbaee0 64 //setup the serial connection to the host machine (used to print debug info)
Kojto 0:fe1445e57b7d 65 Serial pc(USBTX, USBRX);
Kojto 0:fe1445e57b7d 66
antoni0 10:1804a9dbaee0 67 //global vars
Kojto 0:fe1445e57b7d 68 HTTPClient http;
antoni0 7:47cd0d3d5e4d 69 char str[10];
antoni0 7:47cd0d3d5e4d 70 int num_emails;
antoni0 7:47cd0d3d5e4d 71
antoni0 10:1804a9dbaee0 72 //directly control the green and red onboard LEDs
antoni0 10:1804a9dbaee0 73 //LEDs are internally pulled-up. To turn on a LED, set its pin to 0.
antoni0 7:47cd0d3d5e4d 74 DigitalOut led_red(LED_RED);
antoni0 7:47cd0d3d5e4d 75 DigitalOut led_green(LED_GREEN);
antoni0 7:47cd0d3d5e4d 76
antoni0 7:47cd0d3d5e4d 77
Kojto 0:fe1445e57b7d 78 int main() {
antoni0 7:47cd0d3d5e4d 79
antoni0 10:1804a9dbaee0 80 //initially turn off both the leds.
antoni0 10:1804a9dbaee0 81 //Blue LED will be always on because it shares the connection with the SPI clock
antoni0 7:47cd0d3d5e4d 82 led_red = 1;
antoni0 8:594cb3bc6f0f 83 led_green = 1;
Kojto 0:fe1445e57b7d 84
antoni0 7:47cd0d3d5e4d 85 printf("EECS149 email notifier. \r\n");
antoni0 10:1804a9dbaee0 86
Kojto 4:b61a6b6190be 87 wifi.init();
Kojto 4:b61a6b6190be 88
antoni0 7:47cd0d3d5e4d 89 while(1) {
antoni0 10:1804a9dbaee0 90
antoni0 10:1804a9dbaee0 91 //continuosly check connection status
antoni0 10:1804a9dbaee0 92 //If not connected, try to set up a connection
antoni0 7:47cd0d3d5e4d 93 if(wifi.is_connected() == false) {
antoni0 10:1804a9dbaee0 94
antoni0 10:1804a9dbaee0 95 //try to connect
antoni0 7:47cd0d3d5e4d 96 if (wifi.connect() == -1) {
antoni0 7:47cd0d3d5e4d 97 printf("Failed to connect. Please verify connection details and try again. \r\n");
antoni0 7:47cd0d3d5e4d 98 } else {
antoni0 7:47cd0d3d5e4d 99 printf("IP address: %s \r\n",wifi.getIPAddress());
antoni0 10:1804a9dbaee0 100
antoni0 10:1804a9dbaee0 101 //once connected, turn green LED on and red LED off
antoni0 8:594cb3bc6f0f 102 led_red = 1;
antoni0 8:594cb3bc6f0f 103 led_green = 0;
antoni0 7:47cd0d3d5e4d 104 }
antoni0 7:47cd0d3d5e4d 105 } else {
antoni0 10:1804a9dbaee0 106 //if the board is connected, fetch the remote file
antoni0 10:1804a9dbaee0 107
antoni0 7:47cd0d3d5e4d 108 printf("\r\nTrying to fetch mail info... \r\n");
antoni0 10:1804a9dbaee0 109
antoni0 10:1804a9dbaee0 110 //GET remote file data
antoni0 7:47cd0d3d5e4d 111 int ret = http.get(MAIL_URL, str, 128);
antoni0 10:1804a9dbaee0 112 //analyze ret code
antoni0 7:47cd0d3d5e4d 113 if (!ret) {
antoni0 7:47cd0d3d5e4d 114 printf("Page fetched successfully - read %d characters \r\n", strlen(str));
antoni0 7:47cd0d3d5e4d 115 printf("Result: %s \r\n", str);
antoni0 7:47cd0d3d5e4d 116
antoni0 7:47cd0d3d5e4d 117 num_emails = atoi(str);
antoni0 7:47cd0d3d5e4d 118
antoni0 7:47cd0d3d5e4d 119 if(num_emails == 0) {
antoni0 10:1804a9dbaee0 120 //no unread emails, turn the green LED on and red LED off
antoni0 7:47cd0d3d5e4d 121 led_red = 1;
antoni0 7:47cd0d3d5e4d 122 led_green = 0;
antoni0 7:47cd0d3d5e4d 123 } else {
antoni0 10:1804a9dbaee0 124 //unread emails. Turn the green LED off and the red one on
antoni0 7:47cd0d3d5e4d 125 led_red = 0;
antoni0 7:47cd0d3d5e4d 126 led_green = 1;
antoni0 7:47cd0d3d5e4d 127 }
antoni0 7:47cd0d3d5e4d 128
antoni0 7:47cd0d3d5e4d 129
antoni0 7:47cd0d3d5e4d 130 } else {
antoni0 10:1804a9dbaee0 131 //error fetching remote file
antoni0 7:47cd0d3d5e4d 132 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
antoni0 7:47cd0d3d5e4d 133 }
antoni0 7:47cd0d3d5e4d 134 }
antoni0 10:1804a9dbaee0 135 //poll the remote file every 5 sec
antoni0 9:f80b62b60b1c 136 wait(5.0);
Kojto 0:fe1445e57b7d 137 }
antoni0 7:47cd0d3d5e4d 138
Kojto 1:58cd57bddd3a 139
Kojto 0:fe1445e57b7d 140 }