Hi Onefinitians!
My name’s Gary, and I’m a self-taught tech enthusiast working on a project for my Old Man. The goal is a common one here on the forums: turning on dust collection and router power based on the state of the machine. I’m throwing in a multi-camera timelapse setup as well, 'cause it’s cool.
I’ve seen several schemes take advantage of the Onefinity controller’s hardware comms pins for input. @SurfinGump has a great writeup here.
This is clean. If we had a breakout board, I probably wouldn’t be writing this. We do not, and I’m ankle-deep in Raspberry Pis, so we’re going with a software-based approach to monitoring the machine state.
This topic came up when I wishfully searched for “Onefinity machine state” on this very Discourse. @wanoo links to the Onefinity github - specifically this list of API endpoints. This is our golden ticket, but I’m getting ahead of myself.
I’m posting here to make this machine more accessible to anyone who wants to automate around it, regardless of programming experience. I’m a novice myself, so if I seem inept to any real live programmers, it’s because I am.
With that in mind, here are a few definitions as I understand them. Please let me know what I get wrong!
What is an API?
API stands for Application Programming Interface. What it is, is a tool programmers can use to allow other programs to send and receive messages from their program. It’s a little like adding a post office to your code.
The messages are sent using HTTP requests.
What’s an HTTP request?
HTTP stands for Hypertext Transfer Protocol. What it is, is a set of rules computers use to send text back and forth over a network to any other computer with an IP address.
What’s an IP address?
An IP address is a number that identifies a computer on a network. It’s analagous to a phone number. You can find the IP address of your machine in the Network settings.
What’s Node-RED?
Node-RED is a programming environment that almost keeps you from having to write any code. You drag and drop commands (represented as colored blocks) from a toolbox onto a “canvas,” and then draw “strings” between them to create a flowchart.
Why do I care?
The user interface that comes with the Onefinity controller is just a web page that listens for information from the API to show status information, and sends commands via the API to initiate actions.
Using a browser on a computer connected to the same network as our Onefinity controller, we can do things like:
- Look at your firmware logs (xxx.xxx.xxx.xxx represents your machine’s IP address) - http://xxx.xxx.xxx.xxx/api/log
- Pause/Unpause a running gcode file - http://xxx.xxx.xxx.xxx/api/pause - http://xxx.xxx.xxx.xxx/api/unpause
- Get a dump of all firmware variables - http://xxx.xxx.xxx.xxx/websocket
Copying and pasting individual API endpoint URIs into a web browser isn’t terribly convenient, but it’s a ham-fisted way to see which endpoints do what in lieu of documentation.
However - we want to do more than manually send commands using our browser. We want to react when something changes. And for that, we’ll need to start our program.
Install Node-RED on something. I’m using a Raspberry Pi 4 'cause I have one handy, but you can run Node-RED on just about anything.
Once Node-RED is installed and you’ve got it up and running in a browser window, string together: websocket in >> debug
Double-click on the websocket node. Set “Type” to “Listen on”, and click the “+” button next to “Path.” Enter “/ws://xxx.xxx.xxx.xxx/websocket” without quotes, substituting your Onefinity controller’s IP address for the Xxes. Click “Add,” then click “Done.” Click “Deploy” in the upper right-hand corner. Congratulations - you’re a programmer!
Now we should start seeing raw messages from your controller as they come in to the websocket node from the Onefinity API over your network. The first packet contains all variable names and their values, and the following packets contain only name/value pairs that have changed since the last update cycle.
Here’s some data that came from our rig: websocket-trimmed.json - Google Drive
My comment on the post linked above has a more detailed example function that filters machine state data from the unfiltered deluge.
I’m bordering on a novel here. Before I lose all coherence, I’ve got to hop off.
Having so much fun with this machine. Hope you are, too.