Does anyone know how to add the following formula in my code?

Hi everyone, I’m making a hydrometer and I’m able to publish the relative humidity, but I also want to publish the dew point and I’ve tried a lot, but I failed everytime I tried. Is there maybe anyone who could help me out? Underneath is my code, and in this code I want to add the formula:

DWP=(237,7FD)/(17,27-FD)
with
FD=(17,27
analogMeas0)/(237,7 + analogMeas0) + ln(RH/100)

My code:

#include "math.h"
/* 
script do to analog measurements. 
- every second, a measurement (as floating point) is written over the Serial line
- every 5 seconds, a measurement (as floating point) is published to the Cloud
- the function "readAnalog" is call-able from the Cloud. A measurement (as integer!)
  is returned. 
*/

//the interval used to measure, in milliseconds
int measureTime = 1000;

//the interval used to measure, in milliseconds
int measureTimeOnline = 30000;
//the name of the event.
String eventName0 = "Temperatuur, normaal";
String eventName1 = "Temperatuur, natte bol";
String eventName2 = "Relatieve Vochtigheid";

//the pin to use for measuring.
int analogPin0 = A0;
int analogPin1 = A1;

//some constants and constructs
int measuredBitValue = 0;
float measuredValue0 = 0.0;
float measuredValue1 = 0.0;

Timer measureTimer(measureTime, readAndSerial);
Timer measureTimerOnline(measureTimeOnline, readAndPublish);

void setup(){
  //start Serial communication
  Serial.begin(9600);

  //set analogPin to INPUT
  pinMode(analogPin0, INPUT);
  pinMode(analogPin1, INPUT);

  //start timer that calls measurement en sends results over Serial
  measureTimer.start();
  measureTimerOnline.start();
  
}

void loop(){}

void readAndSerial(){
  int analogMeas0 = analogRead(analogPin0);
  int analogMeas1 = analogRead(analogPin1);
  Serial.print("0: ");
  Serial.print(analogMeas0);
  Serial.print(" 1: ");
  Serial.println(analogMeas1);
  
}

void readAndPublish(){
  float analogMeas0 = round((((analogRead(analogPin0)*0.8056640625)- 500)/10)*10)/10;
  float analogMeas1 = round((((analogRead(analogPin1)*0.8056640625)- 500)/10)*10)/10;
  float RH = round((RelVocht(analogMeas1,analogMeas0))*10)/10;
  Particle.publish(eventName0, (String) analogMeas0);
  Particle.publish(eventName1, (String) analogMeas1);
  Particle.publish(eventName2, String(RH,1) + " % en " + String(analogMeas0,1));
}

float RelVocht(float T1, float T0){
    float Yb = 0.61*pow(2.71828184, 17.3*T1/(237+T1));
    float Pdmax = 0.61*pow(2.71828184, 17.3*T0/(237+T0));
    float Pd = -0.066*(T0-T1)+Yb;
    float RelH = 100*Pd/Pdmax;
    return RelH;
    
}

In programming languages you (usually) need to use the dot (.) as decimal seperator not the comma (,) as we’d do in German talking regions and round about :wink:

Otherwise I’m not sure what your issue might be, since there are several calculations in that code that should be a good starting point about the HowTo.

1 Like