CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

JSON decoder
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 2:53 pm     Reply with quote

You do understand that CCS default for an integer is UNSIGNED and NOT signed ?

I don't know what the JSON code needs, but if it does need a signed int, you will have to recode to make it so.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 8:23 pm     Reply with quote

I understand you, but it’s Not the problem. Even if the type is wrong the code will compile. I have checked and edit this at the beginning.
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 8:41 pm     Reply with quote

If you are getting errors it is NOT compiling.

If the code does compile without errors what is it
doing (or not doing) vs what you expect?
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Nov 21, 2022 3:20 am     Reply with quote

Looking at the code, it is using push and pop functions on a stack.
The PIC16, does not have a software accessible stack. Result not possible.
You could write your own software stack emulation functions, but easier
honestly to not use a stack based approach to do it on this chip.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 21, 2022 6:50 am     Reply with quote

I went back to the original TBxxxx.PDF. Seems it was written for an AVR not a PIC, under the Microchip compiler and not CCS C.
There is NO way the code,as presented WILL compile for a PIC without a LOT of rewriting/ translation.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Tue Nov 22, 2022 2:47 pm     Reply with quote

Hi All,

I have write my simple "JSON" decoder and workaround this unnecessary example.

Best Regards,
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Nov 22, 2022 3:49 pm     Reply with quote

you should post your program in the code library here for others to see.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Tue Nov 22, 2022 6:52 pm     Reply with quote

Hi,

I'm not sure it's for code library because I'm using just "string" library and incremental char pointer to search parts of string, but I'll post here:


Code:

#include <16F1459.h>
#device ANSI
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO,NOPLLEN,NOPROTECT,MCLR,PUT,NOWDT  // CLKOUT -> Enable clk/4 out on PIN_A4
#use delay(int=16MHz, clock=24MHz)
#use RS232(DEBUGGER,rcv=PIN_B6,xmit=PIN_B6,stream=STREAM_SERIAL_INPUT)

#define TEST_STRING "{\"yellow\":\"off\",\"red\":\"on\",\"blue\":\"off\",\"green\":\"on\"}"
#define SRC_STRING "\"red\":"

#define RED_LED_ON() output_high(PIN_C5)
#define RED_LED_OFF() output_low(PIN_C5)

#include <string.h>

int1 func(char *buff, char *src){

   int1 FL = FALSE;
   char *cPtr;
   int8 n;
   
   
   cPtr = strstr (buff, src);
   n = strlen(src);
   
   if(cPtr != NULL){
      if(memcmp(cPtr+n, "\"on\"", 4) == 0)
        FL = TRUE;
   }
   
   return FL;
}

void main(void){
   
   delay_ms(100);
   
   delay_ms(100);                //StartUp Delay.
   setup_adc(ADC_OFF);           //Stop ADC.
   setup_adc_ports(NO_ANALOGS);  //Disable Adc-channels.
   setup_comparator(NC_NC_NC_NC);//Disable Comparator module.   
   
   printf("\r\nStr1: %s,\r\nStr2: %s", TEST_STRING, SRC_STRING);
   
   if(func(TEST_STRING, SRC_STRING))
      RED_LED_ON();
   else
      RED_LED_OFF();
     
   while(TRUE){
      // user code...
   }
}


Best Regards,
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Mon Nov 28, 2022 4:53 pm     Reply with quote

kmp84 wrote:
Hi,

I'm not sure it's for code library because I'm using just "string" library and incremental char pointer to search parts of string, but I'll post here:


Code:

#include <16F1459.h>
#device ANSI
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO,NOPLLEN,NOPROTECT,MCLR,PUT,NOWDT  // CLKOUT -> Enable clk/4 out on PIN_A4
#use delay(int=16MHz, clock=24MHz)
#use RS232(DEBUGGER,rcv=PIN_B6,xmit=PIN_B6,stream=STREAM_SERIAL_INPUT)

#define TEST_STRING "{\"yellow\":\"off\",\"red\":\"on\",\"blue\":\"off\",\"green\":\"on\"}"
#define SRC_STRING "\"red\":"

#define RED_LED_ON() output_high(PIN_C5)
#define RED_LED_OFF() output_low(PIN_C5)

#include <string.h>

int1 func(char *buff, char *src){

   int1 FL = FALSE;
   char *cPtr;
   int8 n;
   
   
   cPtr = strstr (buff, src);
   n = strlen(src);
   
   if(cPtr != NULL){
      if(memcmp(cPtr+n, "\"on\"", 4) == 0)
        FL = TRUE;
   }
   
   return FL;
}

void main(void){
   
   delay_ms(100);
   
   delay_ms(100);                //StartUp Delay.
   setup_adc(ADC_OFF);           //Stop ADC.
   setup_adc_ports(NO_ANALOGS);  //Disable Adc-channels.
   setup_comparator(NC_NC_NC_NC);//Disable Comparator module.   
   
   printf("\r\nStr1: %s,\r\nStr2: %s", TEST_STRING, SRC_STRING);
   
   if(func(TEST_STRING, SRC_STRING))
      RED_LED_ON();
   else
      RED_LED_OFF();
     
   while(TRUE){
      // user code...
   }
}


Best Regards,




Here you have a simple mqtt/json parser code (part of it) with printf() outputs for debugging purposes:

JSON MQTT data is coming from SIM7600 board. You can use ex_sisr.c in order to implement ring_buffer with enough size.

Data from YAT 2.1 terminal (for debug)

Code:

+CMQTTRXSTART: 0,15,52<CR><LF> (24)
+CMQTTRXTOPIC: 0,15<CR><LF> (21)
8680200310XXXXX<CR><LF> (17)
+CMQTTRXPAYLOAD: 0,52<CR><LF> (23)
{"action":"post", "resource":"relay_0","state":"on"}<CR><LF> (54)
+CMQTTRXEND: 0<CR><LF> (16)
<CR><LF> (2)


I hope this be useful


in rx_buffer you can store data from rda_isr for post processing, using strstr to find key string position and strtok to get the data.

With a hard and dedicated work, you can make it real...

for incomming messages, poll the rx_buffer

Code:


uint8_t  mqtt_loop() {
     
      byte ret_val = AT_RESP_ERR_DIF_RESP; //default value (not open)
     
     
      /*              # Chars
      +CMQTTRXSTART:    14
      +CMQTTRXTOPIC:    14
      <           >     XX    // Topic name
      +CMQTTRXPAYLOAD:  16
      <           >     XX    // Payload data
      +CMQTTRXEND:      12
               Total    56
      */
     
      fprintf(pc,"MQTT Loop: srtlen(rx_buffer): %lu \r\n", strlen(rx_buffer));
   
       if(  (strlen(rx_buffer)>56) && isStringReceived("START:") && isStringReceived("END: ") ) {  // Valid MQTT received packet
       
       fprintf(pc,"There are JSON/MQTT data\r\n");
     
      ret_val=TRUE;
     
      }
   
   
     
      return(ret_val);
   
   }



You can make a customized function for testing any string in the rx_buffer with strstr()

isStringReceived() is strstr() based on, like arduino SIMCOM modules code



Code:

uint8_t get_mqtt_message() {
         fprintf(pc,"get_mqtt_message(): RX_Buffer:%s\r\n",rx_buffer);


         p_aux = strstr( rx_buffer,"START:");      // get starting pos of mqtt msj


         //strtok(rx_buffer,"\r\n");         // discard first LINE
         strtok(p_aux,"\r\n");               // discard first LINE
         //strtok(rx_buffer,"\r\n");         // discard TOPIC specs
         strtok(NULL,"\r\n");                // discard TOPIC specs
         
            //topic    =  strtok(NULL,"\r\n");       // store TOPIC name
            strcpy(topic,strtok(NULL,"\r\n"));
            fprintf(pc,"TOPIC NAME:%s\r\n",topic);
         
         //p_aux =  strtok(NULL,"\r\n");             // discard PAYLOAD specs
         strtok(NULL,"\r\n");             // discard PAYLOAD specs
         
            //rx_payload  =  strtok(NULL,"\r\n");       // store PAYLOAD DATA.
            strcpy(rx_json_payload,strtok(NULL,"\r\n"));
            fprintf(pc,"JSON PAYLOAD:%s\r\n",rx_json_payload);
         
         buffer_flush();   //clear rx_buffer
}




uint8_t process_mqtt_message(void)   {

   // Topic is device ID
   // Actions and Status instrucctions are included inside JSON Payload
   // Each actions/status commands have responses from device
   
   
   if(strcmp(topic,IMEI)==0)  {
   
         fprintf(pc,"MQTT MESSAGE PROCESSING.....\r\n");
   
         /* JSON MQTT Message
         {"action":"post",
         "resource":"relay_x",   //X=0,1,2,3:
         "state":"on"}
         */
         
         // Parse rx_payload data
         
         char action[10];
         char resource[10];
         char state[4];
         
         char *action_p   =  strstr(rx_json_payload,"\"action\"");
         char *resource_p =  strstr(rx_json_payload,"\"resource\"");
         char *state_p    =  strstr(rx_json_payload,"\"state\"");
         
         //Action
         strtok(action_p,":");                           // discard first LINE
         strcpy(action,strtok(NULL,","));               // get action name "get" or "post"
         fprintf(pc,"MQTT MESSAGE: \"Action\": %s\r\n", action);
         
         //Resource
         strtok(resource_p,":");                         // discard first LINE
         strcpy(resource,strtok(NULL,","));              // get action name "get" or "post"
         fprintf(pc,"MQTT MESSAGE: \"Resource\": %s\r\n", resource);
         
         //State
         strtok(state_p,":");                             // discard first LINE
         strcpy(state,strtok(NULL,"}"));                 // get action name "get" or "post"
         
         fprintf(pc,"MQTT MESSAGE: \"State\": %s\r\n", state);
         
         //fprintf(pc,"STRCMP: ACTION vs POST: %u\r\n", strcmp(action,"\"post\""));
         
         if( strcmp(action,"\"post\"")==0)   {
         
            if (strcmp(state,"\"on\"")==0) {
                             
               if(strcmp(resource,"\"relay_0\"")==0)  {
                  sprintf(tx_json_payload,"{\"report\":\"state\",\"relay_0\":\"on\"}");
                  output_high(relay_0);
                  mqtt_publish(IMEI, tx_json_payload, 0, 60, 0);   // send a feedback about the control action status to the remote controller (MQTT client or any other control platform)
                  memset(tx_json_payload, '\0', 128);
                  buffer_flush();
                }
                             
             } 
             
             if (strcmp(state,"\"off\"")==0) {
                             
               if(strcmp(resource,"\"relay_0\"")==0)  {
                  sprintf(tx_json_payload,"{\"report\":\"state\",\"relay_0\":\"off\"}");
                  output_low(relay_0);
                  mqtt_publish(IMEI, tx_json_payload, 0, 60, 0); // send a feedback about the control action status to the remote controller (MQTT client or any other control platform)
                  memset(tx_json_payload, '\0', 128);
                  buffer_flush();
                }
               
             
               
        }  //if (strcmp(state,"\"on\"")
         
     
      }  //if( strcmp(action,"\"post\"")==0)
   }  // if (IMEI)
}  //process_mqtt_message(void)




KR
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group