You are reading a single comment by @verdeimpacat and its replies. Click here to read the full conversation.
  • Danke UliMerkel ! It is mentioned indeed!
    Second pass over the code without any additional functionality but just with the advices of Gordon implemented.
    Will use:
    Espruino pico as controller
    I2C1 for communication with HTU21D
    IE: pin B7 I2C1 SDA .... DATA, pin B6 I2C1 SCL... CLOCK
    triac control is connected via a MOC3083 optocoupler whoes LED is sourced via a resistor on pin A8

    var triac;
    I2C1.setup( {scl: B6, sda: B7, bitrate 56000 } );
    var htu = require('HTU21D').connect( I2C1 );
    var temp;
    var averageTemp;
    var humidity;
    var compHumidity; // improved accuracy for humidity
    var targetTemp;
    // for the finetuning of the regulation we will keep in TempHistory the  
    // historical values of the last hour
    var tempHistory = Float32Array(60); 
    
    function onInit () {
       // Before anything else we should turn off the triac
       triac=A8;
       digitalWrite(triac,0); //this shoud reset the pin and set it as output
       temp = htu.readTemperature();
       //at power-on we assume the temperature was constant for the last hour
       tempHistory.fill(temp);
       averageTemp = temp;
       //turn HTU21D heater on for two seconds just to make sure the HTU21D sensor
       //has no condensation
       htu.setHeaterOn( true );
       setTimeout('htu.setHeaterOn( false )'; 2000);
       targetTemp = 8.50;
    }
           
    function pidControlLoop() {
       temp = htu.readTemperature();
       humidity = htu.readHumidity();
       compHumdity = htu.getCompensatedHumidity( humidity, temp );
       var regulatedTemp = targetTemp;
       //we want to check that moving towards regulated temp does not produce 
       //condensation. Furthermore, if the current temp is close or bellow
       //dew point we wish to move to higher temp
       var dewPointTemp=htu.getDewPoint(temp,compHu­mdity);
       if (((targetTemp-dewPointTemp) < 1) || ((temp - dewPointTemp) < 1)) {
          regulatedTemp=dewPointTemp + 1;
       }
       //same, we want to check that the temperature one hour ago was above
       //the dew point temperature
       //this is because the metal and glass of the photo lenses has thermal  
       //inertia and we don't want condensation to occur on these. We better
       //move slowly to higher temperature if such.
       if ((tempHistory[59] - dewPointTemp) < 1) {
          regulatedTemp = tempHistory[59] + 1;
       }
       // here we apply the proportional-integrative-derivative control with some  
       // coefficients we feel appropriate. We will make some temperature regulation 
       // graphs to finetune these coefficients once the system is running
       var PIDresult =0.5 * (regulatedTemp-temp + 10 * (regulatedTemp-averageTemp) + 25 * (tempHistory[0]-temp));
    
       //for summer days we should imagine vent./AC control if PIDresult is < zero...
       calculatedPWM  = E.clip (PIDresult, 0, 1);
       analogWrite(triac, calculatedPWM, { freq : 2 });
       //Prepare for the next loop
       averageTemp = averageTemp - (tempHistory[59] - temp)/60;
       for (i=59;i>0;i--) {
          tempHistory[i] = tempHistory[i-1];
       }
       tempHistory[0] = temp;
    }
    
    //power on initialization
    onInit();
    // hereby we set the control loop to 60 seconds
    setInterval('pidControlLoop()',60000);
    

    My "mission" over the week-end: tweak the HTU21D module and define instead some sort of a function that reads the values of temperature and humidity from two potentiometers just to move forward with code debugging and triac control while awaiting for the prototype components.
    Wish you will enjoy your weekend too !

About