Sending Two Datasources from Arduino to Exosite


#1

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[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");
} 

}
}


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.


#2

Since Exosite only enables me to add 1 image per post, I am going to post an image here that might be helpful in demonstrating the above problem.

The image here is the Arduino Serial monitor and as you can see for the first reading, it showed that “Connected Ok” to Exosite Server in line 5. However, for the second reading, there was “error communicating with Exosite” in line 12. What is also interesting is that actually the nric in line 7 should just be S1234567 and not S1234567%08. Look forward to receiving your suggestions on how to fix this problem.


#3

Could you print out ‘writeString’ before you send?

I think what you are doing incorrectly is that you are modifying ‘writeString’ by adding to it and then you are looping and modifying that string - so you are creating a big writeString each loop that continues to add the same values.

writeString += String(nric);
writeString += "&temp="; 
writeString += String(temp);

So this code is essentially creating a really large string that will look like this the second time through the loop and then will continue to add on:

nric=S1234567&temp=36.3nric=S1234567&temp=39.8

Instead, look at this example, how I add the strings together but I do not modify them.
from: https://github.com/exosite-garage/arduino_exosite_library/blob/master/examples/Murano-SmartLightBulb-ThingDevBoard/Murano-SmartLightBulb-ThingDevBoard.ino

 if (exosite.writeRead(writeString +String(t) + writeString2 + String(uptime) + writeString3 + String(h), readString, returnString)) {
 //do something
} else
{
 //do something
{


#4

Hi MikeA,

thanks for your reply. The screenshot is here
The writeString print is after the Temperature reading.
What would my readString in this example be? Because in your code the readstring is state


#5

Hey MikeA,

Thanks so much for your help. Managed to resolve the issue :slight_smile:

The final screenshot looks like this:

And I have attached my code here in case you wanted to have a look at it. Btw, I just did readString = “” cos I’m not really sure what it does but it works!

#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.

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");
} 

}
}

Cheers!