Microchip® Advanced Software Framework

demo.c File Reference

IoT Cloud Demo.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "asf.h"
#include "common/include/nm_common.h"
#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"
#include "demo.h"
#include "PubNub.h"

Data Structures

struct  s_msg_temp_keepalive
 
struct  s_msg_temp_prov
 
struct  s_msg_temp_report
 
struct  s_msg_user_input
 Message format definitions. More...
 

Macros

#define HEX2ASCII(x)   (((x) >= 10) ? (((x) - 10) + 'A') : ((x) + '0'))
 
#define IPV4_BYTE(val, index)   ((val >> (index * 8)) & 0xFF)
 
#define STRING_EOL   "\r\n"
 
#define STRING_HEADER
 
#define TEST_BUFFER_SIZE   1460
 Android app: receive buffer definition. More...
 

Typedefs

typedef struct s_msg_temp_keepalive t_msg_temp_keepalive
 
typedef struct s_msg_temp_prov t_msg_temp_prov
 
typedef struct s_msg_temp_report t_msg_temp_report
 
typedef struct s_msg_user_input t_msg_user_input
 Message format definitions. More...
 
typedef enum wifi_status wifi_status_t
 

Enumerations

enum  wifi_status {
  WifiStateInit,
  WifiStateWaitingProv,
  WifiStateConnecting,
  WifiStateConnected,
  WifiStateDisConnected
}
 

Functions

static uint32_t button_press_duration (bool current_button_state)
 Get duration of the SW0 button pressed. More...
 
static void configure_button_led (void)
 
static void configure_console (void)
 Configure UART console. More...
 
static void configure_light_sensor (void)
 
static void m2m_wifi_socket_handler (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Callback to get the Socket event. More...
 
static void m2m_wifi_state (uint8_t u8MsgType, void *pvMsg)
 Callback to get the Wi-Fi status update. More...
 
int main (void)
 Main application function. More...
 
static void set_dev_name_to_mac (uint8 *name, uint8 *mac_addr)
 
static void setup_android_sockets (void)
 
static void socket_resolve_cb (uint8_t *hostName, uint32_t hostIp)
 Callback of gethostbyname function. More...
 
void SysTick_Handler (void)
 

Variables

struct adc_module adc_instance
 UART module for debug. More...
 
struct sockaddr_in addr
 
static struct usart_module cdc_uart_module
 UART module for debug. More...
 
static uint8 gau8SocketTestBuffer [TEST_BUFFER_SIZE]
 
static const char gchHttpProvDomainName [] = DEMO_WLAN_AP_DOMAIN_NAME
 
static char gPSK [64] = { 0 }
 
static char gs8DeviceName [] = DEMO_WLAN_AP_NAME
 
static int gSecType = M2M_WIFI_SEC_WPA_PSK
 
static char gSSID [64] = { 0 }
 
static tstrM2MAPConfig gstrM2MAPConfig
 
static uint32_t gu32androidDelay = 0
 Android app: global counter delay for android app report. More...
 
static uint32_t gu32disconnectCount = 0
 WiFi disconnect event counter. More...
 
volatile uint32_t gu32MsTicks = 0
 SysTick counter to avoid busy wait delay. More...
 
static uint32_t gu32publishDelay = 0
 Global counter delay for timer. More...
 
static uint32_t gu32subscribeDelay = 0
 
wifi_status_t gWifiState = WifiStateInit
 WiFi status variable. More...
 
static t_msg_temp_keepalive msg_temp_keepalive
 Message format declarations. More...
 
static t_msg_temp_report msg_temp_report
 
static pubnub_tpPubNubCfg
 
static char PubNubChannel [] = DEMO_PUBNUB_CHANNEL
 
static const char PubNubPublishKey [] = DEMO_PUBNUB_PUBLISH_KEY
 PubNub global variables. More...
 
static const char PubNubSubscribeKey [] = DEMO_PUBNUB_SUBSCRIBE_KEY
 
static SOCKET sock_rx_android = -1
 Android app: RX and TX socket descriptors. More...
 
static SOCKET sock_tx_android = -1
 

#define HEX2ASCII (   x)    (((x) >= 10) ? (((x) - 10) + 'A') : ((x) + '0'))

Referenced by set_dev_name_to_mac().

#define IPV4_BYTE (   val,
  index 
)    ((val >> (index * 8)) & 0xFF)

Referenced by socket_resolve_cb().

#define STRING_EOL   "\r\n"
#define STRING_HEADER
Value:
"-- IoT Cloud Demo --"STRING_EOL \
"-- "BOARD_NAME " --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Name string macro.
Definition: samd21_xplained_pro.h:65
#define STRING_EOL
Definition: demo.c:108

Referenced by main().

#define TEST_BUFFER_SIZE   1460

Android app: receive buffer definition.

Referenced by m2m_wifi_socket_handler().

Message format definitions.

typedef enum wifi_status wifi_status_t

Enumerator
WifiStateInit 
WifiStateWaitingProv 
WifiStateConnecting 
WifiStateConnected 
WifiStateDisConnected 

static uint32_t button_press_duration ( bool  current_button_state)
static

Get duration of the SW0 button pressed.

Returns
number of ms the button has been pressed.

References gu32MsTicks.

Referenced by main().

static void configure_button_led ( void  )
static
static void m2m_wifi_state ( uint8_t  u8MsgType,
void *  pvMsg 
)
static
int main ( void  )

Main application function.

Initialize board and WINC1500 Wi-Fi module. Publish and subscribe event to pubnub.

Returns
program return value.

References adc_instance, adc_read(), adc_start_conversion(), addr, at30tse_init(), at30tse_read_temperature(), tstrM2MAPConfig::au8SSID, button_press_duration(), close(), configure_button_led(), configure_console(), configure_light_sensor(), delay_init(), DEMO_ANDROID_REPORT_INTERVAL, DEMO_PUBNUB_PUBLISH_INTERVAL, DEMO_PUBNUB_SUBSCRIBE_INTERVAL, gchHttpProvDomainName, gs8DeviceName, gu32androidDelay, gu32MsTicks, gu32publishDelay, gu32subscribeDelay, gWifiState, pubnub::last_result, s_msg_temp_report::led, LED0_ACTIVE, LED0_INACTIVE, LED0_PIN, LED_0_PIN, s_msg_temp_report::light, m2m_strlen(), M2M_SUCCESS, m2m_wifi_default_connect(), m2m_wifi_get_mac_address(), m2m_wifi_get_otp_mac_address(), m2m_wifi_handle_events(), m2m_wifi_init(), m2m_wifi_set_device_name(), m2m_wifi_socket_handler(), m2m_wifi_start_provision_mode(), m2m_wifi_state(), s_msg_temp_keepalive::name, s_msg_temp_report::name, nm_bsp_init(), NULL, PBTT_NONE, PBTT_PUBLISH, tstrWifiInitParam::pfAppWifiCb, PNR_IO_ERROR, PNR_OK, port_pin_get_input_level(), port_pin_get_output_level(), port_pin_set_output_level(), port_pin_toggle_output_level(), PS_IDLE, pubnub_get(), pubnub_get_ctx(), pubnub_init(), pubnub_publish(), pubnub_subscribe(), PubNubChannel, PubNubPublishKey, PubNubSubscribeKey, registerSocketCallback(), sendto(), set_dev_name_to_mac(), sock_tx_android, socket_resolve_cb(), socketInit(), pubnub::state, STRING_HEADER, SW0_PIN, system_cpu_clock_get_hz(), system_init(), pubnub::tcp_socket, s_msg_temp_report::temp, pubnub::trans, WifiStateConnected, WifiStateConnecting, and WifiStateWaitingProv.

