cc3000 twitter demo, uses supertweet services. Set your account there and then fill a user and a secret inside the demo.

Dependencies:   HTTPClient NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Uses http://www.supertweet.net/, please register there and fill user and secret inside the application.

Committer:
Kojto
Date:
Mon Oct 07 18:42:23 2013 +0200
Revision:
1:f35cc7e300f7
Parent:
0:5aa9273cb749
Child:
2:0ceaeaa53a81
Again, comments and \r

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:5aa9273cb749 1 /* mbed Microcontroller Library
Kojto 0:5aa9273cb749 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 0:5aa9273cb749 3 *
Kojto 0:5aa9273cb749 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 0:5aa9273cb749 5 * you may not use this file except in compliance with the License.
Kojto 0:5aa9273cb749 6 * You may obtain a copy of the License at
Kojto 0:5aa9273cb749 7 *
Kojto 0:5aa9273cb749 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 0:5aa9273cb749 9 *
Kojto 0:5aa9273cb749 10 * Unless required by applicable law or agreed to in writing, software
Kojto 0:5aa9273cb749 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 0:5aa9273cb749 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 0:5aa9273cb749 13 * See the License for the specific language governing permissions and
Kojto 0:5aa9273cb749 14 * limitations under the License.
Kojto 0:5aa9273cb749 15 */
Kojto 0:5aa9273cb749 16 #include "mbed.h"
Kojto 0:5aa9273cb749 17 #include "cc3000.h"
Kojto 0:5aa9273cb749 18 #include "main.h"
Kojto 0:5aa9273cb749 19 #include "HTTPClient.h"
Kojto 0:5aa9273cb749 20
Kojto 0:5aa9273cb749 21 using namespace mbed_cc3000;
Kojto 0:5aa9273cb749 22
Kojto 0:5aa9273cb749 23 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 0:5aa9273cb749 24 #if (MY_BOARD == WIGO)
Kojto 0:5aa9273cb749 25 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
Kojto 0:5aa9273cb749 26 Serial pc(USBTX, USBRX);
Kojto 0:5aa9273cb749 27 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 0:5aa9273cb749 28 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), PIN_INT0_IRQn);
Kojto 0:5aa9273cb749 29 Serial pc(UART_TX, UART_RX);
Kojto 0:5aa9273cb749 30 #else
Kojto 0:5aa9273cb749 31
Kojto 0:5aa9273cb749 32 #endif
Kojto 0:5aa9273cb749 33
Kojto 0:5aa9273cb749 34 tUserFS user_info;
Kojto 0:5aa9273cb749 35 HTTPClient twitter;
Kojto 0:5aa9273cb749 36 char str[512];
Kojto 0:5aa9273cb749 37
Kojto 0:5aa9273cb749 38 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 0:5aa9273cb749 39 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
Kojto 0:5aa9273cb749 40 #else
Kojto 0:5aa9273cb749 41 const uint8_t smartconfigkey = 0;
Kojto 0:5aa9273cb749 42 #endif
Kojto 0:5aa9273cb749 43
Kojto 0:5aa9273cb749 44 /**
Kojto 0:5aa9273cb749 45 * \brief Print cc3000 information
Kojto 0:5aa9273cb749 46 * \param none
Kojto 0:5aa9273cb749 47 * \return none
Kojto 0:5aa9273cb749 48 */
Kojto 0:5aa9273cb749 49 void print_cc3000_info() {
Kojto 0:5aa9273cb749 50 uint8_t myMAC[8];
Kojto 0:5aa9273cb749 51
Kojto 0:5aa9273cb749 52 printf("MAC address + cc3000 info \r\n");
Kojto 0:5aa9273cb749 53 wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:5aa9273cb749 54 wifi.get_mac_address(myMAC);
Kojto 0:5aa9273cb749 55 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
Kojto 0:5aa9273cb749 56
Kojto 0:5aa9273cb749 57 printf(" FTC %i \r\n",user_info.FTC);
Kojto 0:5aa9273cb749 58 printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
Kojto 0:5aa9273cb749 59 printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
Kojto 0:5aa9273cb749 60 printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
Kojto 0:5aa9273cb749 61 printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
Kojto 0:5aa9273cb749 62 }
Kojto 0:5aa9273cb749 63
Kojto 0:5aa9273cb749 64 /**
Kojto 0:5aa9273cb749 65 * \brief Connect to SSID with a timeout
Kojto 0:5aa9273cb749 66 * \param ssid Name of SSID
Kojto 0:5aa9273cb749 67 * \param key Password
Kojto 0:5aa9273cb749 68 * \param sec_mode Security mode
Kojto 0:5aa9273cb749 69 * \return none
Kojto 0:5aa9273cb749 70 */
Kojto 0:5aa9273cb749 71 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
Kojto 0:5aa9273cb749 72 printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid);
Kojto 0:5aa9273cb749 73 if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
Kojto 0:5aa9273cb749 74 printf(" Connected \r\n");
Kojto 0:5aa9273cb749 75 } else {
Kojto 0:5aa9273cb749 76 printf(" Connection timed-out (error). Please restart. \r\n");
Kojto 0:5aa9273cb749 77 while(1);
Kojto 0:5aa9273cb749 78 }
Kojto 0:5aa9273cb749 79 }
Kojto 0:5aa9273cb749 80
Kojto 0:5aa9273cb749 81 /**
Kojto 0:5aa9273cb749 82 * \brief Connect to SSID without security
Kojto 0:5aa9273cb749 83 * \param ssid Name of SSID
Kojto 0:5aa9273cb749 84 * \return none
Kojto 0:5aa9273cb749 85 */
Kojto 0:5aa9273cb749 86 void connect_to_ssid(char *ssid) {
Kojto 0:5aa9273cb749 87 wifi.connect_open((uint8_t *)ssid);
Kojto 0:5aa9273cb749 88 }
Kojto 0:5aa9273cb749 89
Kojto 0:5aa9273cb749 90 /**
Kojto 0:5aa9273cb749 91 * \brief First time configuration
Kojto 0:5aa9273cb749 92 * \param none
Kojto 0:5aa9273cb749 93 * \return none
Kojto 0:5aa9273cb749 94 */
Kojto 0:5aa9273cb749 95 void do_FTC(void) {
Kojto 0:5aa9273cb749 96 printf("Running First Time Configuration \r\n");
Kojto 0:5aa9273cb749 97 wifi.start_smart_config(smartconfigkey);
Kojto 0:5aa9273cb749 98 while (wifi.is_dhcp_configured() == false) {
Kojto 0:5aa9273cb749 99 wait_ms(500);
Kojto 0:5aa9273cb749 100 printf("Waiting for dhcp to be set. \r\n");
Kojto 0:5aa9273cb749 101 }
Kojto 0:5aa9273cb749 102 user_info.FTC = 1;
Kojto 0:5aa9273cb749 103 wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:5aa9273cb749 104 wifi._wlan.stop();
Kojto 0:5aa9273cb749 105 printf("FTC finished.\n");
Kojto 0:5aa9273cb749 106 }
Kojto 0:5aa9273cb749 107
Kojto 0:5aa9273cb749 108 /**
Kojto 1:f35cc7e300f7 109 * \brief Twitter demo
Kojto 0:5aa9273cb749 110 * \param none
Kojto 0:5aa9273cb749 111 * \return int
Kojto 0:5aa9273cb749 112 */
Kojto 0:5aa9273cb749 113 int main() {
Kojto 0:5aa9273cb749 114 init(); /* board dependent init */
Kojto 0:5aa9273cb749 115 pc.baud(115200);
Kojto 0:5aa9273cb749 116
Kojto 0:5aa9273cb749 117 wifi.start(0);
Kojto 1:f35cc7e300f7 118 printf("cc3000 twitter demo. \r\n");
Kojto 0:5aa9273cb749 119 print_cc3000_info();
Kojto 0:5aa9273cb749 120
Kojto 0:5aa9273cb749 121 printf("Attempting SSID Connection. \r\n");
Kojto 0:5aa9273cb749 122 #if (USE_SMART_CONFIG == 1)
Kojto 0:5aa9273cb749 123 if (user_info.FTC == 1) {
Kojto 0:5aa9273cb749 124 wifi._wlan.ioctl_set_connection_policy(0, 1, 1);
Kojto 0:5aa9273cb749 125 } else {
Kojto 0:5aa9273cb749 126 printf("Smart config is not set, starting configuration. \r\n");
Kojto 0:5aa9273cb749 127 do_FTC();
Kojto 0:5aa9273cb749 128 printf("Smart config is set. Please restart your board. \r\n");
Kojto 0:5aa9273cb749 129 while(1);
Kojto 0:5aa9273cb749 130 }
Kojto 0:5aa9273cb749 131 #else
Kojto 0:5aa9273cb749 132 wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
Kojto 0:5aa9273cb749 133 #ifndef CC3000_TINY_DRIVER
Kojto 0:5aa9273cb749 134 #ifdef AP_KEY
Kojto 0:5aa9273cb749 135 connect_to_ssid(SSID, AP_KEY, AP_SECURITY);
Kojto 0:5aa9273cb749 136 #else
Kojto 0:5aa9273cb749 137 connect_to_ssid(SSID);
Kojto 0:5aa9273cb749 138 #endif
Kojto 0:5aa9273cb749 139 #else
Kojto 0:5aa9273cb749 140 connect_to_ssid(SSID);
Kojto 0:5aa9273cb749 141 #endif
Kojto 0:5aa9273cb749 142 #endif
Kojto 0:5aa9273cb749 143 printf("DHCP request \r\n");
Kojto 0:5aa9273cb749 144 while (wifi.is_dhcp_configured() == false) {
Kojto 0:5aa9273cb749 145 wait_ms(500);
Kojto 0:5aa9273cb749 146 printf(" Waiting for dhcp to be set. \r\n");
Kojto 0:5aa9273cb749 147 }
Kojto 0:5aa9273cb749 148
Kojto 0:5aa9273cb749 149 tNetappIpconfigRetArgs ipinfo2;
Kojto 0:5aa9273cb749 150 wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 0:5aa9273cb749 151 printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
Kojto 0:5aa9273cb749 152
Kojto 0:5aa9273cb749 153 //POST data
Kojto 0:5aa9273cb749 154 HTTPMap map;
Kojto 0:5aa9273cb749 155 HTTPText inText(str, 512);
Kojto 0:5aa9273cb749 156 map.put("status", "I am tweeting from mbed with cc3000.");
Kojto 1:f35cc7e300f7 157
Kojto 1:f35cc7e300f7 158 printf("\r\nTrying to post data...\r\n");
Kojto 0:5aa9273cb749 159 twitter.basicAuth("username", "secret"); //We use basic authentication, replace with you account's parameters
Kojto 0:5aa9273cb749 160 int ret = twitter.post("http://api.supertweet.net/1.1/statuses/update.json", map, &inText);
Kojto 0:5aa9273cb749 161 if (!ret)
Kojto 0:5aa9273cb749 162 {
Kojto 1:f35cc7e300f7 163 printf("Executed POST successfully - read %d characters\r\n", strlen(str));
Kojto 1:f35cc7e300f7 164 printf("Result: %s \r\n", str);
Kojto 0:5aa9273cb749 165 }
Kojto 0:5aa9273cb749 166 else
Kojto 0:5aa9273cb749 167 {
Kojto 1:f35cc7e300f7 168 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, twitter.getHTTPResponseCode());
Kojto 0:5aa9273cb749 169 }
Kojto 1:f35cc7e300f7 170
Kojto 1:f35cc7e300f7 171 printf("Twitter demo completed.\r\n");
Kojto 0:5aa9273cb749 172 }