Expert needed RasPi project I2C, GPIO issues

Hi dear friends, thank you for your kind attention and support.
I'm involved into a project-domotic system, for this project I developer an hardware board connected to I2C bus and some buttons to GPIO, since now I tried to merge RaspiArduino with the Raspbian but I had some difficulties:

  1. when RaspiArduino uses I2C bus it looks it keep it busy, so Raspbian can't for example communicate with RTC more. Unfortunately I didn't find a way to release the bus for a while, for example with command "wire.end();", but seems not.
  2. I was thinking to develope all automation with RaspiArduino cause I know enough Arduino IDE and its language, puls take advance of libraries there, start the result .bin file as service and communicate with it by mqtt. But the mqtt working library I found sometimes loose packets, not reliable.

For these reasons I'm loooking for a platform like Node Red where I can directly drive I2C, in this case I hope:
a) I will be sure that system can communicate anytime with RTC and eve if power goes off and internet is disconnected the clock will keep the right time
b) I won't need mqtt and I can manage the hardware from Nore Red. Is it possible?

c) Is is possible to write automation scripts inside Node Red to manage the hardware? In which language?
d) does anyhow supports arfuino libraries to manage the I2C hardware?

Thank you a lot for your kind help!

Can someone please give me any suggestions?
Thank you

Did you check on flows.nodered.org ? - there is this set of nodes -
https://flows.nodered.org/node/node-red-contrib-i2c
that may help. (I've not used them though so can't comment further). - or the node-red-contrib-gpio nodes also do some i2c also.

Node-RED uses javascript under to add things in the function node - but you can do lot with the pre-built nodes without needing to write code.

1 Like

Thank you mate for your kind answer, I'm a bit disoriented because I'm new here, I don't know javascript but I can learn it.. somehow..

If you may suggest me the best solution, the situation is this:

  1. I have several I2C hardware connected in the bus, RTC, ADC, 2x16 bit I/O, LCD display (4x20char), 2x16 PWM boards (I use them for switching on/off relay). As you see it's not a few, anyway I've already sperimented them and I can initiate them/program and get values sending proper byte datas, without the need of libraries. I hope all can be possible so, but I don't understand the difference between synchronous and asynchronous usage of I2C bus..
  2. I have 4 buttons on the GPIO (it's not I2C, they are hardware mapped), I hope there is a way to access them from Nodered
  3. About RTC, I hope I can configure it to the OS directly, and that the OS can obtain NTP from internet and synchronize NTP itself, and I don't have to worry about clock more, simply read it. I also hope that using the I2C OS driver there won't be conflict with other I2C devices, I mean, the OS can soon or later access RTC if in that moment the I2C was busy. Using RaspiArduino thr I2C will always result busy, and the system can't never access RTC more (yes I merged it into OS following guides/instructions).
  4. I suppose I've to create a module for Nodered where I'll write all my HW commands and some automation, to exchange data with Nodered core should I use mqtt?

Thank you a lot!

Looks like the two libraries that allow running Arduino sketches in a Raspberry (Raspiduino and Piduino) are not being maintained and apparently, both of them have issues to handle I2C. I see people opening issues in Github to get no response at all. I guess it is a wise move for you to move away from those libraries to Node-RED.

As for you more recent post: Perhaps it is better that you explain better what you mean by "synchronous and asynchronous usage of I2C bus". I2C in inherently synchronous.

In regards to point #2, I could refer you to this nice video on youtube that shows the usage of push buttons in Node-RED (on Raspberry). Probably this video will answer also part of your questions from point #4.

1 Like

Thank you really a lot for your kind support! I'm going now to see the video,
Answering you I checked: https://flows.nodered.org/node/node-red-contrib-i2c and found that fivdi/i2c-bus
is betterm then in his readme I saw the two ways to manage comunication to/from the bus:
https://github.com/fivdi/i2c-bus/commit/65cd309314601b14ee284ff79b6deef52a264060

I understand now. You were referring to async Javascript programming of course (not to the physical layer and clock line of I2C).

Probably you will want to use the pre-built node that Dave pointed out ( node-red-contrib-i2c ). It already uses, as a dependency, the I2C-bus package from fivdi.

I installed package, I hope all went fine, I had some warnings:

SOLINK_MODULE(target) Release/obj.target/i2c.node
COPY Release/i2c.node
make: Leaving directory '/home/pi/node_modules/i2c-bus/build'
npm WARN saveError ENOENT: no such file or directory, open '/home/pi/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/pi/package.json'
npm WARN pi No description
npm WARN pi No repository field.
npm WARN pi No README data
npm WARN pi No license field.

  • node-red-contrib-i2c@0.5.2
    added 4 packages from 10 contributors and audited 4 packages in 24.238s
    found 0 vulnerabilities

Can you kindly explain me the difference between synchronous and asynchronous mode? Maybe code stucks in synchronous awaing some answer/info from I2C device?
Thank you a lot

It looks as if you were in the wrong directory when you installed it. As per the nodes ReadMe (and with Node-RED in general) if you want to install a node using the command line you need to be in your .node-red directory before you run the npm command. Alternatively you can now install using the Manage Palette option in the Node-RED menu

1 Like

ok I didn't know it, I couldn't see I2c nodes, then following some instruction I run
sudo apt-get install npm
sudo npm install -g npm

I don't know, I tried several, anway now it works and I start operationg already with I2C bus, let's see )

