I am having a problem with the photon, where the device constantly disconnects and reconnects from the cloud every few minutes. I am trying to read values from the serial port (rx,tx pins) and pass those back through a function to a server i wrote on the web. Here is my firmware:
String output;
void setup() {
Serial1.begin(9600);
Serial1.write("#Z,1\r");
Serial1.flush();
Particle.function("startConnection", start);
Particle.function("getTemp", getTemp);
Particle.variable("output", output);
}
void loop() {
}
int start(String extra){
Serial1.write("#Z,1\r");
Serial1.flush();
return 1;
}
int getTemp(String extra){
Serial1.write("#G\r");
Serial1.flush();
String content = "";
content = Serial1.readString();
if(getValue(content,'G',1)!="" && getValue(content,'G',1)!=" " && getValue(content,'.',1)!=""){
output=getValue(content,'G',1);
Particle.publish("Input",getValue(content,'G',1));
}else{
return getTemp("");
}
return getValue(content,'G',1).toInt();
}
void send(){
Serial1.write("#G\r");
Serial1.flush();
String content = "";
content = Serial1.readString();
Serial1.flush();
output=getValue(content,'G',1);
Particle.publish("Input",getValue(content,'G',1));
}
void read(){
String content = "";
content = Serial1.readString();
Particle.publish("Input",content);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
I call getTemp from the cloud every 10 seconds. Any ideas?