mbartonpye
Posts: 2
Joined: Tue Nov 24, 2015 11:01 am

Accessing the GPIO from a webpage

Tue Nov 24, 2015 2:34 pm

Sorry if this is a repeat/duplicate of discussions which have already been posted but I have it in mind that there was something to do with the recent Jessie and Epiphany releases which makes it possible to interrogate the state of a GPIO pin from within a simple webpage running Javascript or other.

It may be that I have imagined this but I am sure that I read something, somewhere.

The reason why I ask is that I have a very simple HTML/CSS/Javascript page which is the only thing which is displayed on the RPi touchscreen, onto which I want to display a temperature from a DS18B20 and if it can be done without having to read from python variables or whatever, it would help to keep it all simple.

I appreciate that this is not the first time that this sort of question has been asked but, in light of recent releases, I hoped that there was, as I might have imagined, some 'new' method of addressing the GPIO.

Thanks for your patience

Heater
Posts: 15777
Joined: Tue Jul 17, 2012 3:02 pm

Re: Accessing the GPIO from a webpage

Tue Nov 24, 2015 5:31 pm

Javascript on a web page cannot access the GPIO.

Javascript runs in a "sandbox" inside your browser for security reasons. You don't want rogue web sites you visit accessing your files and so on.

What yo can do is have the web page request the state of the GPIO from the server running on a Pi. The server reads/writes the GPIO and sends it back to javascript in the browser for display.

Personally I do this kind of thing using a simple webserver written in Javacript that runs under node.js. It's neat to be able to use the same language on both the server and the client.
Memory in C++ is a leaky abstraction .

fruitoftheloom
Posts: 23022
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Accessing the GPIO from a webpage

Tue Nov 24, 2015 5:46 pm

mbartonpye wrote:Sorry if this is a repeat/duplicate of discussions which have already been posted but I have it in mind that there was something to do with the recent Jessie and Epiphany releases which makes it possible to interrogate the state of a GPIO pin from within a simple webpage running Javascript or other.

It may be that I have imagined this but I am sure that I read something, somewhere.

The reason why I ask is that I have a very simple HTML/CSS/Javascript page which is the only thing which is displayed on the RPi touchscreen, onto which I want to display a temperature from a DS18B20 and if it can be done without having to read from python variables or whatever, it would help to keep it all simple.

I appreciate that this is not the first time that this sort of question has been asked but, in light of recent releases, I hoped that there was, as I might have imagined, some 'new' method of addressing the GPIO.

Thanks for your patience
:?: :?: :?:
http://webiopi.trouch.com
Rather than negativity think outside the box !
RPi 4B 4GB (SSD Boot)..
Asus ChromeBox 3 Celeron is my other computer...

mbartonpye
Posts: 2
Joined: Tue Nov 24, 2015 11:01 am

Re: Accessing the GPIO from a webpage

Wed Nov 25, 2015 10:44 am

Thanks for the replies!
I did wonder if I had dreamt this up as I had it strongly in my mind that there was some new way to address the GPIO directly from a webpage but it did seem a little insecure!
Your suggestions are very helpful though and will help me to get to where I want to be!

Thanks
Matthew

User avatar
joan
Posts: 14876
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Accessing the GPIO from a webpage

Wed Nov 25, 2015 11:15 am

mbartonpye wrote:Thanks for the replies!
I did wonder if I had dreamt this up as I had it strongly in my mind that there was some new way to address the GPIO directly from a webpage but it did seem a little insecure!
Your suggestions are very helpful though and will help me to get to where I want to be!

Thanks
Matthew
You are possibly thinking of the new /dev/gpiomem which allows access to the GPIO if you are in group gpio. This allows access to the GPIO without requiring root privileges.

As far as access is concerned CGI is a standard method of accessing local machine resources from a web page.

rottjr
Posts: 11
Joined: Tue Mar 22, 2016 3:47 pm

Re: Accessing the GPIO from a webpage

Tue Mar 22, 2016 10:17 pm

You can take a look at the PHP console I made viewtopic.php?f=34&t=141280

As stated by Heater, it runs primarily on shell execution through the php interface.

Hope this helps,

RottJR

