zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Temperature and light controller in NodeJS - sensor reading slow

Wed Jan 17, 2018 3:33 pm

Hello there!

I want to do (actually already done a part) a fun project which is making my Raspberry PI Zero W a terrarium controller for my pet snake. Since I have some programming skills in Javascript so I decided to go with NodeJS (all other programming languages are still option if this does turn out not to be the best way).

So the basics:
A friend of mine already put together all the hardware stuff (converting from 220V to required input, added 2 relays, etc). I am not much into hardware so I will be focusing on the software part. But I have to mention that everything hardware related is OK and tested.
The controller will do basic stuff:
- reading temperature from 2x DS18B20 sensors
- reading data from DHT22
- there is also an OLED SSD1306 screen for displaying data

So the idea is reading data from sensors, and depending on values, deciding if controller should turn heater on or off. Same goes for other relay which will be turning on / off the light source.

I have already written some code which works alright but found out that it is very slow. It looks like that reading from sensors takes some time so main loop is delayed. Is this related to hardware I/O PIN read limits so that raspberry is not capable of reading multiple sensors at the same time? Or should this be possible to achieve with any other programming language?
I have tested few different intervals for the loop, all from 1s to 5s. It always takes around 5-7 seconds for the interval to finish. In case the interval is too short, reading from sensors fails (it probably has no time to finish or another reading to the sensor is executed so it actually never finishes).

As I said, I am running this on Raspberry Pi Zero W. I also have PI 3 and PI 2 and I want to try running code on them to see if it is any faster, but I am still waiting for my pin stacking header to arrive.


I would really appreciate some help since I am stuck in this for some time now and really want to continue my development. I am sorry if I am asking non sense here, but I would really need some help since I am beginner.
If you need any more information just way, will provide anything. :)

Hope you can help. Regards.

Code: Select all

// So this is main loop. Called with:
var mainLoopInterval = setInterval(mainloop, INTERVAL_MAIN);   // INTERVAL_MAIN is 5000, so 5 seconds

function mainloop() {
  // READ DHT
  if (!readingDHT) {
    readingDHT = true;
    readDht().then(function(result) {
      airTemperature = result.temperature;
      humidity = result.humidity;
      readingDHT = false;
    }).catch(function(error) {
      readingDHT = false;
      console.log("ERROR READING DHT: " + error);
    });
  } else {
    console.log('ALREADY READING DHT, WAIT FOR NEXT ROUND');
  }
  
  // READ DS18B20 FLOOR TEMP SENSOR
  if (!readingTemp1) {
    readingTemp1 = true;
    readTemperature(TEMP_SENSOR_FLOOR).then(function(result) {
      readingTemp1 = false;
      floorTemperature = result;
    }).catch(function(error) {
      readingTemp1 = false;
      console.log("ERROR READING TEMPERATURE: " + error);
    });
  } else {
    console.log('ALREADY READING TEMP1, WAIT FOR NEXT ROUND');
  }
  
    // READ DS18B20 CONTROLLER TEMP SENSOR
  if (!readingTemp2) {
    readingTemp2 = true;
    readTemperature(TEMP_SENSOR_CONTROLLER).then(function(result) {
      readingTemp2 = false;
      controllerAirTemperature = result;
    }).catch(function(error) {
      readingTemp2 = false;
      console.log("ERROR READING TEMPERATURE: " + error);
    });
  } else {
    console.log('ALREADY READING TEMP2, WAIT FOR NEXT ROUND');
  }
  
  setHeater();
  setLight();
  updateScreen();
}

// READ DHT FUNCTION
function readDht() {
  return new Promise(function(resolve, reject) {
    dhtSensor.read(22, PIN_DHT, function(err, temp, humi) {
      if (!err) {
        var data = {
          temperature: temp,
          humidity: humi
        }

        return resolve(data);
      } else {
        return reject(err);
      }
    });
  });
}

// READ DS18B20 FUNCTION (also experimented here a bit. Tested if there are any differences in speed between reading value from file and directly via library
function readTemperature(SENSOR_ID) {
  return new Promise(function(resolve, reject) {

    fs.readFile('/sys/bus/w1/devices/' + SENSOR_ID + '/w1_slave', function(err, buffer) {
      if (err){
         reject(err);
      }

      var data = buffer.toString('ascii').split(" ");
      var temp  = parseFloat(data[data.length-1].split("=")[1])/1000.0;
      temp = Math.round(temp * 10) / 10;

      resolve(temp);
   });
  
  // COMMENTED THIS OUT JUST FOR TESTING PURPOSES
  /*
    temperatureSensor.readC(SENSOR_ID, 2, (err, temp) => {
      if (err) {
        return reject(err);
      } else {
        return resolve(temp);
      }
    });
    */
   )};
}

// SET LIGHT FUNCTION
function setLight() {
  let currentTime = new Date();
  
  if (currentTime >= sunrise && currentTime <= sunset) {
    if (!lightOn) {
      rpio.write(PIN_LIGHT, rpio.HIGH);
    }

    lightOn = true;
  } else {
    if (lightOn) {
      rpio.write(PIN_LIGHT, rpio.LOW);
    }

    lightOn = false;
  }
}

// SET HEATER FUNCTION
function setHeater() {
  if (floorTemperature != null) {
    if (floorTemperature < minTemp) {
      if (!heaterOn) {
        rpio.write(PIN_HEATER, rpio.HIGH);
      }

      heaterOn = true;
    } else if (floorTemperature >= maxTemp) {
      if (heaterOn) {
        rpio.write(PIN_HEATER, rpio.LOW);
      }

      heaterOn = false;
    }
  } else {
    if (heaterOn) {
      rpio.write(PIN_HEATER, rpio.LOW);
      heaterOn = false;
    }
  }
}

// UPDATE SCREEN FUNCTION
function updateScreen() {
  if (updateScreenFinished) {
    updateScreenFinished = false;
    oled1.clearDisplay();
    oled1.setCursor(1, 1);
    oled1.writeString(font, 1, 'FLOOR TEMP:', 1, true);

    oled1.setCursor(1, 12);
    if (floorTemperature !== null) {
      oled1.writeString(font, 2, floorTemperature.toFixed(1) + ' C', 200, true);
    } else {
      oled1.writeString(font, 2, 'Error', 200, true);
    }

    oled1.setCursor(1, 31);
    oled1.writeString(font, 1, 'CONTROLLER:', 1, true);

    oled1.setCursor(90, 31);
    if (controllerAirTemperature !== null) {
      oled1.writeString(font, 1, controllerAirTemperature.toFixed(1) + 'C', 200, true);
    } else {
      oled1.writeString(font, 1, 'Error', 200, true);
    }

    oled1.setCursor(1, 43);
    oled1.writeString(font, 1, 'AIR TEMP:', 1, true);

    oled1.setCursor(90, 43);
    if (airTemperature !== null) {
      oled1.writeString(font, 1, airTemperature.toFixed(1) + 'C', 200, true);
    } else {
      oled1.writeString(font, 1, 'Error', 200, true);
    }

    oled1.setCursor(1, 55);
    oled1.writeString(font, 1, 'HUMIDITY:', 1, true);

    oled1.setCursor(90, 55);
    if (humidity !== null) {
      oled1.writeString(font, 1, humidity.toFixed(1) + '%', 200, true);
    } else {
      oled1.writeString(font, 1, 'Error', 200, true);
    }

    if (heaterOn) {
      oled1.setCursor(117, 1);
      oled1.writeString(font, 2, 'H', 1, true);
    }

    if (lightOn) {
      oled1.setCursor(97, 1);
      oled1.writeString(font, 2, 'L', 1, true);
    }

    updateScreenFinished = true;
  } else {
    console.warn('Not updating screen, still in progress.: ' + new Date());
  } 
}
EDIT: Link to my main.js file https://pastebin.com/2iAg8ASF
Last edited by zassx on Thu Jan 18, 2018 8:59 am, edited 1 time in total.

Heater
Posts: 8633
Joined: Tue Jul 17, 2012 3:02 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Wed Jan 17, 2018 11:28 pm

There is no way we are going to read and try to understand 400 lines of code and try to figure out what it is supposed to do and figure out what it actually does and figure out where there may be a problem.

Your best bet here is to pull out some teeny tiny piece of that code that shows the delay problem that you speak about. 10 lines or so. Perhaps reading just one sensor and printing the results.

Then you can ask a meaningful question about that and perhaps someone may take the time to look at 10 lines of code and perhaps they will quickly come up with a suggestion to fix it.

Of course you may have many such problems in there. Take them one at a time.

JS under node.js is not slow. So something is up.

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Thu Jan 18, 2018 9:08 am

I have stripped out the pieces of code that should be causing a problem and have updated my question.
There must be something in the main loop, some of the functions for reading sensors or updating the screen. I tested everything separately and it works well, there is some delay but should be ok. But when I try to do everything at once (reading all the sensors and updating screen), it somehow creates delay so main loop execution time gets longer. And if interval time is set for lets say 2s or less, sensors don't even read data, like read request couldn't enter execution queue and main loop would always overtake it.