static void set_dev_name_to_mac ( uint8 name,
uint8 mac_addr 
)
static

References HEX2ASCII, and m2m_strlen().

Referenced by main().

static void socket_resolve_cb ( uint8_t *  hostName,
uint32_t  hostIp 
)
static

Callback of gethostbyname function.

Parameters
[in]doamin_nameDomain name.
[in]server_ipIP of server.

References handle_dns_found(), and IPV4_BYTE.

Referenced by main().

void SysTick_Handler ( void  )

References gu32MsTicks.

struct adc_module adc_instance

UART module for debug.

Referenced by configure_light_sensor(), and main().

struct sockaddr_in addr

Referenced by main(), and setup_android_sockets().

struct usart_module cdc_uart_module
static

UART module for debug.

Referenced by configure_console().

uint8 gau8SocketTestBuffer[TEST_BUFFER_SIZE]
static

Referenced by m2m_wifi_socket_handler().

const char gchHttpProvDomainName[] = DEMO_WLAN_AP_DOMAIN_NAME
static

Referenced by m2m_wifi_state(), and main().

char gPSK[64] = { 0 }
static
char gs8DeviceName[] = DEMO_WLAN_AP_NAME
static

Referenced by main().

int gSecType = M2M_WIFI_SEC_WPA_PSK
static
char gSSID[64] = { 0 }
static
tstrM2MAPConfig gstrM2MAPConfig
static
Initial value:
= {
}
#define DEMO_WLAN_AP_SECURITY
Definition: demo.h:47
#define DEMO_WLAN_AP_CHANNEL
Definition: demo.h:43
#define DEMO_WLAN_AP_WEP_KEY
Definition: demo.h:46
#define DEMO_WLAN_AP_IP_ADDRESS
Definition: demo.h:50
#define DEMO_WLAN_AP_NAME
Access point configuration for provisioning.
Definition: demo.h:42
#define DEMO_WLAN_AP_WEP_SIZE
Definition: demo.h:45
#define DEMO_WLAN_AP_WEP_INDEX
Definition: demo.h:44
#define DEMO_WLAN_AP_MODE
Definition: demo.h:48
uint32_t gu32androidDelay = 0
static

Android app: global counter delay for android app report.

Referenced by m2m_wifi_socket_handler(), and main().

uint32_t gu32disconnectCount = 0
static

WiFi disconnect event counter.

Referenced by m2m_wifi_state().

volatile uint32_t gu32MsTicks = 0

SysTick counter to avoid busy wait delay.

Referenced by button_press_duration(), main(), and SysTick_Handler().

uint32_t gu32publishDelay = 0
static

Global counter delay for timer.

Referenced by main().

uint32_t gu32subscribeDelay = 0
static

Referenced by main().

WiFi status variable.

Referenced by m2m_wifi_socket_handler(), m2m_wifi_state(), and main().

t_msg_temp_keepalive msg_temp_keepalive
static
Initial value:
=
{
.id0 = 0,
.id1 = 1,
.type = 2,
}

Message format declarations.

t_msg_temp_report msg_temp_report
static
Initial value:
=
{
.id0 = 0,
.id1 = 2,
.led = 0,
.temp = 0,
}
pubnub_t* pPubNubCfg
static
char PubNubChannel[] = DEMO_PUBNUB_CHANNEL
static

Referenced by main().

const char PubNubPublishKey[] = DEMO_PUBNUB_PUBLISH_KEY
static

PubNub global variables.

Referenced by main().

const char PubNubSubscribeKey[] = DEMO_PUBNUB_SUBSCRIBE_KEY
static

Referenced by main().

SOCKET sock_rx_android = -1
static

Android app: RX and TX socket descriptors.

Referenced by m2m_wifi_socket_handler(), and setup_android_sockets().

SOCKET sock_tx_android = -1
static

Referenced by main(), and setup_android_sockets().