CC3000 Chat example posting messages to husk

Dependencies:   HTTPClient cc3000_hostdriver_mbedsocket mbed tsi_sensor

Fork of CC3000_demo by Ben Zhang

Committer:
antoni0
Date:
Fri Oct 10 23:54:39 2014 +0000
Revision:
8:594cb3bc6f0f
Parent:
7:47cd0d3d5e4d
Child:
9:f80b62b60b1c
fixed led behavior on connection

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