Look I am not expecting to check through my code and point out: you did wrong that in row 75. My question here is simple and if someone has any suggestions I am more than happy with it:
The code I am executing is async, but somehow sensor read requests take some time and block my main thread. Is it possible that Raspberry can't handle so many operations at once?

Heater
Posts: 8633
Joined: Tue Jul 17, 2012 3:02 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Fri Jan 19, 2018 1:20 pm

Personally I would:

1) Get rid of that main loop idea.

Main loops are an idea from old fashioned languages like C, Pascal, Python, etc. Javascript is built for event driven programing.

Rather I would read each sensor in it's own "loop" driven by the events it generates. Example below:

2) Get rid of all that promises stuff. They don't do anything for you here and just make the code longer and harder to understand.

3) Similarly drive the screen updates and other outputs from their own timers.

Example:

Code: Select all

function readDht() {
    dhtSensor.read(22, PIN_DHT, function(err, temp, humi) {
        if (err) {
            return console.log("Error: readDht", err);
        }
        airTemperature = temp;
        humidity = humi;
        setTimeout(readGht, 1000);
    });
}

function readTemperature(SENSOR_ID) {
    fs.readFile('/sys/bus/w1/devices/' + SENSOR_ID + '/w1_slave', function(err, buffer) {
        if (err) {
            return console.log("Error: readTemperature", err);
        }
        var data = buffer.toString('ascii').split(" ");
        var temp  = parseFloat(data[data.length-1].split("=")[1])/1000.0;
        floorTemperature = Math.round(temp * 10) / 10;
        setTimeout(readTemperature, 1000, SENSOR_ID);
    });
}

// Kick of the sensor readers.
readDht();
readTemperature(1);
readTemperature(2);
Not tested code. Just an idea of how I would approach the problem. Add proper error handling and data validation to your taste.

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Fri Jan 19, 2018 2:09 pm

Thank you very much for ideas, will definitely try it out later!
Do you think it is possible to achieve almost realtime reading (or at least as close as possible) or are there any limitations that would prevent that? Or at least for screen updates and button inputs, so that there is no delay when pressing a button because some other stuff is executing at the moment.

Thanks again, this are the tips that I needed.

Heater
Posts: 8633
Joined: Tue Jul 17, 2012 3:02 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Fri Jan 19, 2018 3:04 pm

I have no idea what sensors you are reading but I would expect to be able to read sensors in a matter of milliseconds.

I suspect the slowest part of your whole program is the screen update. You could should be able to time the execution of that.

The important thing in a JS program is not to hang up the event loop with any code that takes a long time to execute. If you do that everything else waiting on timers or other events will be stalled. Your screen update code is all synchronous as far as I can tell so perhaps that is a hold up. You could try breaking that up and update parts of the display at a time.

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Fri Jan 19, 2018 3:51 pm

