Webhook publishing incorrect values to Thingspeak

I am using an Adafruit MAX31855 thermocouple breakout board to read temperature values to my Photon. The Photon then publishes the values to the cloud so that I can view them. Additionally, the values are being converted to a string, then webhooked to the Thingspeak website. I am having issues where my temp sensor is publishing correct temp values, but the webhook is publishing zero values.

Here is a picture of the event logs:

You can see that the temp sensor has normal values, but the webhook is still zero.

Below is my code:

#include "math.h"
#include "Adafruit_MAX31855.h"

#define publish_cycle 20000 // Only publish every 20 seconds

int thermo1CLK = A3;
int thermo1CS = A2;
int thermo1DO = A4;

int thermo2CLK = D4;
int thermo2CS = D5;
int thermo2DO = D3;

double c;   // Temp 1
double v;   // Temp 2

unsigned long lastPublish = 0;
unsigned long TIME;

const String key = "NUMBERS"; //ThinkSpeak Key (removed for privacy)

Adafruit_MAX31855 thermocouple1(thermo1CLK, thermo1CS, thermo1DO);
Adafruit_MAX31855 thermocouple2(thermo2CLK, thermo2CS, thermo2DO);
  
void setup() {
    Serial.begin(9600);
    
    Serial.println("Templogger Output");
    
    // wait for MAX chip to stabilize
    delay(500);
}

void loop() {
    
    TIME = millis();
    
    ///////////////////////////////////////////////////////
    //                  Temperature 1                    //
    ///////////////////////////////////////////////////////    
    
    c = thermocouple1.readFarenheit();
    
    //String x = String(thermocouple1.readInternal());
    //Particle.publish("temperature internal 1", x);
    
    if (isnan(c)) {
        
        Serial.println("Something wrong with thermocouple!");
     
    } else {
       
        Serial.print("temperature one = ");
        Serial.println(c);
        String temp1 = String(c);
        Particle.variable("temp1", c);
        Particle.publish("temperature one", temp1);
     
    }

    ///////////////////////////////////////////////////////
    //                  Temperature 2                    //
    ///////////////////////////////////////////////////////     
    
    v = thermocouple2.readFarenheit();
    //String y = String(thermocouple2.readInternal());
    //Particle.publish("temperature internal 2", y);
   
    if (isnan(v)) { 
       
        Serial.println("Something wrong with thermocouple!");
     
    } else {

        Serial.print("temperature two = ");
        Serial.println(v);
        String temp2 = String(v);
        Particle.variable("temp2", v);
        Particle.publish("temperature two", temp2);
     
    }
    
    // Publish to thinkspeak
    if (((TIME - lastPublish) > publish_cycle)) {
        
        Serial.println("Wedhook!!");
      
        Particle.publish("thingSpeakWrite_All", "{ \"1\": \"" + String(c,2) + "\"," + "\"2\": \"" + String(v,2) + "\"," + "\"k\": \"" + key + "\" }", 60, PRIVATE);
        lastPublish = TIME;
    
    } 

    delay(5000);
}

Please let me know if you have any ideas as to why the webhook is doing this. Thanks!

Your use of Particle.variabl() is wrong - these get set up in setup() and have no business in loop()

Iā€™d rather use snprintf() instead of these String concatenations for your ThingSpeak message.

The missing temperature one & temperature two events before your zero readings for ThingSpeak suggest that your values are NaN at these instances. If you also add an error publish for the isnan() case, you might see the pattern.

How would you go about using the snprintf() functions with my temperature outputs?

Like this

  char msg[64];
  snprintf(msg, sizeof(msg), "{ \"1\": \"%.2f\",\"2\": \"%.2f\",\"k\": \"%s\" }", c, v, (const char*)key);
  Particle.publish("thingSpeakWrite_All", msg, 60, PRIVATE);

http://www.cplusplus.com/reference/cstdio/printf/
http://www.cplusplus.com/reference/cstdio/snprintf/

1 Like

Thank you for the response! In my attempts to use the snprintf() function, I was trying to do it once for each temp sensor and was getting compilation errors. Doing it all at once makes way more sense. Thank you for the reference links as well!

1 Like