gcgarner / IOTstack

docker stack for getting started on IOT on the Raspberry PI
GNU General Public License v3.0
1.5k stars 578 forks source link

i2c in nodered #250

Closed e-abdi closed 1 year ago

e-abdi commented 1 year ago

Hi guys, any ideas how to enable i2c for nodered container? I have the i2c enabled on the Raspberry itself but I can't seem to be able to access it inside nodered.

Paraphraser commented 1 year ago

Please read #194 because this repo (gcgarner) is dormant. That issue explains what you should do to migrate to the active repo (SensorsIot).


I have never tried to use any of the GPIO pins on a Raspberry Pi for an actual project so I can only suggest things to try.

I tackle I2C problems with ESP32 or ESP8266 development boards. There are good libraries for both I2C and MQTT (for the command-and-control comms), and plenty of examples to follow.

You haven't said anything about your project so I'm a bit in the dark. There's a node-red-contrib-i2c add-on node which might do the trick. It seems to want devices in the form /dev/i2c-n where n is in the range 0…7.

My Raspberry Pi 4 appears to have I2C devices at /dev/i2c-20 and -21. If you're also using a Pi4, you could try mapping those into the container by adding these lines to the nodered service definition:

    devices:
      - "/dev/i2c-20:/dev/i2c-1"
      - "/dev/i2c-21:/dev/i2c-2"

If you're using something like a Zero2W, it seems to have a different set of I2C devices:

/dev/i2c-0  /dev/i2c-10  /dev/i2c-11  /dev/i2c-2

I have no idea how any of those devices (either the Pi4 or the Zero2W) map to pins so you'll have to figure out that part for yourself.

Hope this helps.

e-abdi commented 1 year ago

@Paraphraser thank you so much for the response! That worked! Sorry for not mentioning anything about my project. This is a for my campervan which has IOTstack running on a raspberry and a bunch of other ESP32 and 8266 in various spots to control different relays/sensors. I just had one of these raspberry relay hats (https://wiki.52pi.com/index.php/EP-0099) laying around so I thought I use it instead of adding another esp8266. I am using the node-red-contrib-i2c add-on as you mentioned. Just needed to map the i2c devices to the container as suggested, then used the "i2c scan" node to find the address and "i2c out" node to control the relays.