sebadfc
Posts: 2
Joined: Thu Sep 22, 2016 11:13 pm

Recommended Languages - Big project

Thu Sep 22, 2016 11:24 pm

Hey everyone.

I'm completely new to Raspberry Pi, so don't be too hard on me if this is a stupid question.

I'm planning on starting a project using the Raspberry Pi. The project includes several features such as consuming from a REST API, video recording between configurable hours and saving to SD card, GPIO (push button that should "mark" the second if a video is being filmed, LED light turning on while filming, etc), allow mobile devices to connect (either via the raspberry's bluetooth or via its wifi) for file transfers and a MySQL database in order to store certain information.

After searching a lot of information about how to achieve all this, i'm kind of confused on what languages I could use to program all of this, and wether I'm going to be able to program it all in one.

What languages would you recommend to program these features, and why?

If the question is too broad or wrong in any way, just point it out to me so I can edit it or give more information.

Thank you in advance!
Seba

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Recommended Languages - Big project

Fri Sep 23, 2016 6:00 am

Well it sounds like most if not all of that could be done with a shell script, thus avoiding programming at all, just write a shell script.

Though if you need to do it compiled, I would recommend something along the lines of C, C++, Pascal, or another procedural language that is compiled.

The question is very broad, though yes it would be quite possible to implement all of that in a single application.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

User avatar
bensimmo
Posts: 4187
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Recommended Languages - Big project

Fri Sep 23, 2016 6:37 am

Which ones do you know.
I guess multiple techniques and solutions will work, but go with wat you know.

Vidio and gpio is easy in python and you can store to the SQL database, it could be done in C, Java etc.
Wi-Fi is just normal Raspian setup, BT might be similar it I've not used it as a AP.
(And android file transfer is easy with the already made apps).


I don't know REST by no doubt shouldn't be hard from any of the major languages if you know what you are doing.


Which comes back to, which do you know?

sebadfc
Posts: 2
Joined: Thu Sep 22, 2016 11:13 pm

Re: Recommended Languages - Big project

Fri Sep 23, 2016 12:31 pm

Thank you both for your replies!

I know a little bit of C and C++.

Have more experience in Java/C# though, and work in web development using PHP and Ruby.

Maybe I didn't explain myself, but the question came up since I was reading about Python (which I have zero knowledge about). From code examples I saw online, I found it more of a scripting language and couldn't tell if I could develop the whole project using nothing but Python.

Regarding your questions about what languages I know, this is a medium-term project (about 1 year of development), so I don't mind having to learn a new language (it might even be a good opportunity to actually do so!). That's why I asked about your opinions and the reasoning behind them without narrowing it down.

Do you guys think this could all be programmed lets say with Python? Or do you recommend not doing so for any reason?

My RPI will be home in about a week and once I have it I will start making tests and small prototypes, but I'm trying to get a bit ahead and gain some time.

Thanks again for your time!
Seba

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Recommended Languages - Big project

Fri Sep 23, 2016 1:45 pm

It could be done in Python for sure. It could be done in just about any programming language.

Most of it could probably be done in shell scripts, there is a little of what you mention that would likely need a proper language such as Python, BASIC, Pascal, Ada, C, C++, Smalltalk, Forth, Java, ECMA Script, etc, etc.

Being the tasks you mention I do not see any advantage of one language over another.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

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

Re: Recommended Languages - Big project

Sat Sep 24, 2016 10:16 pm

sebadfc,
What languages would you recommend to program these features, and why?
If you can handle C/C++, Java and C# and your up for something a bit different I will suggest Javascript running under node.js.
Yes it looks like those language use know but it is also very different. JS has always had advanced features that those languages are only now getting in new standards. It also performs very well. Far better than Python, for example/

Why? You have a lot of asynchronous events going on, button inputs, resonses arriving from RESTfull API requests, requests from mobile devices, database responses etc. The Javascript event model makes juggling a bunch of things like that very easy. That is what it is designed for.

Also node.js makes it really easy to deal with web protocols, HTTP, HTTPS, websockets.

Also it's really nice to use the same language server side as in the client browser.

Practically, below is the beginnings of all the parts you have on your requirements list with links to further resources:

consuming from a REST API
-------------------------

Code: Select all

var http = require('http');

//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
    host: 'localhost',
    path: '/'
};

