diff --git a/CMakeLists.txt b/CMakeLists.txt index cb87605..7643443 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,15 +12,16 @@ project(openthread_cli) # NORDIC SDK APP START target_sources(app PRIVATE src/main.c src/utils.c - src/mqttsn.c - src/gpio.c + # src/mqttsn.c + # src/gpio.c src/openthread_client.c - src/lorawan_client.c - src/gpsparser.c - src/minmea.c - src/nvs.c + # src/lorawan_client.c + # src/gpsparser.c + # src/minmea.c + # src/nvs.c src/app_bluetooth.c - src/bluetooth/lns_client.c) + src/bluetooth/lns_client.c + ) # NORDIC SDK APP END target_sources_ifdef(CONFIG_CLI_SAMPLE_LOW_POWER app PRIVATE src/low_power.c) diff --git a/Kconfig b/Kconfig index e0403d1..f80acae 100644 --- a/Kconfig +++ b/Kconfig @@ -82,5 +82,5 @@ source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" # Configure LoRaWAN Client module = LORAWAN_CLIENT -module-str = gps-parser +module-str = lorawan-client source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" diff --git a/boards/nrf5340tag_nrf5340_cpuapp.overlay b/boards/nrf5340tag_nrf5340_cpuapp.overlay index 0ccbf78..c14e830 100644 --- a/boards/nrf5340tag_nrf5340_cpuapp.overlay +++ b/boards/nrf5340tag_nrf5340_cpuapp.overlay @@ -66,6 +66,15 @@ ; }; }; + + spi2_default: spi2_default { + group1 { + psels = , + , + ; + }; + }; + }; &uart0 { @@ -96,8 +105,27 @@ spi-max-frequency = <125000>; }; }; + &gpio_fwd { uart { gpios = <&gpio0 11 0>; }; +}; + +&spi2 { + cs-gpios = <&gpio1 9 0>; + status = "okay"; + pinctrl-0 = <&spi2_default>; + pinctrl-names = "default"; +}; + +/ { + nrf21540-fem { + compatible = "nordic,nrf21540-fem"; + tx-en-gpios = <&gpio1 13 GPIO_ACTIVE_HIGH>; + rx-en-gpios = <&gpio1 14 GPIO_ACTIVE_HIGH>; + pdn-gpios = <&gpio1 12 GPIO_ACTIVE_HIGH>; + mode-gpios = <&gpio1 15 GPIO_ACTIVE_HIGH>; + spi-if = <&spi2>; + }; }; \ No newline at end of file diff --git a/prj.conf b/prj.conf index f10fc16..3d8b6cd 100644 --- a/prj.conf +++ b/prj.conf @@ -55,7 +55,7 @@ CONFIG_MQTT_SNCLIENT_TOPIC_PREFIX="ot" CONFIG_BT_DEVICE_NAME="nRF528xx" CONFIG_BT=y -#CONFIG_BT_DEBUG_LOG=y +CONFIG_BT_DEBUG_LOG=y CONFIG_BT_CENTRAL=y CONFIG_BT_SMP=y CONFIG_BT_GATT_CLIENT=y @@ -84,7 +84,7 @@ CONFIG_MAIN_STACK_SIZE=4096 # Drivers # Support temperature sensor -CONFIG_NRFX_TEMP=y +# CONFIG_NRFX_TEMP=y # TODO: Support USB update without booting into DFU mode #CONFIG_STREAM_FLASH=y @@ -104,11 +104,11 @@ CONFIG_LOG=y # of seeing errors... CONFIG_LOG_PROCESS_THREAD=y CONFIG_LOG_MODE_DEFERRED=n -#CONFIG_LOG_MAX_LEVEL=4 -##CONFIG_OPENTHREAD_DEBUG=y -##CONFIG_OPENTHREAD_L2_DEBUG=y -#CONFIG_OPENTHREAD_LOG_LEVEL_WARN=y -##CONFIG_OPENTHREAD_LOG_LEVEL_DEBG=y +CONFIG_LOG_MAX_LEVEL=4 +CONFIG_OPENTHREAD_DEBUG=y +CONFIG_OPENTHREAD_L2_DEBUG=y +CONFIG_OPENTHREAD_LOG_LEVEL_WARN=y +CONFIG_OPENTHREAD_LOG_LEVEL_DEBG=y # Logging modules CONFIG_OT_COMMAND_LINE_INTERFACE_LOG_LEVEL_INF=y @@ -173,18 +173,23 @@ CONFIG_BT_HCI_ACL_FLOW_CONTROL=y #CONFIG_NET_BUF_POOL_USAGE=y # LORA -CONFIG_SPI=y -CONFIG_LORA=y -CONFIG_LORA_SHELL=y -CONFIG_LORAWAN=y -CONFIG_LORA_SX126X=y -CONFIG_LORAMAC_REGION_EU868=y -#CONFIG_LORA_LOG_LEVEL_DBG=y -#CONFIG_LORAWAN_LOG_LEVEL_DBG=y -CONFIG_MPU_ALLOW_FLASH_WRITE=y +# CONFIG_SPI=y +# CONFIG_LORA=y +# CONFIG_LORA_SHELL=y +# CONFIG_LORAWAN=y +# CONFIG_LORA_SX126X=y +# CONFIG_LORAMAC_REGION_EU868=y +# #CONFIG_LORA_LOG_LEVEL_DBG=y +# #CONFIG_LORAWAN_LOG_LEVEL_DBG=y +# CONFIG_MPU_ALLOW_FLASH_WRITE=y # GPS -CONFIG_GPS_PARSER_LOG_LEVEL_DBG=y +# CONFIG_GPS_PARSER_LOG_LEVEL_DBG=y # LoRaWAN Client -CONFIG_LORAWAN_CLIENT_LOG_LEVEL_DBG=y +# CONFIG_LORAWAN_CLIENT_LOG_LEVEL_DBG=y + +# FEM +CONFIG_MPSL=y +CONFIG_MPSL_FEM=y +CONFIG_MPSL_FEM_NRF21540_GPIO_SPI=y \ No newline at end of file diff --git a/src/main.c b/src/main.c index c753bf4..910c45a 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ #include #include -#include +// #include #include #include "openthread/instance.h" @@ -23,7 +23,7 @@ #include "utils.h" #include "mqttsn.h" #include "app_bluetooth.h" -#include "gpio.h" +// #include "gpio.h" #if defined(CONFIG_CLI_SAMPLE_LOW_POWER) #include "low_power.h" @@ -76,6 +76,7 @@ int main(int aArgc, char *aArgv[]) ret); continue; } + LOG_INF("*** Hello Mesh from while!!! ***"); k_msleep(100); } #endif @@ -93,7 +94,9 @@ int main(int aArgc, char *aArgv[]) // appbluetoothInit(); // Start MQTT-SN client - mqttsnInit(); + LOG_INF("*** before mqttsnInit ***"); + // mqttsnInit(); + LOG_INF("*** after mqttsnInit ***"); return 0; } diff --git a/src/mqttsn.c b/src/mqttsn.c index 4909afc..5904bc9 100644 --- a/src/mqttsn.c +++ b/src/mqttsn.c @@ -9,11 +9,11 @@ #include "openthread/mqttsn.h" #include "openthread/link.h" -#include -#include +// #include +// #include #include -#include "gpio.h" +// #include "gpio.h" #include "app_bluetooth.h" #include "bluetooth/lns_client.h" @@ -59,6 +59,8 @@ LOG_MODULE_REGISTER(mqttsn, CONFIG_MQTT_SNCLIENT_LOG_LEVEL); static void mqttsnSubscribedHandler(otMqttsnReturnCode aCode, const otMqttsnTopic* aTopic, otMqttsnQos aQos, void* aContext) { + LOG_DBG("*** in mqttsnSubscribedHandler ***"); + // Handle registered if (aCode == kCodeAccepted) { @@ -74,20 +76,28 @@ static void mqttsnSubscribedHandler(otMqttsnReturnCode aCode, const otMqttsnTopi else LOG_WRN("Subscribed Error Name %s", aTopic->mData.mTopicName); } + + LOG_DBG("*** out mqttsnSubscribedHandler ***"); } static void mqttsnHandlePublished(otMqttsnReturnCode aCode, void* aContext) { + LOG_DBG("*** in mqttsnHandlePublished ***"); + OT_UNUSED_VARIABLE(aCode); OT_UNUSED_VARIABLE(aContext); // Handle published LOG_INF("Published"); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); + + LOG_DBG("*** out mqttsnHandlePublished ***"); } static void mqttsnHandleRegistered(otMqttsnReturnCode aCode, const otMqttsnTopic* aTopic, void* aContext) { + LOG_DBG("*** in mqttsnHandleRegistered ***"); + // Handle registered - TODO: Fix support for short topic name if (aCode == kCodeAccepted) { @@ -119,7 +129,7 @@ static void mqttsnHandleRegistered(otMqttsnReturnCode aCode, const otMqttsnTopic // We've done registering the Publication topic now Subscribe to the subscriptitopic - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); memcpy(&_aTopicPub, aTopic, sizeof(otMqttsnTopic)); // Build topic @@ -131,29 +141,33 @@ static void mqttsnHandleRegistered(otMqttsnReturnCode aCode, const otMqttsnTopic aTopicSub.mData.mTopicName = data; LOG_DBG("Subscribing to topic: %s", data); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); _eMQTTSNClientState = STATE_SUBSCRIBING; otMqttsnSubscribe(instance, &aTopicSub, kQos0, mqttsnSubscribedHandler, (void *)instance); break; } + + LOG_DBG("*** out mqttsnHandleRegistered ***"); } static void mqttsnHandleConnected(otMqttsnReturnCode aCode, void* aContext) { + LOG_DBG("*** in mqttsnHandleConnected ***"); + // Handle connected otInstance *instance = (otInstance *)aContext; if (aCode == kCodeAccepted) { LOG_DBG("HandleConnected - Accepted"); - otLedToggle(LED_YELLOW);; + //otLedToggle(LED_YELLOW);; // Build topic char data[128]; sprintf(data, "%s/%s", TOPIC_PREFIX, _eui64); LOG_DBG("Registering Topic: %s", data); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); // Obtain target topic ID _eMQTTSNClientState = STATE_REGISTERING_PUB_TOPIC; @@ -179,10 +193,14 @@ static void mqttsnHandleConnected(otMqttsnReturnCode aCode, void* aContext) break; } } + + LOG_DBG("*** out mqttsnHandleConnected ***"); } static otMqttsnReturnCode mqttsnHandlePublishReceived(const uint8_t* aPayload, int32_t aPayloadLength, const otMqttsnTopic* aTopic, void* aContext) { + LOG_DBG("*** in mqttsnHandlePublishReceived ***"); + char buffer[128]; memset(buffer, 0, sizeof(buffer)); @@ -204,19 +222,24 @@ static otMqttsnReturnCode mqttsnHandlePublishReceived(const uint8_t* aPayload, i int cycles = 20; while(cycles--) { - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); k_sleep(K_MSEC(250)); } } + + LOG_DBG("*** out mqttsnHandlePublishReceived ***"); + return kCodeAccepted; } static void mqttsnHandleSearchGw(const otIp6Address* aAddress, uint8_t aGatewayId, void* aContext) { + LOG_DBG("*** in mqttsnHandleSearchGw ***"); + OT_UNUSED_VARIABLE(aGatewayId); LOG_DBG("Got search gateway response"); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); // Handle SEARCHGW response received // Connect to received address @@ -247,25 +270,36 @@ static void mqttsnHandleSearchGw(const otIp6Address* aAddress, uint8_t aGatewayI // Connect to the MQTT broker (gateway) otMqttsnConnect(instance, &config); + + LOG_DBG("*** out mqttsnHandleSearchGw ***"); } void mqttsnSearchGateway(otInstance *instance) { + LOG_DBG("*** in mqttsnSearchGateway ***"); + otIp6Address address; otIp6AddressFromString(GATEWAY_MULTICAST_ADDRESS, &address); LOG_DBG("Searching for gateway on %s", GATEWAY_MULTICAST_ADDRESS); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); + + LOG_DBG("*** calling in mqttsnHandleSearchGw ***"); otMqttsnSetSearchgwHandler(instance, mqttsnHandleSearchGw, (void *)instance); // Send SEARCHGW multicast message + LOG_DBG("*** Send SEARCHGW multicast ***"); otMqttsnSearchGateway(instance, &address, GATEWAY_MULTICAST_PORT, GATEWAY_MULTICAST_RADIUS); + + LOG_DBG("*** out mqttsnSearchGateway ***"); } void mqttsnPublishWorkHandler(struct k_work *work) { + LOG_DBG("*** in mqttsnPublishWorkHandler ***"); + LOG_DBG("Publish Handler %d", _stateCount); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); otInstance *instance = openthread_get_default_instance(); otMqttsnClientState state = otMqttsnGetState(instance); @@ -299,7 +333,7 @@ void mqttsnPublishWorkHandler(struct k_work *work) static int count = 0; LOG_DBG("Client state %d", otMqttsnGetState(instance)); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); // Get RLOC16 uint16_t uRLOC16 = otLinkGetShortAddress(instance); @@ -333,7 +367,7 @@ void mqttsnPublishWorkHandler(struct k_work *work) // Publish message to the registered topic LOG_INF("Publishing..."); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); const char* strdata = "{\"ID\":\"%s\", \"RLOC16\":\"%04X\", \"Version\":\"%d\", \"Count\":%d, \"Role\":\"%s\", \"Status\":\"%s\", \"Battery\":%d, \"GPSLock\": %d, \"Latitude\":%d, \"Longitude\":%d, \"Elevation\":%d, \"Temperature\":%d.%02u }"; char data[256]; @@ -357,27 +391,34 @@ void mqttsnPublishWorkHandler(struct k_work *work) mqttsnHandlePublished, NULL); LOG_DBG("Publishing %d bytes rsp %d", length, err); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); } // Restart timer k_timer_start(&mqttsnPublishTimer, K_SECONDS(10), K_NO_WAIT); + LOG_DBG("*** out mqttsnPublishWorkHandler ***"); } K_WORK_DEFINE(mqttsnPublishWork, mqttsnPublishWorkHandler); void mqttsnPublishHandler(struct k_timer *dummy) { + LOG_DBG("*** in mqttsnPublishHandler ***"); + k_work_submit(&mqttsnPublishWork); + + LOG_DBG("*** out mqttsnPublishHandler ***"); } otError mqttsnInit() { + LOG_DBG("*** in mqttsnInit ***"); + otInstance *instance = openthread_get_default_instance(); // Start MQTT-SN client LOG_INF("Starting MQTT-SN on port %d", CLIENT_PORT); - otLedToggle(LED_YELLOW); + //otLedToggle(LED_YELLOW); // Store EUI64 otExtAddress extAddress; @@ -399,5 +440,7 @@ otError mqttsnInit() if(error == OT_ERROR_NONE) k_timer_start(&mqttsnPublishTimer, K_SECONDS(10), K_NO_WAIT); + LOG_DBG("*** out mqttsnInit ***"); + return error; -} \ No newline at end of file +} diff --git a/src/openthread_client.c b/src/openthread_client.c index 5730028..7c8bc93 100644 --- a/src/openthread_client.c +++ b/src/openthread_client.c @@ -4,7 +4,7 @@ #include #include -#include +// #include #include #include "openthread/instance.h" @@ -17,7 +17,7 @@ #include "utils.h" #include "mqttsn.h" #include "app_bluetooth.h" -#include "gpio.h" +// #include "gpio.h" #if defined(CONFIG_CLI_SAMPLE_LOW_POWER) #include "low_power.h" @@ -33,6 +33,8 @@ LOG_MODULE_REGISTER(openthread_client, CONFIG_OT_COMMAND_LINE_INTERFACE_LOG_LEVE static void otStateChanged(otChangedFlags aFlags, void *aContext) { + LOG_INF("*** in otStateChanged ***"); + otInstance *instance = (otInstance *)aContext; // when thread role changed @@ -40,7 +42,7 @@ static void otStateChanged(otChangedFlags aFlags, void *aContext) { otDeviceRole role = otThreadGetDeviceRole(instance); - otLedRoleIndicator(role); + //otLedRoleIndicator(role); switch(role) { case 0: // OT_DEVICE_ROLE_DISABLED: @@ -63,7 +65,9 @@ static void otStateChanged(otChangedFlags aFlags, void *aContext) // If role changed to any of active roles then send SEARCHGW message if (role == OT_DEVICE_ROLE_CHILD || role == OT_DEVICE_ROLE_ROUTER || role == OT_DEVICE_ROLE_LEADER) { - mqttsnSearchGateway(instance); + LOG_INF("*** before calling mqttsnSearchGateway ***"); + // mqttsnSearchGateway(instance); + LOG_INF("*** after calling mqttsnSearchGateway ***"); } } else @@ -166,10 +170,14 @@ static void otStateChanged(otChangedFlags aFlags, void *aContext) break; } } + + LOG_INF("*** out otStateChanged ***"); } void openthread_client_thread(void) { + LOG_INF("*** in openthread_client_thread ***"); + #if defined(CONFIG_CLI_SAMPLE_LOW_POWER) low_power_enable(); #endif @@ -216,7 +224,7 @@ void openthread_client_thread(void) #endif // Start LED - otLedInit(); + //otLedInit(); // Register notifier callback to receive thread role changed events error = otSetStateChangedCallback(instance, otStateChanged, instance); @@ -233,7 +241,9 @@ void openthread_client_thread(void) int8_t txpower; error = otPlatRadioGetTransmitPower(instance, &txpower); LOG_INF("Tx Power is %d dB", txpower); + + LOG_INF("*** out openthread_client_thread ***"); } K_THREAD_DEFINE(openthread_client_id, 2048, openthread_client_thread, NULL, NULL, NULL, - 7, 0, 0); \ No newline at end of file + 7, 0, 0);