Divide by zero breaks webhook

Newbie question but how can I handle a scenario where i have a sensor going bad and producing a 0 output that then gives me a divide by zero error. I don’t want it to cause my publish to break, but i do want to know that its gone bad and isn’t reading anymore.

  VRL = ads.readADC_SingleEnded(0)*(5/1666.0); //convert bits to volts
  Rs = ((5*RL)/VRL)-RL; //Use formula to get Rs value (from datasheet)

this then breaks my publish to ubidots.

  char data[80];
  snprintf(data, sizeof(data)        
          , "{ \"H\" : %.1f, \"T\" : %.1f, \"C\" : %.1f, \"P\" : %.1f, \"L\" : %.1f }"
          , humidity
          , temp1
          , co2avg
          , ppm
          , luxvalue
          );
      
 //PUBLISH TO CLOUD          
  Particle.publish("Ubidots", data, PRIVATE);

this is what my string looks like coming into the event console:

{ "H" : 61.1, "T" : 73.0, "C" : 1082.0, "P" : nan, "L" : 24.0 }

Just catch the zero case and pass an obvious “error indicator value”.
e.g.

  if (VRL == 0)
    Rs = -9999;
  else 
    Rs = ((5*RL)/VRL)-RL;

not really rocket science

2 Likes

ouch ! lol I am not a rocket scientist for sure.
I was thinking of replacing it with 999 or 0 … didn’t occur to me to use a negative
Thanks for the help though!

1 Like

For what its worth, dividing by 0 breaks everything else too, haha