callback = function(response) {
    var str = '';

    // Another chunk of data has been recieved, so append it to `str`
    response.on('data', function (chunk) {
        str += chunk;
    });

    // The whole response has been recieved, so we just print it out here
    response.on('end', function () {
        console.log(str);
        // Do soemthing with reponse here.
    });
}

http.request(options, callback).end();
See: https://docs.nodejitsu.com/articles/HTT ... P-request/
https://nodejs.org/api/http.html

saving to SD card
-----------------

Code: Select all

fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
    if (err) return console.log(err);
    console.log('Hello World > helloworld.txt');
});
See: https://docs.nodejitsu.com/articles/fil ... in-nodejs/
https://nodejs.org/api/fs.html

GPIO
----

Code: Select all

var Gpio = require('pigpio').Gpio,
  button = new Gpio(4, {
    mode: Gpio.INPUT,
    pullUpDown: Gpio.PUD_DOWN,
    edge: Gpio.EITHER_EDGE
  }),
  led = new Gpio(17, {mode: Gpio.OUTPUT});

button.on('interrupt', function (level) {
  led.digitalWrite(level);
});
See here: https://github.com/fivdi/pigpio

Allow mobile devices to connect
-------------------------------

Code: Select all

var express = require('express'); // Express is a web server
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});
See here: https://expressjs.com/


MySQL database
--------------

Code: Select all

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
    if (err) throw err;
    console.log('The solution is: ', rows[0].solution);
});

connection.end();
See here: https://github.com/mysqljs/mysql


video recording between configurable hours and saving to SD card,
-----------------------------------------------------------------

Hmmm...I guess you want to run raspivid...

Code: Select all

const spawn = require('child_process').spawn;
const ls = spawn('raspivid', ['-o', 'vid.h264']);

ls.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
});
See: https://nodejs.org/api/child_process.html
Memory in C++ is a leaky abstraction .

User avatar
mc007ibi
Posts: 66
Joined: Wed Dec 16, 2015 7:36 pm
Location: barcelona

Re: Recommended Languages - Big project

Fri Oct 14, 2016 10:09 pm

@heater nice post!
I agree, with Node.JS and some shell scripts you can be done in short time.
Database: sqlite,
For the recording, bash.
Reading,setting gpio, bash.
file operations, bash.

and overall sugar to glue all together, I´d even take Lua. smallest, fastest and more portable on the long run.

User avatar
mc007ibi
Posts: 66
Joined: Wed Dec 16, 2015 7:36 pm
Location: barcelona

Re: Recommended Languages - Big project

Fri Oct 14, 2016 10:35 pm

what you are recording actually, your cats, right ? you could also use a opencv detector or ir sensor, record the file and tell over mqtt another device there is a new recording at http://.... that exists as ready to use, almost here: https://github.com/net-commander/window ... tracker.py. on the target computer you grab the file then just. no sd card or extra efforts needed :mrgreen:

peterlite
Posts: 720
Joined: Sun Apr 17, 2016 4:00 am

Re: Recommended Languages - Big project

Fri Oct 14, 2016 11:43 pm

"a shell script, thus avoiding programming at all"
A shell script is programming.

I would look for detailed examples then use that language. Python appears to be the most popular. When things are really tight, perhaps on a Zero, you often only have to write the most frequently used components on a faster language.

For something like a character translation, you might find a fast C library containing a function you can use in your easy slow language program.

As a comparison, I use a utility written in Python that is fast for a small file then slows down worse than a dead snail when presented with any significant work. I started writing a replacement in C. Painful. Writing the user interface in C is the worse torture imaginable outside of having to use Java.

I looked at writing the user interface in Python and the file processing in C. Unfortunately a big part of the problem is the slow Python user interface.

In the end, I used a library for the user interface, making a user interface in C almost as easy as in Python. The result is small enough to run on a Zero and faster than the USB 2 connection to the disk. The result is so fast that I think it often finishes before it starts. :)

User avatar
mc007ibi
Posts: 66
Joined: Wed Dec 16, 2015 7:36 pm
Location: barcelona

Re: Recommended Languages - Big project

Sat Oct 15, 2016 8:18 pm

so what´s you choice at the end?

python seems best fit rpi, for a long time. that´s there your native extensions need and will receive way more steadiness.

doing things with nodejs is pretty hard on the pi, it takes a while to get native extensions like serial-port or gpio or even audio access ready to deploy for sure. doing a fft on a wav formatted stream for instance consumes easily the whole chip without extra care. but that´s something you can leave to libvlc.

good luck,

DieOma
Posts: 86
Joined: Mon Aug 03, 2015 4:02 pm

Re: Recommended Languages - Big project

Sun Oct 16, 2016 6:33 am

If you see the possibilities in node-red you can imagine that nodejs should work. The community already has done the GPIO part. I see no time critical part on your description. Any language will do.

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

Re: Recommended Languages - Big project

Sun Oct 16, 2016 10:23 am

mc007ibi,
doing things with nodejs is pretty hard on the pi, it takes a while to get native extensions like serial-port or gpio or even audio access ready to deploy for sure.
How so? Do you mean getting node.js and any modules you require installed in the first place? That is perhaps a bit harder than it should be as the node version in Raspian is so old. But it's still very easy. Like so:

Use the node version manager to install node.js:

$ wget -qO- https://raw.githubusercontent.com/creat ... install.sh | bash
$ nvm install stable

Create your project directory:

$ mkdir myProject
$ cd myProject

Create a package.json file in your project (Useful later)

$ npm init -y

Install serial port module:

$ npm install serialport --save

Install GPIO module:

$ npm install pigpio --save

Install express module to make creating a web server dead easy:

$ npm install express --save

Now write your node.js program:

$ vim myProject.js
....

And run it:

$ node myProject

Or did you mean run time performance? When the questions of a comparison between Node and Python performance have come up here in the past we have always ended up with similar functionality in both and found node to be many times faster.

If you have an example of doing an FFT in Python I'd love to take on the challenge of creating the node.js equivalent to beat it :)

Having said that, typically one should not do long running computations in node as it will hang event handling and disrupt whatever else your program is doing.

But then you will have the same issues in Python if you create a loop that hangs for a long while.
Memory in C++ is a leaky abstraction .

qbo87028
Posts: 6
Joined: Fri Oct 14, 2016 11:53 am

Re: Recommended Languages - Big project

Tue Oct 18, 2016 4:24 pm

Heater's answer is really good. If you don't choose JavaScript, I recommend Python.

Some related libraries to check:

Video: picamera
Web, rest: Flask, Django
Mobile: Apache Cordova (python solutions are not recommended)

java factorial
Last edited by qbo87028 on Thu Jan 19, 2017 12:45 am, edited 1 time in total.

User avatar
mc007ibi
Posts: 66
Joined: Wed Dec 16, 2015 7:36 pm
Location: barcelona

Re: Recommended Languages - Big project

Tue Oct 18, 2016 7:09 pm

Looks the poster is already gone somewhere else with his project :cry:

@heater: yes, for a beginner it can be nasty to install things just but as soon it's there, it works pretty well; I agree. About FFT, I have one here in C but I still need to wrap this as native NodeJS module sometime. As much I'd have liked to have it done in JS, it seems better to do it in C just. The one in C is pretty good when it comes to low-spec devices, consuming little only. I used this for an iphone app to find your phone by whistling.... There is also another sound processing library called "node-core-audio" but you will end up in awful installation errors, same for serial-port (won't work because of their installer script) btw. Doing recently lots with audio/video on the PI, I am more likely to extend libvlc just. Their foundation code is pretty good.

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

Re: Recommended Languages - Big project

Tue Oct 18, 2016 8:05 pm

Have you tried compiling your FFT in C into Javascript using Emscripten?

I have found that such "transpiled" code can end up being almost as fast as compiling to native code. Give or take a factor of two.

Not sure how well this works out for JS run on ARM though.

And of course there is the complication that a big FFT will take significant time, so it should run in a separate process/thread and call back asynchronously when it is done.

It's true enough that node.js is still quite new and has been developing rapidly. And things break.
Memory in C++ is a leaky abstraction .

User avatar
mc007ibi
Posts: 66
Joined: Wed Dec 16, 2015 7:36 pm
Location: barcelona

Re: Recommended Languages - Big project

Wed Oct 19, 2016 9:09 am

@heater, hey yeah. fully forgot about this one. let me try :P

Return to “General programming discussion”