You are reading a single comment by @verdeimpacat and its replies. Click here to read the full conversation.
  • I received my first pico boards few days ago ( thank you Gordon, brilliant design from both software and hardware perspective!).
    I started this temperature regulator project, mainly to have a start with Java Script which is unknown to me until now, but also with a real application in mind: as an amateur photographer I maintain a nice inventory of vintage cameras and lenses in a dependency of our house which is unheated over the winter.
    Now, the design goals, adapted to this application are:
    -maintain a low regulated temperature when I am not working, for the sake of decent electricity bill;
    -maintain a comfortable temp whil I am spending time in that room;
    -take care about the condensation and avoid rapid jumps in temperature when moving between the two;
    Nice to have:
    -remote control;
    -temperature indication on some LCD or LEDs;
    -Air Conditioning control for the summer days;
    -some possibility to check for efficiency and status of control algorithm;
    -real time clock to be able to program on a daily basis the target temperature;
    I was not wise and I commited the aquisition of some components before having in mind the final design and thus I spend few good days trying hopelessly to write a HTU21D module... Christmas miracle: on December 22nd I found the just published HTU21D module on the Espruino site !!! Thank you Tom Gidden and Luwar !
    Now, the code as of today ( functionality not checked yet since I am awaiting the prototype components):

    // 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
    
    // Before anything else we should turn off the triac
    var triac=A8;
    digitalWrite(triac,0); //this shoud reset the pin and set it as output
    
    I2C1.setup( {scl: B6, sda: B7, bitrate 50000 } );
    var htu = require('HTU21D').connect( I2C1 );
    
    //soft reset is required before use of HTU21D
    htu.softReset();
    
    //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);
    
    var temp = htu.readTemperature();
    var humidity = htu.readHumidity(); 
    var CompHumidity = htu.getCompensatedHumidity( humidity, temp ); 
    //returns improved accuracy for humidity
    var TargetTemp = 8.50; 
    // this assumes 8.50°C as a perfect target for the scope of the regulation
    
    // for the finetuning of the regulation we will keep in TempHistory the
    // hystorical values of the last hour
    var TempHistory  = Float32Array(60); 
    
    // at power-on we assume the measured temperature was constant for the last hour
    TempHistory.fill(temp);
    var AverageTemp = temp;
    
    new function PWMregulation(t,CH) {
       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(t,CH);
       if ((TargetTemp-DewPointTemp) < 1) || ((t - 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 photolenses
       // has thermal inertia and we don't want condensation to occur on these. 
       // We better move slowly to higher temperature if such.
       // Remember, the project aim is to protect photo gear & lenses against 
       // condensation and resulting  fungus !
      
       if (TempHistory[59] - DewPointTemp) < 1 {
          RegulatedTemp = TempHistory[59] + 1;
       }
       //here we apply the proportional-integrative-derivative control with some 
      // coeficients we feel appropriate. 
       //we will make some temperature regulation graphs to finetune these coeficients
       // once the system is running
       var PIDresult =0.5*(RegulatedTemp-t + 10*(RegulatedTemp-AverageTemp) + 40*(TempHistory[0]-t));
       var calculatedPWM  = E.clip (PIDresult, 0, 1);
       return calculatedPWM;
    }
    
    // we set the control loop to 60 seconds
    // all planned activities for optimal regulation are executed inside the bellow loop
    setInterval( function() {
       temp = htu.readHumidity();
       humidity = htu.readHumidity();
       CompHumdity = htu.getCompensatedHumidity( humidity, temp );
       analogWrite(triac, PWMregulation(temp,CompHumdity), { freq : 1 });
       //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;
    },60000);
    

    As you may notice the code is under construction and not all the design goals are fulfiled as of today. But I would welcome your thoughts on the approach, any errors I might have done sofar and ideas for code optimization.

About