Stepper Pi Node

The mission on this page is to control the three motor drivers on the Stepper Pi Hat using a web browser. It is evident that this requires various items:

  • A web page - by which I mean html, css and javascript
  • A server to pass the web page to the browser
  • Some way of exchanging commands and responses between the browser and the stepper process (see Stepper Pi for the stepper executable).

For the web page I had the idea I would be able produce beautiful designs if I used the Bootstrap framework. In addition this means web pages should adapt to mobile devices.

For the server I used node.js with Express.

The stepper process communicates via two FIFO local file system pipes - textual commands and responses flow in and out through these. WebSockets allow javascript running on the browser to connect to these two pipes. I use the Socket.IO library for node.js to implement the WebSockets.

Joining the WebSockets to the FIFO pipes sounds trivial. I struggled for a while. The obvious idea is when one gets a connection from the browser open the FIFO pipes with node.js fs streams and set them up so that when data arrives it is sent to the socket that has just connected. I found I lost data. What was happening was that the browser had opened a new connection with new streams created but the old streams were still open and passing data to non-existent sockets. I worked out that I should destroy the old streams on disconnect events, but that is not as simple as it should be and although I got it to work a lot better it was not perfect. I eventually realised that a stepper motor, like a man, can only have one master and whilst most people using Socket.IO have multiple browser sessions, what is required here is a crude single connection implementation with global variables.

Installation
Get Stepper Pi

Then install the latest version of node.js following these instructions
Install Latest Node JS on Raspberry Pi All Models

Download:

The download will unpack to a folder called 'test' which you can put anywhere - typically in Documents.

Use
Open a terminal in the stepper folder, issue the command:

sudo ./stepper pipe debug

Open a terminal in the 'test' folder which contains the node.js app, issue the command:

node app.js

Both these terminal sessions will not return immediately and should be left open. You can Ctrl+C stop them. They will print information about commands being passed.

Finally in a browser enter the URL:

http://localhost:8080

and the web page interface will appear. Commands can be entered and buttons clicked.

I also enabled WiFi on the Pi following this page:
USING YOUR NEW RASPBERRY PI 3 AS A WIFI ACCESS POINT WITH HOSTAPD

I ignored all the dhcp stuff and used a fixed ip address for the Pi on the WiFi network of 10.0.1.1 which meant I could connect from my tablet using the URL:

http://10.0.1.1:8080

This proved powerful magic since Chrome on the tablet managed to upgrade itself from the internet via the Pi.

Software sources:

Comments

Click to return to index

Page last modified on March 12, 2019, at 12:42 AM
Powered by PmWiki