What do we actually need to meet the opening posters requirements?
1) A program running on the Pi that can read and write GPIO, interact with sensors attached via I2C, SPI, Serial whatever. Perhaps read and write files or to a database. Perhaps interact with other devices over the network.
2) HTML to render in the browser as some kind of GUI interface.
3) Javascript to go with that HTML page. This will make it possible to get data for display in real-time. Self-updating as it were. It makes things like rolling charts and such possible.
4) A means of getting that HTML and JS into the browser. That is to say a web server.
5) A means of communicating with between the web page and the program we have running so as to get data updates post user input for control etc.
Clearly we are stuck with HTML and Javascript. That is the only thing we can use in the browser GUI. That determines 2) and 3) above. What about the rest of it ?
Our program could be written in almost any language, C, C++, Python etc. There are libraries available for many of these to help with the hardware interfacing.
Items 4) and 5) are going to require a web server.
Traditionally this was provided by Apache or some such server package. Because that is all there was basically.
Of course that server can't just serve up static HTML text files to the browser. In order to display data that changes with time it needs to generate those pages on demand with current data values. So we need some programmability in the web server. Traditionally this was provided by PHP.
Now we have complex mess on our hands, we need to work in many languages, C or Python or whatever plus PHP plus HTML plus Javascript plus CSS. And we need that Apache thing. And we need to get them all playing together nicely. This is horrible.
We can simplify things by throwing out Apache and PHP and serving the HTML and JS from our control program directly. There are libraries available for many languages to make this easy.
That's better, now we only have our control program and the web page. We only need to work in Python (Or other) plus HTML and JS. Nice and simple.
But let's go one step further. Write the control program Javascript as well. Now we have a dead simple system of only JS and HTML. Sweet.
As a bonus node.js makes real-time communication between our program and our web page dead easy. Using web sockets.
Node.js makes this possible. Not only possible but very easy. With superior performance to Python or Ruby. And arguably a much bigger user community and hence support network. Very important when you are not sure what you are doing!
This is a "Hello world" web server written in node.js:
Code: Select all
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Interacting with your sensors, GPIO, files, etc is similarly simple.
Contrary to mfa298 I would not call the node.js solution a "framework". You are just programming in a programming language, like any other. node is the run time for Javascript in the same way as Python or Java has run times. "framework" makes it sound like some big complex thing that dictates the form of your program. On the contrary, it's just a language with tens of thousands of libraries (modules) that people have contributed. Use them if you like.