Unable to access gpio pins from Java


#1

I’m using the following code to set the direction of a gpio pin, but getting the following error. Looks like I don’t have enough permissions to write into the gpio files. I already gave full read/write access to all the files as shown below. I’m using targetSdkVersion 22. I greatly appreciate any insights into what I’m missing here. I’m doing the IoT course in Coursera, and I can’t go ahead without accessing the gpios from the code. Thanks again!!

File Permissions
1|shell@msm8916_64:/sys/class/gpio $ ls -l
–w------- root root 4096 1969-12-31 16:00 export
lrwxrwxrwx root root 1969-12-31 16:01 gpio930 -> …/…/devices/soc.0/1000000.pinctrl/gpio/gpio930
lrwxrwxrwx root root 1969-12-31 16:01 gpio935 -> …/…/devices/soc.0/1000000.pinctrl/gpio/gpio935

shell@msm8916_64:/sys/class/gpio/gpio935 $ ls -l
-rw-r–r-- root root 4096 1969-12-31 16:35 active_low
lrwxrwxrwx root root 1969-12-31 16:35 device -> …/…/…/1000000.pinctrl
-rwxrwxrwx root root 4096 1969-12-31 16:40 direction
-rw-r–r-- root root 4096 1969-12-31 16:35 edge
drwxr-xr-x root root 1969-12-31 16:35 power
lrwxrwxrwx root root 1969-12-31 16:35 subsystem -> …/…/…/…/…/class/gpio
-rw-r–r-- root root 4096 1969-12-31 16:35 uevent
-rwxrwxrwx root root 4096 1969-12-31 16:41 value

==============================================================================

Code
private void setDirection(String direction) {
Log.v(TAG,“Setting Direction”);
BufferedWriter out = null;
try {
String command = String.format(“echo %s > /sys/class/gpio%s/direction”, direction, this.pin);
Runtime.getRuntime().exec(new String[] {“su”, “-c”, command});
}
catch (IOException e) {
Log.e(TAG,"Error: " + e.getMessage());
}
}

=====================================================================================
Error Message
12-31 17:48:25.510 25294-25294/net.calit2.mooc.iot_db410c.ledblink E/GpioProcessor: Error: Error running exec(). Command: [su, -c, echo out > /sys/class/gpio935/direction] Working Directory: null Environment: null

===================================================================================
Permissions in the manifest file
uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />
uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE” />
uses-permission android:name=“android.permission.WRITE_INTERNAL_STORAGE” />
uses-permission android:name=“android.permission.READ_INTERNAL_STORAGE” />
uses-permission android:name=“android.permission.MOUNT_UNMOUNT_FILESYSTEMS” />


#2

On Coursera it was a point where you should have added some permission, chmod for the GPIO in the “init.qcom.post_boot.sh”.
Did you do it?

I cannot enter to the course anymore as I should enrolled but here was the code to add:

/system/bin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

set -A pins 938 915 1017 926 937 930 914 971 901 936 935
for i in 0 1 2 3 4 5 6 7 8 9 10
do
echo ${pins[i]} > /sys/class/gpio/export;
chmod 777 /sys/class/gpio/gpio${pins[i]};
chmod 777 /sys/class/gpio/gpio${pins[i]}/value;
chmod 777 /sys/class/gpio/gpio${pins[i]}/direction;
done

jean-marc


#3

Yes, I did that. However, looks like that shell script isn’t working fully. With the script only the directory level permissions are getting set, not the permissions for the files ‘value’ and ‘direction’. Once I set the permissions manually and used the following code instead of my earlier code, things started working. Thanks for responding.

=========================================================

private void setDirection(String direction) {
Log.v(TAG,“Setting Direction”);
BufferedWriter out = null;
try {
FileWriter fstream = new FileWriter(PATH + “/gpio” + pin + “/direction”, false); //t
out = new BufferedWriter(fstream);
out.write(direction);
out.close();
} catch (IOException e) {
Log.e(TAG,"Error: " + e.getMessage());
}
}


#4

Great Settys… but really the script is done to avoid doing it manually…

For the code: it is the same one I am using and is part of the GpioProcessor of the db410c_gpiolib made for the MOOC.

I would double check the script file and be sure it was pushed in the system.

Good work… :slight_smile: