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

How to parse and read JSON file correctly?

Hi guys,

I am using the nRF52832 board and I want to parse and read my JSON file. I don't have too much experience working with cJSON and here you will find my simple code where I try to understand how to define things properly. I will appreciate a lot if you can help me with some advice or better if you can suggest me where I made errors.

Best regards,

Adnan.

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"


char *create_hello(void) {
	
	char *string;
	
	cJSON *hello = cJSON_CreateObject();
	
	cJSON *functionName = cJSON_CreateString("hello_world");
	cJSON_AddItemToObject(hello, "functionName", functionName);
	
	string = cJSON_Print(hello);
	
	cJSON_Delete(hello);
	return string;
	
	}

void hello_world (void) {
	
	printk("Hello World! %s\n", CONFIG_BOARD);
	}


void main(void)
{
	const char *hello = "{\"functionName\": \"hello_world\"}";
	cJSON *hello_json = cJSON_Parse(hello);
	cJSON *functionName = cJSON_GetObjectItemCaseSensitive(hello_json, "functionName");
	
	while(true){
		
		if (functionName == "hello_world") {
			hello_world();
			k_sleep(K_SECONDS(1));
			}		
			
		}
}

  • Hi again, Adnan!

    Could you share your prj.conf? In other to achieve proper RTT the following should be present:

    #Activate serial
    CONFIG_SERIAL=y
    
    #Deactivate UART
    CONFIG_UART_CONSOLE=n
    
    #Use RTT for logging
    CONFIG_RTT_CONSOLE=y
    CONFIG_USE_SEGGER_RTT=y

    ----

    What kind of error are you getting? One of the steps in the installation guides I have shared earlier is installing west, so If you've followed those I don't believe that's the problem.

    As I've said earlier, when following the NCS installation guidelines, Zephyr will be installed.

    Best regards,
    Carl Richard

  • Hi Carl, 

    Thanks for this advice related to my proj.conf file! I will send you my proj.conf file to check. I have followed all the NCS installation steps, as we talked about before. Also, I installed Zephyr inside the same folder as nrf, but I think the main problem is in Zephyr-SDK...I did not install it because you told me before that's not necessary to have. But, I send you my error, and then maybe you will be able to see more clearly what the issue is. 

    proj.conf file: 

    CONFIG_BT=y
    CONFIG_BT_DEBUG_LOG=y
    CONFIG_BT_SMP=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_GATT_DIS=y
    CONFIG_BT_GATT_DIS_PNP=n
    CONFIG_BT_GATT_BAS=y
    CONFIG_BT_DEVICE_NAME="Zephyr Health Thermometer"
    CONFIG_BT_DEVICE_APPEARANCE=768
    CONFIG_BT_ATT_ENFORCE_FLOW=n
    CONFIG_ADC=y
    CONFIG_LOG=y
    CONFIG_LOG_PRINTK=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_NEWLIB_LIBC=y
    
    
    

  • Hi again.

    If you add the config lines from my previous comment to your prj.conf I believe it should work.

    When it comes build problems. Can you try to run the command "West update" and provide me the output? And to repeat myself, the SDK is necessary, but it's installed in the NCS folder when running west init -m github.com/.../sdk-nrf --mr <ncs_revision_here>.

    Best regards,
    Carl Richard

  • Hi again,

    I added the new lines in my configuration file and now it works properly. Thanks a lot! I know I installed sdk-nrf, but I think that the Zephyr SDK is mentioned in the error...or I am missing something? Also, do you maybe have some examples where I can find how to use non-volatile memory? 

    Thanks in advance, 

    Adnan.

  • Hi!

    Good to hear. West is the tool that is used to handling things like dependencies, so when using west init for the sdk-nrf, Zephyr should be installed along with it. What was the output of "west update"?

    For your second query I suggest that you open a new ticket.

    Best regards,
    Carl Richard

Related