Thank you again. It will be easier now that I have some guidelines to follow.
I am guessing too (didn't have really time lately to properly debug everything) that updating screen is the issue. Will post back result after I update code.

User avatar
joan
Posts: 12982
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Temperature and light controller in NodeJS - sensor reading slow

Fri Jan 19, 2018 5:00 pm

What functions are causing the delays? You must be able to add timers to see where the delays are happening.

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Sat Jan 20, 2018 1:56 am

Okay, just updated my code and tested only reading DHT and DS18B20.
The interval for DHT is 1 second, DS 3 seconds. From the output I can see, that the code is not running like it is supposed to and is interrupting one another. JS code below log.

Code: Select all

0|main     | READ DS: Sat Jan 20 2018 02:51:27 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:28 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:30 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:32 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:32 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:35 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:35 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:39 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:40 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:41 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:43 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:44 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:46 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:47 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:50 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:51 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:53 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:54 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:51:56 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:51:57 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:52:00 GMT+0100 (CET)
0|main     | READ DHT: Sat Jan 20 2018 02:52:01 GMT+0100 (CET)
0|main     | READ DS: Sat Jan 20 2018 02:52:03 GMT+0100 (CET)

Code: Select all

readDHT();
readDS18B20(Constants.SENSOR_ID.FLOOR, floorTemperature, 1000);

function readDHT() {
  if (!readingDHT) {
    readingDHT = true;
    console.info('READ DHT: ' + new Date());
    dhtSensor.read(22, PIN_DHT, function(err, temp, humi) {
      if (!err) {
        var data = {
          temperature: temp,
          humidity: humi
        }
      } else {
        console.error('Couldn\'t read DHT sensor: ' + err);
      }
      readingDHT = false;
      setTimeout(readDHT, 3000);
    });
  }
}

function readDS18B20(SENSOR_ID, temperatureVar, delay) {
  console.info('READ DS: ' + new Date());
  temperatureSensor.readC(SENSOR_ID, 2, (err, temp) => {
    if (err) {
      console.error('Couldn\'t read DS18B20 sensor: ' + err);
    } else {
      temperatureVar = temp;
    }

    setTimeout(readDS18B20, delay, SENSOR_ID, temperatureVar, delay);
  });
}

Heater
Posts: 8633
Joined: Tue Jul 17, 2012 3:02 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Sat Jan 20, 2018 9:37 am

I don't understand what you mean by "the code is not running like it is supposed to and is interrupting one another".

Looks to me like it is running exactly as planned. There are two timeouts going on here. When a timeout arrives the appropriate function is run. So of course they are interleaved in time. As your output shows.

Why do you have that readingDHT thing in there?

Also having that "var data = {..." in there means your reader has no output !

I think you could even get rid of the setTimeout if you want it to run as fast as possible:

Code: Select all

let data = {
    temperature: temp,
    humidity: humi
};

function readDHT() {
    console.info('READ DHT: ' + new Date());
    dhtSensor.read(22, PIN_DHT, function(err, temp, humi) {
        if (!err) {
            data.temperature = temp;
            data.humidity = humi;
        } else {
            console.error('Couldn\'t read DHT sensor: ' + err);
        }
        readDHT();
    });
}

readDHT();

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Sun Jan 21, 2018 11:10 am

The data object in above example isn't relevant, the important output is just time. Also readingDHT is a flag that indicates reading data from DHT sensor. I tested reading only DHT on very short interval and it got flooded - sensor was trying to read but didn't return any data. That is why I added flag so I don't read twice in case it is already reading.


Ok, I added another sensor to the test so you can see the problem here.
The code is supposed to be async and call every X. Yes, setInterval won't be exact every time, but we shouldn't see big difference in times, right?

Here are delays: I am calling one DHT sensor and two DS18B20 sensors.
DHT Delay - 1s
DS1 Delay - 1s
DS2 Delay - 1.5s

Code: Select all

0|main     | READ DHT: Sun Jan 21 2018 11:55:24 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:24 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:24 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:30 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:31 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:32 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:33 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:35 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:36 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:36 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:37 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:37 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:38 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:39 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:40 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:40 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:41 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:42 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:44 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:45 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:46 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:46 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:47 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:48 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:49 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:49 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:51 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:52 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:53 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:53 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:54 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:55 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:56 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:55:56 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:55:58 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:55:59 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:00 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:00 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:01 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:02 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:03 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:04 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:04 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:05 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:06 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:06 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:08 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:09 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:10 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:10 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:11 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:12 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:13 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:14 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:14 GMT+0100 (CET)

It isn't always like this but here you can see that DHT is skipping sometimes when DS is reading.
DHT should be executed every second, check the times of READ DHT below:

Code: Select all

0|main     | READ DHT: Sun Jan 21 2018 11:56:05 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:06 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:06 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:08 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:09 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:10 GMT+0100 (CET)
0|main     | READ DS (1000): Sun Jan 21 2018 11:56:10 GMT+0100 (CET)
0|main     | READ DS (1500): Sun Jan 21 2018 11:56:11 GMT+0100 (CET)
0|main     | READ DHT: Sun Jan 21 2018 11:56:12 GMT+0100 (CET)

Heater
Posts: 8633
Joined: Tue Jul 17, 2012 3:02 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Sun Jan 21, 2018 12:08 pm

You don't need a flag to prevent reading the sensor twice. Just get your program logic right. If you don't re-call the sensor reading function until the sensor has read the data, as per my example, then you cannot end up reading the sensor twice at the same time.

Does your sensor reading work with consistent timing when you only read one sensor in your program? Have you tried that for all your sensors?

I don't know about your sensors but if they are all hanging of the same bus, I2C, SPI, whatever, then maybe when you try to read two of them they trip over each other. You might need to make sure you don't read a second sensor until the first one has replied.

zassx
Posts: 17
Joined: Mon Jan 15, 2018 4:37 pm

Re: Temperature and light controller in NodeJS - sensor reading slow

Sun Jan 21, 2018 5:04 pm

Yes I have tried reading sensors separately and it works ok. Well first few reads aren't consistent but later on the interval looks good.

Oh that could be the problem. Sorry I am new to this so I don't know exactly what are they using. For display I know it is I2C, probes are 1wire set to certain PIN number from config file. The only sensor left is DHT, for which I didn't configure anything. Just using library for it and it's working.

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 5 guests