Xively demo for cc3000
Dependencies: NVIC_set_all_priorities cc3000_hostdriver_mbedsocket libxively mbed
main.cpp@4:cea07f0d1eeb, 2013-11-07 (annotated)
- Committer:
- Kojto
- Date:
- Thu Nov 07 20:22:09 2013 +0000
- Revision:
- 4:cea07f0d1eeb
- Parent:
- 3:6eb505b0beb3
- updated to the host driver rev 45; - using the new EthernetInterface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:cc7d755246b3 | 1 | /* mbed Microcontroller Library |
Kojto | 0:cc7d755246b3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 0:cc7d755246b3 | 3 | * |
Kojto | 0:cc7d755246b3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 0:cc7d755246b3 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 0:cc7d755246b3 | 6 | * You may obtain a copy of the License at |
Kojto | 0:cc7d755246b3 | 7 | * |
Kojto | 0:cc7d755246b3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 0:cc7d755246b3 | 9 | * |
Kojto | 0:cc7d755246b3 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 0:cc7d755246b3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 0:cc7d755246b3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 0:cc7d755246b3 | 13 | * See the License for the specific language governing permissions and |
Kojto | 0:cc7d755246b3 | 14 | * limitations under the License. |
Kojto | 0:cc7d755246b3 | 15 | */ |
Kojto | 0:cc7d755246b3 | 16 | #include "mbed.h" |
Kojto | 0:cc7d755246b3 | 17 | #include "cc3000.h" |
Kojto | 0:cc7d755246b3 | 18 | #include "main.h" |
Kojto | 0:cc7d755246b3 | 19 | #include "xively.h" |
Kojto | 0:cc7d755246b3 | 20 | #include "xi_err.h" |
Kojto | 0:cc7d755246b3 | 21 | |
Kojto | 2:ed5cf21e63b4 | 22 | #define XI_FEED_ID 123 // set Xively Feed ID (numerical, no quoutes) |
Kojto | 2:ed5cf21e63b4 | 23 | #define XI_API_KEY "apikey" // set Xively API key (double-quoted string) |
Kojto | 0:cc7d755246b3 | 24 | |
Kojto | 0:cc7d755246b3 | 25 | using namespace mbed_cc3000; |
Kojto | 0:cc7d755246b3 | 26 | |
Kojto | 0:cc7d755246b3 | 27 | DigitalOut myled(LED1); |
Kojto | 2:ed5cf21e63b4 | 28 | Ticker flipper_seconds; |
Kojto | 2:ed5cf21e63b4 | 29 | Ticker flipper_led; |
Kojto | 1:ad3b3e58ba36 | 30 | uint32_t time_seconds; |
Kojto | 0:cc7d755246b3 | 31 | |
Kojto | 0:cc7d755246b3 | 32 | /* cc3000 module declaration specific for user's board. Check also init() */ |
Kojto | 0:cc7d755246b3 | 33 | #if (MY_BOARD == WIGO) |
Kojto | 4:cea07f0d1eeb | 34 | cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false); |
Kojto | 0:cc7d755246b3 | 35 | Serial pc(USBTX, USBRX); |
Kojto | 0:cc7d755246b3 | 36 | #elif (MY_BOARD == WIFI_DIPCORTEX) |
Kojto | 4:cea07f0d1eeb | 37 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false); |
Kojto | 0:cc7d755246b3 | 38 | Serial pc(UART_TX, UART_RX); |
Kojto | 4:cea07f0d1eeb | 39 | #elif (MY_BOARD == MBED_BOARD_EXAMPLE) |
Kojto | 4:cea07f0d1eeb | 40 | cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false); |
Kojto | 4:cea07f0d1eeb | 41 | Serial pc(USBTX, USBRX); |
Kojto | 0:cc7d755246b3 | 42 | #else |
Kojto | 0:cc7d755246b3 | 43 | |
Kojto | 0:cc7d755246b3 | 44 | #endif |
Kojto | 0:cc7d755246b3 | 45 | |
Kojto | 0:cc7d755246b3 | 46 | /** |
Kojto | 2:ed5cf21e63b4 | 47 | * \brief Count seconds |
Kojto | 2:ed5cf21e63b4 | 48 | * \param none |
Kojto | 2:ed5cf21e63b4 | 49 | * \return none |
Kojto | 2:ed5cf21e63b4 | 50 | */ |
Kojto | 1:ad3b3e58ba36 | 51 | void update_timer() { |
Kojto | 1:ad3b3e58ba36 | 52 | time_seconds++; |
Kojto | 1:ad3b3e58ba36 | 53 | } |
Kojto | 2:ed5cf21e63b4 | 54 | |
Kojto | 0:cc7d755246b3 | 55 | /** |
Kojto | 0:cc7d755246b3 | 56 | * \brief xively demo |
Kojto | 0:cc7d755246b3 | 57 | * \param none |
Kojto | 0:cc7d755246b3 | 58 | * \return int |
Kojto | 0:cc7d755246b3 | 59 | */ |
Kojto | 0:cc7d755246b3 | 60 | int main() { |
Kojto | 0:cc7d755246b3 | 61 | init(); /* board dependent init */ |
Kojto | 0:cc7d755246b3 | 62 | pc.baud(115200); |
Kojto | 0:cc7d755246b3 | 63 | |
Kojto | 0:cc7d755246b3 | 64 | printf("cc3000 xively demo. \r\n"); |
Kojto | 4:cea07f0d1eeb | 65 | wifi.init(); |
Kojto | 4:cea07f0d1eeb | 66 | if (wifi.connect() == -1) { |
Kojto | 4:cea07f0d1eeb | 67 | printf("Failed to connect. Please verify connection details and try again. \r\n"); |
Kojto | 4:cea07f0d1eeb | 68 | } else { |
Kojto | 4:cea07f0d1eeb | 69 | printf("IP address: %s \r\n", wifi.getIPAddress()); |
Kojto | 0:cc7d755246b3 | 70 | } |
Kojto | 4:cea07f0d1eeb | 71 | |
Kojto | 0:cc7d755246b3 | 72 | xi_feed_t feed; |
Kojto | 0:cc7d755246b3 | 73 | printf("Size of xi_feed_t: %d \r\n", sizeof(xi_feed_t)); //with default settings this can get over 11kB |
Kojto | 0:cc7d755246b3 | 74 | std::memset(&feed, 0, sizeof( xi_feed_t ) ); |
Kojto | 0:cc7d755246b3 | 75 | |
Kojto | 0:cc7d755246b3 | 76 | feed.feed_id = XI_FEED_ID; |
Kojto | 1:ad3b3e58ba36 | 77 | feed.datastream_count = 2; |
Kojto | 0:cc7d755246b3 | 78 | |
Kojto | 0:cc7d755246b3 | 79 | feed.datastreams[0].datapoint_count = 1; |
Kojto | 0:cc7d755246b3 | 80 | xi_datastream_t* status_datastream = &feed.datastreams[0]; |
Kojto | 1:ad3b3e58ba36 | 81 | strcpy(status_datastream->datastream_id, "LED1"); |
Kojto | 0:cc7d755246b3 | 82 | xi_datapoint_t* led_status = &status_datastream->datapoints[0]; |
Kojto | 0:cc7d755246b3 | 83 | |
Kojto | 1:ad3b3e58ba36 | 84 | feed.datastreams[1].datapoint_count = 1; |
Kojto | 1:ad3b3e58ba36 | 85 | xi_datastream_t* counter_datastream = &feed.datastreams[1]; |
Kojto | 1:ad3b3e58ba36 | 86 | strcpy(counter_datastream->datastream_id, "Uptime"); |
Kojto | 1:ad3b3e58ba36 | 87 | xi_datapoint_t* counter = &counter_datastream->datapoints[0]; |
Kojto | 1:ad3b3e58ba36 | 88 | |
Kojto | 0:cc7d755246b3 | 89 | xi_context_t* xi_context = xi_create_context(XI_HTTP, XI_API_KEY, feed.feed_id); |
Kojto | 0:cc7d755246b3 | 90 | |
Kojto | 3:6eb505b0beb3 | 91 | if (xi_context == NULL) |
Kojto | 0:cc7d755246b3 | 92 | { |
Kojto | 1:ad3b3e58ba36 | 93 | printf("Context failed to initialized. \r\n"); |
Kojto | 0:cc7d755246b3 | 94 | return -1; |
Kojto | 0:cc7d755246b3 | 95 | } |
Kojto | 0:cc7d755246b3 | 96 | |
Kojto | 2:ed5cf21e63b4 | 97 | flipper_seconds.attach(&update_timer, 1.0); |
Kojto | 1:ad3b3e58ba36 | 98 | |
Kojto | 1:ad3b3e58ba36 | 99 | while(1) { |
Kojto | 3:6eb505b0beb3 | 100 | xi_set_value_f32(led_status, myled); |
Kojto | 3:6eb505b0beb3 | 101 | xi_set_value_f32(counter, time_seconds); |
Kojto | 1:ad3b3e58ba36 | 102 | |
Kojto | 3:6eb505b0beb3 | 103 | printf( "update...\r\n"); |
Kojto | 3:6eb505b0beb3 | 104 | xi_feed_update(xi_context, &feed); |
Kojto | 3:6eb505b0beb3 | 105 | printf( "done...\r\n"); |
Kojto | 3:6eb505b0beb3 | 106 | wait(10); |
Kojto | 3:6eb505b0beb3 | 107 | myled = myled ^ 0x1; |
Kojto | 0:cc7d755246b3 | 108 | } |
Kojto | 0:cc7d755246b3 | 109 | |
Kojto | 0:cc7d755246b3 | 110 | } |
Kojto | 0:cc7d755246b3 | 111 |