Skip to content

Commit b98b5ee

Browse files
authored
Merge pull request #1 from dbduino-prjs/develop
RPI Basic GPIO Python
2 parents 98a4629 + 6913483 commit b98b5ee

File tree

12 files changed

+795
-0
lines changed

12 files changed

+795
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
js/node_modules/
2+
credentials.py
3+
*.pyc
4+
package*.json
5+
node-red/docker/data/node_modules/
6+
.npm
7+
.config*
8+
.flows*
9+
*.tgz
10+
*.tar.gz
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Raspberry-Pi Basic GPIOs
2+
widgets:
3+
- height: 8
4+
height_mobile: 9
5+
name: Value evolution
6+
type: Chart
7+
variables:
8+
- thing_id: Raspberry-Basic-GPIO
9+
variable_id: test_value
10+
width: 10
11+
width_mobile: 10
12+
x: 4
13+
x_mobile: 0
14+
"y": 0
15+
y_mobile: 4
16+
- height: 4
17+
height_mobile: 4
18+
name: Push Button
19+
type: Push Button
20+
variables:
21+
- thing_id: Raspberry-Basic-GPIO
22+
variable_id: button
23+
width: 4
24+
width_mobile: 4
25+
x: 18
26+
x_mobile: 0
27+
"y": 0
28+
y_mobile: 13
29+
- height: 4
30+
height_mobile: 4
31+
name: Button
32+
options:
33+
mode: True / False
34+
type: Status
35+
variables:
36+
- thing_id: Raspberry-Basic-GPIO
37+
variable_id: button
38+
width: 4
39+
width_mobile: 4
40+
x: 18
41+
x_mobile: 0
42+
"y": 4
43+
y_mobile: 17
44+
- height: 4
45+
height_mobile: 4
46+
name: LED
47+
options:
48+
mode: Both Colors
49+
type: LED
50+
variables:
51+
- thing_id: Raspberry-Basic-GPIO
52+
variable_id: led
53+
width: 4
54+
width_mobile: 4
55+
x: 0
56+
x_mobile: 4
57+
"y": 4
58+
y_mobile: 13
59+
- height: 4
60+
height_mobile: 4
61+
name: LED
62+
options:
63+
showLabels: true
64+
type: Switch
65+
variables:
66+
- thing_id: Raspberry-Basic-GPIO
67+
variable_id: led
68+
width: 4
69+
width_mobile: 4
70+
x: 0
71+
x_mobile: 0
72+
"y": 0
73+
y_mobile: 0
74+
- height: 4
75+
height_mobile: 4
76+
name: Value
77+
type: Value
78+
variables:
79+
- thing_id: Raspberry-Basic-GPIO
80+
variable_id: test_value
81+
width: 4
82+
width_mobile: 4
83+
x: 14
84+
x_mobile: 4
85+
"y": 4
86+
y_mobile: 0
87+

assets/RPI-GPIO-Basic-Dashboard.png

86 KB
Loading

assets/RPI-GPIO-Basic-Diagram.png

163 KB
Loading
30.2 KB
Loading
30.5 KB
Loading

js/gpio-basic/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
sudo apt install gpiod libgpiod2 libgpiod-dev libnode-dev
4+
npm install --save node-libgpiod node-fetch
5+
6+
Make sure that all the variables are global (chip, ledLine, buttonLine, ...). Otherwise, any timed operation will not work properly.

