Skip to content

Commit

Permalink
Some new examples and other updates (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
plerup authored Feb 23, 2023
1 parent a07f982 commit 965453c
Show file tree
Hide file tree
Showing 30 changed files with 526 additions and 18 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ If you already have the Nordic nRFConnect SDK installed and know how to use it,

On the other hand if this is your first encounter with the Nordic nrf53 chipset and possibly also with git, a complete installation script which includes everything needed for development is provided. In this case [goto this page](install/README.md) for more information about the installation process.

You can of course also install everything yourself following the instructions at the [Nordic web site](https://www.nordicsemi.com/Products/Development-software/nrf-connect-sdk). The repository has been tested with nRF Connect versions 1.7.0 up to 2.1.0. However some of the examples do require at least version 1.9.0.
You can of course also install everything yourself following the instructions at the [Nordic web site](https://www.nordicsemi.com/Products/Development-software/nrf-connect-sdk). The repository has been tested with nRF Connect versions 1.7.0 up to 2.2.0. However some of the examples do require at least version 1.9.0.

It also works if you have your own tailored installation. You just have to specify where to find the nRFConnect environment parts. More about this below.

Expand All @@ -40,6 +40,8 @@ The actual flash process is different if you have a debuger unit or are just usi

**Also please note** that by flashing these examples you will overwrite the "Sensor Aggregation" firmware which is installed in a fresh XPLR-IOT-1. Should you later want to restore that firmware, [this page](https://github.com/u-blox/XPLR-IOT-1-software) will show you how.

Some of the Bluetooth examples requires at least version 1.2 of ubxlib, to be released in March 2023.

# Getting started

Once you have everything installed and have connected your XPLR-IOT-1, you can start exploring the functionality of this repository.
Expand Down Expand Up @@ -198,7 +200,7 @@ To flash the built network cpu firmware the following command must be used after

This is only required to be done once.

Please note that this command will only work for examples that has defined CONFIG_BT. The current examples doing that are: ibeacon, scanner and aoa_tag. In this case the command will be for ibeacon:
Please note that this command will only work for examples that has defined CONFIG_BT. The current examples doing that are: ble_ibeacon_z, ble_scan_z and aoa_tag. In this case the command will be for ibeacon_z:

do flash_net -e ibeacon
do flash_net -e ble_ibeacon_z

4 changes: 4 additions & 0 deletions config/xplriot1.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,7 @@ based on the Nordic nrf53dk board

};
};

&spi4 {
status = "disabled";
};
File renamed without changes.
File renamed without changes.
95 changes: 95 additions & 0 deletions examples/ble_ibeacon/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2023 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
*
* A demo on setting up an iBeacon using ubxlib GAP/GATT apis.
*
* IMPORTANT! ubxlib version 1.2 or later is required.
*
*/

#include <stdio.h>

#include "ubxlib.h"

#define ADV_INTERVAL_MS 1000
uint8_t ibeacon_data[] = {
0x4c, 0x00, // Apple ID
0x02, 0x15, // iBeacon Type and size
0x01, 0x02, 0x03, 0x04, // Dummy UUID
0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C,
0x0D, 0x0E, 0x0F, 0x10,
0x00, 0x01, // Major
0x00, 0x02, // Minor
0xC3 // RSSI
};

uDeviceHandle_t gDeviceHandle;
uDeviceCfg_t gDeviceCfg;

void main()
{
static uNetworkCfgBle_t networkCfg = {
.type = U_NETWORK_TYPE_BLE,
.role = U_BLE_CFG_ROLE_PERIPHERAL,
.spsServer = false,
};

// Remove the line below if you want the log printouts from ubxlib
uPortLogOff();
// Initiate ubxlib
uPortInit();
uDeviceInit();
// And the U-blox module
int32_t errorCode;
uDeviceGetDefaults(U_DEVICE_TYPE_SHORT_RANGE, &gDeviceCfg);
printf("\nInitiating the module...\n");
errorCode = uDeviceOpen(&gDeviceCfg, &gDeviceHandle);
if (errorCode == 0) {
printf("Starting BLE...\n");
errorCode = uNetworkInterfaceUp(gDeviceHandle, networkCfg.type, &networkCfg);
if (errorCode == 0) {
// Setup the iBeacon advertisement
uint8_t advData[32];
uBleGapAdvConfig_t advCfg = {
.minIntervalMs = 200,
.maxIntervalMs = 200,
.connectable = false,
.pRespData = "", // Want empty response advertisement
.respDataLength = 1, // Just the 0
.pAdvData = advData
};
advCfg.advDataLength = uBleGapSetAdvData(NULL,
ibeacon_data, sizeof(ibeacon_data),
advData, sizeof(advData));
errorCode = uBleGapAdvertiseStart(gDeviceHandle, &advCfg);
if (errorCode == 0) {
printf("Started iBeacon advertising\n");
char mac[U_SHORT_RANGE_BT_ADDRESS_SIZE];
uBleGapGetMac(gDeviceHandle, mac);
printf("Mac address is: %s\n", mac);
} else {
printf("* Failed to start advertising: %d\n", errorCode);
}
} else {
printf("* Failed to bring up the network: %d\n", errorCode);
}
} else {
printf("* Failed to initiate the module: %d\n", errorCode);
}
}
17 changes: 17 additions & 0 deletions examples/ble_ibeacon_z/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 u-blox
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.13.1)
include(../common.cmake)
project(ibeacon_z)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

cmake_minimum_required(VERSION 3.13.1)
include(../common.cmake)
project(scanner)
project(ble_nus)
13 changes: 13 additions & 0 deletions examples/ble_nus/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2022 u-blox
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# No extra configuration needed for this example
222 changes: 222 additions & 0 deletions examples/ble_nus/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* Copyright 2023 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
*
* A demo on how to set up a NUS (Nordic Uart Service) server or client
* using ubxlib.
*
* IMPORTANT! ubxlib version 1.2 or later is required.
*
*/

#include <stdio.h>
#include <string.h>

#include "ubxlib.h"

#include "leds.h"

#define SERVER_NAME "NUS-Demo-Server"
#define SOLID_LED RED_LED
#define BLINK_LED BLUE_LED

// This application can run as either server (peripheral) or client (central).
// Choose here which to run.
bool gActAsServer = true;

static char gPeerMac[U_SHORT_RANGE_BT_ADDRESS_SIZE] = {0};
static char gPeerResponse[100] = {0};

char gLocalMac[U_SHORT_RANGE_BT_ADDRESS_SIZE] = {0};

uDeviceHandle_t gDeviceHandle;
uDeviceCfg_t gDeviceCfg;

// Convert mac address without colon to one with
const char *colonMac(const char *ubxMac)
{
static char mac[U_SHORT_RANGE_BT_ADDRESS_SIZE + 5];
int pos = 0;
for (int i = 0; i < 12; i++) {
mac[pos++] = ubxMac[i];
if (i % 2) {
mac[pos++] = ':';
}
}
mac[pos - 1] = 0;
return mac;
}

// Callback for scan response.
static bool scanResponseCb(uBleScanResult_t *pScanResult)
{
if (strstr(pScanResult->name, SERVER_NAME)) {
strncpy(gPeerMac, pScanResult->address, sizeof(gPeerMac));
return false;
}
return true;
}

// Callback for incoming NUS messages from client or server.
static void peerIncomingCb(uint8_t *pValue, uint8_t valueSize)
{
uint8_t maxSize = sizeof(gPeerResponse) - 1;
if (valueSize > maxSize) {
valueSize = maxSize;
}
memcpy(gPeerResponse, pValue, valueSize);
gPeerResponse[valueSize] = 0;
}

// Run as a NUS server.
static void server()
{
// Set up proper advertisement data.
uint8_t respData[32];
uint8_t advData[32];
uBleGapAdvConfig_t advCfg = {
.minIntervalMs = 200,
.maxIntervalMs = 200,
.connectable = true,
.maxClients = 1,
.pRespData = respData,
.pAdvData = advData
};
advCfg.respDataLength = uBleNusSetAdvData(respData, sizeof(respData));
advCfg.advDataLength = uBleGapSetAdvData(SERVER_NAME,
NULL, 0,
advData, sizeof(advData));
if (advCfg.respDataLength > 0) {
printf("Server initiated.\n");
printf("Connect using e.g. the nRf toolbox app in a phone,\n");
printf(" using the \"Utils services UART\".\n");
printf("Server address is: %s (%s)\n", gLocalMac, colonMac(gLocalMac));
printf("Waiting for connections....\n");
uBleGapAdvertiseStart(gDeviceHandle, &advCfg);
uBleNusInit(gDeviceHandle, NULL, peerIncomingCb);
} else {
printf("* Failed to start NUS server");
return;
}
ledsInit();
bool ledIsOn = true;
bool ledBlinking = false;
ledSet(SOLID_LED, ledIsOn);
while (true) {
char response[20] = {0};
if (gPeerResponse[0] != 0) {
// Client sent a message, see if it is recognizable.
printf("Incoming command: %s\n", gPeerResponse);
if (strstr(gPeerResponse, "hello")) {
snprintf(response, sizeof(response), "Hello from server!");
} else if (strstr(gPeerResponse, "led")) {
if (ledBlinking) {
ledBlink(BLINK_LED, 0, 0);
ledBlinking = false;
}
ledIsOn = !ledIsOn;
ledSet(SOLID_LED, ledIsOn);
snprintf(response, sizeof(response), "Led is %s", ledIsOn ? "on" : "off");
} else if (strstr(gPeerResponse, "blink")) {
if (ledIsOn) {
ledIsOn = false;
ledSet(SOLID_LED, ledIsOn);
}
ledBlinking = !ledBlinking;
ledBlink(BLINK_LED, ledBlinking ? 250 : 0, 250);
snprintf(response, sizeof(response), "Led is %s", ledBlinking ? "blinking" : "off");
} else {
snprintf(response, sizeof(response), "Unknown command");
}
uBleNusWrite(response, strlen(response) + 1);
gPeerResponse[0] = 0;
} else {
uPortTaskBlock(1000);
}
}
}

// Run as NUS client.
static void client()
{
int32_t errorCode;
printf("NUS client started, scanning for servers...\n");
while (true) {
gPeerMac[0] = 0;
// Search for module running the as a server with this code.
errorCode = uBleGapScan(gDeviceHandle,
U_BLE_GAP_SCAN_DISCOVER_ALL_ONCE,
true, 2000,
scanResponseCb);
if (strlen(gPeerMac)) {
printf("Connecting to %s...\n", gPeerMac);
errorCode = uBleNusInit(gDeviceHandle, gPeerMac, peerIncomingCb);
if (errorCode == 0) {
uPortTaskBlock(2000);
const char *com = "blink";
uBleNusWrite(com, strlen(com) + 1);
uPortTaskBlock(1000);
if (gPeerResponse[0] != 0) {
printf("Server response: %s\n", gPeerResponse);
}
printf("Disconnecting\n");
uBleNusDeInit();
uPortTaskBlock(10000);
} else {
printf("* Connect failed: %d\n", errorCode);
}
}
}
}

void main()
{
static uNetworkCfgBle_t networkCfg = {
.type = U_NETWORK_TYPE_BLE,
.spsServer = false,
};

// Remove the line below if you want the log printouts from ubxlib
uPortLogOff();
// Initiate ubxlib
uPortInit();
uDeviceInit();
// And the U-blox module
int32_t errorCode;
uDeviceGetDefaults(U_DEVICE_TYPE_SHORT_RANGE, &gDeviceCfg);
printf("\nInitiating the module...\n");
errorCode = uDeviceOpen(&gDeviceCfg, &gDeviceHandle);
if (errorCode == 0) {
printf("Starting BLE...\n");
networkCfg.role = gActAsServer ? U_BLE_CFG_ROLE_PERIPHERAL : U_BLE_CFG_ROLE_CENTRAL;
errorCode = uNetworkInterfaceUp(gDeviceHandle, networkCfg.type, &networkCfg);
if (errorCode == 0) {
if (uBleGapGetMac(gDeviceHandle, gLocalMac) > 0) {
printf("Local mac address: %s\n", gLocalMac);
}
if (gActAsServer) {
server();
} else {
client();
}
} else {
printf("* Failed to bring up the network: %d\n", errorCode);
}
} else {
printf("* Failed to initiate the module: %d\n", errorCode);
}
}
Loading

0 comments on commit 965453c

Please sign in to comment.