RogueAfterlife
Posts: 4
Joined: Sun Apr 17, 2016 5:57 pm

Re: Accessing the GPIO from a webpage

Sun Apr 17, 2016 6:10 pm

I think that maybe you should look at running NodeJS on the pi and write all of your "hardware" code i.e., polling sensor data, in NodeJS. I recently discovered an NPM package--rpio--that allows the gpio pins to be controlled using NodeJS. In terms of updating data on a webpage in real-time, I suggesting using some sort of socket.io implementation to maintain a socket between the server and client. There are examples of this on the web. You could also write your hardware code in python, open a socket between python script and NodeJS server, and then socket the server to the client. The possiblilities are endless. Hope this helps!

Heater
Posts: 15777
Joined: Tue Jul 17, 2012 3:02 pm

Re: Accessing the GPIO from a webpage

Sun Apr 17, 2016 7:57 pm

You cannot access hardware from a web page. Not unless you wrap your web page up in Electron.

I agree with RogueAfterlife. Use node.js as your webserver, which can get to GPIO pins and so just about anything. Use socket.io to communicate from the Javascript server to Javascript in the web page.
Memory in C++ is a leaky abstraction .

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Accessing the GPIO from a webpage

Sun Apr 17, 2016 9:39 pm

Heater wrote:You cannot access hardware from a web page. Not unless you wrap your web page up in Electron.

I agree with RogueAfterlife. Use node.js as your webserver, which can get to GPIO pins and so just about anything. Use socket.io to communicate from the Javascript server to Javascript in the web page.
Node-Red can do all that, and much more, and is installed on the more recent versions of Raspbian

User avatar
brekee12
Posts: 335
Joined: Wed Feb 03, 2016 3:36 pm
Location: HU

Re: Accessing the GPIO from a webpage

Mon Apr 18, 2016 6:51 am

It is easy to solve. PIGPIO Libraryhttp://abyz.co.uk/rpi/pigpio/python.html has python and C interface. The library has module version with which you can control the lines over even network. Using php exec function it is possible to run your program and read the value returned an external program or even control the lines one by one. So, it is the right equipment, everything else is just your idea how to solve it.
Brekee12
on a Raspberry B+ with whezzy, two Zero with Jessie Light

Heater
Posts: 15777
Joined: Tue Jul 17, 2012 3:02 pm

Re: Accessing the GPIO from a webpage

Mon Apr 18, 2016 7:01 am

pigpio is also supported from Javascript running under node.js https://www.npmjs.com/package/pigpio

No messy Python, PHP or shell exec required.

I have a very simple demo of how to drive GPIO from a web page using Javascript and pigpio here: https://bitbucket.org/zicog/pigpio2html
Memory in C++ is a leaky abstraction .

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Accessing the GPIO from a webpage

Mon Apr 18, 2016 7:53 am

Heater wrote:pigpio is also supported from Javascript running under node.js https://www.npmjs.com/package/pigpio

No messy Python, PHP or shell exec required.

I have a very simple demo of how to drive GPIO from a web page using Javascript and pigpio here: https://bitbucket.org/zicog/pigpio2html
pigpio is supported by probably everything, stated that pigpiod is running.
i was able to write a basic php class to interact with it (and i'm not that good at coding LOL) (it's on the forum, somewhere..)

this just to say that i'm a little bit tired of these religious wars applied to languages..
you can hate php (Dougie does :lol: and he is always right), but it's widely used and it's not devil..

Heater
Posts: 15777
Joined: Tue Jul 17, 2012 3:02 pm

Re: Accessing the GPIO from a webpage

Mon Apr 18, 2016 8:24 am

Massi,

Nothing religious about it.

Cold, hard, uncaring reality slaps you in the face every time you sit down to program anything.

My opinions are formed from a not insignificant amount of time invested in evaluating, learning and using for serious work, many different languages and systems spread over many years. And noticing that the severity of the slap in the face varies considerably.

My only hope is that such experience does not go to waste and I can help others avoid a lot of time wasting frustration.

With the caveat, that one has to select the tool to match the job in hand.

Anyway, a good programming language war is always educational and will help readers find the tool that suits their problem.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”