Tormach's Path Pilot Home Assistant Integration
by greg.vinyard915 in Workshop > CNC
931 Views, 4 Favorites, 0 Comments
Tormach's Path Pilot Home Assistant Integration
The purpose of the Project was to integrate Tormach's Path Pilot controller into my existing IOT network so that I could receive notifications regarding the status of my CNC Job's, primarly tool changes as I lack an Automatic Tool Changer. This can be extended into to many different custom uses to monitor different hardware, as well as using custom M-codes to execute software in Path Pilots Linux cnc background.
Supplies
- Raspberry Pi 4b
- SD Card
- Alternate carrier for HASS.IO (docker, or Virtual Machine)
Prerequisites
This walk-through will assume that you already have some baseline knowledge. Here are some things you should know/have or at least be relatively familiar with before attempting to follow this guide.
A VERY IMPORTANT NOTE: make sure whichever version, or method you use to install HASS is SUPERVISED, otherwise the following instructions will not work. BEFORE DOING ANYTHING READ ALL THE WAY TO THE END OF THIS DOCUMENT, there's some notes in step 6 that can save you a lot of time.
- How to Install Home Assistant in whichever manner you want. I have found great stability out of a Raspberry Pi, but some people use alternate methods. This has been beat to death on Youtube so just search "Home Assistant Installation".
- Basic Navigation through CLI in Linux.
- How to execute python code through CLI in Linux.
- How to install Python and Python packages.
- Internet connection to Path Pilot Controller. (Save yourself a headache and hard wire it, more on this later).
ADDITIONAL NOTE: The Hass OS release from 16 days ago (December 28th) is extremely unstable when I have run it on an RPI 4. You can try the RPI3 (if your using an RPI3) version, otherwise go to the link below and choose a release from 12/03/21 and back. Those have proven stable for me.
https://github.com/home-assistant/operating-system/releases
Set Up MQTT in Home Assistant
Firstly we want to setup home assistant as an MQTT broker. MQTT is a publish and subscribe IOT protocol that is fairly standard so it has very wide integration into a lot of platforms, including python.
- Navigate in HASS (Home Assistant) to the Gear Icon on the bottom left.
- Choose Devices and services.
- Click Configure supervisor.
- Bottom right there will be another add on store.
- Search MQTT and there broker should pop-up, Go ahead and Install it and Start it.
- Add a New User by going back to configuration and choosing people and zones. I've named mine MQTT, make sure to allow the person to login, this will allow you to set a username and password which is needed for Authentication.
- Now go back to devices and integrations and on the first screen, not in the supervisor you should now see an MQTT broker.
- Click Configure and remove the stock user and password credentials and replace them with the ones you just created. Go ahead and click save, another screen will pop-up just leave everything stock and save the configuration.
- You can now test this on the same screen by listening to a topic. I used an APP on the iphone called MQQTool - by Brent Petit. Just make a connection to the MQTT broker (your HA's IP address on port 1883), use the username and password you created above and click connect.
- On the bottom of the app click publish and create a topic and message.
- Set the MQTT on the configuration page to listen to the same topic. Note it is case sensitive. Once you publish the message it should appear in Home assistant.
Note: If you Struggle here this is a great video that will walk you through most of the MQTT install.
https://www.youtube.com/watch?v=dqTn-Gk4Qeo
Install Paho-Mqtt on Path Pilot Controller
Ok so you have made it through the Home Assistant Set-up, this step should be somewhat easier if you have done anything in linux before.
- We first must install PAHO-MQTT unto the Path Pilot Controller so we must reach the desktop from Path Pilot. Simply hit exit and when the pop appear's hit ok and then immediatly hold down alt+shift until the navbar for the desktop appears at the bottom.
- Once it appears open the terminal and verify if Python3 is installed (Note: python 2.7 is no longer supported as of the 1st of 2020.) Type "python3 --version". It was already installed on my Path Pilot Controller.
- Now install Paho MQTT by using "pip3 install paho-mqtt". You may need to install pip if you do this should work "sudo apt-get -y install python3-pip".
- If it installed sucessfully move onto the next step.
Custom M Command
OK so this is where things can get murky so I'm gonna do my best to explain clearly.
The theory of what we are doing is that LinuxCNC which Pathpilot is built on, is capable of accepting custom M-codes. There was no documentation for this that made sense, or was complete enough to follow to accomplish this so I muddled through. M-code commands in the LinuxCNC documentation are able to be executable files. We will make an executable python file using Paho-MQTT that will publish a message and topic that home assistant can hear. We can then leverage that in our Home Assistant Automations to notify our phones. My use case is that its time to change a tool, but if your using an automatic tool changer, you can set this up to notify you when your program is done and its time to load more stock.
- Firstly, and most important, make sure your Path Pilot is up to date it should be 2.8.3 as of this writing.
- Enter the folder in the following path through the GUI not the CLI. /home/operator/v2.8.3/configs/tormach_mill/nc_subs.
- In this file Make a file with the following Syntax M###.py. Must be capital M and the 3 #'s must be replaced with a number between 101-199. I named mine. M150.py. This Syntax is from Linux CNC.
- Right click on the file and change the permissions to allow it to be executable.
- Now paste the code from below, make sure you input your own information in the fields, and verify the python code indents are correct.
- Navigate to the folder from Terminal and execute the python file using python3, make sure it prints success. Also Make sure that your Home Assistant is listening on the Topic you established in the code. If it worked Home Assistant should hear it and look like the picture above. Congrats if you have made it this far we are about 95% of the way through.
- If your doing this on windows be aware of EOL and Encoding, otherwise just type in on the Path Pilot Computer.
#!/usr/bin/env python3
# coding=utf-8
import paho.mqtt.client as mqtt
mqttBroker ="" #insert Home assistant IP Address here between the qoutes.
client = mqtt.Client("Change Tool") #Name the client whatever you want
client.username_pw_set(username="user",password="password") #use username and password from Step 1
client.connect(mqttBroker) #this connects to the broker
#This loop creates a topic called "NEW", and Sends a payload of "Change Tool"
while True:
client.publish("New", "Change Tool")
print('success')
break #make sure this is there other you will flood your own mqtt Broker with a infinite loop.
Edit the .INI File and Execute M-CODE
Here we are at the last step.
- Remove the .PY from the end of the file you created in the last step. I'm not sure if this is necessary but the LinuxCNC Documentation said the M code file format must be the same as the code. So your file should look like this M150, instead of M150.py.
- Now copy the folder path it or at least keep it handy, it should be the same as the last step "/home/operator/v2.8.3/configs/tormach_mill/nc_subs".
- Move to the enclosing folder and open your machines .INI file. For me its tormach_770m.ini.
- Find the line with USER_M_Path and copy and past the line above. Comment the original line out and paste in the file path from above and save it. It Should look like this USER_M_PATH = /home/operator/v2.8.3/configs/tormach_mill/nc_subs
- Reboot Path Pilot and home your axis'. Make sure your Home Assistant Server is listening as we did above and issue the new M-code (M150 for Me) and the green light should flash on the Cycle Start button below the MDI.
- Check and See if Home Assistant registered the message. If it did Congratulations Path Pilot is now an IOT Device!
Final Notes and Next Steps
At this point you have your custom M-code that can talk to Home Assistant via MQTT. From here your next step is to edit your post processor to implement the M command based on your use case scenario, for me I changed my post processor to input M150 after each M01 command. This was trial and error for me but this is the video that got me through it (https://youtu.be/4OWT-O4oN8E) from NYCNC. Now you can install the home assistant app on your phone and connect to your server and get notifications using automations through home assistant. There are numerous guides on how to do this simply search MQTT automation Home Assistant on youtube. Or MQTT Notify Phone Home Assistant.
Final Notes:
There are a few quirks I came across that you should note that required extensive time in debugging, and I thought that I would include them here to save you time and anguish in implementing this so below is the list.
- Path Pilot's wifi configuration is unstable. Hard wire it, if you can't get wire there throw a range extender that has an ethernet port and plug that into the controller. Trust me on this. The one I used is TP-Link's N300.
- This one I don't have an explanation for. When I initially input that M-code, it was into the path already specified in the USER_M_PATH, but it did not activate the M-code when called from the MDI, It wasn't until I appended the INI file with the exact file path as per the instructions that Path Pilot was able to recongize and issue the command. Funny thing is that after you reboot the controller and open the .INI file the changes you made are gone but the M-code still works. If anyone has an explanation as to why appending the file path suddenly allows the M-code to function but the change isn't locked in I would love to hear it. Additionally changing the Read/Write permissions does not do anything when the controller is rebooted the changes are still undone. This means if you attempt to use any other directory or Path this process will fail.
- Path Pilot's Ethernet connection to controller uses the IP Address of 10.10.10.9. So if your like me with a network that also uses 10.10.10.# than a wifi connection will be impossible, you will need to change your network to use a differen't range of IP address's. Most people will use something like 192.168.1.# so it shouldn't be a problem but make a note of it and don't waste an hour trying to figure it out like I did.
- DO NOT ATTEMPT to execute the python file created from any of the built in IDE's inside of the path pilot controller, it will result in a module not found error because the default python intepreter is 2.7. Even if you attempt to change the interpreter from Geany or another IDE, it still did not work for me.
- I will login here to check comments occasionally and respond to questions, if you don't here back immediately be patient.