Martin Kojtal / Mbed 2 deprecated cc3000_xively_demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket libxively mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "cc3000.h"
00018 #include "main.h"
00019 #include "xively.h"
00020 #include "xi_err.h"
00021 
00022 #define XI_FEED_ID 123 // set Xively Feed ID (numerical, no quoutes)
00023 #define XI_API_KEY "apikey" // set Xively API key (double-quoted string) 
00024 
00025 using namespace mbed_cc3000;
00026 
00027 DigitalOut myled(LED1);
00028 Ticker flipper_seconds;
00029 Ticker flipper_led;
00030 uint32_t time_seconds;
00031 
00032 /* cc3000 module declaration specific for user's board. Check also init() */
00033 #if (MY_BOARD == WIGO)
00034 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
00035 Serial pc(USBTX, USBRX);
00036 #elif (MY_BOARD == WIFI_DIPCORTEX)
00037 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
00038 Serial pc(UART_TX, UART_RX);
00039 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
00040 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
00041 Serial pc(USBTX, USBRX);
00042 #else
00043 
00044 #endif
00045 
00046 /**
00047  *  \brief Count seconds
00048  *  \param none
00049  *  \return none
00050  */
00051 void update_timer() {
00052     time_seconds++;
00053 }
00054 
00055 /**
00056  *  \brief xively demo
00057  *  \param  none
00058  *  \return int
00059  */
00060 int main() {
00061     init(); /* board dependent init */
00062     pc.baud(115200);
00063 
00064     printf("cc3000 xively demo. \r\n");
00065     wifi.init();
00066     if (wifi.connect() == -1) {
00067         printf("Failed to connect. Please verify connection details and try again. \r\n");
00068     } else {
00069         printf("IP address: %s \r\n", wifi.getIPAddress());
00070     }
00071     
00072     xi_feed_t feed;
00073     printf("Size of xi_feed_t: %d \r\n", sizeof(xi_feed_t)); //with default settings this can get over 11kB
00074     std::memset(&feed, 0, sizeof( xi_feed_t ) );
00075    
00076     feed.feed_id = XI_FEED_ID;
00077     feed.datastream_count = 2;
00078     
00079     feed.datastreams[0].datapoint_count = 1;
00080     xi_datastream_t* status_datastream = &feed.datastreams[0];
00081     strcpy(status_datastream->datastream_id, "LED1");
00082     xi_datapoint_t* led_status = &status_datastream->datapoints[0];
00083 
00084     feed.datastreams[1].datapoint_count = 1;
00085     xi_datastream_t* counter_datastream = &feed.datastreams[1];
00086     strcpy(counter_datastream->datastream_id, "Uptime");
00087     xi_datapoint_t* counter = &counter_datastream->datapoints[0];
00088     
00089     xi_context_t* xi_context = xi_create_context(XI_HTTP, XI_API_KEY, feed.feed_id);
00090 
00091     if (xi_context == NULL)
00092     {
00093         printf("Context failed to initialized. \r\n");
00094         return -1;
00095     }
00096         
00097     flipper_seconds.attach(&update_timer, 1.0);
00098 
00099     while(1) {  
00100         xi_set_value_f32(led_status, myled);  
00101         xi_set_value_f32(counter, time_seconds);
00102       
00103         printf( "update...\r\n");
00104         xi_feed_update(xi_context, &feed);
00105         printf( "done...\r\n");
00106         wait(10);
00107         myled = myled ^ 0x1;
00108     }
00109 
00110 }
00111