Struggling to figure out why the code below is returning incorrect json data for the ORP measurement but a direct publish to Particle cloud is correct. See switch (code), case 1. The resulting json shows the ph measurement for the ORP reading
int channel_ids[] = {98,99};
char *channel_names[] = {"ORP","PH"};
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
void setup() { // startup function
Serial.begin(9600); // Set the hardware serial port.
Wire.begin(); // enable I2C port.
Particle.variable("poolTemp", fahrenheit);
Particle.publish("version", "v1.2");
Blynk.begin(auth);
}
void loop() {
Blynk.run();
//-------------------------------------
//get and send temp
//-------------------------------------
if (millis() - msLastSample >= msSAMPLE_INTERVAL){
getTemp();
}
if (millis() - msLastMetric >= msMETRIC_PUBLISH){
Serial.println("Publishing now.");
root["timestamp"] = Time.now();
publishTempData();
getPublishProbes();
//write json string
root.printTo(jsonString, sizeof(jsonString));
Serial.println(jsonString);
//publish the json to particle cloud
Particle.publish("PoolSensor", jsonString,60,PRIVATE);
}
}
void publishTempData(){
if(!ds18b20.crcCheck()){ //make sure the value is correct
return;
}
sprintf(szInfo, "%2.2f", fahrenheit);
Particle.publish("Temp", szInfo, PRIVATE);
Blynk.virtualWrite(V5, szInfo);
root["temp"] = szInfo;
msLastMetric = millis();
}
void getPublishProbes(){
for (int channel = 0; channel < TOTAL_CIRCUITS; channel++) { // loop through all the sensors
Wire.beginTransmission(channel_ids[channel]); // call the circuit by its ID number.
Wire.write('r'); // request a reading by sending 'r'
Wire.endTransmission(); // end the I2C data transmission.
delay(1000); // AS circuits need a 1 second before the reading is ready
sensor_bytes_received = 0; // reset data counter
memset(sensordata, 0, sizeof(sensordata)); // clear sensordata array;
Wire.requestFrom(channel_ids[channel], 48, 1); // call the circuit and request 48 bytes (this is more then we need).
code = Wire.read();
while (Wire.available()) { // are there bytes to receive?
in_char = Wire.read(); // receive a byte.
if (in_char == 0) { // null character indicates end of command
Wire.endTransmission(); // end the I2C data transmission.
break; // exit the while loop, we're done here
}
else {
sensordata[sensor_bytes_received] = in_char; // append this byte to the sensor data array.
sensor_bytes_received++;
}
}
Serial.print(channel_names[channel]); // print channel name
Serial.print(':');
switch (code) { // switch case based on what the response code is.
case 1: // decimal 1 means the command was successful.
root[channel_names[channel]] = sensordata; //adds sensor data to the json buffer for given the channel name (ORP, PH)
root.printTo(jsonString, sizeof(jsonString));
Serial.println(jsonString);
Particle.publish(channel_names[channel], sensordata);
Blynk.virtualWrite(channel_ids[channel], sensordata);
break; // exits the switch case.
case 2: // decimal 2 means the command has failed.
Serial.println("command failed"); // print the error
break; // exits the switch case.
case 254: // decimal 254 means the command has not yet been finished calculating.
Serial.println("circuit not ready"); // print the error
break; // exits the switch case.
case 255: // decimal 255 means there is no further data to send.
Serial.println("no data"); // print the error
Particle.publish("poolmon2", "No Data");
break; // exits the switch case.
}
} // for loop
}