Sending Two Datasources from Arduino to Exosite

I managed to send two datasources from arduino to exosite using an ethernet shield. the two data sources are called nric and temp respectively. The issue I am facing is that in the second time I send the data, the nric data goes to the temp field instead of the nric field. The diagram below will help explaining the problem I am facing.

My code:

#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Exosite.h>
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘S’},
{‘4’,‘5’,‘6’,‘T’},
{‘7’,‘8’,‘9’,‘F’},
{’.’,‘0’,’#’,‘G’}
};
byte rowPins[ROWS] = { A5, A4, A3, A2 };
byte colPins[COLS] = { A1, A0, 2, 3 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

define ledpin 13
int i;
int j=0;
int k=0;
int l=0;
char data[12];
char nric[8];
char temp[4];
String cikData = “”;
byte macData = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// Use these variables to customize what datasources are read and written to.
String readString = “temp&nric”;
String writeString = “nric=”;
String returnString;

EthernetClient client;
Exosite exosite(cikData, &client);

void setup(){
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
Serial.println(“Boot”);

Ethernet.begin(macData);

// wait 10 seconds for connection:
delay(1000);
}

void loop(){
char key = kpd.getKey();
if(key) // Check for a valid key.
if (key != ‘#’)
{data[l]= key;
l++;
Serial.print(key);
}
else {
Serial.println("");
Serial.print("NRIC: ");
for (i = 0; i < 8; i = i + 1) {
nric[k]=data*;*
//Serial.print(nric[k]);
k++;
}
Serial.print(nric);
//one way to print array all at once
delay(500);
Serial.println("");
Serial.print("Temperature: ");
for (i=8; i<12; i=i+1){
temp[j]=data*;*
j++;}
Serial.print(temp);
Serial.println("");
l=0;
j=0;
writeString += String(nric);
writeString += “&temp=”;
writeString += String(temp);
//Write to “temp” and “nric”, read from “temp” and “nric” datasources.
if ( exosite.writeRead(writeString, readString, returnString)){
Serial.println(“OK”);
Serial.println(returnString);
}else{
* Serial.println(“Error”);*
}
}
}
In the first time, data is displayed successfully on Exosite.
The image shows the second time data is sent to Exosite. An error message appears in the Serial Monitor of the Arduino but this did not happen in the first time. The nric data and the temp data in this image are the first data and not the second data. Also, the Exosite website shows that the nric from the first data is added in the same string as the temp string.
I would really appreciate it if someone could help me out with this! Feel free to ask more in the event that the info I provided is not sufficient.

What image?

Read the rules about posting code in the how to use this forum sticky post.

It is here
How to use this forum

See how the whole second half of your post is italic? That means something is amiss. and why you should consider Grump_Mike's advice.

Note, it helps if you put your code inside a [ code ][ /code ] (but without the spaces) block so the forum software doesn’t try to parse it as other markup, it’ll save the whitespace that way too.

#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Exosite.h>
#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
{'1','2','3','S'},
{'4','5','6','T'},
{'7','8','9','F'},
{'.','0','#','G'}
};
byte rowPins[ROWS] = { A5, A4, A3, A2 };
byte colPins[COLS] = { A1, A0, 2, 3 }; 
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

define ledpin 13
int i;
int j=0;
int k=0;
int l=0;
char data[12];
char nric[8];
char temp[4];
String cikData = ""; 
byte macData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 
// Use these variables to customize what datasources are read and written to.
String readString = "temp&nric";
String writeString = "nric=";
String returnString;

EthernetClient client;
Exosite exosite(cikData, &client);

void setup(){
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
Serial.println("Boot");

Ethernet.begin(macData);

// wait 10 seconds for connection:
delay(1000);
}

void loop(){
char key = kpd.getKey();
if(key) // Check for a valid key.
if (key != '#')
{data[l]= key;
l++;
Serial.print(key);
}
else {
Serial.println(""); 
Serial.print("NRIC: ");
for (i = 0; i < 8; i = i + 1) {
nric[k]=data[i]; 
//Serial.print(nric[k]); 
k++;
} 
Serial.print(nric);
//one way to print array all at once
delay(500);
Serial.println("");
Serial.print("Temperature: ");
for (i=8; i<12; i=i+1){
temp[j]=data[i];
j++;} 
Serial.print(temp);
Serial.println("");
l=0;
j=0;
writeString += String(nric);
writeString += "&temp="; 
writeString += String(temp);
//Write to "temp" and "nric", read from "temp" and "nric" datasources.
if ( exosite.writeRead(writeString, readString, returnString)){
Serial.println("OK");
Serial.println(returnString);

}else{
  Serial.println("Error");
}
}
}

The first problem I see is that you’re appending the the global variable writeString, but never removing what you’ve appended. You can either reset what you have in the string after you’re done with it or simply move the declaration to the top of the loop function so it will always reinitialize itself on every loop.

Thanks Grumpy_Mike for your advice! Will do my best to go in accordance to the rules of forum posts.
The image I wanted to post in my first post is attached.

Azdle you were right. So what happened is that my previous values are added to the string the next time I took in the keypad readings. I did two things:

  1. Put writeString inside the loop function so that it reinitialize itself
  2. Created two variables: writeString & writeString2 for each variable and concatenated them with their respective strings (nric fo writeString and temp for writeString2).
#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Exosite.h>
#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3','S'},
  {'4','5','6','T'},
  {'7','8','9','F'},
  {'.','0','#','G'}
};
byte rowPins[ROWS] = { A5, A4, A3, A2 };
byte colPins[COLS] = { A1, A0, 2, 3 }; 
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
int i;
int j=0;
int k=0;
int l=0;
char data[12];
char nric[8];
char temp[4];
String cikData = "22781660f6fa7fbfe773057dfa4e93cb83e85869"; 
byte macData[] = {0x90, 0xA2, 0xDA, 0x02, 0x00, 0x00};        
// Use these variables to customize what datasources are read and written to.


EthernetClient client;
Exosite exosite(cikData, &client);

void setup(){  
  pinMode(ledpin,OUTPUT);
  Serial.begin(9600);
  Serial.println("Boot");
  
  Ethernet.begin(macData);
  
  // wait 10 seconds for connection:
  delay(1000);
}

void loop(){
  String readString = "";
  String writeString = "nric=";
  String writeString2 = "&temp=";
  String returnString;
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
      if (key != '#')
    {data[l]= key;
     l++;
     Serial.print(key);
    }
  else {
   Serial.println(""); 
   Serial.print("NRIC: ");
                  k=0;
                  for (i = 0; i < 8; i = i + 1) {
                  nric[k]=data[i];     //when i used nric[i] = data[i], serial.print(nric) gave out the entire data array - because of this issue i created another variable called k 
                  //Serial.print(nric[k]); 
                  k++;
                  }               
               Serial.print(nric);
                  //one way to print array all at once
              delay(500);
              Serial.println("");
              Serial.print("Temperature: ");
                  for (i=8; i<12; i=i+1){
                  temp[j]=data[i];
                  j++;}    
              Serial.print(temp);
              Serial.println("");
              l=0;
              j=0;
   /*writeString += String(nric);
   writeString += "&temp="; 
   writeString += String(temp);
   Serial.println(writeString);*/
    //Write to "temp" and "nric", read from "temp" and "nric" datasources.
    if (exosite.writeRead(writeString + String(nric) + writeString2 + String(temp), readString, returnString)){
      Serial.println("OK");
      Serial.println(returnString);
      
    }else{
      Serial.println("Error");
    } 
  }
}

So many thanks to Azdle for helping me solve this issue :slight_smile: