Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

Postby wegunterjr » Tue Dec 18, 2018 3:23 pm

So, currently, I am sending a JSON message containing a value I need to send from an ESP32 UART uart_write_bytes, but am not sure where I am going wrong in the conversion.
Currently, if I send 234, it goes out the UART as 50, 51, 52, and not 234.
Thoughts?
I am using esp-idf not Arduino.

Code: Select all

    cJSON* message'
    int intValue = 0;
    char *stringValue= "999";
    
    if ((message = cJSON_GetObjectItem(root->child, "msg")) != NULL)
    {
    	if((intValue = cJSON_GetObjectItem(message, "Number")->valueint) != NULL)
    	{
    		ESP_LOGI(LOG_TAG, " this is the Number %i ", intValue);
    	}
    	if((stringValue = cJSON_GetObjectItem(message, "Number")->valuestring) != NULL)
    	{
    	   ESP_LOGI(LOG_TAG, " This is NumberString %s ", stringValue);
    	}	
    }
    
    char numStrStr[3];
    
    char *p = numStr;
    
    for(int j = 0; j < sizeof(str); j++)
    {
        //hex[j] = *p++;
    	hex[j] = numStrStr[j];
    }
    					
    char s[200];
    uint8_t buf[BUF_SIZE];
    sprintf(s, hex);
    memset(buf, 0, sizeof(buf));
    memset(s, 0, sizeof(s));
    ESP_LOGI(LOG_TAG, "this is the hex: %s ", (char *)hex);
    ESP_LOGI(LOG_TAG, "this is the hex as s: %s ", s);
    
    int checkIt = uart_write_bytes(UART_NUM_2, (const char *)hex, strlen(hex));

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

Postby chegewara » Tue Dec 18, 2018 10:06 pm

This is structure of cJSON and as you can see you can send number value as double:

Code: Select all

typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSON;
But if you send number value as a string then you need to convert it first. I would suggest atoi, atol and atoll function
https://en.cppreference.com/w/c/string/byte/atoi
Its c++ reference but its the same for C.

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

Postby wegunterjr » Wed Dec 19, 2018 5:52 am

i want to send it over serial one byte at a time:
2
4
0
So, I was hoping to convert the stringvalue to something in an array/buffer that can be sent.

Thoughts?

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

Postby fly135 » Wed Dec 19, 2018 5:26 pm

ch -= '0'; Will convert ascii digit to binary value.

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: Converting valuestring or valueint from cJSON to send using uart_write_bytes is failing

Postby wegunterjr » Wed Dec 19, 2018 5:31 pm

This is how i got it to work. I send the value in a string format in JSON "234", then process it as shown in the code below.
The key is iterating over the 234 with %10 which gives you each of the 1's, 10's, 100's as individual values. That sends just fine.

Code: Select all

cJSON *message;
int intValue = 0;
const char *buffValue = "999";

hex[0] = 0xfe;
hex[1] = 0x01;
hex[2] = 0x01;
hex[3] = 0x00;

if((buffValue = cJSON_GetObjectItem(message, "NUmber")->valuestring) != NULL)
{
   ESP_LOGI(LOG_TAG, " This is Value String %s ", buffValue);
   ESP_LOGI(LOG_TAG, "strtol %ld ", strtol(buffValue, NULL, 10));
   intValue = strtol(buffValue, NULL, 10);

	int valueArray[3] = {0};
	int increment = 0;
	while(intValue > 0)
	{
		int digitValue = intValue % 10;
		valueArray[increment] = digitValue;
		intValue /= 10;
		increment ++;
	}
	hex[3] = valueArray[2];
	hex[4] = valueArray[1];
	hex[5] = valueArray[0];
}	
						
	
int checkIt = uart_write_bytes(UART_NUM_2, (const char *)hex, strlen(hex));	

Who is online

Users browsing this forum: No registered users and 95 guests