How can I send data using JSON or XML

Dear Sir,

I am collecting coordinate due to a GPS module.
I can collect and send it to a server.

I would like to perfect it because the data are send as a string ($_POST) and I can only send one coordinate by time
Below I will post my code.

My code run into a loop. Each minute it collect the coordinate and each minute it send the data to the server.
First I would prefer to use JSON or a xml file and because I would like now to send not on coordinate but a collection of coordinate.
Then, in my point of view, it would better to use a xml file or JSON.

Do you have a recommandation how to send data to a remote server with JSON?

Here I show you my code. You can observe the function:
inet.httpPOST(host, port_post, path_post, coords, “Result”, 0);
the coords varaiable contain the string to send to the server

I would like to modify that code to have like a JSON structure and send it to the server. In case case I would be a ble to collect all coords in the JSON structure and send it to the server.

// for information
char co[]="co=";
char lo[]="&lo=";
char la[]="&la=";
char al[]="&al=";
char ti[]="&ti=";
char ve[]="&ve=";

void SendData(void){
   #ifdef DEBUG
     Serial.println(F(""));
     Serial.println(F("\nSENDING COORDS TO THE REMOTE SERVER"));
     Serial.println(F("---------------------------------------"));
   #endif
     
    stat=gps.getStat();
    
    #ifdef DEBUG
      if(stat==0){
        Serial.println(F("GPS OFF"));
        Serial.println(F("No coords sent"));
      }else if(stat==1){
         Serial.println(F("NOT FIXED"));
         Serial.println(F("No coords sent"));
      }else if(stat==2){
         Serial.println(F("2D FIXED"));
      }else if(stat==3){
         Serial.println(F("3D FIXED"));
      }
    #endif

     //Get data from GPS
     
     // Send the data only if the are FIXes, otherwise giveup the send function
     // This is to avoid battery consomation
     if(stat == 2 || stat == 3){
     
       gps.getPar(lon,lat,alt,time,vel);
       #ifdef DEBUG
         Serial.println(lat);
         Serial.println(lon);
       #endif
       
       char coords[97];
    
       // Convert Int to char
       char course_id[6] = {0};
       sprintf(course_id, "%d", courseid);
       puts(course_id);
       
       strcpy(coords,co);          //3
       //strncat(coords,lon,15);    //18
       strcat(coords,course_id);
          
       strcat(coords,lo);          //3
       strcat(coords,lon);
   
       strcat(coords,la);         //22
       strcat(coords,lat);
    
       strcat(coords,al);         //41
       strcat(coords,alt);
  
       strcat(coords,ti);         //60
       //strcat(coords,time);
    
       strcat(coords,ve);         //78
       strcat(coords,vel);
          
       #ifdef DEBUG
         Serial.println(F("Sending now... . Wait for 5 to 10 seconds"));
       #endif      
       
       inet.httpPOST(host, port_post, path_post, coords, "Result", 0);
          
       #ifdef DEBUG
         Serial.print(F("Data sent : "));
         Serial.println(coords);
         Serial.println(F("Wait for 10 second"));
       #endif
          
       delay(10000);
     }       
     enterCommand();
}
// End sendDATA()

Any idea ?

CHeers

does this help?

https://github.com/interactive-matter/aJson

Thank a lot. I found this but I have difficulties to understand how to make it working because it does not since I am trying.

I am going to provide you more code. Here is a function which a called in a sequence of time. For exemple 2mn. I commented some explication

// Send data to the sremote erver
void SendData(void){
   #ifdef DEBUG
     Serial.println(F("\nSENDING COORDS TO THE REMOTE SERVER"));
   #endif
     
    // It collect statictis and return 0,1,2 or or 3. See below
    stat=gps.getStat();
    
    #ifdef DEBUG
      if(stat==0){
        Serial.println(F("GPS OFF"));
        Serial.println(F("No coords sent"));
      }else if(stat==1){
         Serial.println(F("NOT FIXED"));
         Serial.println(F("No coords sent"));
      }else if(stat==2){
         Serial.println(F("2D FIXED"));
      }else if(stat==3){
         Serial.println(F("3D FIXED"));
      }
    #endif

     //Get data from GPS
     
     // Send the data only if the are FIXes, otherwise giveup the send function
     // This is to avoid battery consomation
     if(stat == 2 || stat == 3 || stat == 0){
     // I added stat==0 for testing, as I do not have allways fixes. It does not matter if I have 0.00000, for JSON testing
      
       // That function collect the coordinate. It's works fine
       gps.getPar(lon,lat,alt,time,vel); // those are char and are delared above exp: char lon[15];
       
       // Convert Int to char
       char course_id[6] = {0};
       sprintf(course_id, "%d", courseid);
       puts(course_id);
       
      /* EXPLICATION:
       HERE IS MY JSON and it does not display my JSON string (value)
       Be aware and I do not know I did it correctly
       I do declared the following below my libraies, on the top of my script:
          //JSON
          aJsonObject *root,*online;
          char* value;

      In my steup(), I add this code:
         //JSON
         root=aJson.createObject();
       */
       // HERE IS MY JSON CODE

       aJson.addItemToObject(root, time, online = aJson.createObject()); 
      // the variable time come from the function  gps.getPar(lon,lat,alt,time,vel);
      
       aJson.addStringToObject(online,"co", "course"); // I will later change "course" with the varibale course_id
       aJson.addStringToObject(online,"lo", "long"); // I will later change "long" with the varibale lon
       aJson.addStringToObject(online,"la", "lat"); // I will later change "lat" with the varibale lat
       aJson.addStringToObject(online,"al", "alt"); //etc
       aJson.addStringToObject(online,"ve", "vel"); //etc
       */
       
       value = aJson.print(root);
       Serial.print("value : ");
       Serial.println(value); // that value display nothing, empty , why
       
       
       
      
       #ifdef DEBUG
         Serial.println(F("Sending now... . Wait for 5 to 10 seconds"));
       #endif      
       
       if(inet.httpPOST(host, port_post, path_post, value, "Result", 0)){
         // Sent OK. JSON variable can be deleted
       }else{
         // Sent KO. JSON variable must have the next coordinate
       }
          
       #ifdef DEBUG
         Serial.print(F("Data sent : "));
         Serial.println(value);
         Serial.println(F("Wait for 10 second"));
       #endif
          
       delay(10000);
     }       
     enterCommand();
}
// End sendDATA()

My goal is to sent a JSON structure. Each time the sendDATA() function is called, the function inet.httpPOST(host, port_post, path_post, value, "Result", 0); send data to the server. The value variable contain the JSON string.

But if there is no 3G or GSM connection, the JSON must increment. It's means, it must work like an array to have something similar of:

{ "2003-08-16 08:00:00": { "co": "course", "la": "lattitude", "lo": "longitude", "al":"altitude", "ve":"speed" } "2003-08-16 08:01:00": { // there is no GSM connection, then it increment to record the data, and it will send in block when GSM will be available "co": "course", "la": "lattitude", "lo": "longitude", "al":"altitude", "ve":"speed" } "2003-08-16 08:02:00": { "co": "course", "la": "lattitude", "lo": "longitude", "al":"altitude", "ve":"speed" } "2003-08-16 08:03:00": { "co": "course", "la": "lattitude", "lo": "longitude", "al":"altitude", "ve":"speed" } }

I am sorry but I am really new with JSON and Arduino and you help would help me a lot

I hope I was clear and you can undertand ma issue and need Feel you free to ask me addition information