Page 1 of 1

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

Posted: Tue Dec 18, 2018 3:23 pm
by wegunterjr
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));

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

Posted: Tue Dec 18, 2018 10:06 pm
by chegewara
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.

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

Posted: Wed Dec 19, 2018 5:52 am
by wegunterjr
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?

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

Posted: Wed Dec 19, 2018 5:26 pm
by fly135
ch -= '0'; Will convert ascii digit to binary value.

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

Posted: Wed Dec 19, 2018 5:31 pm
by wegunterjr
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));