5.7.1 Execution
main.c - Initialize the board, connect to a PubNub Data Stream Network and communicate with the cloud.
- Code summary:
- Modify the following
3 string values in main.h file. Those must be
set in the PubNub
console.
/** PubNub settings. */ #define MAIN_PUBNUB_PUBLISH_KEY "demo" // "pub-c-e71e5bed-0fee-4263-a843-c9de85c8825e" #define MAIN_PUBNUB_SUBSCRIBE_KEY "demo" // "sub-c-746522be-7e35-11e5-98ab-0619f8985a4f" #define MAIN_PUBNUB_CHANNEL "WINC1500_00:00" /**< Do not change - last digits will be updated with MAC address. */
The user can create their publish key and subscribe key in the PubNub web page.
- Configure the network
parameters in
main.h.
/** Wi-Fi Settings */ #define MAIN_WLAN_SSID "DEMO_AP" /* < Destination SSID */ #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WPA_PSK /* < Security manner */ #define MAIN_WLAN_PSK "12345678" /* < Password for Destination SSID */
- The
main.c file has all the code needed to do
the system initialization by configuring the necessary components
such as UART, temperature sensor, Wi-Fi driver and
socket.
/* Initialize Socket API. */ socketInit(); registerSocketCallback(m2m_tcp_socket_handler, socket_resolve_cb); ... /* Connect to AP using Wi-Fi settings from main.h. */ printf("main: Wi-Fi connecting to AP using hardcoded credentials...\r\n"); m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); while (1) { m2m_wifi_handle_events(NULL) /* Device is connected to AP. */ .... printf("main: subscribe event, PNR_OK\r\n"); pubnub_subscribe(pPubNubCfg, PubNubChannel); } /* Process any received messages from the channel we subscribed. */ while (1) { char const *msg = pubnub_get(pPubNubCfg); .... /* Any other type of JSON message. */ printf("main: received message: %s\r\n", msg); ... pubnub_subscribe(pPubNubCfg, PubNubChannel);
-
The first step is to initialize the PubNub client module by calling the function
pubnub_init()
. This function receives three parameters which arepublishkey
,subscribe key
and a pointer to the context retrieved from thepubnub_get_ctx
function./* Initialize PubNub API. */ printf("main: PubNub configured with following settings:\r\n"); printf("main: - Publish key: \"%s\", Subscribe key: \"%s\", Channel: \"%s\".\r\n\r\n", PubNubPublishKey, PubNubSubscribeKey, PubNubChannel); pPubNubCfg = pubnub_get_ctx(0); pubnub_init(pPubNubCfg, PubNubPublishKey, PubNubSubscribeKey);
- In order to send the
data from the temperature sensor on the I/O1 to the PubNub server,
the function
pubnub_publish()
should be used as shown below:/* Publish the temperature measurements periodically. */ if (gu32MsTicks - gu32publishDelay > MAIN_PUBNUB_PUBLISH_INTERVAL) { gu32publishDelay = gu32MsTicks; adc_start_conversion(&adc_instance); temperature = at30tse_read_temperature(); adc_read(&adc_instance, &light); ... pubnub_publish(pPubNubCfg, PubNubChannel, buf);
-
In order to subscribe to a Pubnub channel, we need to use
pubnub_subscribe()
andpubnub_get()
functions as shown below:char const *msg = pubnub_get(pPubNubCfg); ... pubnub_subscribe(pPubNubCfg, PubNubChannel);
- Modify the following
3 string values in main.h file. Those must be
set in the PubNub
console.
- Build the program and download it into the board.
- Start the application.
- Once the SAM D21 is connected to the AP with internet connectivity, it will immediately connect to the PubNub server and will start sending the temperature data at regular intervals.