Custom web page for IO Monitoring etc
-
Hi I'm using Omega 2S+ at the centre of a new controller.
I'd like to create some custom web pages on the Omega such that I can see and control some GPIO pins, but I have no idea where to begin with the specifics.
(I mean I know how to write HTML, upload files to the Omeag etc, and I understand that the Omega is running uhttpd on /www. But I don't knwo how to access GPIO from within the html)
Is there a guide on how to access stuff like GPIO from within the webserver?
-
@SpiderKenny A simple method would be to write your pages in PHP and use the exec function to call fastgpio command to interact with the GPIO.
-
@SpiderKenny Another method is to access the GPIO as a file, which you can also do using PHP or other method, here is how I do it in C. Not all my code, but can't recall the original author to credit.
#define GPIO_MAIN_DIRECTORY "/sys/class/gpio/" #define GPIO_ENABLE_FILE "/sys/class/gpio/export" #define GPIO_DISABLE_FILE "/sys/class/gpio/unexport" #define GPIO_MAX_NUMBER 30 static int gpio_list[GPIO_MAX_NUMBER] = {0}; int gpio_get(int gpio_number) { FILE *fp; char filename[50]; char str[3]; int value; if (gpio_list[gpio_number] != 1) { return EXIT_FAILURE; } snprintf("%sgpio%d/value", 50, GPIO_MAIN_DIRECTORY, gpio_number); fp = fopen(filename, "r"); if (fp == NULL) { return EXIT_FAILURE; } value = getc(fp); fclose(fp); return value - 48; /* 48 is the ascii-value for '0' */ } 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; } snprintf("%sgpio%d/direction", 50, GPIO_MAIN_DIRECTORY, gpio_number); 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; } snprintf("%sgpio%d/value", 50, GPIO_MAIN_DIRECTORY, gpio_number); 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; const char filename[] = GPIO_ENABLE_FILE; char str[3]; gpio_list[gpio_number] = 1; fp = fopen(filename, "w"); if (fp == NULL) { return EXIT_FAILURE; } fprintf(fp, "%d", gpio_number); fclose(fp); return EXIT_SUCCESS; } int gpio_close(int gpio_number) { FILE *fp; const char filename[] = GPIO_DISABLE_FILE; char str[3]; fp = fopen(filename, "w"); if (fp == NULL) { printf("Could not open %s\n", filename); return EXIT_FAILURE; } fprintf(fp, "%d", gpio_number); fclose(fp); return EXIT_SUCCESS; } int gpio_close_all() { int i; for (i = 0; i < GPIO_MAX_NUMBER; i++) { if (gpio_list[i] == 1) { gpio_close(i); gpio_list[i] = 0; } } return EXIT_SUCCESS; }
-
@crispyoz Thank you!
I've done a bit of PHP in the past too. I'll give it a try.
-
@crispyoz BTW, in my C/C++ programming I usef the fastgpio library which uses direct register access. It's very quick and lightweight for accessing GPIO.
-
@SpiderKenny Yes fastgpio is better but I showed code that uses the file system instead as I'm not sure of your level of experience and this method is easy to implement.