micro-python cgi to arduino dock
-
Back on my project to set my Onion 2 to use its uhttpd server to listen for commands to trip an attached relay to open/close my garage door. With the elimination of the Relay Expansion it's not clear how to tie this. Searching I found an old doc to load up php5 and its php5-cgi that should be the bridge.
The problem is that php7 (and likely php5 if I could find it) suck up all available space and thus unable to load any library that can talk to Omega's GPIO.
On paper micro python ought to be small enough to allow other software. While there is a micropython-cgi library, the pip (or upip) module didn't come with this package. I've installed the micropython-libmicropython -v MicroPython v1.9.2 on 2021-01-07; linux version
If I can get (u)pip installed and then micropython-cgi is there a library to tie the cgi to the Onion's GPIO? Are there examples of micropython communicating with its GPIO? thx, sam
-
@sam-walton You can certainly run php7 on Omega2, below is a list of the modules I run on mine. You also don't need python to operate a gpio, you can do it in a script or I use C code for my own purposes.
Here is some code to open and set a gpio:
#define GPIO_MAIN_DIRECTORY "/sys/class/gpio/" int gpio_set_direction(int gpio_number, char in_or_out[]) { FILE *fp; char filename[50] = GPIO_MAIN_DIRECTORY; char str[3]; if (gpio_list[gpio_number] != 1) { return EXIT_FAILURE; } strcat(filename, "gpio"); sprintf(str, "%d", gpio_number); strcat(filename, str); strcat(filename, "/direction"); fp = fopen(filename, "w"); if (fp == NULL) { return EXIT_FAILURE; } fprintf(fp, in_or_out); fclose(fp); return EXIT_SUCCESS; } int gpio_set(int gpio_number, int value) { FILE *fp; char filename[50] = GPIO_MAIN_DIRECTORY; char str[3]; if (gpio_list[gpio_number] != 1) { return EXIT_FAILURE; } if (value > 0) { value = 1; } strcat(filename, "gpio"); sprintf(str, "%d", gpio_number); strcat(filename, str); strcat(filename, "/value"); fp = fopen(filename, "w"); if (fp == NULL) { return EXIT_FAILURE; } sprintf(str, "%d", value); fprintf(fp, str); fclose(fp); return EXIT_SUCCESS; } int gpio_open(int gpio_number) { FILE *fp; char filename[50] = GPIO_MAIN_DIRECTORY; char str[3]; gpio_list[gpio_number] = 1; fp = fopen(filename, "w"); if (fp == NULL) { return EXIT_FAILURE; } sprintf(str, "%d", gpio_number); fprintf(fp, str); fclose(fp); return EXIT_SUCCESS; } int gpio_close(int gpio_number) { FILE *fp; const char filename[] = "/sys/class/gpio/unexport"; char str[3]; fp = fopen(filename, "w"); if (fp == NULL) { printf("Could not open %s\n", filename); return EXIT_FAILURE; } sprintf(str, "%d", gpio_number); fprintf(fp, str); fclose(fp); return EXIT_SUCCESS; }
php7 - 7.2.34-1
php7-cgi - 7.2.34-1
php7-fastcgi - 7.2.34-1
php7-mod-curl - 7.2.34-1
php7-mod-json - 7.2.34-1
php7-mod-pdo - 7.2.34-1
php7-mod-pdo-sqlite - 7.2.34-1
php7-mod-session - 7.2.34-1
php7-mod-sqlite3 - 7.2.34-1
php7-mod-zip - 7.2.34-1
-
@crispyoz Thank you for replying. I agree I can install php7 but little else.
Forgoing git and its libraries I've followed the link above and downloaded the Omega.php from this gist.
As the post states, it's a wrapper around the fast-gpio command.Following its example running
fast-gpio set 10 1 2>&1
returns Set GPIO10: 1
But my relay on pin 10 yields no change. I believe it's working because I wirelessly uploaded a simple Arduino script that cycles pin 10 on and off successfully. Could it be the fast that I'm running an Arduino expansion board for Omega that's letting the fast-gpio command on the Omega fail silently?
Is there another Omega library I need to drive Omega's GPIO?
Thx for viewing, sam
-
@sam-walton Let's clarify a few things, "Pin 10" is not GPIO 10, the pins are documented here: https://tinyurl.com/Omega2-Data-Sheet I don't see GPIO 10 mentioned as some are used for other purposes, I haven't checked what 10 is used for but pick another unused GPIO.
Regarding the gist, I'm not sure why you would want to call exec to set the GPIO when you can do it by simply writing to the trigger file as shown in the code. Also check the documents here: https://docs.onion.io/omega2-docs/using-gpios.html as many GPIO are multiplexed pins so you may need to set them to GPIO in order to use them
When you install php7 you also get additional modules installed, you can remove those to save space. Also look at what software has been installed by default and remove some of that. You can also install an SD Card and use this to extend your available space. https://docs.onion.io/omega2-docs/using-a-microsd-card.html
-
@crispyoz Thank you for your clarification. When the Relay expansion was discontinued I presumed that I would have to use the Arduino dock to drive a relay. I was using the Arduino's pin 10 in my experiment. Your clarification prompted me to consider that I could drive the relay directly from the Omega.
I chose pin 18 because it appears from the docs that it's a straight digital and doesn't appear to be multiplexed. It reads as input which s/b safe and the relay is now getting its voltage from the 3.3v pin.
The php script works as expected. In reading the linked doc, I don't suppose I can get this pin to stay off on omega reboot (after power failure). The relay is set to normally open.
Thank you again for your clarification, sam
-
@sam-walton The easiest way to set the state of a GPIO at boot is to add the relevant command to the /etc/rc.local file. Depending on your requirements you would use something like gpioctl dirout-low 18