This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Thingy:91 + Azure IoT Hub


Hi,

I am trying to use the thingy91 to publish some sensor data to an Azure IoT Hub, I've tried to look at mqtt_simple and modified "asset_tracker" application but I couldn't succeed.

so is there any example showing how to publish data to Azure IoT Hub (MQTT broker) using thingy91?

Thank you in advance...

  • Hello, 

    Can you provide some more information on what is failing? Are you able to connect the server? Do you get any errors?

    We have an unofficial example project called Bifravst which will connect the Thingy:91 to Azure. This is however work in progress, and I urge to read through what is documented.

    Kind regards,
    Øyvind

  • You may also want to look at this github PR which will be part of NCS 1.4.0, when that is released.
    -Øyvind

  • Note that we do not have Azure support in the Bifravst firmware, yet: github.com/.../43

  • Hi,
    sorry for late replay, I was reading and learning how to use Zephyr API and kernel services to communicate with peripherals.
    I've modified the simple MQTT sample to publish the temperature, the humidity of the sensor to Azure IoT Hub, I am using j-link to flash Thingy91 and VScode with cmd terminal to compile and flash my code.

    I've used  "MQTT_TRANSPORT_NON_SECURE" and I tested the compiled code on two devices  "Thingy91", I am facing two issues:

    1-I need to run #west flash twice to flash the build successfully.

    2-The first device "Thingy91" works properly and send data to Azure when pressing the button , while the other one showing an error  "ERROR: mqtt_connect -95" , I've tried to update the FW of modem and debug this issue by AT commands, but still didn't figure out how to solve it. I've attached a log file for this issue. 

     

    #     prj_thingy91_nrf9160ns.conf
    # Copyright (c) 2019 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    # General config
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_ASSERT=y
    CONFIG_REBOOT=y
    CONFIG_LOG=y
    CONFIG_LOG_IMMEDIATE=y
    CONFIG_LEGACY_TIMEOUT_API=y
    
    
    # Network
    CONFIG_NETWORKING=y
    CONFIG_NET_NATIVE=n
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    
    # LTE link control
    #CONFIG_POWER_OPTIMIZATION_ENABLE=n
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_NETWORK_MODE_LTE_M_GPS=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    CONFIG_LTE_LINK_CONTROL_LOG_LEVEL_DBG=y
    
    CONFIG_LTE_NETWORK_MODE_NBIOT=n
    CONFIG_LTE_LEGACY_PCO_MODE=n
    CONFIG_LTE_PSM_REQ_RPTAU="00000110"
    CONFIG_LTE_PSM_REQ_RAT="00000010"
    
    CONFIG_LTE_LOCK_BANDS=y
    # Enable band 2, 3, 4, 8, 12, 13 ,20 and 28
    CONFIG_LTE_LOCK_BAND_MASK="1000000010000001100010001110"
    
    # AT Host
    CONFIG_UART_INTERRUPT_DRIVEN=y 
    CONFIG_AT_HOST_LIBRARY=y  
    
    
    # Modem info
    CONFIG_MODEM_INFO=y
    
    # BSD library
    CONFIG_BSD_LIBRARY=y
    CONFIG_BSD_LIBRARY_TRACE_ENABLED=n
    
    # nRF Cloud
    CONFIG_CLOUD_API=y
    CONFIG_NRF_CLOUD=y
    CONFIG_NRF_CLOUD_LOG_LEVEL_DBG=y
    CONFIG_NRF_CLOUD_AGPS=y
    CONFIG_NRF_CLOUD_CONNECTION_POLL_THREAD=y
    CONFIG_NRF_CLOUD_NONBLOCKING_SEND=y
    # Needed for the cloud codec
    CONFIG_CJSON_LIB=y
    # Shorter to prevent NAT timeouts
    CONFIG_MQTT_KEEPALIVE=120
    # Don't resubscribe to topics if broker remembers them
    CONFIG_CLOUD_PERSISTENT_SESSIONS=y
    
    # Sensors
    CONFIG_SENSOR=y
    #CONFIG_ACCEL_USE_EXTERNAL=y
    #CONFIG_ACCEL_DEV_NAME="ADXL362"
    CONFIG_ADXL362=y
    CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD=y
    CONFIG_ADXL362_ABS_REF_MODE=1
    CONFIG_ADXL362_ACTIVITY_THRESHOLD=200
    CONFIG_ADXL362_INACTIVITY_THRESHOLD=200
    CONFIG_ADXL362_INTERRUPT_MODE=1
    CONFIG_TEMP_USE_EXTERNAL=y
    CONFIG_TEMP_DEV_NAME="BME680"
    CONFIG_ENVIRONMENT_DATA_SEND_INTERVAL=10
    
    #CONFIG_SPI_NRFX_SPIM_MISO_NO_PULL=y
    # Deselect CONFIG_BME680 if CONFIG_USE_BME680_BSEC is selected
    CONFIG_BME680=y
    CONFIG_USE_BME680_BSEC=n
    
    # GPS
    CONFIG_NRF9160_GPS_LOG_LEVEL_DBG=y
    CONFIG_NRF9160_GPS=y
    CONFIG_NRF9160_GPS_LOG_LEVEL_DBG=y
    #CONFIG_GPS_USE_EXTERNAL=y
    #CONFIG_GPS_DEV_NAME="NRF9160_GPS"
    #CONFIG_GPS_CONTROL_PSM_ENABLE_ON_START=y
    
    #CONFIG_GPS_CONTROL_FIRST_FIX_CHECK_DELAY=10
    #CONFIG_GPS_CONTROL_FIX_CHECK_INTERVAL=30
    #CONFIG_GPS_CONTROL_FIX_TRY_TIME=360
    #CONFIG_GPS_CONTROL_PSM_DISABLE_ON_STOP=n
    #CONFIG_GPS_START_ON_MOTION=y
    
    # Library for buttons and LEDs
    CONFIG_DK_LIBRARY=y
    CONFIG_DK_LIBRARY_INVERT_LEDS=n
    
    # Console
    CONFIG_CONSOLE_SUBSYS=y
    CONFIG_CONSOLE_HANDLER=y
    CONFIG_CONSOLE_GETCHAR=y
    
    # Heap and stacks
    CONFIG_HEAP_MEM_POOL_SIZE=16384
    CONFIG_MAIN_STACK_SIZE=8192
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    CONFIG_HW_STACK_PROTECTION=y
    
    # Settings
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_FCB=y
    CONFIG_SETTINGS=y
    CONFIG_SETTINGS_FCB=y
    CONFIG_MPU_ALLOW_FLASH_WRITE=y
    
    # MCUBOOT
    CONFIG_BOOTLOADER_MCUBOOT=y
    CONFIG_IMG_MANAGER=y
    CONFIG_MCUBOOT_IMG_MANAGER=y
    CONFIG_IMG_ERASE_PROGRESSIVELY=y
    
    # AWS FOTA
    CONFIG_AWS_FOTA=y
    CONFIG_FOTA_DOWNLOAD=y
    CONFIG_AWS_FOTA_LOG_LEVEL_DBG=y
    CONFIG_AWS_JOBS_LOG_LEVEL_DBG=y
    CONFIG_DFU_TARGET=y
    
    # Download client (needed by AWS FOTA)
    CONFIG_DOWNLOAD_CLIENT=y
    CONFIG_DOWNLOAD_CLIENT_STACK_SIZE=4096
    
    # Fatal error
    CONFIG_RESET_ON_FATAL_ERROR=n
    CONFIG_WATCHDOG=y
    
    # Appliaction #all added for azure
    CONFIG_MQTT_PUB_TOPIC="devices/Thingy91/messages/events/$.ct=application%2Fjson&$.ce=utf-8"
    CONFIG_MQTT_SUB_TOPIC="/my/subscribe/topic"
    CONFIG_MQTT_CLIENT_ID="Thingy91"
    CONFIG_MQTT_BROKER_HOSTNAME="Nordic.azure-devices.net"
    CONFIG_MQTT_BROKER_PORT=8883
        
    CONFIG_MQTT_MESSAGE_BUFFER_SIZE=512
    CONFIG_MQTT_PAYLOAD_BUFFER_SIZE=512
    # MQTT
    CONFIG_MQTT_LIB=y  
    CONFIG_MQTT_LIB_TLS=n
    #CONFIG_AT_CMD_LOG_LEVEL_DBG=y
    #CONFIG_SEC_TAG=4096  
    #CONFIG_PEER_VERIFY=0  
    
    #CONFIG_HEAP_MEM_POOL_SIZE=2048
    #   Kconfig
    # Copyright (c) 2018 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    
    menu "MQTT simple sample"
    config MQTT_PUB_TOPIC
    	string "MQTT publish topic"
    	default "devices/Thingy91/messages/events/"
    
    config MQTT_SUB_TOPIC
    	string "MQTT subscribe topic"
    	default "my/subscribe/topic"
    
    config MQTT_CLIENT_ID
    	string "MQTT Client ID"
    	default "Thingy91"
    
    config MQTT_BROKER_HOSTNAME
    	string "MQTT broker hostname"
    	default "Nordic.azure-devices.net"
    
    config MQTT_BROKER_PORT
    	int "MQTT broker port"
    	default 8883
    
    config MQTT_MESSAGE_BUFFER_SIZE
    	int "MQTT message buffer size"
    	default 512
    
    config MQTT_PAYLOAD_BUFFER_SIZE
    	int "MQTT payload buffer size"
    	default 512
    
    endmenu
    
    menu "Environment sensors"
    
    config USE_BME680_BSEC
    	bool "Use Bosch BME680 BSEC lib"
    	default n
    	help
    	  Enable use of Bosch BSEC library. The library itself is not a part of
    	  NCS and must be downloaded from Bosch Sensortec.
    
    config BOSCH_BSEC_LIBRARY_PATH
    	string "Path to Bosch BSEC library folder"
    	default "$(ZEPHYR_BASE)/../nrf/ext/BSEC_1.4.7.4_Generic_Release"
    	help
    	  Path to the folder where the Bosch BSEC library is placed.
    
    config ENVIRONMENT_DATA_SEND_INTERVAL
    	int "Interval in seconds for sending environment data"
    	default 30
    	help
    	  Interval between each sampling and sending of environment sensor data.
    	  If the GPS is active, sensor data will not be sent before it is
    	  stopped. The unit is seconds.
    
    config ENVIRONMENT_DATA_BACKOFF_TIME
    	int "Time to delay sending environment data if GPS active"
    	default 20
    	help
    	  Time in seconds before retrying to send environment sensor data if
    	  the GPS is active.
    
    choice
    	prompt "Temperature measurement device"
    	default TEMP_USE_SIM
    	help
    	  Select which device will provide temperature data.
    
    config TEMP_USE_SIM
    	bool "Use simulated temperature data"
    	select SENSOR
    	select SENSOR_SIM
    	help
    	  Use sensor simulator to generate temperature data to send to
    	  nRF Cloud.
    
    config TEMP_USE_EXTERNAL
    	bool "User provided temperature device"
    	help
    	  The user provides a temperature device that is
    	  used for temperature measurement.
    
    endchoice
    
    if TEMP_USE_SIM
    config TEMP_DEV_NAME
    	string
    	default SENSOR_SIM_DEV_NAME
    
    config TEMP_TRIGGER
    	bool
    	default SENSOR_SIM_TRIGGER
    
    config TEMP_SIM_BUTTON
    	int "Button or switch to trigger 'data ready' event"
    	range 1 4 if BOARD_NRF9160DK_NRF9160NS
    	range 1 1 if BOARD_THINGY91_NRF9160NS
    	default 3 if BOARD_NRF9160DK_NRF9160NS
    	default 1 if BOARD_THINGY91_NRF9160NS
    	help
    	  Button or switch number to use for triggering
    	  a 'data ready' event.
    	  1 - Button 1
    	  2 - Button 2
    	  3 - Switch 1
    	  4 - Switch 2
    endif
    
    if TEMP_USE_EXTERNAL
    
    config TEMP_DEV_NAME
    	string "Temperature device name"
    	help
    	  Device from which temperature data will be fetched and
    	  sent to nRF Cloud.
    
    endif
    
    endmenu # Environment sensors
    
    menu "User Interface"
    	visible if USE_UI_MODULE
    
    config UI_LED_USE_PWM
    	bool "Use PWM to control LEDs"
    	default y if BOARD_THINGY91_NRF9160NS
    	select PWM if BOARD_THINGY91_NRF9160NS
    	select PWM_0 if BOARD_THINGY91_NRF9160NS
    
    if UI_LED_USE_PWM
    
    config UI_LED_PWM_DEV_NAME
    	string "PWM device name for RGB LED"
    	default "PWM_0" if BOARD_THINGY91_NRF9160NS
    
    config UI_LED_RED_PIN
    	int "Red LED pin number"
    	default 29 if BOARD_THINGY91_NRF9160NS
    
    config UI_LED_GREEN_PIN
    	int "Green LED pin number"
    	default 30 if BOARD_THINGY91_NRF9160NS
    
    config UI_LED_BLUE_PIN
    	int "Blue LED pin number"
    	default 31 if BOARD_THINGY91_NRF9160NS
    
    endif # UI_LED_USE_PWM
    
    config UI_BUZZER
    	bool "Enable buzzer"
    	default y if BOARD_THINGY91_NRF9160NS
    	select PWM if BOARD_THINGY91_NRF9160NS
    	select PWM_1 if BOARD_THINGY91_NRF9160NS
    
    if UI_BUZZER
    
    config UI_BUZZER_PWM_DEV_NAME
    	string "PWM device name for buzzer"
    	default "PWM_1" if BOARD_THINGY91_NRF9160NS
    
    config UI_BUZZER_PIN
    	int "Buzzer pin number"
    	default 28 if BOARD_THINGY91_NRF9160NS
    
    config UI_BUZZER_MIN_FREQUENCY
    	int "Minimum buzzer frequency"
    	default 100
    
    config UI_BUZZER_MAX_FREQUENCY
    	int "Maximum buzzer frequency"
    	default 10000
    
    endif # UI_BUZZER
    
    config UI_NMOS
    	bool "Enable NMOS control"
    	default y if BOARD_THINGY91_NRF9160NS
    	select PWM if BOARD_THINGY91_NRF9160NS
    	select PWM_2 if BOARD_THINGY91_NRF9160NS
    
    if UI_NMOS
    
    config UI_NMOS_PWM_DEV_NAME
    	string "PWM device name for buzzer"
    	default "PWM_2" if BOARD_THINGY91_NRF9160NS
    
    config UI_NMOS_1_PIN
    	int "NMOS 1 pin"
    	default 13 if BOARD_THINGY91_NRF9160NS
    
    config UI_NMOS_2_PIN
    	int "NMOS 2 pin"
    	default 14 if BOARD_THINGY91_NRF9160NS
    
    config UI_NMOS_3_PIN
    	int "NMOS 3 pin"
    	default 15 if BOARD_THINGY91_NRF9160NS
    
    config UI_NMOS_4_PIN
    	int "NMOS 4 pin"
    	default 16 if BOARD_THINGY91_NRF9160NS
    
    endif # UI_NMOS
    
    endmenu
    
    menu "Zephyr Kernel"
    source "$ZEPHYR_BASE/Kconfig.zephyr"
    endmenu
    
    2020-10-05T22:33:38.313Z INFO Application data folder: C:\Users\tade\AppData\Roaming\nrfconnect\pc-nrfconnect-linkmonitor
    2020-10-05T22:33:38.358Z DEBUG App pc-nrfconnect-linkmonitor v1.1.6 official
    2020-10-05T22:33:38.358Z DEBUG App path: C:\Users\tade\.nrfconnect-apps\node_modules\pc-nrfconnect-linkmonitor
    2020-10-05T22:33:38.358Z DEBUG nRFConnect 3.5.0 is supported by the app (^3.2.0)
    2020-10-05T22:33:38.358Z DEBUG nRFConnect path: C:\Users\tade\AppData\Local\Programs\nrfconnect\resources\app.asar
    2020-10-05T22:33:38.358Z DEBUG HomeDir: C:\Users\tade
    2020-10-05T22:33:38.358Z DEBUG TmpDir: C:\Users\tade\AppData\Local\Temp
    2020-10-05T22:33:41.043Z INFO Modem port is opened
    2020-10-05T22:33:41.061Z DEBUG modem >> AT+CFUN?
    2020-10-05T22:33:41.104Z DEBUG modem << +CFUN: 1
    2020-10-05T22:33:41.108Z DEBUG modem << 
    2020-10-05T22:33:41.109Z DEBUG modem << OK
    2020-10-05T22:33:41.126Z DEBUG modem >> AT+CGSN=1
    2020-10-05T22:33:41.142Z DEBUG modem << +CGSN: "352656100440539"
    2020-10-05T22:33:41.146Z DEBUG modem << OK
    2020-10-05T22:33:41.163Z DEBUG modem >> AT+CGMI
    2020-10-05T22:33:41.179Z DEBUG modem << Nordic Semiconductor ASA
    2020-10-05T22:33:41.188Z DEBUG modem << 
    2020-10-05T22:33:41.194Z DEBUG modem << OK
    2020-10-05T22:33:41.205Z DEBUG modem >> AT+CGMM
    2020-10-05T22:33:41.221Z DEBUG modem << nRF9160-SICA
    2020-10-05T22:33:41.230Z DEBUG modem << OK
    2020-10-05T22:33:41.235Z DEBUG modem >> AT+CGMR
    2020-10-05T22:33:41.256Z DEBUG modem << mfw_nrf9160_1.2.1
    2020-10-05T22:33:41.260Z DEBUG modem << 
    2020-10-05T22:33:41.262Z DEBUG modem << OK
    2020-10-05T22:33:41.265Z INFO Nordic Semiconductor ASA nRF9160-SICA [mfw_nrf9160_1.2.1] SerNr: 352656100440539
    2020-10-05T22:33:41.278Z DEBUG modem >> AT+CEMODE?
    2020-10-05T22:33:41.295Z DEBUG modem << +CEMODE: 2
    2020-10-05T22:33:41.310Z DEBUG modem << OK
    2020-10-05T22:33:41.339Z DEBUG modem >> AT%XCBAND=?
    2020-10-05T22:33:41.358Z DEBUG modem << %XCBAND: (2,3,4,8,12,13,20,28)
    2020-10-05T22:33:41.360Z DEBUG modem << 
    2020-10-05T22:33:41.361Z DEBUG modem << OK
    2020-10-05T22:33:41.378Z DEBUG modem >> AT+CMEE?
    2020-10-05T22:33:41.392Z DEBUG modem << +CMEE: 0
    2020-10-05T22:33:41.413Z DEBUG modem << OK
    2020-10-05T22:33:41.418Z DEBUG modem >> AT+CMEE=1
    2020-10-05T22:33:41.434Z DEBUG modem << OK
    2020-10-05T22:33:41.443Z DEBUG modem >> AT+CNEC?
    2020-10-05T22:33:41.461Z DEBUG modem << +CNEC: 0
    2020-10-05T22:33:41.471Z DEBUG modem << OK
    2020-10-05T22:33:41.476Z DEBUG modem >> AT+CNEC=24
    2020-10-05T22:33:41.496Z DEBUG modem << OK
    2020-10-05T22:33:41.510Z DEBUG modem >> AT+CGEREP?
    2020-10-05T22:33:41.528Z DEBUG modem << +CGEREP: 0,0
    2020-10-05T22:33:41.550Z DEBUG modem << 
    2020-10-05T22:33:41.552Z DEBUG modem << OK
    2020-10-05T22:33:41.557Z DEBUG modem >> AT+CGDCONT?
    2020-10-05T22:33:41.577Z DEBUG modem << +CGDCONT: 0,"IP","services.telenor.se","100.117.174.228",0,0
    2020-10-05T22:33:41.579Z DEBUG modem << OK
    2020-10-05T22:33:41.597Z DEBUG modem >> AT+CGACT?
    2020-10-05T22:33:41.616Z DEBUG modem << +CGACT: 0,1
    2020-10-05T22:33:41.642Z DEBUG modem << 
    2020-10-05T22:33:41.645Z DEBUG modem << OK
    2020-10-05T22:33:41.671Z DEBUG modem >> AT+CGEREP=1
    2020-10-05T22:33:41.689Z DEBUG modem << OK
    2020-10-05T22:33:41.695Z DEBUG modem >> AT+CIND=1,1,1
    2020-10-05T22:33:41.713Z DEBUG modem << OK
    2020-10-05T22:33:41.722Z DEBUG modem >> AT+CEREG=2
    2020-10-05T22:33:41.742Z DEBUG modem << OK
    2020-10-05T22:33:41.745Z DEBUG modem >> AT+CEREG?
    2020-10-05T22:33:41.765Z DEBUG modem << +CEREG: 2,1,"8602","04CAA80A",7
    2020-10-05T22:33:41.778Z DEBUG modem << 
    2020-10-05T22:33:41.779Z DEBUG modem << OK
    2020-10-05T22:33:41.809Z DEBUG modem >> AT+COPS=3,2
    2020-10-05T22:33:41.825Z DEBUG modem << OK
    2020-10-05T22:33:41.829Z DEBUG modem >> AT+COPS?
    2020-10-05T22:33:41.846Z DEBUG modem << +COPS: 0,2,"24008",7
    2020-10-05T22:33:41.862Z DEBUG modem << 
    2020-10-05T22:33:41.864Z DEBUG modem << OK
    2020-10-05T22:33:41.883Z DEBUG modem >> AT%XCBAND
    2020-10-05T22:33:41.899Z DEBUG modem << %XCBAND: 20
    2020-10-05T22:33:41.904Z DEBUG modem << OK
    2020-10-05T22:33:41.932Z DEBUG modem >> AT+CGDCONT?
    2020-10-05T22:33:41.961Z DEBUG modem << +CGDCONT: 0,"IP","services.telenor.se","100.117.174.228",0,0
    2020-10-05T22:33:41.963Z DEBUG modem << OK
    2020-10-05T22:33:41.979Z DEBUG modem >> AT+CGACT?
    2020-10-05T22:33:41.996Z DEBUG modem << +CGACT: 0,1
    2020-10-05T22:33:42.013Z DEBUG modem << 
    2020-10-05T22:33:42.020Z DEBUG modem << OK
    2020-10-05T22:33:42.033Z DEBUG modem >> AT%CESQ=1
    2020-10-05T22:33:42.050Z DEBUG modem << OK
    2020-10-05T22:33:42.062Z DEBUG modem >> AT+CESQ
    2020-10-05T22:33:42.083Z DEBUG modem << +CESQ: 99,99,255,255,19,60
    2020-10-05T22:33:42.095Z DEBUG modem << OK
    2020-10-05T22:33:42.106Z DEBUG modem >> AT%XSIM=1
    2020-10-05T22:33:42.129Z DEBUG modem << OK
    2020-10-05T22:33:42.146Z DEBUG modem >> AT%XSIM?
    2020-10-05T22:33:42.166Z DEBUG modem << %XSIM: 1
    2020-10-05T22:33:42.186Z DEBUG modem << 
    2020-10-05T22:33:42.188Z DEBUG modem << OK
    2020-10-05T22:33:42.195Z DEBUG modem >> AT+CPIN?
    2020-10-05T22:33:42.211Z DEBUG modem << +CPIN: READY
    2020-10-05T22:33:42.220Z DEBUG modem << OK
    2020-10-05T22:33:42.251Z DEBUG modem >> AT+CPINR="SIM PIN"
    2020-10-05T22:33:42.276Z DEBUG modem << +CPINR: "SIM PIN",3
    2020-10-05T22:33:42.279Z DEBUG modem << 
    2020-10-05T22:33:42.280Z DEBUG modem << OK
    2020-10-05T22:33:42.296Z DEBUG modem >> AT+CIMI
    2020-10-05T22:33:42.313Z DEBUG modem << 240084712615998
    2020-10-05T22:33:42.329Z DEBUG modem << OK
    2020-10-05T22:33:42.334Z INFO IMSIdentity: 240084712615998
    2020-10-05T22:34:07.746Z DEBUG modem << *** Booting Zephyr OS build v2.3.0-rc1-ncs2  ***
    2020-10-05T22:34:07.753Z DEBUG modem << Flash regionsDomainPermissions
    2020-10-05T22:34:07.756Z DEBUG modem << 00 02 0x00000 0x18000 Securerwxl
    2020-10-05T22:34:07.759Z DEBUG modem << 03 31 0x18000 0x100000 Non-Securerwxl
    2020-10-05T22:34:07.764Z DEBUG modem << Non-secure callable region 0 placed in flash region 2 with size 32.
    2020-10-05T22:34:07.937Z DEBUG modem << SRAM regionDomainPermissions
    2020-10-05T22:34:07.943Z DEBUG modem << 00 07 0x00000 0x10000 Securerwxl
    2020-10-05T22:34:07.944Z DEBUG modem << 08 31 0x10000 0x40000 Non-Securerwxl
    2020-10-05T22:34:07.946Z DEBUG modem << PeripheralDomainStatus
    2020-10-05T22:34:07.954Z DEBUG modem << 00 NRF_P0               Non-SecureOK
    2020-10-05T22:34:07.955Z DEBUG modem << 01 NRF_CLOCK            Non-SecureOK
    2020-10-05T22:34:07.957Z DEBUG modem << 02 NRF_RTC0             Non-SecureOK
    2020-10-05T22:34:07.960Z DEBUG modem << 03 NRF_RTC1             Non-SecureOK
    2020-10-05T22:34:07.975Z DEBUG modem << 04 NRF_NVMC             Non-SecureOK
    2020-10-05T22:34:07.979Z DEBUG modem << 05 NRF_UARTE1           Non-SecureOK
    2020-10-05T22:34:07.982Z DEBUG modem << 06 NRF_UARTE2           SecureSKIP
    2020-10-05T22:34:07.985Z DEBUG modem << 07 NRF_TWIM2            Non-SecureOK
    2020-10-05T22:34:07.988Z DEBUG modem << 08 NRF_SPIM3            Non-SecureOK
    2020-10-05T22:34:07.989Z DEBUG modem << 09 NRF_TIMER0           Non-SecureOK
    2020-10-05T22:34:07.990Z DEBUG modem << 10 NRF_TIMER1           Non-SecureOK
    2020-10-05T22:34:07.991Z DEBUG modem << 11 NRF_TIMER2           Non-SecureOK
    2020-10-05T22:34:07.992Z DEBUG modem << 12 NRF_SAADC            Non-SecureOK
    2020-10-05T22:34:07.994Z DEBUG modem << 13 NRF_PWM0             Non-SecureOK
    2020-10-05T22:34:08.010Z DEBUG modem << 14 NRF_PWM1             Non-SecureOK
    2020-10-05T22:34:08.014Z DEBUG modem << 15 NRF_PWM2             Non-SecureOK
    2020-10-05T22:34:08.022Z DEBUG modem << 16 NRF_PWM3             Non-SecureOK
    2020-10-05T22:34:08.025Z DEBUG modem << 17 NRF_WDT              Non-SecureOK
    2020-10-05T22:34:08.028Z DEBUG modem << 18 NRF_IPC              Non-SecureOK
    2020-10-05T22:34:08.038Z DEBUG modem << 19 NRF_VMC              Non-SecureOK
    2020-10-05T22:34:08.040Z DEBUG modem << 20 NRF_FPU              Non-SecureOK
    2020-10-05T22:34:08.043Z DEBUG modem << 21 NRF_EGU1             Non-SecureOK
    2020-10-05T22:34:08.045Z DEBUG modem << 22 NRF_EGU2             Non-SecureOK
    2020-10-05T22:34:08.052Z DEBUG modem << 23 NRF_DPPIC            Non-SecureOK
    2020-10-05T22:34:08.070Z DEBUG modem << 24 NRF_GPIOTE1          Non-SecureOK
    2020-10-05T22:34:08.071Z DEBUG modem << 25 NRF_REGULATORS       Non-SecureOK
    2020-10-05T22:34:08.073Z DEBUG modem << SPM: NS image at 0x18200
    2020-10-05T22:34:08.075Z DEBUG modem << SPM: NS MSP at 0x2002e8f0
    2020-10-05T22:34:08.078Z DEBUG modem << SPM: NS reset vector at 0x1d595
    2020-10-05T22:34:08.080Z DEBUG modem << SPM: prepare to jump to Non-Secure image.
    2020-10-05T22:34:08.249Z DEBUG modem << *** Booting Zephyr OS build v2.3.0-rc1-ncs2  ***
    2020-10-05T22:34:08.256Z DEBUG modem << The MQTT simple for Azure IoT Hub get started
    2020-10-05T22:34:08.268Z DEBUG modem << DNS correctly set
    2020-10-05T22:34:08.270Z DEBUG modem << LTE Link Connecting ...
    2020-10-05T22:34:08.278Z DEBUG modem << [00:00:00.214,050] [0m<dbg> lte_lc.lte_lc_system_mode_set: Sending AT command to set system mode: AT%XSYSTEMMODE=1,0,1,0[0m
    2020-10-05T22:34:08.335Z DEBUG modem << [00:00:00.234,069] [0m<dbg> lte_lc.lte_lc_system_mode_set: Sending AT command to set system mode: AT%XSYSTEMMODE=1,0,1,0[0m
    2020-10-05T22:34:09.839Z DEBUG modem << +CEREG: 2,"8602","04CAA80A",7,0,0,"11100000","11100000"
    2020-10-05T22:34:09.850Z DEBUG modem << [00:00:01.786,651] [0m<dbg> lte_lc.at_handler: +CEREG notification: +CEREG: 2,"8602","04CAA80A",7,0,0,"11100000","11100000"
    2020-10-05T22:34:09.852Z DEBUG modem << [0m
    2020-10-05T22:34:09.859Z DEBUG modem << [00:00:01.798,583] [0m<dbg> lte_lc.parse_psm_cfg: TAU: -1 sec, active time: -1 sec
    2020-10-05T22:34:09.861Z DEBUG modem << [0m
    2020-10-05T22:34:09.891Z DEBUG modem << +CSCON: 1
    2020-10-05T22:34:09.896Z DEBUG modem << [00:00:01.836,517] [0m<dbg> lte_lc.at_handler: +CSCON notification[0m
    2020-10-05T22:34:11.353Z DEBUG modem << +CEREG: 1,"8602","04CAA80A",7,,,"11100000","11100000"
    2020-10-05T22:34:11.360Z DEBUG modem >> AT+COPS=3,2
    2020-10-05T22:34:11.367Z DEBUG modem << [00:00:03.300,140] [0m<dbg> lte_lc.at_handler: +CEREG notification: +CEREG: 1,"8602","04CAA80A",7,,,"11100000","11"
    2020-10-05T22:34:11.368Z DEBUG modem << [0m
    2020-10-05T22:34:11.372Z DEBUG modem << [00:00:03.311,920] [0m<dbg> lte_lc.parse_psm_cfg: TAU: -1 sec, active time: -1 sec
    2020-10-05T22:34:11.374Z DEBUG modem << [0m
    2020-10-05T22:34:11.377Z DEBUG modem << LTE Link Connected!
    2020-10-05T22:34:11.380Z DEBUG modem << OK
    2020-10-05T22:34:11.386Z DEBUG modem >> AT+COPS?
    2020-10-05T22:34:11.394Z DEBUG modem << +COPS: 0,2,"24008",7
    2020-10-05T22:34:11.408Z DEBUG modem << 
    2020-10-05T22:34:11.412Z DEBUG modem << OK
    2020-10-05T22:34:11.426Z DEBUG modem >> AT%XCBAND
    2020-10-05T22:34:11.443Z DEBUG modem << %XCBAND: 20
    2020-10-05T22:34:11.453Z DEBUG modem << OK
    2020-10-05T22:34:11.476Z DEBUG modem >> AT+CGDCONT?
    2020-10-05T22:34:11.478Z DEBUG modem << IPv4 Address found 13.79.172.43
    2020-10-05T22:34:11.481Z DEBUG modem << username: Nordic.azure-devices.net/Thingy91/?api-version=2018-06-30, size:57
    2020-10-05T22:34:11.488Z DEBUG modem << password: SharedAccessSignature sr=Nordic.azure-devices.net%2Fdevices%2FThingy91&sig=JH5dvWZ98gjAnUIEDm%2B8sN3ZBHmPzDvaZbFT%2FYdQOa0%3D&se=3600001600713127, size:145
    2020-10-05T22:34:11.489Z DEBUG modem << MQTPORT_NON_SECURE --1--,0 :
    2020-10-05T22:34:11.495Z DEBUG modem << +CGDCONT: 0,"IP","services.telenor.se","100.105.214.93",0,0
    2020-10-05T22:34:11.497Z DEBUG modem << OK
    2020-10-05T22:34:11.514Z DEBUG modem >> AT+CGACT?
    2020-10-05T22:34:11.524Z DEBUG modem << +CGACT: 0,1
    2020-10-05T22:34:11.528Z DEBUG modem << 
    2020-10-05T22:34:11.530Z DEBUG modem << OK
    2020-10-05T22:34:11.608Z DEBUG modem << ERROR: mqtt_connect -95
    2020-10-05T22:34:24.326Z DEBUG modem << +CSCON: 0
    2020-10-05T22:34:24.333Z DEBUG modem << [00:00:16.274,017] [0m<dbg> lte_lc.at_handler: +CSCON notification[0m
    

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <random/rand32.h>
    #include <net/mqtt.h>
    #include <net/socket.h>
    #include <nrf_socket.h>
    #include <modem/lte_lc.h>
    #if defined(CONFIG_LWM2M_CARRIER)
    #include <lwm2m_carrier.h>
    #endif
    
    #include <drivers/sensor.h>
    #include <dk_buttons_and_leds.h> //Button and LED
    
    
    #if defined(CONFIG_MQTT_LIB_TLS)
    #include "certificates.h"
    #if defined(CONFIG_MODEM_KEY_MGMT)
    #include <modem/modem_key_mgmt.h>
    #endif
    #endif
    
    /* Buffers for MQTT client. */
    static u8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    static u8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    static u8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE];
    
    /* The mqtt client struct */
    static struct mqtt_client client;
    
    /* MQTT Broker details. */
    static struct sockaddr_storage broker;
    
    /* Connected flag */
    static bool connected;
    
    /* File descriptor */
    static struct pollfd fds;
    
    ////////////////////////////////////////////////
    #if defined(CONFIG_BSD_LIBRARY)
    
    /**@brief Recoverable BSD library error. */
    void bsd_recoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib recoverable error: %u\n", (unsigned int)err);
    }
    
    #endif /* defined(CONFIG_BSD_LIBRARY) */
    
    #if defined(CONFIG_LWM2M_CARRIER)
    K_SEM_DEFINE(carrier_registered, 0, 1);
    
    void lwm2m_carrier_event_handler(const lwm2m_carrier_event_t *event)
    {
    	switch (event->type) {
    	case LWM2M_CARRIER_EVENT_BSDLIB_INIT:
    		printk("LWM2M_CARRIER_EVENT_BSDLIB_INIT\n");
    		break;
    	case LWM2M_CARRIER_EVENT_CONNECT:
    		printk("LWM2M_CARRIER_EVENT_CONNECT\n");
    		break;
    	case LWM2M_CARRIER_EVENT_DISCONNECT:
    		printk("LWM2M_CARRIER_EVENT_DISCONNECT\n");
    		break;
    	case LWM2M_CARRIER_EVENT_READY:
    		printk("LWM2M_CARRIER_EVENT_READY\n");
    		k_sem_give(&carrier_registered);
    		break;
    	case LWM2M_CARRIER_EVENT_FOTA_START:
    		printk("LWM2M_CARRIER_EVENT_FOTA_START\n");
    		break;
    	case LWM2M_CARRIER_EVENT_REBOOT:
    		printk("LWM2M_CARRIER_EVENT_REBOOT\n");
    		break;
    	}
    }
    #endif /* defined(CONFIG_LWM2M_CARRIER) */
    
    /**@brief Function to print strings without null-termination
     */
    static void data_print(u8_t *prefix, u8_t *data, size_t len)
    {
    	char buf[len + 1];
    
    	memcpy(buf, data, len);
    	buf[len] = 0;
    	printk("%s%s\n", prefix, buf);
    }
    
    /**@brief Function to publish data on the configured topic
     */
    static int data_publish(struct mqtt_client *c, enum mqtt_qos qos,
    	u8_t *data, size_t len)
    {
    	struct mqtt_publish_param param;
    
    	param.message.topic.qos = qos;
    	param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC;
    	param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC);
    	param.message.payload.data = data;
    	param.message.payload.len = len;
    	param.message_id = sys_rand32_get();
    	param.dup_flag = 0;
    	param.retain_flag = 0;
    
    	data_print("Publishing: ", data, len);
    	printk("to topic: %s len: %u\n",
    		CONFIG_MQTT_PUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));
    
    	return mqtt_publish(c, &param);
    }
    
    /**@brief Function to subscribe to the configured topic
     */
    static int subscribe(void)
    {
    	struct mqtt_topic subscribe_topic = {
    		.topic = {
    			.utf8 = CONFIG_MQTT_SUB_TOPIC,
    			.size = strlen(CONFIG_MQTT_SUB_TOPIC)
    		},
    		.qos = MQTT_QOS_1_AT_LEAST_ONCE
    	};
    
    	const struct mqtt_subscription_list subscription_list = {
    		.list = &subscribe_topic,
    		.list_count = 1,
    		.message_id = 1234
    	};
    
    	printk("Subscribing to: %s len %u\n", CONFIG_MQTT_SUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));
    
    	return mqtt_subscribe(&client, &subscription_list);
    }
    
    /**@brief Function to read the published payload.
     */
    static int publish_get_payload(struct mqtt_client *c, size_t length)
    {
    	u8_t *buf = payload_buf;
    	u8_t *end = buf + length;
    
    	if (length > sizeof(payload_buf)) {
    		return -EMSGSIZE;
    	}
    
    	while (buf < end) {
    		int ret = mqtt_read_publish_payload(c, buf, end - buf);
    
    		if (ret < 0) {
    			int err;
    
    			if (ret != -EAGAIN) {
    				return ret;
    			}
    
    			printk("mqtt_read_publish_payload: EAGAIN\n");
    
    			//err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
                            err = poll(&fds, 1,
    				   CONFIG_MQTT_KEEPALIVE * MSEC_PER_SEC);
    			if (err > 0 && (fds.revents & POLLIN) == POLLIN) {
    				continue;
    			} else {
    				return -EIO;
    			}
    		}
    
    		if (ret == 0) {
    			return -EIO;
    		}
    
    		buf += ret;
    	}
    
    	return 0;
    }
    
    /**@brief MQTT client event handler
     */
    void mqtt_evt_handler(struct mqtt_client *const c,
    		      const struct mqtt_evt *evt)
    {
    	int err;
    
    	switch (evt->type) {
    	case MQTT_EVT_CONNACK:
    		if (evt->result != 0) {
    			printk("MQTT connect failed %d\n", evt->result);
    			break;
    		}
    
    		connected = true;
    		printk("[%s:%d] MQTT client connected!\n", __func__, __LINE__);
    		subscribe();
    		break;
    
    	case MQTT_EVT_DISCONNECT:
    		printk("[%s:%d] MQTT client disconnected %d\n", __func__,
    		       __LINE__, evt->result);
    
    		connected = false;
    		break;
    
    	case MQTT_EVT_PUBLISH: {
    		const struct mqtt_publish_param *p = &evt->param.publish;
    
    		printk("[%s:%d] MQTT PUBLISH result=%d len=%d\n", __func__,
    		       __LINE__, evt->result, p->message.payload.len);
    		err = publish_get_payload(c, p->message.payload.len);
    		if (err >= 0) {
    			data_print("Received: ", payload_buf,
    				p->message.payload.len);
    			/* Echo back received data */
    			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);
    		} else {
    			printk("mqtt_read_publish_payload: Failed! %d\n", err);
    			printk("Disconnecting MQTT client...\n");
    
    			err = mqtt_disconnect(c);
    			if (err) {
    				printk("Could not disconnect: %d\n", err);
    			}
    		}
    	} break;
    
    	case MQTT_EVT_PUBACK:
    		if (evt->result != 0) {
    			printk("MQTT PUBACK error %d\n", evt->result);
    			break;
    		}
    		//data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				//payload_buf, p->message.payload.len);
    		printk("[%s:%d] PUBACK packet id: %u\n", __func__, __LINE__,
    				evt->param.puback.message_id);
    		break;
    
    	case MQTT_EVT_SUBACK:
    		if (evt->result != 0) {
    			printk("MQTT SUBACK error %d\n", evt->result);
    			break;
    		}
    
    		printk("[%s:%d] SUBACK packet id: %u\n", __func__, __LINE__,
    				evt->param.suback.message_id);
    		break;
    
    	default:
    		printk("[%s:%d] default: %d\n", __func__, __LINE__,
    				evt->type);
    		break;
    	}
    }
    
    /**@brief Resolves the configured hostname and
     * initializes the MQTT broker structure
     */
    static int broker_init(void)
    {
    	int err;
    	struct addrinfo *result;
    	struct addrinfo *addr;
    	struct addrinfo hints = {
    		.ai_family = AF_INET,
    		.ai_socktype = SOCK_STREAM
    	};
    
    	err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &hints, &result);
    	if (err) {
    		printk("ERROR: getaddrinfo failed %d\n", err);
    		return -ECHILD;	
    	}
    	addr = result;
    	/* Look for address of the broker. */
    	while (addr != NULL) {
    		/* IPv4 Address. */
    		if (addr->ai_addrlen == sizeof(struct sockaddr_in)) {
    			struct sockaddr_in *broker4 =
    				((struct sockaddr_in *)&broker);
    			char ipv4_addr[NET_IPV4_ADDR_LEN];
    
    			broker4->sin_addr.s_addr =
    				((struct sockaddr_in *)addr->ai_addr)
    				->sin_addr.s_addr;
    			broker4->sin_family = AF_INET;
    			broker4->sin_port = htons(CONFIG_MQTT_BROKER_PORT);
    
    			inet_ntop(AF_INET, &broker4->sin_addr.s_addr,
    				  ipv4_addr, sizeof(ipv4_addr));
    			printk("IPv4 Address found %s\n", ipv4_addr);
    			break;
    		} else {
    				printk("ai_addrlen = %u should be %u or %u\n",
    				(unsigned int)addr->ai_addrlen,
    				(unsigned int)sizeof(struct sockaddr_in),
    				(unsigned int)sizeof(struct sockaddr_in6));
    		}
    
    		addr = addr->ai_next;
    		//break;
    	}
    
    	/* Free the address. */
    	freeaddrinfo(result);
    	return err;
    }
    
    /**@brief Initialize the MQTT client structure
     */
    static int client_init(struct mqtt_client *client)
    {
    	int err;
    	mqtt_client_init(client);
    	err = broker_init();
    	if (err) {
    		printk("Failed to initialize broker connection\n");
    		return err;
    	}
    
    	struct mqtt_utf8 pass,name; // define two structs and every struct has *utf8 and size
    	char * token ="SharedAccessSignature sr=Nordic.azure-devices.net%2Fdevices%2FThingy91&sig=JH5dvWZ98gjAnUIEDm%2B8sN3ZBHmPzDvaZbFT%2FYdQOa0%3D&se=3600001600713127";
    	//char * token ="SharedAccessSignature sr=Nordic.azure-devices.net%2Fdevices%2FThingy91&sig=%2B6CdJ2kI2VSv6wDtZIn%2BQI%2B94eDocpY6BEMAlrqqDmQ%3D&se=361601892258";
    	pass.size = (u32_t)strlen(token);
    	pass.utf8 = (u8_t*)token;
    	name.size = (u32_t)strlen("Nordic.azure-devices.net/Thingy91/?api-version=2018-06-30");
    	name.utf8 = (u8_t *)"Nordic.azure-devices.net/Thingy91/?api-version=2018-06-30";
    
    	printk("username: %s, size:%d\n",name.utf8,name.size);
    	printk("password: %s, size:%d\n",pass.utf8,pass.size);
    
    	/* MQTT client configuration */
    	client->broker = &broker;
    	client->evt_cb = mqtt_evt_handler;
    	client->client_id.utf8 = (u8_t *)CONFIG_MQTT_CLIENT_ID;  
    	client->client_id.size = strlen(CONFIG_MQTT_CLIENT_ID); 
    	client->password = &pass;
    	client->user_name = &name;
    	client->protocol_version = MQTT_VERSION_3_1_1;
    
    	//printk(client->client_id.utf8);
    
    	/* MQTT buffers configuration */
    	client->rx_buf = rx_buffer;
    	client->rx_buf_size = sizeof(rx_buffer);
    	client->tx_buf = tx_buffer;
    	client->tx_buf_size = sizeof(tx_buffer);
    	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
    	printk("MQTT_TRANSPORT_NON_SECURE --1--,%d :\n",client->transport.type);
    
    #if defined(CONFIG_MQTT_LIB_TLS)
        //struct mqtt_sec_config *tls_config = &client->transport.tls.config;
        
        client->transport.type = MQTT_TRANSPORT_SECURE;
        
        //tls_config->peer_verify = CONFIG_PEER_VERIFY;
        //tls_config->cipher_count = 0;
        //tls_config->cipher_list = NULL;
        //tls_config->sec_tag_count = ARRAY_SIZE(sec_tag_list);
        //tls_config->sec_tag_list = sec_tag_list;
        //tls_config->hostname = CONFIG_MQTT_BROKER_HOSTNAME;
    #else /* MQTT transport configuration */
    	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
    	printk("MQTT_TRANSPORT_NON_SECURE---2--\n");
    #endif /* defined(CONFIG_MQTT_LIB_TLS) */
    	return err;
    }
    
    /**@brief Initialize the file descriptor structure used by poll.
     */
    static int fds_init(struct mqtt_client *c)
    {
    	if (c->transport.type == MQTT_TRANSPORT_NON_SECURE) {
    		fds.fd = c->transport.tcp.sock;
    	} else {
    #if defined(CONFIG_MQTT_LIB_TLS)
    		fds.fd = c->transport.tls.sock;
    #else
    		return -ENOTSUP;
    #endif
    	}
    
    	fds.events = POLLIN;
    
    	return 0;
    }
    
    /**@brief Configures modem to provide LTE link. Blocks until link is
     * successfully established.
     */
    static void modem_configure(void)
    {
    #if defined(CONFIG_LTE_LINK_CONTROL)
    	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
    		/* Do nothing, modem is already turned on
    		 * and connected.
    		 */
    	} else {
    #if defined(CONFIG_LWM2M_CARRIER)
    		/* Wait for the LWM2M_CARRIER to configure the modem and
    		 * start the connection.
    		 */
    		printk("Waitng for carrier registration...\n");
    		k_sem_take(&carrier_registered, K_FOREVER);
    		printk("Registered!\n");
    #else /* defined(CONFIG_LWM2M_CARRIER) */
    		int err;
    		//Set manually DNS server
    		struct nrf_in_addr dns;
    		dns.s_addr = 134744072; // Google DNS, 8.8.8.8 in u32 rapresentation
    		err = nrf_setdnsaddr(2, &dns);
    		if (err ==0) {
          		printk("DNS correctly set\n");
        	} 
    		else {
          		printk("Error %d, DNS not set\n", err);
        		}
    
    		printk("LTE Link Connecting ...\n");
    		err = lte_lc_init_and_connect();
    		__ASSERT(err == 0, "LTE link could not be established.");
    		printk("LTE Link Connected!\n");
    #endif /* defined(CONFIG_LWM2M_CARRIER) */
    	}
    #endif /* defined(CONFIG_LTE_LINK_CONTROL) */
    }
    
    char* sensor (){
    
    	struct device *dev = device_get_binding(DT_LABEL(DT_INST(0, bosch_bme680)));
    	struct sensor_value temp, press, humidity, gas_res;
    	k_sleep(K_MSEC(3000));
    
    	sensor_sample_fetch(dev);
    	sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
    	sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
    	sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
    	sensor_channel_get(dev, SENSOR_CHAN_GAS_RES, &gas_res);
    	sprintf(payload_buf,"{ Temperature: %d.%d , Pressure: %d.%d , Humidity: %d.%d}",
    	temp.val1, temp.val2/1000, press.val1, press.val2/1000,
    	humidity.val1, humidity.val2/1000);
    	//printf("payload_buf=%s\n", payload_buf);
    	return payload_buf;
    }
    
    static void button_handler(u32_t button_state, u32_t has_changed)
    {	
    	int err;
    	char * data = sensor();
    	printk("sensor shwoing.... \n%s\n", data); 
    	err = data_publish(&client, MQTT_QOS_0_AT_MOST_ONCE, (u8_t *){data}, (size_t)strlen(data)); 
    	//printk("err=%d\n", err);
    	if (err == 0)
    	{
    		printk("Message has been published , msg size is:%s \n",(u8_t *){data});
    	}
    	else
    	{
    		printk("err=%d\n", err);
    	}
    }
    
    
    
    void main(void)
    {
    	int err;
    	printk("The MQTT simple for Azure IoT Hub get started\n");
    	err = dk_buttons_init(button_handler);
    	if (err) {
    		printk("Failed to initialize buttons (err %d)\n", err);
    		return;
    	}
    
    	modem_configure();
    
    	err = client_init(&client);
    	if (err != 0) {
    		printk("ERROR: client_init %d\n", err);
    		return;
    	}
    
    	err = mqtt_connect(&client);
    	if (err != 0) {
    		printk("ERROR: mqtt_connect %d\n", err);
    		return;
    	}
    
    	err = fds_init(&client);
    	if (err != 0) {
    		printk("ERROR: fds_init %d\n", err);
    		return;
    	}
    
    	err = poll(&fds, 1, CONFIG_MQTT_KEEPALIVE * MSEC_PER_SEC);
        if (err < 0) {
                printk("ERROR: poll %d\n", errno);
        }
    
        if ((fds.revents & POLLIN) == POLLIN) {
                err = mqtt_input(&client);
                if (err != 0) {
                        printk("ERROR: mqtt_input %d\n", err);
                }
        }
    	
    	
    }
    
    

  • Hello, 

    Deri said:
    sorry for late replay, I was reading and learning how to use Zephyr API and kernel services to communicate with peripherals.

    No worries, fully understand the prioritization.  

    Deri said:
    1-I need to run #west flash twice to flash the build successfully.

     Can you elaborate? Are you getting an error?

    Deri said:
    2-The first device "Thingy91" works properly and send data to Azure when pressing the button , while the other one showing an error  "ERROR: mqtt_connect -95" , I've tried to update the FW of modem and debug this issue by AT commands, but still didn't figure out how to solve it. I've attached a log file for this issue. 

     error  -95 is often seen when there is an issue with the certificates. Have followed the steps for Prerequisites for connecting to Azure IoT Hub and Provisioning of the certificates?

    Thanks!
    -Øyvind

Related