diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a36b36e --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +js/node_modules/ +credentials.py +*.pyc +package*.json +node-red/docker/data/node_modules/ +.npm +.config* +.flows* +*.tgz +*.tar.gz diff --git a/arduino-cloud/rpi-gpio-basic-dashboard.yaml b/arduino-cloud/rpi-gpio-basic-dashboard.yaml new file mode 100644 index 0000000..a0c488e --- /dev/null +++ b/arduino-cloud/rpi-gpio-basic-dashboard.yaml @@ -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 + diff --git a/assets/RPI-GPIO-Basic-Dashboard.png b/assets/RPI-GPIO-Basic-Dashboard.png new file mode 100644 index 0000000..ac454ca Binary files /dev/null and b/assets/RPI-GPIO-Basic-Dashboard.png differ diff --git a/assets/RPI-GPIO-Basic-Diagram.png b/assets/RPI-GPIO-Basic-Diagram.png new file mode 100644 index 0000000..c3dc8c5 Binary files /dev/null and b/assets/RPI-GPIO-Basic-Diagram.png differ diff --git a/assets/RPI-GPIO-Basic-Thing_Variables.png b/assets/RPI-GPIO-Basic-Thing_Variables.png new file mode 100644 index 0000000..0a5cf8d Binary files /dev/null and b/assets/RPI-GPIO-Basic-Thing_Variables.png differ diff --git a/assets/RPI-GPIO-Basic-Thing_Variables2.png b/assets/RPI-GPIO-Basic-Thing_Variables2.png new file mode 100644 index 0000000..07af7af Binary files /dev/null and b/assets/RPI-GPIO-Basic-Thing_Variables2.png differ diff --git a/js/gpio-basic/README.md b/js/gpio-basic/README.md new file mode 100644 index 0000000..3d35005 --- /dev/null +++ b/js/gpio-basic/README.md @@ -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. \ No newline at end of file diff --git a/js/gpio-basic/gpio-basic.js b/js/gpio-basic/gpio-basic.js new file mode 100755 index 0000000..92bb2e8 --- /dev/null +++ b/js/gpio-basic/gpio-basic.js @@ -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); diff --git a/node-red/README.md b/node-red/README.md new file mode 100644 index 0000000..d21ab45 --- /dev/null +++ b/node-red/README.md @@ -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 +``` + + diff --git a/node-red/gpio-basic/flows.json b/node-red/gpio-basic/flows.json new file mode 100644 index 0000000..7f774a9 --- /dev/null +++ b/node-red/gpio-basic/flows.json @@ -0,0 +1,246 @@ +[ + { + "id": "77a698253f4e0f3b", + "type": "tab", + "label": "Flow 1", + "disabled": false, + "info": "", + "env": [] + }, + { + "id": "75cf75b699400a70", + "type": "arduino-connection", + "applicationname": "Arduino Cloud" + }, + { + "id": "24cfd9cc1c1afa32", + "type": "debug", + "z": "77a698253f4e0f3b", + "name": "debug 1", + "active": true, + "tosidebar": true, + "console": false, + "tostatus": false, + "complete": "false", + "statusVal": "", + "statusType": "auto", + "x": 680, + "y": 60, + "wires": [] + }, + { + "id": "c867832c3d1e4d7b", + "type": "inject", + "z": "77a698253f4e0f3b", + "name": "", + "props": [ + { + "p": "payload" + }, + { + "p": "topic", + "vt": "str" + } + ], + "repeat": "", + "crontab": "", + "once": false, + "onceDelay": 0.1, + "topic": "", + "payload": "0", + "payloadType": "num", + "x": 150, + "y": 180, + "wires": [ + [ + "74096cf67e153bb0", + "45efe7b0aee2a4a1" + ] + ] + }, + { + "id": "72088a8b307e6580", + "type": "inject", + "z": "77a698253f4e0f3b", + "name": "", + "props": [ + { + "p": "payload" + }, + { + "p": "topic", + "vt": "str" + } + ], + "repeat": "", + "crontab": "", + "once": false, + "onceDelay": 0.1, + "topic": "", + "payload": "1", + "payloadType": "num", + "x": 150, + "y": 240, + "wires": [ + [ + "74096cf67e153bb0", + "45efe7b0aee2a4a1" + ] + ] + }, + { + "id": "2ed4ef3e5a6259bd", + "type": "debug", + "z": "77a698253f4e0f3b", + "name": "debug 2", + "active": true, + "tosidebar": true, + "console": false, + "tostatus": false, + "complete": "false", + "statusVal": "", + "statusType": "auto", + "x": 440, + "y": 300, + "wires": [] + }, + { + "id": "63c94b31249472ac", + "type": "gpio-in", + "z": "77a698253f4e0f3b", + "name": "", + "state": "INPUT", + "device": "gpiochip4", + "pin": "4", + "x": 320, + "y": 100, + "wires": [ + [] + ] + }, + { + "id": "74096cf67e153bb0", + "type": "gpio-out", + "z": "77a698253f4e0f3b", + "name": "", + "state": "OUTPUT", + "device": "gpiochip4", + "pin": "17", + "x": 450, + "y": 180, + "wires": [ + [] + ] + }, + { + "id": "1732b73c9a35b80c", + "type": "inject", + "z": "77a698253f4e0f3b", + "name": "", + "props": [ + { + "p": "topic", + "vt": "str" + } + ], + "repeat": "1", + "crontab": "", + "once": false, + "onceDelay": 0.1, + "topic": "", + "x": 150, + "y": 100, + "wires": [ + [ + "244b9558c4ea408c" + ] + ] + }, + { + "id": "65724da039f208af", + "type": "rbe", + "z": "77a698253f4e0f3b", + "name": "", + "func": "rbe", + "gap": "", + "start": "", + "inout": "out", + "septopics": true, + "property": "payload", + "topi": "topic", + "x": 490, + "y": 100, + "wires": [ + [ + "24cfd9cc1c1afa32", + "d73d5d99772925b2" + ] + ] + }, + { + "id": "d73d5d99772925b2", + "type": "property out", + "z": "77a698253f4e0f3b", + "connection": "75cf75b699400a70", + "thing": "74c49bf8-3844-4239-a663-76927d173cb2", + "property": "93fc3c3f-d936-4d74-ac26-982fb6a2d7c4", + "name": "button", + "propname": "button", + "defaultname": true, + "sendasdevice": false, + "device": "09d3a634-e1ad-4927-9da0-dde663f8e5c6", + "x": 670, + "y": 100, + "wires": [] + }, + { + "id": "2a01b1b47921fcfd", + "type": "property in", + "z": "77a698253f4e0f3b", + "connection": "75cf75b699400a70", + "thing": "74c49bf8-3844-4239-a663-76927d173cb2", + "property": "49eb3950-5fab-41ed-8baf-95d45ad0357e", + "name": "led", + "propname": "led", + "defaultname": true, + "variableName": "led", + "x": 130, + "y": 300, + "wires": [ + [ + "2ed4ef3e5a6259bd", + "45efe7b0aee2a4a1" + ] + ] + }, + { + "id": "244b9558c4ea408c", + "type": "gpio-in", + "z": "77a698253f4e0f3b", + "name": "", + "state": "INPUT", + "device": "gpiochip4", + "pin": "15", + "x": 330, + "y": 60, + "wires": [ + [ + "65724da039f208af" + ] + ] + }, + { + "id": "45efe7b0aee2a4a1", + "type": "gpio-out", + "z": "77a698253f4e0f3b", + "name": "", + "state": "OUTPUT", + "device": "gpiochip4", + "pin": "14", + "x": 450, + "y": 240, + "wires": [ + [] + ] + } +] \ No newline at end of file diff --git a/python/gpio-basic/README.md b/python/gpio-basic/README.md new file mode 100644 index 0000000..14863b3 --- /dev/null +++ b/python/gpio-basic/README.md @@ -0,0 +1,285 @@ +# Raspberry PI GPIO Basic control with Python using Arduino Cloud + +This project shows how to interact with the Raspberry Pi GPIOs from a dashboard created using Arduino Cloud with an application programmed in Python. It can serve as an example to create your own applications that require access to RPI GPIOs and that can be ultimately controlled using a dashboard. + +## The setup + +In this project I have used a Raspberry Pi 5 connected to an LED and a push button, both inserted in a breadboard. This is the diagram + +![alt text](../../assets/RPI-GPIO-Basic-Diagram.png) + +> Note: This project should work with any Raspberry Pi version and actually with any Linux-based machine that supports libgpiod. Please, drop your comments in **Issues** if it does not work with your board in order to review it. + +## How to create your project + +The process to use this project is very straighforward: +1. Create the Device and Thing in the Arduino Cloud and get the Arduino Cloud API Key +2. Create your python environment +3. Develop the python application +4. Create the Arduino Cloud dashboard +5. Test everything + +## 1. Create the Device and Thing in the Arduino Cloud + +> Note: Before getting started, make sure that you have an [Arduino Cloud account](https://cloud.arduino.cc/home/?get-started=true) + +### Create the Device +Go to the [Devices](https://app.arduino.cc/devices) section of the Arduino IoT Cloud and click on **+ DEVICE**. + +Select **Any Device** and follow the instructions on the wizard. + +> Note: Save your `Device ID` and `Secret Key` as they will be used in your python code. + +### Create the Thing +In your recently created device page, go to the Associated Thing section, click on **Create Thing** and rename it. + +> Note: You can also create the Thing from the [Things list](https://app.arduino.cc/things) and associate it later. + +### Create the Variables +Add the variables by clicking on the ADD button. At the end of the process, your list of variables should look like this. + +| Name | Type | Description | +|---------------------|------------|-------------| +| button | Boolean | It will hold the status of the physical button | +| led | Boolean | The variable that we will use to act over the physical LED | +| test_value | Integer | This is a value that will change periodically in the application | + +> Note: All the variables have to be READ-WRITE. You can define the periodicity you wish or set them with the policy ON-CHANGE. + +This is a screenshot for reference. + +![Arduino Cloud variables](../../assets/RPI-GPIO-Basic-Thing_Variables2.png) + +## 2. Create your python environment + +Now it is time to install the python dependencies in order to use Arduino Cloud. You have a [full tutorial](https://docs.arduino.cc/arduino-cloud/guides/python/) that describes the process in detail. + +It can be summarized as follows: +1. Install GPIOD library in the system +2. Install the Python GPIOD package +3. Install the Python Arduino Cloud packages + +### Install GPIOD library in the system + +First, you have to install library in the system. If you are using Ubuntu or any other Debian-based machine, you can follow these instructions + +``` +sudo apt install gpiod libgpiod-dev libgpiod2 python3-libgpiod +``` + +### Install the GPIOD Python package using PIP +Next, you have to install the PIP package. For that, my recommendation is that you install it in an isolated virtual environment. So you have to install the following packages + +``` +sudo apt install python3-pip python3-virtualenv +``` + +To create and activate the virtual environment, use the following code + +``` +cd +virtualenv venv +source venv/bin/activate +``` + +Install the package +``` +pip install xxxxx +``` + +### Install Arduino Cloud python packages + +To install the Arduino IoT Cloud client library, you only have to do the following: + +``` +pip install arduino-iot-cloud swig +``` + +## 3. Develop the python application + +Use your favourite programming environment and edit the `gpio-basic.py` file. + +Create a file called `credentials.py` inside the `gpio-basic` folder with the following content +``` +DEVICE_ID = b"YOUR_DEVICE_ID" +SECRET_KEY = b"YOUR_SECRET_KEY" +``` + +If you are using a different Raspberry Pi flavour or any other machine, you should check which is the right chipset and which are the lines that you want to use. Then, modify the following lines accordingly: + +``` +LED=14 # GPIO14, Pin 8 +BUTTON=15 # GPIO15, Pin 10 +chip = gpiod.Chip('/dev/gpiochip4') +``` + +If you want to learn more, check the [Annex](README.md#notes) at the end of this doc. + +## 4. Create the Arduino Cloud dashboard + +The dashboard that we are going to build will look like this + +![alt text](../../assets/RPI-GPIO-Basic-Dashboard.png) + +There are 2 ways to create the dashboard: +1. Create it manually. Replicate the one shown above following the instructions in [this guide](https://docs.arduino.cc/arduino-cloud/cloud-interface/dashboard-widgets/). + - The LED widgets should be linked to the variable led + - The Value widgets should be linked to the variable test_value + - The Button widgets should be linked to the variable button +2. Clone the one provided by this tutorial following the instructions in the [Annex](README.md#clone-the-dashboard-using-cloud-cli) + +## 5. Test everything + +Run your code + +``` +(venv)sxxx$ python ./gpio-basic.py +``` + +You should see the following: +* Every time push the button, the button widget should be updated +* Every time you change the LED widget in the dashboard, the physical LED should switch to ON or OFF +* The value of `test_value` should change randomly every 10s and the value updated in the dashboard + +Enjoy! + +## Additional information +### Arduino Cloud +[Arduino Cloud](https://cloud.arduino.cc/) is a platform that simplifies the process of developing, deploying, and managing IoT devices. It supports various hardware, including Arduino boards, ESP boards and any device programmed with Python or Javascript. It makes it easy for makers, IoT enthusiasts, and professionals to build connected projects without high programming skills. + +The platform allows for easy management and monitoring of connected devices through customizable dashboards, which provide real-time visualisations of the device's data. The dashboards can be accessed remotely through the mobile app Arduino IoT Cloud Remote, which is available for both Android and iOS devices, allowing users to manage their devices from anywhere. + +#### Clone the dashboard using Cloud CLI + +As described in the tutorial, you can create the dashboard on your own, but here I will show you a very handy trick so that you can just make a copy of a template that I have created. For that, you need to use [Arduino Cloud CLI](https://docs.arduino.cc/arduino-cloud/arduino-cloud-cli/getting-started/). + +The steps are the following: +1. Download and extract the latest release. +Download it from [here](https://github.com/arduino/arduino-cloud-cli/releases) +Make sure it is in your machine's PATH, so that it can be used globally. +After installation, check that it is working by opening a terminal, and type: + +2. Set your credentials +To authenticate with the Arduino Cloud, we will need to first set our credentials, using our clientId and clientSecret which are obtained from the Arduino Cloud [API keys section](https://app.arduino.cc/api-keys). Run the following command and introduce the credentials: +``` +arduino-cloud-cli credentials init +``` + +3. Create the dashboard + +``` +arduino-cloud-cli dashboard create \ + --name \ + --template rpi-gpio-basic-dashboard.yaml \ + --override Raspberry-Basic-GPIO= +``` +Replace *\* and *\* with your actual data. + +### GPIOs and Raspberry Pi +There are many GPIO libraries that can be used with Raspberry Pis. Among some of the most popular, we can find: gpiozero, gpiod, RPi.GPIO. One of the issues that I found is that some of the libraries only work for certain versions of RPI. For instance, the new RPI 5, has a brand new chipset for managing GPIOs, and not all the libraries work for it. + +After a lot of googling and searching, I ended up using *gpiod* as it is the one supported by the Linux kernel team directly and it can be used across all the RPI flavours. + +#### **GPIOD** library + +##### Installation +First, you have to install library in the system. +If you are using Ubuntu or any other Debian-based machine, you can follow these instructions + +``` +sudo apt install gpiod libgpiod-dev libgpiod2 python3-libgpiod +``` + +Next, you have to install the PIP package, for that, my recommendation is that you install it in an isolated virtual environment. So, you have to install the following packages + +``` +sudo apt install python3-pip python3-virtualenv +``` + +To create and activate the virtual environment, use the following code + +``` +cd +virtualenv venv +source venv/bin/activate +``` + +##### Notes +The GPIOD library has evolved quite a lot in time. The versions before 2.0.0 have a different API than the newer ones. This project is based on the API and syntax of version v2.1.3. + +These are the official pages of the Python package: +* https://pypi.org/project/gpiod/ +* https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python +* https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/ + +If you want to check what is the gpiochip and line that you have to use in the code, you can use the following command line commands. + +This is an example of the output in a Raspberry PI 5 +``` +$ sudo gpiodetect +gpiochip0 [gpio-brcmstb@107d508500] (32 lines) +gpiochip1 [gpio-brcmstb@107d508520] (4 lines) +gpiochip2 [gpio-brcmstb@107d517c00] (17 lines) +gpiochip3 [gpio-brcmstb@107d517c20] (6 lines) +gpiochip4 [pinctrl-rp1] (54 lines) +$ +$ sudo gpioinfo gpiochip4 +gpiochip4 - 54 lines: + line 0: "ID_SD" unused input active-high + line 1: "ID_SC" unused input active-high + line 2: "PIN3" unused input active-high + line 3: "PIN5" unused input active-high + line 4: "PIN7" unused input active-high + line 5: "PIN29" unused input active-high + line 6: "PIN31" unused input active-high + line 7: "PIN26" "spi0 CS1" output active-low [used] + line 8: "PIN24" "spi0 CS0" output active-low [used] + line 9: "PIN21" unused input active-high + line 10: "PIN19" unused input active-high + line 11: "PIN23" unused input active-high + line 12: "PIN32" unused input active-high + line 13: "PIN33" unused input active-high + line 14: "PIN8" "rpi-acloud-gpio-basic" output active-high [used] + line 15: "PIN10" unused input active-high + line 16: "PIN36" unused input active-high + line 17: "PIN11" unused input active-high + line 18: "PIN12" unused input active-high + line 19: "PIN35" unused input active-high + line 20: "PIN38" unused input active-high + line 21: "PIN40" unused input active-high + line 22: "PIN15" unused input active-high + line 23: "PIN16" unused input active-high + line 24: "PIN18" unused input active-high + line 25: "PIN22" unused input active-high + line 26: "PIN37" unused input active-high + line 27: "PIN13" unused input active-high + line 28: "PCIE_RP1_WAKE" unused input active-high + line 29: "FAN_TACH" unused input active-high + line 30: "HOST_SDA" unused input active-high + line 31: "HOST_SCL" unused input active-high + line 32: "ETH_RST_N" "phy-reset" output active-low [used] + line 33: "-" unused input active-high + line 34: "CD0_IO0_MICCLK" "cam0_reg" output active-high [used] + line 35: "CD0_IO0_MICDAT0" unused input active-high + line 36: "RP1_PCIE_CLKREQ_N" unused input active-high + line 37: "-" unused input active-high + line 38: "CD0_SDA" unused input active-high + line 39: "CD0_SCL" unused input active-high + line 40: "CD1_SDA" unused input active-high + line 41: "CD1_SCL" unused input active-high + line 42: "USB_VBUS_EN" unused output active-high + line 43: "USB_OC_N" unused input active-high + line 44: "RP1_STAT_LED" "PWR" output active-low [used] + line 45: "FAN_PWM" unused output active-high + line 46: "CD1_IO0_MICCLK" "cam1_reg" output active-high [used] + line 47: "2712_WAKE" unused input active-high + line 48: "CD1_IO1_MICDAT1" unused input active-high + line 49: "EN_MAX_USB_CUR" unused output active-high + line 50: "-" unused input active-high + line 51: "-" unused input active-high + line 52: "-" unused input active-high + line 53: "-" unused input active-high + +``` + +Check the full documentation of libgpiod and the command line tools [here](https://github.com/brgl/libgpiod). diff --git a/python/gpio-basic/gpio-basic.py b/python/gpio-basic/gpio-basic.py new file mode 100644 index 0000000..f7e262b --- /dev/null +++ b/python/gpio-basic/gpio-basic.py @@ -0,0 +1,58 @@ +#! /usr/bin/python3 + +import random +import gpiod +from gpiod.line import Direction, Value, Bias +from arduino_iot_cloud import ArduinoCloudClient +# To set your credentials you have two options: +# 1. Create a credentials.py file with the DEVICE_ID and SECRET_KEY +# 2. Comment out the following line and uncomment and fill the DEVICE_ID, SECRET_KEY below +from credentials import DEVICE_ID, SECRET_KEY +#DEVICE_ID = b"YOUR_DEVICE_ID" +#SECRET_KEY = b"YOUR_SECRET_KEY" + +LED=14 # GPIO14, Pin 8 +BUTTON=15 # GPIO15, Pin 10 +# For Raspberry PI 5, the chip is gpiochip4. Check for other RPI flavours. +chip = gpiod.Chip('/dev/gpiochip4') +req=chip.request_lines(consumer="rpi-acloud-gpio-basic", + config= { + LED : gpiod.LineSettings(direction=Direction.OUTPUT), + BUTTON : gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_UP), + }) + +# This function is executed every 1.0 seconds (as defined in the registration) and +# returns a random integer value between 0 and 100 +def read_button(client): + button = req.get_value(BUTTON) + if button == Value.INACTIVE: + return False + else: + return True + +# This function is executed every 10.0 seconds (as defined in the registration) and +# returns a random integer value between 0 and 100 +def read_value(client): + return random.randint(0, 100) + +# This function is executed each time the "led" variable changes +def on_led_changed(client, value): + if value: + req.set_value(LED, Value.ACTIVE) + else: + req.set_value(LED, Value.INACTIVE) + print("LED change! Status is: ", value) + + +if __name__ == "__main__": + # Create Arduino Cloud connection + client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY) + + # Register the Arduino Cloud variables with the callback functions + client.register("test_value", on_read=read_value, interval=10.0) + client.register("button", on_read=read_button, interval=1.0) + client.register("led", value=None, on_write=on_led_changed) + + # Start the client + client.start() +