JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

Which programming language to use

Sun Apr 28, 2013 12:31 pm

I want to put together a system based on Rpi and be able to connect remotely from a web page and view a number of information such a temperature, humidity, voltage monitor and possibly a webcam and have the possibility to send an email on a specific trigger.
I know I need to configure lamp and have done this but I would like some guidance as to which language is best to successfully achieve my goal. Can this all be done in PHP or do I need to add some part in Python or other? I would like to stay away from C if possible.

Thanks for any suggestions

Mike

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Which programming language to use

Sun Apr 28, 2013 3:16 pm

You should be able to do this with PHP, but it may require some Python - we would need to know what hardware is used to obtain the temperature, humidity, and voltage data, and webcam video.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

Re: Which programming language to use

Mon Apr 29, 2013 12:51 pm

My temperature sensor is an DS18B20, Humidity sensor is AM2302 (DHT22)
I also want to add a water alarm and I am still working on finding the hardware solution but it will come down to a simple GPIO input switch.
I also have not found a hardware solution yet for the voltage monitor.
I am not planning on having the cam for now but I would like my design to allow for this add-on without having to redo the programming.

I was looking at http://code.google.com/p/webiopi/ and it appears to be a solution for some of my requirements, am I going the right way?

TeachYourselfStuff
Posts: 13
Joined: Sun Dec 02, 2012 9:21 pm

Re: Which programming language to use

Mon Apr 29, 2013 2:42 pm

Python, Java, Ruby, PHP, they are all based on C

So you might as well go learn it.

The only languages that are really different from C are BASIC languages, and those aren't too popular on RPi, Though if you install the RISC OS image you can program in BBC Basic on that.

plugwash
Forum Moderator
Forum Moderator
Posts: 3602
Joined: Wed Dec 28, 2011 11:45 pm

Re: Which programming language to use

Mon Apr 29, 2013 3:00 pm

The first big choice you have to make is what structure to use for the system.

Normally web applications work by the web server running a program when the user sends a web request and the program then terminates on completion of the web request. It's possible to have such programs talk to hardware directly but it's generally a bad idea because there is nothing to stop two copies of your program running at once and fighting over the hardware and there is no real way to handle background tasks such as monitoring the hardware for alarm conditions within this structure.

So this gives you a few choices for architecture
1: build a long running program that contains an embedded web server.
2: write a long running program that receives requests from a websever over a protocol like fastcgi
3: write a long running program that is talked to by programs that run each time there is a web request (in a similar way to many web apps talk to a long running database server)

PHP was designed for writing short lived scripts and from what I can gather it's memory management sucks. Therefore i'd advise against it for long running programs. If you go with option 3 then you could write the web part of the application in PHP but you'd still want to write the long running part in something else.

Webopi looks like it's taking approach 1, I don't know how good an implementation it is though.

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Which programming language to use

Mon Apr 29, 2013 7:31 pm

TeachYourselfStuff wrote:Python, Java, Ruby, PHP, they are all based on C
So you might as well go learn it.
The only languages that are really different from C are BASIC languages, and those aren't too popular on RPi, Though if you install the RISC OS image you can program in BBC Basic on that.
This is more than a bit off, as thousands of programming "languages" have been developed over the past half-century, and the vast majority of them have nothing to do with C, which is really a platform(hardware and OS)-independent assembly language (of which there are a number of partially-incompatible versions), nor with BASIC, which can be interpreted or compiled (and there are many flavors of BASIC).

The choice of a programming language depends on a lot of factors of which performance is only one. There are a lot of contributing elements to performance that can each vary widely across the various implementations of a language on different platforms. These elements can be determined by the platform hardware and/or OS (e.g., slow memory and/or storage can't be sped up with more efficient code, no matter what kind of whizzy braniac you might be). Performance may not even be a consideration compared with the costs of development - how much time it takes to design, test, document, etc., in addition to implementation (just coding to the unwashed, which I used to be) - which typically only occupies about 15% of the total time spent developing a software project. A bad design has to be avoided not just for perceived foofy academic principles, but because the resulting software may not be valid code - getting the wrong answer or an answer that doesn't apply to the problem at hand at the speed of heat is not a very worthwhile pursuit!

PHP used to be a bit wanting in the efficiency department before it underwent some radical body-building over the last few versions (I'll bet 99% of the people here don't even know PHP stands for Personal Home Page - emphasis on the Personal, not world-class server farm efficiency that current versions possess). Now it can not only run faster and jump higher, but it supports object-oriented programming (but only if you want/need it), some of the less ideal syntax and semantics have been deprecated with new-and-vastly-improved replacements or just left for dead where that's appropriate, and it's otherwise been spiffied up to run circles around the Original Flavor offering. Performance can differ widely depending on the capabilities of a given platform, of course, so on a memory-constrained Pi, if you do something stupid that allocates memory without giving it up when apropos, things may slow to a crawl, especially on a 256 MB Pi with at least half of the RAM dedicated to the GPU.

Anyway, that's not why we came here. I'm checking out the sensors the OP wants to use - one thing that may be needed is some A/D conversion, which the Pi can't do. There are solutions for around $25 that provide something like 16 channels of 10-bit (or more, IIRC) resolution, and up to four of them can be stacked to provide up to 64 channels via a single Pi's GPIO interface.

More to Come, So Stay Tuned, as they say on the late-night talk shows! :D
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

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

Re: Which programming language to use

Mon Apr 29, 2013 7:37 pm

C.

Others have given the reason.

TeachYourselfStuff
Posts: 13
Joined: Sun Dec 02, 2012 9:21 pm

Re: Which programming language to use

Mon Apr 29, 2013 8:09 pm

This is more than a bit off, as thousands of programming "languages" have been developed over the past half-century, and the vast majority of them have nothing to do with C, which is really a platform(hardware and OS)-independent assembly language (of which there are a number of partially-incompatible versions), nor with BASIC, which can be interpreted or compiled (and there are many flavors of BASIC).
There's thousands of spoken languages in the world as well. What of it? C and it's spinoffs are unmoung the most widely used languages in the industry. Ignore it at your own peril. Those languages not directly rooted in C, will have borrowed syntax from it. With some exceptions of course. But at the end of the day, ever since Unix showed up C as been the staple of operating system programming, if you want fast efficiently programs you learn C.

Don't get me wrong though, I also use BASIC style languages all the time. They are fantastic prototyping platforms. But when writing serious software, I use C. More than anything else if you study and master C, you'll get work. No point learning skills that can't ever pay the bills in my book.

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

Re: Which programming language to use

Mon Apr 29, 2013 8:18 pm

My answer to this question is JavaScript.

Until recently JavaScript was that little language interpreter built into web browsers to help with things like responding to button presses and form input validation.

Today you can run JavaScript from the command line, as you would Perl or Python or BASIC. It's done with the Node.js interpreter. http://nodejs.org/

Node.js provide the ability to create a webserver entirely in a handfull of lines of JavaScript, see examples here: http://howtonode.org/hello-node

Then there are Node.js modules for dealing with serial ports: https://github.com/voodootikigod/node-serialport

Then there are Node.js modules for dealing with GPIO on the Raspi: https://github.com/EnotionZ/GpiO

All in all you can go from serial/GPIO input/output to web page with a couple of pages of code after a few hours of study.

This is altogether much easier than setting up a LAMP stack or writing a custom HTTP server in C or whatever.

Highly recommended.

Oh, yes. I don't think raw performance is an issue here but I have found that despite it's reputation JavaScript can perform almost as well as C++ when applied to web applications. After all most of the code that is doing the work is not your JavaScript application but the HTTP, XML, SSL whatever modules you are using, which are often written in C++ anyway.
Memory in C++ is a leaky abstraction .

SimonSmall
Posts: 220
Joined: Tue Oct 09, 2012 8:13 pm

Re: Which programming language to use

Mon Apr 29, 2013 10:04 pm

Jim Manley wrote:... (I'll bet 99% of the people here don't even know PHP stands for Personal Home Page...
There was me thinking that it was one of those early MIT joke things - a recursive acronym for PHP Hypertext Preprocessor (renamed 1997) :D

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Which programming language to use

Tue Apr 30, 2013 2:06 pm

TeachYourselfStuff wrote:There's thousands of spoken languages in the world as well. What of it? C and it's spinoffs are unmoung the most widely used languages in the industry. Ignore it at your own peril. Those languages not directly rooted in C, will have borrowed syntax from it. With some exceptions of course. But at the end of the day, ever since Unix showed up C as been the staple of operating system programming, if you want fast efficiently programs you learn C.
More than anything else if you study and master C, you'll get work. No point learning skills that can't ever pay the bills in my book.
There are more things in Heaven and Earth than are dreamt of in your philosophy ... at least you recognize that C (being essentially a general-purpose, cross-platform assembly language) is fine for "system programming" (which is more than just operating systems, BTW - there are thousands of embedded and real-time systems that have no operating system at all). The OP just wants to put some data on a web page, and you're recommending that he use a nuclear weapon to do some fly swatting - a nuclear weapon with a detonator that can be triggered by mistake thousands of ways. The other key words and tricky phrase are "if you study and master C" - we're talking about a rank beginner who will know nothing about the dangers of buffer overrun and underrun, providing inadvertent access to hackers to data structures in memory that have nothing to do with the sensor data he's trying to display, etc. It takes years of experience to master C, but there are hundreds of thousands of people who have never been properly trained to avoid the quite prolific pitfalls inherent in direct access to memory as well as allocation and release. Even a lot of people who supposedly should know better have spent decades unleashing bug-ridden, insecure piles of code equivalent of Swiss cheese masquerading as commercial-quality system software. There's nothing like using the right tool for the job, but using the wrong tool is almost universally a disaster waiting to happen.

I just find it disingenuous to recommend something that's completely inappropriate for what someone wants to do, especially when they haven't stated any intention of becoming a full-time software professional - they just want to solve a very specific problem as quickly and easily as possible. I don't see any C programmers recommending that people hand-build, maintain, and repair their automobiles, complete with a hand-crank in the front. If you're going to insist that everyone be a low-level expert, then be consistent and eat your own dog food in all you do.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Which programming language to use

Tue Apr 30, 2013 2:08 pm

SimonSmall wrote:
Jim Manley wrote:... (I'll bet 99% of the people here don't even know PHP stands for Personal Home Page...
There was me thinking that it was one of those early MIT joke things - a recursive acronym for PHP Hypertext Preprocessor (renamed 1997) :D
Recursion is the curse that keeps on giving ... ;)
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

Re: Which programming language to use

Tue Apr 30, 2013 2:19 pm

Thank you all for your responses.
I've been doing a lot of browsing and reading.
I have changed a few things (still on paper only)
I will drop the Humidity sensor
Replace the temperature sensor DS18B20 with a Pressure and Temperature sensor BMP085
I want to monitor 2 batteries and will use ADS1015 (a 4 channel ADC)
I will design a basic single transistor switch as a water detector and a few other things detector.

The more I read, the more I realize what I am trying to do (and much more) is supported by webiopi.
See this URL: http://code.google.com/p/webiopi/wiki/DEVICES It looks like I can do it all using Python or Javascript.

I think now is the time for me to stop reading and start testing, in the mean time I am always open to suggestions and ideas.

Mike

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Which programming language to use

Tue Apr 30, 2013 10:05 pm

A bit late on the Software discussions, but I chose Bash cos its universal to all Unix/Linux/BSD derivatives and it gets to the CPU directly.

C does the same but the learning curve ( arguably) is a lot steeper.

Assembler has a severe productivity restraint.

I have come across buggy C compliers also brilliant optimising C compilers. But my learning curve time is restricted so I thought , 'Lets go for the Jugular' *LOL*

:D :D :D

Tim

Return to “General discussion”