Skip to main content

@cdswindell posted:

I have my engines starting up and shutting down via Alexa!! It's a start...

  -- Dave

Alexa Skill can now:

  • Start up and shut down engines with or without dialog
  • Set engine speed, with and without dialogs
  • Reset engines
  • Blow whistle/horn
  • Ring bell
  • Change direction
  • Stop immediate
  • Open front/rear couplers
  • Send Halt command
  • Throw switches
  • Fire routes

Now onto setting speed, etc...

  -- Dave

Last edited by cdswindell

The PyTrain API project is up and live on PyPi. You can download it here. The project includes the single executable, pytrain_api, which is all you need to start serving commands. The package includes a Uvicon web server, which launches the PyTrain API on port 8000.

You will need an API Key to make calls. I need to figure out how I want to do this long term (if others download and use the API), as you really don't want someone taking over your layout! For now, use the API key:

e54d4431-5dab-474e-b71a-0db1fcb9e659


This will change, but it will work for now.

Once you install the package (from a virtual environment, of course), hit the URL:

http://<your host ip>:8000/pytrain/v1


to get a list of the supported APIs, You can even try them out from this web page! To do so, hit the green [Authorize] button at the top right of the page and enter the key from above. All of this functionality is provided automagically by FastAPI; really cool stuff!

  -- Dave

@cdswindell posted:

The PyTrain API project is up and live on PyPi. You can download it here. The project includes the single executable, pytrain_api, which is all you need to start serving commands. The package includes a Uvicon web server, which launches the PyTrain API on port 8000.

You will need an API Key to make calls. I need to figure out how I want to do this long term (if others download and use the API), as you really don't want someone taking over your layout! For now, use the API key:

e54d4431-5dab-474e-b71a-0db1fcb9e659


This will change, but it will work for now.

Once you install the package (from a virtual environment, of course), hit the URL:

http://<your host ip>:8000/pytrain/v1


to get a list of the supported APIs, You can even try them out from this web page! To do so, hit the green [Authorize] button at the top right of the page and enter the key from above. All of this functionality is provided automagically by FastAPI; really cool stuff!

  -- Dave

Dave,
Not sure if this would help
import secrets
import os

def generate_api_key(length=32):
"""Generates a secure API key."""
return secrets.token_hex(length)

def save_api_key(api_key, filename=".env"): you can change the filename here
"""Saves the API key to a file, handling existing files."""
if os.path.exists(filename):
with open(filename, "a") as f:
f.write(f"API_KEY={api_key}\n")
else:
with open(filename, "w") as f:
f.write(f"API_KEY={api_key}\n")

if __name__ == "__main__":
new_api_key = generate_api_key()
print("Generated API Key:", new_api_key)
save_api_key(new_api_key)
print("API key saved to .env file.") and here

The above code will auto gen an api key results of what it does below

biqu@SBM4P-CB1:~$ python3 api_gen.py
Generated API Key: d51887d41f748d25bd8cc409372cffb7b64683451b81f2e950997e324ca22cc6
API key saved to .env file. will list your filename

Last edited by Shawn_Chronister
@cdswindell posted:

The API and PyTrain DB commands now report consist information (what components make up the train).

I'm guessing this means when there's a lash-up that's been assigned a "TR" number in the system? All of a sudden, I had visions of putting IR sensors on every piece of rolling stock and reporting on entire consists and the locomotives they are being pulled by... 😂

@J.Dooley posted:

I'm guessing this means when there's a lash-up that's been assigned a "TR" number in the system? All of a sudden, I had visions of putting IR sensors on every piece of rolling stock and reporting on entire consists and the locomotives they are being pulled by... 😂

LOL, yes, multi-engine consists defined with the Cab 2/Cab 3 TR button. The Base 3 makes the information available as to what components make up the consist, along with which direction they're facing, and if dialog and horn sounds are masked or not. Adding support to decode/report this was on my "to do" list.

  -- Dave

I’ve added a couple of new features, including:

  • can now send PDI commands directly to LCS ASC2 and BPC2 (via Base 3 only). The ASC2 command lets you explicitly turn on and off a relay, rather than depending on the timing of how fast multiple acc aux2 commands come in. Also better at reporting accessory state.
  • pytrain clients can now send pdi commands (proxies thru a pytrain server to the Base 3)
  • added -repeat option to support sending accessory commands (to simulate holding down aux1 key)
  • added -duration option, which allows you to do things like "blow the horn for 10 seconds", and "rotate the men inside the control tower for 20 seconds". This could be simulated by using the "-repeat" command, but the -duration option is much easier to use.
  • added relative speed command control for accessories (TMCC-style)
  • sensor track state now remembers the last engine/train that traversed as well as the direction (left to right, right to left). I’m toying with adding some form of automated block control and this will be useful to know…
  • fixed bugs...

  -- Dave

Last edited by cdswindell
@cdswindell posted:

I’ve added a couple of new features, including:

  • can now send PDI commands directly to LCS ASC2 and BPC2 (via Base 3 only). The ASC2 command lets you explicitly turn on and off a relay, rather than depending on the timing of how fast multiple acc aux2 commands come in. Also better at reporting accessory state.
  • pytrain clients can now send pdi commands (proxies thru a pytrain server to the Base 3)
  • added -repeat option to support sending accessory commands (to simulate holding down aux1 key)
  • added -duration option, which allows you to do things like "blow the horn for 10 seconds", and "rotate the men inside the control tower for 20 seconds". This could be simulated by using the "-repeat" command, but the -duration option is much easier to use.
  • added relative speed command control for accessories (TMCC-style)
  • sensor track state now remembers the last engine/train that traversed as well as the direction (left to right, right to left). I’m toying with adding some form of automated block control and this will be useful to know…
  • fixed bugs...

  -- Dave

I've added these features to the PyTrain REST Api.

  -- Dave

After writing complicated code to use rotary encoders and potentiometers to control the Lionel Crane Car, Gantry Crane, and Smoke Fluid Loader, I've realized its much simpler to just use a momentary contact switch-based joystick. These devices have just 2 states per position; on and off. They can directly map to Cab-1 RELATIVE SPEED or the Boost/Brake command. Using the gpiozero library, you can code it up so that if the joystick is held "on", the command can be fired repeatedly. I'll send along a video shortly, but this code is much simpler and works much more reliably!! I still allow the use of a rotary encoder to rotate the crane housing, but it also just sends a relative speed command with no acceleration factor. It doesn't look like a cab rotates any faster or slower on the Cab-3, so there isn't a need for complexity.

  -- Dave

Here is a video of my TMCC Crane Car being controlled by a 2 axis joystick. And another of the TMCC Smoke Fluid Loader being controlled by a rotary encoder. Since making this video, I modified the code to speed up the boom movement by sending commands every 0.020 seconds (the Cab 3 sends commands every 0.010 second). These sorts of changes are very easy to do in software. I may even make it a parameter so users could control the timing themselves without recoding.

  -- Dave

P.S. I just added a parameter to these classes to allow you to set this time without recoding

Last edited by cdswindell
@BillYo414 posted:

Well that is simply amazing to me! Excellent work!

It's also pretty cool that they're both connected to the same Pi. What length of wire do you estimate you can put between your controllers and the Pi?

Do you mean the length of wire connecting the joystick to the pi? Probably something measured in yards, given it is simply carrying a 3.3v signal to ground. My goal is to keep that distance short because I will mount the control panels close to where the layout components will live and use a separate Pi in each panel (I just bought 10 new Pi 5 computer modules!!)

  — Dave

Add Reply

Post
×
×
×
×
Link copied to your clipboard.
×
×