This tutorial is focused on illustrating the hardware aspects of the ISOIOM17427 Isolated I/O module specifically accessing the control registers via a remote web app running on a local browser.
This tutorial assumes you're somewhat familiar with the Raspberry Pi and have setup the NodeJS environment. All the code in this tutorial is written in JavaScript and will utilize the à la mods "comm" library (alamods-comm.js) to simplify reading and writing the modules internal registers.
The .js source file for the backend can be downloaded here.
This tutorial creates two independent files that make up the complete app. One is the backend .js file that runs in the node environment, acts as a webserver to serve the web app page then as a websocket server to communicate with the frontend app running in the browser.
All of the hardware control is performed by the backend NodeJS process, but it is directed by the messages from the front end web app.
The second file is the web app index.html file that runs in a browser.
We need to load the à la mods "comm" library first before we begin working on the app. To get the à la mods "comm" library and install it run the following commands from the home directory. This will retrieve the compressed library file, extract all the files into its directory structure and then using "npm" install all the NodeJS dependency modules.
Another method of getting the à la mods "comm" library is via the "software" section within the resources page of the website.
One more general thing to do before we get into the actual application. We need to setup the file structure for NodeJS development. When running "npm init" you can use the defaults to most of the questions.
We need to add a couple of modules that are not part of the native NodeJS set. Within the same directory run the following commands:
Once that is complete we can develop an app that runs on the Raspberry Pi to control the I/O and LED of the ISOIOM17427 module. If you're developing this directly on the Raspberry Pi you can use "nano" or your favorite editor. if you're using a PC you can use any web development IDE on a PC. We use JetBrain's WebStorm and Microsoft's Visual Studio Code, but anything will work that allows you to create and load the files onto the Raspberry Pi.
Add the following code:
When the app file is completed save it and run the following command:
The output should look like:
> 17427
> httpd listening on port 3000...
The output on the terminal should be 17427, the LED should go to magenta and a line should appear stating the http web server is listening on port 3000. Now the backend app is ready to serve the web app page and accept websocket connections.
Next we'll need to create the web app front end index.html file. This file should be located in the same directory as the backend isoiom17427-webapp-registers.js file.
To run the app make sure the backend app is running and listening for web connections.
In a web browser on the same machine as the backend app type in following URL:
Or on another machine on the same network as the Raspberry Pi type in following URL:
At this point you should see a web page with two buttons. One is "ON" and the other is "OFF". If all is working as planned when you click on the "ON" button the LED on the ISOIOM17427 module will turn magenta and all the relays will be energized (their associated LEDs should turn on also).
What is happening here with all of this? ALOT!
The backend has a web server listening on the port specified (3000). When the web app page is requested by going to "localhost:3000" the web server looks for the "index.html" file and sends it back to the browser. So far pretty standard web stuff. As the browser begins to parse the "index.html" file and setup the DOM alot more happens.
Once the browser hits the "script" tags the fun begins. The first one tells the browser to go out and request "socket.io.js" which is a library that makes handling asynchronous bi-directional messaging between the frontend web app page and the backend very straightforward. It has tremendous capability. Check it out.
After "socket.io.js" is loaded the browser starts to setup the next script which first tells the browser to open a websocket connection back to host where the page came from. Once the connection is opened the browser installs two event handlers on the buttons. Now the web app is completely setup and ready to go.
When one of the buttons is clicked the appropriate event handler fires sending a message over the open websocket back to the backend. The message is a serialized JSON object that is created by JSON.stringify({ type: 'on' })). Once it reaches the backend app it is deserialized by JSON.parse(data) and, therefore turned back into a JSON object. Which is very easy for the backend to handle.
The backend processes the message according to the "type" string in the message (i.e. 'on'). This occurs in the switch statement of the WEB APP CLIENT section. Inside each "case" of the switch statement is communication to the ISOIOM17427 module to perform the appropriate action according to the message type received from the front end web app.
Although we're showing uni-directional flow more could be added so the backend could push status updates asynchronously to the web app front end.