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));
			}		
			
		}
}

Parents
  • Hello, Aduka!

    Thank you for reaching out. First of all, have you defined the correct prj.conf for your application? Mine looks like this for the hello_world sample:

    # nothing here
    CONFIG_CJSON_LIB=y
    
    # newlibc
    CONFIG_NEWLIB_LIBC=y

    Secondly, your code is mostly correct, but you have done an illegal comparison in your while loop. Look at my main below, where the error is corrected:

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

    In the code above I have changed line 45 so that cJSON_Print is used to write the content to a string. Then I have changed 49 to use strcmp, for comparing the strings. The line numbers I reference are from your original code. 

    Hope this helps you on your way, and please reach out if you wonder about anything else. 

    Best regards,
    Carl Richard

  • Hi Carl,

    Thanks a lot for this help and suggestions! I tried this code with these corrections, but again I am not able to compile it on my board. Now the problem occurs in my proj.conf file I think.

    This is the error:

    error: Aborting due to Kconfig warnings

    CMake Error at /home/adnan/zephyrproject/zephyr/cmake/kconfig.cmake:217 (message):
    command failed with return code: 1
    Call Stack (most recent call first):
    /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake:510 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:40 (include_boilerplate)
    CMakeLists.txt:5 (find_package)


    -- Configuring incomplete, errors occurred!
    See also "/home/adnan/zephyrproject/zephyr/build/CMakeFiles/CMakeOutput.log".
    See also "/home/adnan/zephyrproject/zephyr/build/CMakeFiles/CMakeError.log".
    FAILED: build.ninja
    /usr/bin/cmake --regenerate-during-build -S/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson -B/home/adnan/zephyrproject/zephyr/build
    ninja: error: rebuilding 'build.ninja': subcommand failed
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/adnan/zephyrproject/zephyr/build

    Can you maybe figure out what could be a potential problem now? And also, I forgot to tell that I use Zephyr if that is important for this problem. Also, my last question is related to Makefile and prj.conf files...where I can find the proper configuration for different samples. For sure, I will need to use cJSON together with my BLE example.

    Thanks in advance and best regards,

    Adnan.

  • Hi! 

    Sorry, did not mean to confuse youI recommend that you install nRF Connect SDK (which includes Zephyr) from scratch, as described in this guide. It could also be that your current Zephyr installation will interfere NCS, so if it's not needed I recommend uninstalling it. 

    Best regards,
    Carl Richard

  • Hi Carl,

    But I need this because of same samples that I use as a base for my app. For example, in your documentation I cannot find this sample for battery voltage reading or this one with temperature sensing and sending data via BLE. Because all of these things, I asked you if it is possible to use both of them in same directory?

    Best regards,

    Adnan.

  • Hi, Adnan!

    Could you provide a link to the samples you are referring to? I'll have a look. If I understand you correctly, then yes, the samples can be combined and used in one directory.

    To be clear: The full Zephyr RTOS is available as a part of NCS.


    Best regards,
    Carl Richard

  • Hi Carl,

    Yes of course I will provide you the samples that I use. To be clear enough, I use this sample where I can get the values for temperature and battery level (in percentage) and send them to my central node on which for now I am only able to read temperature value. Then, I put this part related to voltage reading just because I wanted to convert these percentages into mV (integer). Now, I got some values in percentage that I don't know how to make the relation between them and mV. Still, my central node is not able to read the battery level that I want to sent him from my peripheral one, and I don't know how to implement that service. Do you suggest me to uninstall my Zephyr standalone and install the nRF Connect SDK with all samples that were already included in Zephyr standalone documentation?

    These are links of examples that I use for now:

    https://github.com/nrfconnect/sdk-zephyr/tree/master/samples/boards/nrf/battery - for voltage reading

    https://github.com/nrfconnect/sdk-zephyr/tree/master/samples/bluetooth/peripheral_ht - for temperature and battery level reading / peripheral node

    https://github.com/nrfconnect/sdk-zephyr/tree/master/samples/bluetooth/central_ht - central node

  • I see. Thanks for providing the links. 

    First: don't know how much you have edited the samples, but from what I can see the peripheral_ht "simulates" the battery level by setting it to 100. Furthermore the Battery Service(BAS) operates with percentages. To calculate these you must know the maximum capacity of your battery, and measure the voltage as you describe.

    Secondly: Yes, I do suggest this. You will get access to the same samples, but in an development environment maintained and supported by us.

    Best regards,
    Carl Richard

Reply
  • I see. Thanks for providing the links. 

    First: don't know how much you have edited the samples, but from what I can see the peripheral_ht "simulates" the battery level by setting it to 100. Furthermore the Battery Service(BAS) operates with percentages. To calculate these you must know the maximum capacity of your battery, and measure the voltage as you describe.

    Secondly: Yes, I do suggest this. You will get access to the same samples, but in an development environment maintained and supported by us.

    Best regards,
    Carl Richard

Children
  • 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