Cant figure out what is wrong here

I have beat myself up forever trying to figure out why when I run some test code this works…

int value = 65;
    String command = "setLevel";
    request.path = subfolder + "/" + oathubid + "/" + command + "/" + String(value) + "?access_token=" + hubitataToken;
    http.get(request, response, headers);

however when I implement it here in my actual code sending real data from a ds18b20 temp probe it doesnt work…

// this is basically the ds18b20 sample code to obtain a temperature reading until the very bottom  
byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    case 0x26:
      type_s = 2;
      break;
    default:
      return;
  }
  ds.reset();               
  ds.select(addr);          
  ds.write(0x44, 0);        
  delay(1000);     
  present = ds.reset();
  ds.select(addr);
  ds.write(0xB8,0);         
  ds.write(0x00,0);         
  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE,0);         
  if (type_s == 2) {
    ds.write(0x00,0);       
  }
  for ( i = 0; i < 9; i++) {           
    data[i] = ds.read();
    }
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s == 2) raw = (data[2] << 8) | data[1];
  byte cfg = (data[4] & 0x60);
  switch (type_s) {
    case 1:
      raw = raw << 3; 
      if (data[7] == 0x10) {
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
      celsius = (float)raw * 0.0625;
      break;
    case 0:
      
      if (cfg == 0x00) raw = raw & ~7;
      if (cfg == 0x20) raw = raw & ~3;
      if (cfg == 0x40) raw = raw & ~1;
      celsius = (float)raw * 0.0625;
      break;
    case 2:
      data[1] = (data[1] >> 3) & 0x1f;
      if (data[2] > 127) {
        celsius = (float)data[2] - ((float)data[1] * .03125);
      }else{
        celsius = (float)data[2] + ((float)data[1] * .03125);
      }
  }
  if((((celsius <= 0 && celsius > -1) && lastTemp > 5)) || celsius > 125) {
      celsius = lastTemp;
  }
  fahrenheit = celsius * 1.8 + 32.0;
  lastTemp = celsius;
//------------------------------------------------------------------------------
// my additions to round and handle the data
//----------------------------------------------------------------------------  
float roatf = (fahrenheit + 0.5); // add .5 so that when you convert it to an interger value it rounds appropriately
  String roat(roatf, 0);  // string of rounded with zero decimal points
  ioat = atoi(roat);   // int of rounded data
  String oat = String(fahrenheit); // raw data string
  // make http refresh command to hubitat
  String command = "setLevel";  // command to send
  request.path = subfolder + "/" + oathubid + "/" + command + "/" + roat + "?access_token=" + hubitataToken; // parse the path as needed
  http.get(request, response, headers); // send the whole request off
// and particle publish
  if (cloudconnected == true){
  Particle.publish("OAT", roat, PRIVATE);
  }

I cant figure out why whatever its sending off in this example would be any different then the test sample where I manually set an interger to send off. but it definitely doesnt like something with the ds18b20 data vs my manual sending of an interger. when I check the console that particle publish says 65

I’d suggest to avoid String and rather use snprintf() to build your path string.
This will also deal with your rounding (which seems a rather obscure way of doing it IMO).

Also Serial.print() your request.path and check whether it actually is what you expect.

1 Like

Ill try that i tried rounding other easier ways I just settled on this example before submitting for help because the number appears to be right… so after printing my path its not the data, even though I figured that was the only thing that was different. For some reason the hubitataToken part wasn’t going in. Is this a bug? I cant see anything wrong with the way I put that in. Basically my request.path was ending with ?access_token=
however if I change that part to ?access_token=tokentypedontheendmanually it works. my hubitaToken is declared at the top of the program for easy editing and it is like so…
String hubitataToken =“tokenhere”;

Im baffled cause this is the exact same way I posted it in my test program I cant figure out why it behaved any
differently

I’d go with something along this line

  char roat[16];
  char path[128];
  snprintf(roat, sizeof(roat), "%.0f", fahrenheit);
  snprintf(path, sizeof(path)
          , "%s/%s/%s/%.0f?access_token=%s"
          , (const char*)subfolder
          , (const char*)oathubid
          , (const char*)command
          , fahrenheit
          , (const char*)hubitataToken
          );
  request.path = path;

If you have all your strings as C strings, you can omit the (const char*) type casts.

1 Like