Simple Demo that gets a webpage

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed

Fork of EECS149_email_notifier by Antonio Iannopollo

Committer:
antoni0
Date:
Fri Oct 10 23:38:09 2014 +0000
Revision:
7:47cd0d3d5e4d
Parent:
5:236e750e4202
Child:
8:594cb3bc6f0f
first working version

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 */
Kojto 0:fe1445e57b7d 16 #include "mbed.h"
Kojto 0:fe1445e57b7d 17 #include "cc3000.h"
Kojto 0:fe1445e57b7d 18 #include "main.h"
Kojto 0:fe1445e57b7d 19
Kojto 0:fe1445e57b7d 20 #include "HTTPClient.h"
Kojto 0:fe1445e57b7d 21
Kojto 0:fe1445e57b7d 22 using namespace mbed_cc3000;
Kojto 0:fe1445e57b7d 23
Kojto 0:fe1445e57b7d 24 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 0:fe1445e57b7d 25 #if (MY_BOARD == WIGO)
Kojto 4:b61a6b6190be 26 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
Kojto 0:fe1445e57b7d 27 Serial pc(USBTX, USBRX);
Kojto 0:fe1445e57b7d 28 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 4:b61a6b6190be 29 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
Kojto 0:fe1445e57b7d 30 Serial pc(UART_TX, UART_RX);
Kojto 4:b61a6b6190be 31 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
Kojto 4:b61a6b6190be 32 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
Kojto 4:b61a6b6190be 33 Serial pc(USBTX, USBRX);
Kojto 0:fe1445e57b7d 34 #else
Kojto 0:fe1445e57b7d 35
antoni0 7:47cd0d3d5e4d 36 //KL25Z 149 connection
antoni0 7:47cd0d3d5e4d 37 cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "antonio", "0123456789", WPA2, false);
antoni0 7:47cd0d3d5e4d 38 Serial pc(USBTX, USBRX);
Kojto 0:fe1445e57b7d 39 #endif
Kojto 0:fe1445e57b7d 40
Kojto 0:fe1445e57b7d 41 HTTPClient http;
antoni0 7:47cd0d3d5e4d 42 char str[10];
antoni0 7:47cd0d3d5e4d 43 int num_emails;
antoni0 7:47cd0d3d5e4d 44
antoni0 7:47cd0d3d5e4d 45 DigitalOut led_red(LED_RED);
antoni0 7:47cd0d3d5e4d 46 DigitalOut led_green(LED_GREEN);
antoni0 7:47cd0d3d5e4d 47
antoni0 7:47cd0d3d5e4d 48 #define MAIL_URL "http://www.eecs.berkeley.edu/~antonio/149_count.txt"
antoni0 7:47cd0d3d5e4d 49
Kojto 0:fe1445e57b7d 50 /**
antoni0 7:47cd0d3d5e4d 51 * \brief EECS149 demo
Kojto 0:fe1445e57b7d 52 * \param none
Kojto 0:fe1445e57b7d 53 * \return int
Kojto 0:fe1445e57b7d 54 */
Kojto 0:fe1445e57b7d 55 int main() {
Kojto 0:fe1445e57b7d 56 init(); /* board dependent init */
Kojto 0:fe1445e57b7d 57 pc.baud(115200);
antoni0 7:47cd0d3d5e4d 58
antoni0 7:47cd0d3d5e4d 59 led_red = 1;
antoni0 7:47cd0d3d5e4d 60 led_green = 0;
Kojto 0:fe1445e57b7d 61
antoni0 7:47cd0d3d5e4d 62 printf("EECS149 email notifier. \r\n");
Kojto 4:b61a6b6190be 63 wifi.init();
Kojto 4:b61a6b6190be 64
antoni0 7:47cd0d3d5e4d 65 while(1) {
antoni0 7:47cd0d3d5e4d 66 if(wifi.is_connected() == false) {
antoni0 7:47cd0d3d5e4d 67 if (wifi.connect() == -1) {
antoni0 7:47cd0d3d5e4d 68 printf("Failed to connect. Please verify connection details and try again. \r\n");
antoni0 7:47cd0d3d5e4d 69 } else {
antoni0 7:47cd0d3d5e4d 70 printf("IP address: %s \r\n",wifi.getIPAddress());
antoni0 7:47cd0d3d5e4d 71 }
antoni0 7:47cd0d3d5e4d 72 } else {
antoni0 7:47cd0d3d5e4d 73 //GET mail data
antoni0 7:47cd0d3d5e4d 74 printf("\r\nTrying to fetch mail info... \r\n");
antoni0 7:47cd0d3d5e4d 75 int ret = http.get(MAIL_URL, str, 128);
antoni0 7:47cd0d3d5e4d 76 if (!ret) {
antoni0 7:47cd0d3d5e4d 77 printf("Page fetched successfully - read %d characters \r\n", strlen(str));
antoni0 7:47cd0d3d5e4d 78 printf("Result: %s \r\n", str);
antoni0 7:47cd0d3d5e4d 79
antoni0 7:47cd0d3d5e4d 80 num_emails = atoi(str);
antoni0 7:47cd0d3d5e4d 81
antoni0 7:47cd0d3d5e4d 82 if(num_emails == 0) {
antoni0 7:47cd0d3d5e4d 83 led_red = 1;
antoni0 7:47cd0d3d5e4d 84 led_green = 0;
antoni0 7:47cd0d3d5e4d 85 } else {
antoni0 7:47cd0d3d5e4d 86 led_red = 0;
antoni0 7:47cd0d3d5e4d 87 led_green = 1;
antoni0 7:47cd0d3d5e4d 88 }
antoni0 7:47cd0d3d5e4d 89
antoni0 7:47cd0d3d5e4d 90
antoni0 7:47cd0d3d5e4d 91 } else {
antoni0 7:47cd0d3d5e4d 92 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
antoni0 7:47cd0d3d5e4d 93 }
antoni0 7:47cd0d3d5e4d 94 }
antoni0 7:47cd0d3d5e4d 95 wait(10.0);
Kojto 0:fe1445e57b7d 96 }
antoni0 7:47cd0d3d5e4d 97
Kojto 1:58cd57bddd3a 98
Kojto 0:fe1445e57b7d 99 }