I?m trying now to install an lcd i2c library, same errors, I adviced I don't have the folder .node-red under /home/pi and I don't understand why.. maybe the new version 10 feature?
I've only folder /node_modules
nore red executes correctly, where it has its files?
Mistery here..
UPDATE:
I tried install this: https://flows.nodered.org/node/node-red-contrib-i2c-lcd-adafruit-sainsmart
with the command: npm install node-red-contrib-i2c-lcd-adafruit-sainsmart
on the folder /home/pi I have long output, probably some errors, on folder /home/pi/node_modules I have this:
npm install -g npm
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
/usr/bin/npx -> /usr/lib/node_modules/npm/bin/npx-cli.js

  • npm@6.5.0
    updated 1 package in 44.986s

I can't see it, even if I execute sudo npm install -g npm

I need help here, now I tried to install another module.. but it fails. Seems it wants to install to the same folder as previous one.. (why?) and don't have rights..
npm install lcdi2c
npm WARN checkPermissions Missing write access to /home/pi/node_modules/node-red-contrib-i2c-lcd-adafruit-sainsmart
npm WARN enoent ENOENT: no such file or directory, open '/home/pi/package.json'
npm WARN pi No description
npm WARN pi No repository field.
npm WARN pi No README data
npm WARN pi No license field.

npm ERR! path /home/pi/node_modules/node-red-contrib-i2c-lcd-adafruit-sainsmart
npm ERR! code ELOOP
npm ERR! errno -40
npm ERR! syscall access
npm ERR! ELOOP: too many symbolic links encountered, access '/home/pi/node_modules/node-red-contrib-i2c-lcd-adafruit-sainsmart'

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-01-09T18_44_35_305Z-debug.log
root@FrancoPi:/home/pi/node_modules#

So hard here for me to understand all these dificulties..

don’t use sudo if the suggestion is not to
don’t use -g if the suggestion is not to

Both are likely to cause you more problems later.

As per both the readme if you want to install using the command line the cd into your .node-red directory (notice the dot) and then run the command

Alternatively after reading the ReadMe if there isn’t any dependencies you need to is tall use the option in Node-RED which will ensure you install it in the right place with the right options.

If you haven’t got a .node-red directory have you started node-red (it gets created on the first run)

Your .node-red directory is also listed in the messages when you start Node-RED

1 Like

ok I got my mistake, I installed with root rights, it installed into /root/.node-red
I accessed that folder and run npm install lcdi2c, output seems ok but can't see node too.
Tried npm install -g npm, nothing helped
now the packages are in /node_modules and .node-red in root, probsbly too messy? How can I solve this? should I format all and reinstall Raspian?
I also can't remove /node_modules/node-red-contrib-i2c even if I force the permissions.
Thank you

Hi Giovanni, this is a simple question that may generate a thousand different replies in a forum. The reason is that the terms "synchronous and asynchronous" are used with many different meanings. I will share the way i reason about them when it comes to Javascript programming with Node.JS for network communications. Probably you are asking this question because you read somewhere or perhaps someone told you, that you should use asynchronous programming for network relating code. This is indeed the case. Our code in JavaScript runs in a single thread and we can easily block the execution of the program if we execute synchronous code for I/O (filesystem or network). JavaScript and Node.JS have mechanisms to allows us to efficiently handle I/O. This is why you hear the expression "asynchronous functions" (callbacks, promises, and others) in JavaScript. This is one of the mechanisms to avoid blocking the execution. You will be using a pre-built node for handling I2C. This library uses asynchronous functions so you are on the safe side.

1 Like

To install node-RED on a pi you should follow the instruction in the docs, which will do everything for you.
https://nodered.org/docs/hardware/raspberrypi
You can try that now and if it succeeds it may well be ok, but having installed stuff in various wrong places and sometimes with root permissions you may find yourself in a mess later. If you have nothing else on the card you might be better to start again.
Then never use sudo or -g with npm or node red unless specifically advised to do that.

1 Like

