I am doing a little project for my pet snake and that is a Terrarium controller with my Raspberry PI Zero W.
Just for fun I created it using NodeJS since I am a bit familiar with it. A friend of mine created everything hardware related and just gave me specifications for pins and what they do.
So to actually explain my problem: The requirements are to read some data from sensors -> - 2x 1 wire DS18B20 sensors - 1x DHT22
and display everything on - OLED SSD1306
I am also using 2 relays to open access for light and heater but I think that actually doesn't matter right now and is not related to the problem.
The app is running in main loop:
var mainLoopInterval = setInterval(mainloop, INTERVAL_MAIN);
function mainloop() {
readDht();
readTemperatures();
setHeater();
setLight();
updateScreen();
}
The problem is that it becomes all laggy, which can be seen when updating screen. (like some pieces of code are waiting for other to finish first) This can be visible when updating screen and also by console.log (for example: if I sent main loop interval time of 4000 it will take more than 4000 to execute everything).
So what I want to know here is what is causing this? Is there a problem in my approach, is it library related? Please see other parts of code and included libraries below. (I removed some code so here are only pieces that will tell you which library is used and what are the functions doing. The missing code is nothing special and shouldn't influence the app performance)
// Libraries Used
var i2c = require('i2c-bus');
var oled = require('oled-i2c-bus');
var font = require('oled-font-5x7');
var dhtSensor = require('node-dht-sensor');
const temperatureSensor = require('ds18b20-raspi');
var rpio = require('rpio');
// Update screen function
function updateScreen() {
oled1.setCursor(1, 1);
oled1.writeString(font, 1, 'Floor Temperature:', 1, true);
oled1.setCursor(1, 20);
oled1.writeString(font, 2, floorTemperature, 200, true);
// ..... many other data, basically full screen
}
function readDht() {
dhtSensor.read(22, PIN_DHT, function(err, temp, humi) {
if (!err) {
airTemperature = temp;
humidity = humi;
} else {
log.error('Error reading DHT: ' + err);
}
});
}
Functions setHeater & setLight are very basic, just assigning pin to LOW, HIGH using rpio library.