Skip to content

RPI Basic GPIO Python #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
js/node_modules/
credentials.py
*.pyc
package*.json
node-red/docker/data/node_modules/
.npm
.config*
.flows*
*.tgz
*.tar.gz
87 changes: 87 additions & 0 deletions arduino-cloud/rpi-gpio-basic-dashboard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Raspberry-Pi Basic GPIOs
widgets:
- height: 8
height_mobile: 9
name: Value evolution
type: Chart
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: test_value
width: 10
width_mobile: 10
x: 4
x_mobile: 0
"y": 0
y_mobile: 4
- height: 4
height_mobile: 4
name: Push Button
type: Push Button
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: button
width: 4
width_mobile: 4
x: 18
x_mobile: 0
"y": 0
y_mobile: 13
- height: 4
height_mobile: 4
name: Button
options:
mode: True / False
type: Status
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: button
width: 4
width_mobile: 4
x: 18
x_mobile: 0
"y": 4
y_mobile: 17
- height: 4
height_mobile: 4
name: LED
options:
mode: Both Colors
type: LED
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: led
width: 4
width_mobile: 4
x: 0
x_mobile: 4
"y": 4
y_mobile: 13
- height: 4
height_mobile: 4
name: LED
options:
showLabels: true
type: Switch
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: led
width: 4
width_mobile: 4
x: 0
x_mobile: 0
"y": 0
y_mobile: 0
- height: 4
height_mobile: 4
name: Value
type: Value
variables:
- thing_id: Raspberry-Basic-GPIO
variable_id: test_value
width: 4
width_mobile: 4
x: 14
x_mobile: 4
"y": 4
y_mobile: 0

Binary file added assets/RPI-GPIO-Basic-Dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/RPI-GPIO-Basic-Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/RPI-GPIO-Basic-Thing_Variables.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/RPI-GPIO-Basic-Thing_Variables2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions js/gpio-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


sudo apt install gpiod libgpiod2 libgpiod-dev libnode-dev
npm install --save node-libgpiod node-fetch

Make sure that all the variables are global (chip, ledLine, buttonLine, ...). Otherwise, any timed operation will not work properly.
69 changes: 69 additions & 0 deletions js/gpio-basic/gpio-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const gpiod = require('node-libgpiod');
const { ArduinoIoTCloud } = require('arduino-iot-js');

const LED = 14; // GPIO14, Pin 8
const BUTTON = 15; // GPIO15, Pin 10

const DEVICE_ID = "09d3a634-e1ad-4927-9da0-dde663f8e5c6";
const SECRET_KEY = "IXD3U1S37QPJOJXLZMP5";

// Make sure these variables are global. Otherwise, they will not
// work properly inside the timers
chip = new gpiod.Chip('gpiochip4');
ledLine = chip.getLine(LED);
buttonLine = chip.getLine(BUTTON);

ledLine.requestOutputMode("gpio-basic");
// To configure the pull-up bias, use 32 instead of gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP if it is undefined
buttonLine.requestInputModeFlags("gpio-basic", gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP);

let client;

// This function is executed every 1.0 seconds, polls the value
// of the button and sends the data to Arduino Cloud
function readButton(client) {
let button = buttonLine.getValue() ? true : false;
if (client)
client.sendProperty("button", button);
console.log("pollButton:", button);
}

// This function is executed every 10.0 seconds, gets a random
// number between 0 and 100 and sends the data to Arduino Cloud
function readValue(client) {
let value = Math.floor(Math.random() * 101);
if (client)
client.sendProperty("test_value", value);
console.log("pollValue", value);
}

// This function is executed each time the "led" variable changes
function onLedChanged(led) {
ledLine.setValue(led ? 1 : 0);
console.log("LED change! Status is: ", led);
}

// Create Arduino Cloud connection
(async () => {
try {
client = await ArduinoIoTCloud.connect({
deviceId: DEVICE_ID,
secretKey: SECRET_KEY,
onDisconnect: (message) => console.error(message),
});
client.onPropertyValue("led", (led) => onLedChanged(led));
}
catch(e) {
console.error("ArduinoIoTCloud connect ERROR", e);
}
})();

// Poll Value every 10 seconds
const pollValue = setInterval(() => {
readValue(client);
}, 10000);

// Poll Button every 1 seconds
const pollButton = setInterval(() => {
readButton(client);
}, 1000);
34 changes: 34 additions & 0 deletions node-red/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## The setup


## GPIOD nodes
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.

In this project, we are going to use `node-red-contrib-libgpiod` which is the

Go to the menu "Manage Palette" --> "Install". Search the following nodes and install them.

@arduino/node-red-contrib-arduino-iot-cloud
node-red-contrib-libgpiod

## Configure the access to Arduino Cloud

## How to install Node-RED
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.
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.

These are the instructions
1. Installation
```
sudo npm install -g --unsafe-perm node-red
```
2. Manage the service
I recommend using pm2 to manage the service and make it start on boot
```
sudo npm install -g pm2
pm2 start /usr/bin/node-red -- -v
pm2 save
sudo env PATH=$PATH:/usr/bin $(which pm2) startup systemd
```


Loading