js/gpio-basic/gpio-basic.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const gpiod = require('node-libgpiod');
2+
const { ArduinoIoTCloud } = require('arduino-iot-js');
3+
4+
const LED = 14; // GPIO14, Pin 8
5+
const BUTTON = 15; // GPIO15, Pin 10
6+
7+
const DEVICE_ID = "09d3a634-e1ad-4927-9da0-dde663f8e5c6";
8+
const SECRET_KEY = "IXD3U1S37QPJOJXLZMP5";
9+
10+
// Make sure these variables are global. Otherwise, they will not
11+
// work properly inside the timers
12+
chip = new gpiod.Chip('gpiochip4');
13+
ledLine = chip.getLine(LED);
14+
buttonLine = chip.getLine(BUTTON);
15+
16+
ledLine.requestOutputMode("gpio-basic");
17+
// To configure the pull-up bias, use 32 instead of gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP if it is undefined
18+
buttonLine.requestInputModeFlags("gpio-basic", gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP);
19+
20+
let client;
21+
22+
// This function is executed every 1.0 seconds, polls the value
23+
// of the button and sends the data to Arduino Cloud
24+
function readButton(client) {
25+
let button = buttonLine.getValue() ? true : false;
26+
if (client)
27+
client.sendProperty("button", button);
28+
console.log("pollButton:", button);
29+
}
30+
31+
// This function is executed every 10.0 seconds, gets a random
32+
// number between 0 and 100 and sends the data to Arduino Cloud
33+
function readValue(client) {
34+
let value = Math.floor(Math.random() * 101);
35+
if (client)
36+
client.sendProperty("test_value", value);
37+
console.log("pollValue", value);
38+
}
39+
40+
// This function is executed each time the "led" variable changes
41+
function onLedChanged(led) {
42+
ledLine.setValue(led ? 1 : 0);
43+
console.log("LED change! Status is: ", led);
44+
}
45+
46+
// Create Arduino Cloud connection
47+
(async () => {
48+
try {
49+
client = await ArduinoIoTCloud.connect({
50+
deviceId: DEVICE_ID,
51+
secretKey: SECRET_KEY,
52+
onDisconnect: (message) => console.error(message),
53+
});
54+
client.onPropertyValue("led", (led) => onLedChanged(led));
55+
}
56+
catch(e) {
57+
console.error("ArduinoIoTCloud connect ERROR", e);
58+
}
59+
})();
60+
61+
// Poll Value every 10 seconds
62+
const pollValue = setInterval(() => {
63+
readValue(client);
64+
}, 10000);
65+
66+
// Poll Button every 1 seconds
67+
const pollButton = setInterval(() => {
68+
readButton(client);
69+
}, 1000);

node-red/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## The setup
2+
3+
4+
## GPIOD nodes
5+
In all the projects in this repository, we are using `libgpiod` and the different wrappers and bindings for the variuos programming languages to interact with the RPI GPIOs. There are many other projects and libraries that can be used, but this is the most portable one across all the Raspberry Pi flavours and it can also be extrapolated to any Linux-based machine.
6+
7+
In this project, we are going to use `node-red-contrib-libgpiod` which is the
8+
9+
Go to the menu "Manage Palette" --> "Install". Search the following nodes and install them.
10+
11+
@arduino/node-red-contrib-arduino-iot-cloud
12+
node-red-contrib-libgpiod
13+
14+
## Configure the access to Arduino Cloud
15+
16+
## How to install Node-RED
17+
There are many ways to install Node-RED in a Raspberry Pi or any other machine. Typically, you can use: docker, snap, apt, native node.js installation.
18+
The tricky point is that we want our Node-RED instance to have access to the GPIOs and host system. This can be achieved with all the installation methods but for most of them it is quite difficult to achieve and many conflicts and issues need to be solved. So, the easiest and recommended way would be to do a native installation using NPM.
19+
20+
These are the instructions
21+
1. Installation
22+
```
23+
sudo npm install -g --unsafe-perm node-red
24+
```
25+
2. Manage the service
26+
I recommend using pm2 to manage the service and make it start on boot
27+
```
28+
sudo npm install -g pm2
29+
pm2 start /usr/bin/node-red -- -v
30+
pm2 save
31+
sudo env PATH=$PATH:/usr/bin $(which pm2) startup systemd
32+
```
33+
34+

0 commit comments

Comments
 (0)