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 Carl,

    I did not uninstall my Zephyr standalone, but I am trying to add sdk-nrf v1.3.2 release to my existing zephyrproject repository. Is that possible or I make the mistake doing this procedure in that way? Also, trying this I got some error like: FATAL ERROR: already initialized in /home/adnan/zephyrproject, aborting. I found that I can resolve this issue unsetting the ZEPHYR_BASE environmental variable. Also, do you maybe know which is the procedure of uninstalling to all existing zephyrproject repository in case that I cannot finish my job like I explained above? And, my last question where can I unset this variable in my documentation?

    Best regards,

    Adnan.

  • Hi again.

    We do not recommend adding the nRF Connect SDK to the standalone Zephyr repository as you have described here. It is not guaranteed that things will work, and do not know how do make it work.

    If you follow the installation procedure described in our documentation, either using the Toolchain Manager or manually I can and will support you if there are any issues. I do not think it should be necessary to uninstall the old Zephyr repository, but if you want to it should only be a matter of deleting the whole directory. The relevant environment variables can be changed manually, if needed.

    Best regards,
    Carl Richard

  • Hi Carl,

    I deleted the whole Zephyr directroy and zephyr-sdk as well. I start from scratch again, but I am still not able to solve the problem related to nrf-sdk. This is the error that I get every time when I try to run this command west init -m github.com/.../sdk-nrf --mr v1.3.2:

    FATAL ERROR: already initialized in /home/adnan, aborting.

    I really don't know how to solve this problem and also I don't know what could be a problem if I removed everything and start from scratch. Also, the second problem is related to the installation of toolchain and required tools. When I install GNU Arm Embedded Toolchain how can I put it inside the folder ~/gnuarmemb if I don't have it at that time. Also, you referred to the Zephyr Getting Started Guidline. I need to follow these steps until Get Zephyr and install Python dependencies this part or even I need to install Zephyr inside the same directory as I did it before ~zephyrproject?

    Can you please help me to solve these issues because I am really quite confusing related to all these part and errors.

    Thanks a lot in advance and best regards,

    Adnan.

  • Hi again! I see! Seems like you are correct that you have to unset the ZEPHYR_BASE environment variable. How you do this depends on your operating system. What is your OS? Secondly, if you don't have the gnuarmemb directory you should create it, and then install the toolchain there. If you follow the NCS installation guide you will end up with a directory where all relevant repositories (including Zephyr) are present. The only thing you should need to do from the Zephyr Getting Started guide is to install the host dependencies, the rest is handled in the nRF Connect SDK installation guide.

    Most Python dependencies should be present from your previous Zephyr installation, but I recommend installing them as described anyways just to be sure. 

    I understand that this is troublesome. 

    Best regards,
    Carl Richard

  • Hi Carl,

    Thanks again for this help! I am using Ubuntu 18.04.5 LTS. Gnuarmemb is just the classic empty folder or what? That is what I am interested in...Is this folder at the end have to be in ncs directory or not? Related to this part with host dependencies I understood. I will try to follow all steps, but sometimes I just think that maybe you put some steps before the ones that should be on that place...if you understand me?

    Best regards,

    Adnan.

Related