uxi99
Posts: 3
Joined: Fri Mar 15, 2019 9:29 pm

PHP raspberry pi servo control help

Fri Mar 15, 2019 9:53 pm

Hello guys I am gonna be honest..
I left this homework for last minute and tomorrow is my deadline and i am clueless as to what to do. I have to use a servo and my raspberry pi is not working again.So basically if someone can help me do my homework.
I have to make a webpage with php that will put a 180C servo in 5 positions using 5 buttons
and buttons that will make the servo take 1 position..
If you could help me out or direct me towards the right way to doing this without being able see tthe servo id appreciate it.

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

Re: PHP raspberry pi servo control help

Sat Mar 16, 2019 2:49 am

I appreciate your honesty. I'm not normally in to doing peoples homework for them but...

I can't help with driving a servo from PHP. I think it's totally the wrong language to be doing that in.

But a quick google for "raspberry Pi PHP drive servo" will find you tons of example code you could borrow/adapt. For example:
https://github.com/offbye/rpiservocamra
https://www.quora.com/Where-can-I-find- ... aspberrypi

I'm not actually sure what you mean by "without being able see the servo id"

xl97
Posts: 126
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP raspberry pi servo control help

Mon Mar 25, 2019 7:20 pm

The PHP part is VERY easy...

However.. as mentioned.... this may not be the correct language (or easiest at least) to be working with for this type of project.

That being said, I usually ONLY do PHP web development.... but being as this a WEB (server side scripting language)... how do you believe the PHP script should communicate with the servo?

I am NOT very experience with RPi stuff (mostly Arduino.. and finally make the jump to RPi based projects as the needs arise)

So.. I would have my PHP script either attempt to communicate with via serial port to the attached Arduino.. that would parse the data and send off the command using the included servo library method()..


or I would use an ESP module to connect to the php/webpage and get the data..(url parameter)... and either parse/take action itself.. (if enough I/O pins)... or send to a connected Arduino via software serial connection..

How is your servo attached?

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

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 4:00 am

xl97,
The PHP part is VERY easy...However.. as mentioned.... this may not be the correct language (or easiest at least) to be working with for this type of project.
Yes, PHP is very easy. However the whole concept behind PHP is as a means of scripting web pages in response to HTTP requests. That is:

a) WEB browser makes HTTP request.
b) WEB server gets HTTP request.
c) WEB server runs the PHP embedded in the requested page text.
d) WEB server replies to browser with whatever that PHP outputs.
e) Browser renders the returned HTML response.

What's missing here is:

1) PHP is only run briefly when requested.
2) Therfore PHP is not sensibly in control of the PWM or other peripheral devices.
3) Is there even an API for PHP to set PWN, GPIO, etc. Might be, I don't know.

Basically the whole model is wrong for managing peripheral devices.
That being said, I usually ONLY do PHP web development.... but being as this a WEB (server side scripting language)... how do you believe the PHP script should communicate with the servo?
That's OK. If you have the skills to master PHP and all the associated HTML, CSS and complications of web development then you can easily pickup other things, be they Python, node.js, C/C++...

PHP may well have an API to set the parameters of a PWM device. I don't know. If not you can use PHP's "system" or "exec" or whatever PHP calls it to run some other code, in Python say, that can talk to PWM.
So.. I would have my PHP script either attempt to communicate with via serial port to the attached Arduino.. that would parse the data and send off the command using the included servo library method()..
Where did this Arduino enter the picture? Not required for using PWM on the Pi.
or I would use an ESP module to connect to the php/webpage and get the data..(url parameter)... and either parse/take action itself.. (if enough I/O pins)... or send to a connected Arduino via software serial connection..
Or ESP.
How is your servo attached?
I do not have a servo attached to any Pi. But I have done similar things.

Personally, given your original requirement statement and ignoring the requirement to use PHP: "make a webpage that will put a 180C servo in 5 positions using 5 buttons and buttons that will make the servo take 1 position.." I would forget about using PHP and Apache.

Rather I would create a simple program Javascript and node.js:
https://nodejs.org/en/

That simple node.js program would implement a web server using the express.js module:
https://expressjs.com/

Simultaneously that node.js program would be managing the PWM device(s) using the pigpio module:
https://github.com/fivdi/pigpio

Now, should it happen that you have many PWM servos and perhaps other devices it may be convenient to control them from an Arduino or other MCU and have that communicate with the Pi where the web interface is running. That's OK, the above scheme can be easily extended to talk over serial ports:
https://serialport.io/

User avatar
B.Goode
Posts: 8059
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 9:47 am

Personally, given your original requirement statement and ignoring the requirement to use PHP: "make a webpage that will put a 180C servo in 5 positions using 5 buttons and buttons that will make the servo take 1 position.." I would forget about using PHP and Apache.
Just to observe, for the sake of clarity, that @heater is responding to a post from @xl97. But the originator of this thread, and writer of the cited spec, was @uxi99.

The latter had a submission deadline a week ago...

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

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 11:54 am

Ha, I must admit that in my pre-morning-coffee state I did not distinguish xl97 from uxi99.

Many coffees later, and reading it again, I'm still confused.

Anyway, don't use PHP for this task.

xl97
Posts: 126
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 11:57 am

@Heater

I think you need re-read the posts here.

* not my requirements at all..
* Arduino mentioned as one way I have done this using PHP (as I dont have many RPi projects under my belt yet especially with the GPIO pins).. and was done to give an understanding of the whole process.
* I mentioned ESP already if it had enough IO pins.. if not.. an attached Arduino to the ESP


And mention AS WELL... I dont think PHP is the right choice either... but sometimes you need to work with your requirements are..

No clue.

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

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 12:07 pm

Yeah. I think I explained my confusion well enough above. Sorry for that.

It would be nice if people would come back after they have asked a question. Either to say thanks, ask for further assistance or generally say what happened. It's inconsiderate to drop a question into a forum, waste peoples time, then disappear without a word.

xl97
Posts: 126
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP raspberry pi servo control help

Tue Mar 26, 2019 4:03 pm

^ agreed.

:)

Return to “Other programming languages”