Sorry mates, I followed your instructions but I still have problems here :((

  1. Flashed again the last Raspian, set I2C bus, RTC and so on, all ok
  2. exit from root and executed bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
  3. after install I entered into cd /home/pi/.node-red and I executed: npm install node-red-contrib-i2c, then npm install lcdi2c (everything without root!)
  4. node-red-start but I can't see I2C bus on nodes and not LCD too.. seems package installed correctly but.. don't see them
  5. executed npm install, didn't help

What can I do? Thank you

I try to figure out myself trying everything, but it's hard.. finally after these 2 commands I got work I2C nodes (but not the LCD node!), probably after lunching sudo apt-get install npm, it happened also before with the dirty installation.
Is this a bug?
here list of my last commands:

sudo npm install -g grunt-cli
/usr/bin/grunt -> /usr/lib/node_modules/grunt-cli/bin/grunt

  • grunt-cli@1.3.2
    updated 1 package in 12.82s
    pi@raspberrypi:~ $ grunt build
    grunt-cli: The grunt command line interface (v1.3.2)

Fatal error: Unable to find local grunt.

If you're seeing this message, grunt hasn't been installed locally to
your project. For more information about installing and configuring grunt,
please see the Getting Started guide:

https://gruntjs.com/getting-started
pi@raspberrypi:~ $ node red
internal/modules/cjs/loader.js:583
throw err;
^

Error: Cannot find module '/home/pi/red'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

pi@raspberrypi:~ $ sudo apt-get install npm
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
npm : Depends: nodejs but it is not going to be installed
Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
Depends: node-ansi-color-table but it is not going to be installed
Depends: node-archy but it is not going to be installed
Depends: node-block-stream but it is not going to be installed
Depends: node-fstream (>= 0.1.22) but it is not going to be installed
Depends: node-fstream-ignore but it is not going to be installed
Depends: node-github-url-from-git but it is not going to be installed
Depends: node-glob (>= 3.1.21) but it is not going to be installed
Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
Depends: node-inherits but it is not going to be installed
Depends: node-ini (>= 1.1.0) but it is not going to be installed
Depends: node-lockfile but it is not going to be installed
Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
Depends: node-gyp (>= 0.10.9) but it is not going to be installed
Depends: node-nopt (>= 3.0.1) but it is not going to be installed
Depends: node-npmlog but it is not going to be installed
Depends: node-once but it is not going to be installed
Depends: node-osenv but it is not going to be installed
Depends: node-read but it is not going to be installed
Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
Depends: node-request (>= 2.25.0) but it is not going to be installed
Depends: node-retry but it is not going to be installed
Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
Depends: node-semver (>= 2.1.0) but it is not going to be installed
Depends: node-sha but it is not going to be installed
Depends: node-slide but it is not going to be installed
Depends: node-tar (>= 0.1.18) but it is not going to be installed
Depends: node-underscore but it is not going to be installed
Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

OK I solved it installing the LCD node from the web interface,
now I have another problem, I?m a newbie here and the guy who wrote it guessed I can understand all:

I try to send by iniect node this:
msg.payload = {
msgs: [
{
msg: "string",
pos: 1,
center: "0"
},
{
msg: "string",
pos: 1,
center: "1"
},
{
msg: "string",
pos: 1,
center: "1"
},
{
msg: "string",
pos: 1,
center: "0"
}
]
};
I tried string, global, JSON, etc. Nothing on display (it works because it appeared SYSTEM START UP) of my messages. I tried also to short it somehow, what I should send to LCD node? Thank you

ANDREI!!
I hope you read me, I can't post more reply for my first day, I reached 10. Thank s for answer,
Thank you a lot for your help! I hope you still here and clear me, exactly what I have to do?
Add function as in picture and paste inside this?:
msg.payload = {\nmsgs: [\n{\nmsg: "string",\npos: 1,\ncenter: "0"\n},\n{\nmsg: "string",\npos: 1,\ncenter: "1"\n},\n{\nmsg: "string",\npos: 1,\ncenter: "1"\n},\n{\nmsg: "string",\npos: 1,\ncenter: "0"\n}\n]\n};\nreturn msg

I can't make it work..
Wow so complicated here.. (

OK DONE ))
I removed all the \ and \n, I don't know why the formatted string became that. WORKS thanks a lot my friend!!

1 Like

Nice to know that your overcomed the installation issues.

There are many different ways to inject the payload you want. One of them is using a function node, so you can just copy and paste. I tested here and your testing payload looks good. If you know how to import flows you can reuse from below. It is good that you add a debug node in the output of the function node as you can see the message in the debug panel.

Edit for clarity:

I changed the picture to show where you are supposed to connect your LCD node. I don´t have such LCD node in my system so I used instead a debug node, just to illustrate the placement of the node.

The LCD node will receive an object like the one below, coming from the function node. If the format of the message is correct then you will see the text displayed (or some error message perhaps).

[{"id":"e6db2524.7ff938","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"6f0e15f4.84b1fc","type":"inject","z":"e6db2524.7ff938","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":160,"wires":[["5e67519.601bfb"]]},{"id":"5e67519.601bfb","type":"function","z":"e6db2524.7ff938","name":"Create payload","func":"msg.payload = {\nmsgs: [\n{\nmsg: \"string\",\npos: 1,\ncenter: \"0\"\n},\n{\nmsg: \"string\",\npos: 1,\ncenter: \"1\"\n},\n{\nmsg: \"string\",\npos: 1,\ncenter: \"1\"\n},\n{\nmsg: \"string\",\npos: 1,\ncenter: \"0\"\n}\n]\n};\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":160,"wires":[["b7751dbb.831b2"]]},{"id":"b7751dbb.831b2","type":"debug","z":"e6db2524.7ff938","name":"Debug1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":540,"y":160,"wires":[]}]

1 Like