Demo application of SNICInterface library for Murata TypeYD, which reports sensor data periodically to Xively cloud server . Hardware platform: mbed application board (https://mbed.org/cookbook/mbed-application-board), mbed LPC1768 (https://mbed.org/platforms/mbed-LPC1768/) and TypeYD.

Dependencies:   C12832 LM75B MMA7660 SNICInterface libxively mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
00002  *  muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 #include "mbed.h"
00020 #include "SNIC_WifiInterface.h"
00021 
00022 #define XI_FEED_ID 1056160623 // set Xively Feed ID (numerical, no quoutes)
00023 #define XI_API_KEY "Wg7CfZDrj7VjIIpiYzdDrMow6wdENAOGjkIfQ0fUjJh6DAw2" // set Xively API key (double-quoted string)
00024 
00025 #include "app_board_io.h"
00026 
00027 #include "xively.h"
00028 #include "xi_err.h"
00029 
00030 #include "MMA7660.h"
00031 #include "LM75B.h"
00032 #include "C12832.h"
00033 
00034 #if defined(TARGET_LPC1768)
00035 #include "PowerControl/EthernetPowerControl.h"
00036 C12832 lcd(p5, p7, p6, p8, p11);
00037 MMA7660 axl(p28, p27);
00038 LM75B tmp(p28, p27);
00039 Serial pc(USBTX, USBRX);    /* for DEBUG_PRINT */
00040 #elif defined(TARGET_KL46Z)
00041 C12832 lcd(D11, D13, D12, D7, D10);
00042 MMA7660 axl(I2C_SDA, I2C_SCL);
00043 LM75B tmp(I2C_SDA, I2C_SCL);
00044 Serial pc(P2_12, P2_11);    /* for DEBUG_PRINT */
00045 #endif
00046 
00047 #include "logo.h"
00048 
00049 #define DEMO_AP_SSID                  "AP_SSID"
00050 #define DEMO_AP_SECURITY_TYPE         e_SEC_WPA2_AES
00051 #define DEMO_AP_SECUTIRY_KEY          "WPA2_PASSPHRASE"
00052 #define DEMO_AP_SECUTIRY_KEY_LEN      15
00053 
00054 /** Wi-Fi SNIC UART Interface*/
00055 #if defined(TARGET_LPC1768)
00056 C_SNIC_WifiInterface     mSNICwifi( p9, p10, NC, NC, p30 );
00057 #elif defined(TARGET_KL46Z)
00058 C_SNIC_WifiInterface     mSNICwifi( D1, D0, NC, NC, D3 );
00059 #endif
00060 
00061 int main() {
00062     
00063 #if defined(TARGET_LPC1768)
00064     PHY_PowerDown();
00065 #endif
00066 #ifdef _DEBUG
00067     pc.baud( 115200 );
00068 #endif
00069     DEBUG_PRINT("main\r\n");
00070     lcd_print_xively_logo();
00071     
00072     // Initialize Wi-Fi interface
00073     int s = mSNICwifi.init();
00074     
00075     lcd_printf("init();\r\n");
00076     
00077     if( s != 0 )
00078     {
00079         lcd_printf( "Could not initialise. Will halt!\n" );        
00080         return -1;
00081     }    
00082     wait(0.5);
00083     s = mSNICwifi.disconnect();
00084     lcd_printf("disconnect();\r\n");
00085     if( s != 0 )
00086     {
00087         printf( "disconnect failed\r\n" );        
00088         return -1;
00089     }    
00090     
00091     wait(0.3);
00092     // Connect AP
00093     mSNICwifi.connect( DEMO_AP_SSID
00094                         , strlen(DEMO_AP_SSID)
00095                         , DEMO_AP_SECURITY_TYPE
00096                         , DEMO_AP_SECUTIRY_KEY
00097                         , DEMO_AP_SECUTIRY_KEY_LEN );
00098     lcd_printf("connect();\r\n");
00099     wait(0.5);
00100     
00101     lcd_printf("IP Config();\r\n");
00102     mSNICwifi.setIPConfig( true );
00103     
00104     wait(0.5);
00105 
00106     xi_feed_t feed;
00107     memset( &feed, NULL, sizeof( xi_feed_t ) );
00108     
00109     feed.feed_id = XI_FEED_ID;
00110     feed.datastream_count = 3;
00111     
00112     feed.datastreams[0].datapoint_count = 1;
00113     xi_datastream_t* orientation_datastream = &feed.datastreams[0];
00114     strcpy( orientation_datastream->datastream_id, "orientation" );
00115     xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
00116 
00117     feed.datastreams[1].datapoint_count = 1;
00118     xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
00119     strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
00120     xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
00121     
00122     feed.datastreams[2].datapoint_count = 1;
00123     xi_datastream_t* temperature_datastream = &feed.datastreams[2];
00124     strcpy( temperature_datastream->datastream_id, "temperature" );
00125     xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
00126     
00127     // create the cosm library context
00128     xi_context_t* xi_context
00129         = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
00130 
00131     // check if everything works
00132     if( xi_context == NULL )
00133     {
00134         return -1;
00135     }
00136 
00137     while(1) {
00138       switch( axl.getSide() ) {
00139         case MMA7660::Front:
00140           xi_set_value_str( current_side_rotation, "front" );
00141           break;
00142         case MMA7660::Back:
00143           xi_set_value_str( current_side_rotation, "back" );
00144           break;
00145         default:
00146           xi_set_value_str( current_side_rotation, "unknown" );
00147           break;
00148       }
00149       
00150       switch( axl.getOrientation() ) {
00151         case MMA7660::Down:
00152           xi_set_value_str( current_orientation, "down" );
00153           break;
00154         case MMA7660::Up:
00155            xi_set_value_str( current_orientation, "up" );
00156            break;
00157         case MMA7660::Right:
00158           xi_set_value_str( current_orientation, "right" );
00159           break;
00160         case MMA7660::Left:
00161           xi_set_value_str( current_orientation, "left" );
00162           break;
00163         default: 
00164           xi_set_value_str( current_orientation, "unknown" );
00165           break;
00166       }
00167       
00168       xi_set_value_f32( current_temperature, tmp.read() );
00169         
00170       lcd_printf( "update...\n" );
00171       xi_feed_update( xi_context, &feed );
00172       lcd_printf( "done...\n" );
00173       
00174       wait( 1.0 );
00175     }
00176 }