Nodered I2C - Raspberry Pi 4 - PM2.5 sensor

Hello everyone,

I launched nodered by means of docker compose up. I connected an air quality sensor to the Raspberry. I installed the i2c packge on Nodered and I connected a timestamp module to the i2c scan node with a debug one at the end. When I click on the timestamp button I find empty into the debug menu. I already checked with the command i2cdetect and these are the results for the i2c channel one.

Can anybody help me telling me why I am not seeing sensor's data on nodered? Thanks
ps. I also tried the chmod 666 /dev/i2c-1, but nothing...

It is probably because node-red running inside a docker container does not, by default, have access to the hardware.

Unless you have a good reason to run in Docker then don't do that. It just makes life complicated. Use the pi install script to install it natively.

that's definitely the issue, apparently you'll need to do something like:

docker run -t -i --device=/dev/i2c-1 ...

to allow the docker image to access the device.

Thanks for the answer.

I will try, but is that a command that I have to use every time that I start the raspberry or not?

Then, is that a command that I have to use in a specific folder, or not?

Thanks

The command you use to start node red in a docker image has to change. If you're using a docker compose file to start node red, then you'll have to make the change there.

It's all about giving docker access to the device and that has to be done using docker.

It's hard to say what exactly has to change since I don't know anything about your setup and how you started node red inside docker.

1 Like

Thanks for the answer.

I used docker compose to run nodered by means of a .yaml file. I always run three containers (one for nodered , one for influxdb and one for grafana).

I think that I have to modify something into the .yaml file, but I don't know what..

Thanks

From your description you're using a docker-compose.yaml, then checkout this post - you'll need to add the following:

nodered:
  image: ...nodered...
  devices:
    - /dev/ttyS0:/dev/ttyUSB0
    - /dev/ttyS1:/dev/ttyUSB1
    - /dev/ttyS2:/dev/ttyUSB2
    - /dev/ttyS3:/dev/ttyUSB3

modifying the device names accordingly

To reinforce what Colin has already said. Docker adds a significant layer of complexity. Those 3 services are easily directly installed and managed without the need for Docker.

If you are going to use Docker, you will need to do some learning or how to use it. You are already using docker-compose I think which is probably be best approach for a small-ish docker installation. But you do need to learn about the restrictions it brings and how to get around them.

Thanks for the answer.

So in my case, that I have not got USB connections for devices but I2C, have I to put into my .yaml file this?

nodered:
image: ...nodered...
devices:
- /dev/ttyS0:/dev/ttyi2c-1
- /dev/ttyS1:/dev/ttyi2c-20
- /dev/ttyS2:/dev/ttyi2c-21

Thanks

Yes sure, where can I learn more about it?

There are loads of tutorials on the web and there is, of course, decent documentation from Docker.

And, there is always ChatGPT and similar AI tools which may be even more useful, especially if you prefer to explore for yourself.

1 Like

Hello, I have tried to modify the yaml file according to my last message but when I try to launch all the containers with the compose up I receive an error from the nodered container: error gathering device information while adding custom device "/dev/ttyS0": no such file or directory.

Can you help me? Thanks

Those a SERIAL PORTS. I'm assuming that, since your sensor is an I2C device, it is either connected direct to the Pi's GPIO. Therefore not serial.

Personally, I have no idea how to allow a Pi's GPIO to be accessed by Docker as I never use a Pi's GPIO, I always use either an Arduino (connected via USB-Serial) or an ESP (connected via Wi-Fi).

try:

  • /dev/ttyi2c-1:/dev/ttyi2c-1

instead

Perhaps also read the manual on docker compose.

If you are going to use docker compose, then understanding its workings might well be an advantage!

Can you explain why you need to use Docker, when it is causing such problems?

Thanks for the answer. I set .yaml file as suggested by gregorius and now I managed to communicate with the sensor, there are still some problems of data unstability (data changes greatly and quickly) but I think that this problem is not linked to node red or docker. Anyway I am using docker compose because with one command I launch all my containers with all applications, it is more suitable, then the enviroment is divided in single containers and that could be safer, just this..

Another problem is that when I turn off the raspberry and turn on again, the permission for the i2c are not more granted even if I modified the yaml file and the communication is not more granted.. strange

is the device connected? the device permissions are set if the device is connected and otherwise the entry in the /dev directory might not even exist.

The only thing that I changed was to supply the sensor with 3.3 V instead of 5V... Now I put again 5 V and now I can't communicate with the sensor again... wow

the yaml file is always the same...

hm - my lightbulbs also don't work when I use 120v instead of 240v! :wink:

Try it in a native install of node-red rather than docker to check whether the problem is docker related.

1 Like