Simplest MurataWifi ping server

Dependencies:   NySNICInterface mbed-rtos mbed

Fork of SNIC-xively-jumpstart-demo by muRata

main.cpp

Committer:
kishino
Date:
2014-06-11
Revision:
17:0bf3c49a83d5
Parent:
16:ed9b9c28f860
Child:
18:62daece493a9

File content as of revision 17:0bf3c49a83d5:

#include "mbed.h"
#include "SNIC_WifiInterface.h"

#define XI_FEED_ID 1056160623 // set Xively Feed ID (numerical, no quoutes)
#define XI_API_KEY "Wg7CfZDrj7VjIIpiYzdDrMow6wdENAOGjkIfQ0fUjJh6DAw2" // set Xively API key (double-quoted string)

#include "app_board_io.h"

#include "xively.h"
#include "xi_err.h"

#include "MMA7660.h"
#include "LM75B.h"
#include "C12832_lcd.h"

#include "PowerControl/EthernetPowerControl.h"

#include "UDPSocket.h"

MMA7660 axl(p28, p27);
LM75B tmp(p28, p27);
C12832_LCD lcd;

#include "logo.h"

#if 0
#define DEMO_AP_SSID                  "muRata12345"
#define DEMO_AP_SECURITY_TYPE         e_SEC_WPA2_AES
#define DEMO_AP_SECUTIRY_KEY          "12345678"
#define DEMO_AP_SECUTIRY_KEY_LEN      8
#else
#define DEMO_AP_SSID                  "E2N1-Lab-Buffalo-D302"
//#define DEMO_AP_SECURITY_TYPE         e_SEC_OPEN
//#define DEMO_AP_SECUTIRY_KEY          ""
#define DEMO_AP_SECURITY_TYPE         e_SEC_WPA2_AES
#define DEMO_AP_SECUTIRY_KEY          "12345678"
#define DEMO_AP_SECUTIRY_KEY_LEN      8
#endif
/** Wi-Fi SNIC UART Interface*/
C_SNIC_WifiInterface     mSNICwifi( p9, p10, NC, NC, p30 );
Serial pc(USBTX, USBRX);

char buf[2048];
int main() {
    
    PHY_PowerDown();
    
    pc.baud( 115200 );
    printf("main\r\n");
    lcd_print_xively_logo();
    
    // Initialize Wi-Fi interface
    int s = mSNICwifi.init();
    
    lcd_printf("init();\r\n");
    
    if( s != 0 )
    {
        lcd_printf( "Could not initialise. Will halt!\n" );        
        return -1;
    }    
        
    wait(0.5);
    mSNICwifi.disconnect();
    lcd_printf("disconnect();\r\n");
    
    wait(0.5);
    // Connect AP
    s = mSNICwifi.connect( DEMO_AP_SSID
                        , strlen(DEMO_AP_SSID)
                        , DEMO_AP_SECURITY_TYPE
                        , DEMO_AP_SECUTIRY_KEY
                        , DEMO_AP_SECUTIRY_KEY_LEN );
    lcd_printf("connect();\r\n");
   if( s != 0 )
    {
        lcd_printf( "Could not connect. Will halt!\n" );
        return -1;
    }
    wait(0.5);

    UDPSocket udp;
    Endpoint  ep;

/*
    // UDP Client
    char udp_data_p[] = {"This is UDP data."};
    int  len = strlen(udp_data_p);
    
    ep.set_address("192.168.10.101", 30000);
    
    while(1)
    {
        udp.sendTo( ep, udp_data_p, len );
        wait(1);
    }
    printf("UDP bind\r\n");
    udp.bind(40000);
    
    int len, i;
    while(1)
    {
        len = udp.receiveFrom( ep, buf, 2048 );
        if( len < 1 )
        {
            Thread::yield();
            continue;
        }
        printf("recv from[%s:%d]\r\n", ep.get_address(), ep.get_port());
        for(i=0;i<len;i++)
        {
            printf("%02x", buf[i]);
        }
        printf("\r\n");
    }
*/
    
#if 1
    xi_feed_t feed;
    memset( &feed, NULL, sizeof( xi_feed_t ) );
    
    feed.feed_id = XI_FEED_ID;
    feed.datastream_count = 3;
    
    feed.datastreams[0].datapoint_count = 1;
    xi_datastream_t* orientation_datastream = &feed.datastreams[0];
    strcpy( orientation_datastream->datastream_id, "orientation" );
    xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];

    feed.datastreams[1].datapoint_count = 1;
    xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
    strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
    xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
    
    feed.datastreams[2].datapoint_count = 1;
    xi_datastream_t* temperature_datastream = &feed.datastreams[2];
    strcpy( temperature_datastream->datastream_id, "temperature" );
    xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
    
    // create the cosm library context
    xi_context_t* xi_context
        = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );

    // check if everything works
    if( xi_context == NULL )
    {
        return -1;
    }

    while(1) {
      
      switch( axl.getSide() ) {
        case MMA7660::Front:
          xi_set_value_str( current_side_rotation, "front" );
          break;
        case MMA7660::Back:
          xi_set_value_str( current_side_rotation, "back" );
          break;
        default:
          xi_set_value_str( current_side_rotation, "unknown" );
          break;
      }
      
      switch( axl.getOrientation() ) {
        case MMA7660::Down:
          xi_set_value_str( current_orientation, "down" );
          break;
        case MMA7660::Up:
           xi_set_value_str( current_orientation, "up" );
           break;
        case MMA7660::Right:
          xi_set_value_str( current_orientation, "right" );
          break;
        case MMA7660::Left:
          xi_set_value_str( current_orientation, "left" );
          break;
        default: 
          xi_set_value_str( current_orientation, "unknown" );
          break;
      }
      
      xi_set_value_f32( current_temperature, tmp.read() );
        
      lcd_printf( "update...\n" );
      xi_feed_update( xi_context, &feed );
      lcd_printf( "done...\n" );
      
      wait( 1.0 );
    }
#endif
}