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() {


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.

closed as too broad by joan, techraf, Steve Robillard, Jacobm001 Jan 19 '18 at 2:17

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You need to instrument your code and time the sections to find the delay. – joan Jan 12 '18 at 17:38
  • It is probably related to your script and implementation. I run a data logger using shell script for reading ds18b20 and write it file without much delay. – hcheung Jan 13 '18 at 14:14
  • @hcheung Huh.. I am reading 2 DS18B20 and DHT + updating OLEd display. After debugging I can see that reading from sensors cause delay to interval. Is there any way read from sensors code could be running "in the background" so it doesn't block the main interval? – ZassX Jan 13 '18 at 19:56
  • 1
    I come from the old school of Remote Sensing and Telemetry than today's fancy rebranded IoT world, there is no point of constantly reading the sensors (which are 1000+ slower if not more compare to any modern processors). for me Data Acquisition is always a different process from Data Processing, all you need is for the Data Acquisition part to run at a scheduled interval and when it is done, it 'raise the hand' to signal the Data Processing to pick it up. NodeJS is good to handle async works like this. – hcheung Jan 14 '18 at 0:34