The project belongs to @imillard and he has clear ideas on what he wants to do and suggestions have to be within that context. Wireless connections between the staff instruments may be more complicated than wire connections but it is pretty much standard in today's world. The original machines were connected by a wire and were electro-mechanical in their construction.
Clearly you have the ability to design and 3d print objects as your staff instrument case looks pretty good!
I am by no means an expert when it comes to 3D designs and subsequent printing of them. I am still learning.
FWIW, I use Autodesk Fusion for my designing and use a Prusa Mk3s filament printer.
After reading the last @tfmccarthy posts and then going back over the all the posts I realise I will probably be of little help. I imagined a much simpler solution and missed the bit at the start that your main desire was to learn to use two ESP32's to 'talk' to each other via ESP-NOW which is something I have never used. The coding I was working on was simply the logic of detecting the direction of the movement of the staff from two sensors and assumed all the instruments would be controlled from a single controller.
I did find it an interesting problem but was coming from a different direction. Sorry for wasting your time.
You haven't wasted my time at all!
Your contributions to the discussion have been well received and keeps the thought process juices flowing!
Cheers,
Ian
Ian Millard
Port Macquarie, NSW, Australia
ESP32/Arduino etc novice
The project belongs to @imillard and he has clear ideas on what he wants to do and suggestions have to be within that context. Wireless connections between the staff instruments may be more complicated than wire connections but it is pretty much standard in today's world. The original machines were connected by a wire and were electro-mechanical in their construction.
As I have said right from the start, I have always been open to any and all suggestion for this project. The main reason I will most likely go down the ESP-NOW route, is after seeing a YouTube video on its capabilities, I thought it would save a lot of time running cables between each machine.
Having said that, I also understand that @tfmccarthy is putting a lot of time into the coding. Also, in the future, this staff machine system could be made available to other modellers, so I was hoping for a 'Plug and Play' type solution to make installing the system easier for others.
The wired solution would definitely work as well, but I would have some quite long cable runs on my layout.
We'll see what @tfmccarthy comes up with and go from there.
Cheers,
Ian
Ian Millard
Port Macquarie, NSW, Australia
ESP32/Arduino etc novice
Also, in the future, this staff machine system could be made available to other modellers, so I was hoping for a 'Plug and Play' type solution to make installing the system easier for others.
And I agree with that. My keyboard, mouse and printer have wireless connections. Actually I ended up using the usb connection to the printer as I was having trouble getting the wireless to work and the printer is not shared with any other computers. Just about every new electronic item can be controlled via a direct wireless connection or via the internet to an iPhone app or PC app. That is the future.
The wired solution would definitely work as well, but I would have some quite long cable runs on my layout.
https://en.wikipedia.org/wiki/Digital_model_railway_control_systems
"Some digital control systems provide the ability to independently control all aspects of operating a model railway using a minimum of wiring, the rails themselves can be the only wiring required. Other systems are wireless. Control is achieved by sending a digital signal as well as power down the rails or wirelessly. These digital signals can control all aspects of the model trains and accessories, including signals, turnouts, lighting, level crossings, cranes, turntables, etc.
Constant power is supplied to the track and digital signals are sent which require electronic decoders to be fitted to locomotives and other devices to interpret the commands."
I actually read a lot about model train controllers some time ago (years) as a number of posters on the forums were model train enthusiasts but I didn't know about the staff machines. Trains today can use mechanical and electrical means to make it impossible for two trains to collide. Even back in those days I don't see why the staff machines couldn't have also locked the train tracks at the other end of a section.
Well, I'm satisfied with what I have now. It serves as a "proof of concept" and should provide a reasonable simulation of the device. Details to follow, but let's see if I can attach the files. There should be 4 files: METS.ino, .cpp.h, .h, and dbws.h.
(The .cpp.h file allows me to attach a cpp file.
What I'm looking of is build verification on 2 machines, (preferably Linux and Mac), but 2 successful builds should be sufficient. Note the version number for esp32 library. I will explain in due time.
Contents
The best is the enemy of the good
- Italian proverb
Overview
METS is a "proof of concept" simulation of a miniature train staff machine used with a HO model train. The concept is fromimillard described here in the DBWS forum.
The train track layout for the simulator is
The software simulates the staff machines on the track labeled "Staff Section B", between Karkool and Ardglen. The simulator software controls a miniature replica of a staff machine. For further details, see the DBWS forum discussion.
References
I used the following DBWS videos as reference material when developing the simulation:
- ESP-NOW – Peer to Peer ESP32 Network – 2022 Videos – DroneBot Workshop Forums
- Using Servo Motors with the ESP32 (dronebotworkshop.com)
I highly recommend viewing these to get a firm understanding of the circuit and the simulator
The ESP-NOW video covers using the ESP-NOW API and is handy for integrating momentary button (MOM) components.
The Servo video is helpul for incorporating and controlling servo motor components
Simulation Model
Circuit
Bill of Materials
- 4 LED lights (2 redm 2 green)
- 4 220K ohm resistors
- 2 MOM miniature buttons
- 2 9gm servo motors, Mine are marked as S51 but any SG90 should work.
- 2 ESP32 boards with USB cable. I used a "run of the mill" WROOM 32 development board.
- 1 bredboard power supply that can supply 5 and 3 volts. I used one from Elegoo.
- ~17 male-male 22 gws wire, ~ 5in. length.
Schematic
GPIO Pins
GPIO pin 19 - plungerServoPin GPIO pin 18 - extractButtonPin GPIO pin 5 - insertButtonPin GPIO pin 21 - SectionServoPin GPIO pin 15 - inuseLEDPin GPIO pin 2 - availableLEDPin
Sorry, I don't have the energy right to work out a proper schematic. The picture below will have to do.
Software
- dbws.h - general purpose constants and types
- METS.h - METS declarations of constants, types, and APIs
- mets.cpp - METS implementation file
- METS.ino - Arduino API
Fast Track
Build
Ardunino IDE
- Create a folder "METS" and copy the files there.
- Run the Arduino IDE and load the METS.ino sketch.
- Compile and upload the sketch to one of the ESPs.
- Open the Serial monitor and goto to the MACAddress step below
Command line
I use the Arduino command line tool to build the program. I've incorporated the tool in my Visual Studio 2022 Commuinty Edition project using the following commands:
arduino-cli compile --no-color --warnings=none -b esp32:esp32:esp32 arduino-cli upload --no-color -b esp32:esp32:esp32 -p COM5 arduino-cli monitor -p COM5 -c baudrate=115200,bits=8,parity=none,stop_bits=1
N.B., the worning level is the lowest and is the default setting for the Arduino IDE. I usually try to change it to the maximum value (--warnings=all) with mixed success. Another very usefull compile option is --clean.
MAC Address
Nota Bene: This step is ctritcal to the proper operation of the program.
This step must be done every time you add a pair of ESP32s
The inital run of the progrm won't do anything but the purpose is to extract the MAC address of the ESP32. Thsi will appear in the Serial monitor as
entry 0x400805f0 setup start METS::begin start METS::banner start METS v0.1.0 (It's not baseball!) from DBWS a division of ACME Industries METS::banner end local mac address: Ardglen : E4:65:B8:4A:29:0C remote peer: Karkool : E4:65:B8:48:3E:64 ...
The local mac address is the one for the ESP.
Copy and save that address and note the ESP32 chip it came from. to a temporary text file. Repeat this foe the second ESP32. You should have 2 different MAC addresses.
In a text editor, openthe METS.h file and go the code
//#define KARKOOL mac_addr_t mac_local{ #ifdef KARKOOL 0xE4, 0x65, 0xB8, 0x48, 0x3E, 0x64 // Karkool #else 0xE4, 0x65, 0xB8, 0x4A, 0x29, 0x0C // Ardglen #endif }; mac_addr_t mac_remote{ #ifdef KARKOOL 0xE4, 0x65, 0xB8, 0x4A, 0x29, 0x0C // Ardglen #else 0xE4, 0x65, 0xB8, 0x48, 0x3E, 0x64 // Karkool #endif };
Arbitrarily assign each ESP32 to one of the station names and copy that ESP32 MAC address tinto the line for the station in both mac_local and mac_remote. Do the same for the second ESP32 using the other station name.
While the program each ESP runs is the same, they are paired with each other through the MAC address. So whenever you upload the program to an ESP32, you need insure the MAC address matches that ESP32. You do this with the line
//#define KARKOOL
As you might expect, when the line is commented out (as above, you're programming the Ardglen ESP32. When the line is uncommented you're programming the Karkool ESP32.
(I don't make the rules here; I just try to make it easy to follow them.)
Deviations, discrepancies, and known defects
The button don't work.
Correct. Sadly, this is true in many ways.
I had to cut out the button code from this version because up until a couple of days ago, the buttons operatesd properly. Then, as I was getting ready to post this, they stopped. The extract button suddenly constanly reported being pressed. The insert button didn't respond at all. I don't know if it's a logic error, a GPIO pin error, or a hardare error due to cheap button. I dunno.
But it didn't want to let the button block me from posting; it was "fish or cut bait". So i cut the buttons out the event detection in the tumbler code
bool tumbler_t::on_event() { //dbg_trace trace("tumbler_t::on_event()"); // todo: button debug; read failure //bool extractkey{ digitalRead(extractButtonPin) }; //bool insertkey{ digitalRead(insertButtonPin) }; bool extractkey{ false }; bool insertkey{ false }; bool result{ extractkey || insertkey }; //...
and then forced the event detection in the METS::on_event function
case IDLE: result = on_idle(); // todo: debug hack // todo: button debug; read failure if (section.isavailable()) { next_event = EXTRACT; result = true; } else { next_event = INSERT; result = true; } break;
The results is the simulation continuously exrtacts and inserts the staffs and everyone else is happy. There's a race condition between the two ESP but that was there before.
The code doesn't check for a response from t heremote ESP.
Correct. I didn't fully work that out. The response is handled by the OnRX code but the code that issued the request doesn't pause to get the response; it assumes an ack reply.
I'm sure I've forgotten something here or don't know it is needed, but I hope this is sufficient to get things up and running.
I'm now looking for verfification of the circuit hardware and software.
And thoughts and feedback are appreciated.
Best to start with,"Tim, you handsome devil!, ..."
As I have said right from the start, I have always been open to any and all suggestion for this project.
Ok. So some observations rather than actual suggestions.
The main reason I will most likely go down the ESP-NOW route, is after seeing a YouTube video on its capabilities, I thought it would save a lot of time running cables between each machine.
I think you only have to run a single wire between the pairs of machines shown as red in the image. This is energized from either pair of connected machines to lock or unlock both machines. The actual machines used relays. The "stick" or latch relay circuit acts as a memory of the last action such as inserting or removing a staff. This can hold the connecting control line on or off until another action is taken. The actual tumbler was locked or unlocked with a lock relay. This is why I suggested a simpler on/off relay solution vs. a more involved servo motor solution.
To enlarge an image, right click mouse and choose Open link in new window.
And thoughts and feedback are appreciated.
Best to start with,"Tim, you handsome devil!, ..."
Tim, you clever man 🙂
A great example of how two ESP32's can 'talk' to each other via ESP-NOW, know how that can also be transferred to other projects. Well done!
Tim, you handsome devil.... 😆 .....and clever man.
All that code means nothing to me at the moment 😀, but am keen to learn over time.
I used the following DBWS videos as reference material when developing the simulation:
- ESP-NOW – Peer to Peer ESP32 Network – 2022 Videos – DroneBot Workshop Forums
- Using Servo Motors with the ESP32 (dronebotworkshop.com)
I highly recommend viewing these to get a firm understanding of the circuit and the simulator
The ESP-NOW video covers using the ESP-NOW API and is handy for integrating momentary button (MOM) components.
The Servo video is helpful for incorporating and controlling servo motor components.
The two videos were among the first I watched on Bill's YT channel prior to joining the forums. I will most certainly watch them again over time.
Bill of Materials
- 4 LED lights (2 redm 2 green)
- 4 220K ohm resistors
- 2 MOM miniature buttons
- 2 9gm servo motors, Mine are marked as S51 but any SG90 should work.
- 2 ESP32 boards with USB cable. I used a "run of the mill" WROOM 32 development board.
- 1 bredboard power supply that can supply 5 and 3 volts. I used one from Elegoo.
- ~17 male-male 22 gws wire, ~ 5in. length.
About the only thing I'll ask is a recommendation on which ESP32 boards to buy so I can start playing with all this myself. Is it worthwhile getting a smaller board with only enough GPIOs to run the two servos and switch inputs at each machine? Is the WROOM 32 dev board overkill for my needs?
I'm now looking for verification of the circuit hardware and software.
Does this mean you would like me to do this, or will you continue testing the hardware and code?
Cheers,
Ian
Ian Millard
Port Macquarie, NSW, Australia
ESP32/Arduino etc novice
All that code means nothing to me at the moment 😀, but am keen to learn over time.
OK. I take it then you have little or no programming experience, right? The Good News is you don't need to understand the code yet. All you need right now is to know that it can work for your project. In due time, I should be able to explain it to you sufficiently so that you can work with it.
About the only thing I'll ask is a recommendation on which ESP32 boards to buy so I can start playing with all this myself.
Buy an ESP starter kit (this one looks reasonable) You may need to buy a few additional ESP32 (a 3 pack is plenty); make sure they have pins attached so they fit in the breadboard from the starter kit. And some cheap SG90 servos. Total about $50-$60 US. That should get you started and allow you to test. You may need an external power supply but by the time you do you'll know what to get.
Does this mean you would like me to do this, or will you continue testing the hardware and code?
You can't avoid it. This is your project, so you need to be familiar with all of it. You don't have do circuit design, but you do need to be able to build the circuit. (Don't worry, it's not more difficult than the 3D CAD work you've already done.) You need to see how my implementation actually works and if it really does match you needs and expectations.
I'll be following the thread and will offer advice and thoughts. It would be nice if you had second implementation for comparison purposes.
In the meantime, keep calm and carry on.
Thanks Tim.
All that code means nothing to me at the moment 😀, but am keen to learn over time.
OK. I take it then you have little or no programming experience, right? The Good News is you don't need to understand the code yet. All you need right now is to know that it can work for your project. In due time, I should be able to explain it to you sufficiently so that you can work with it.
Yes, you are correct. I have no programming experience at all. As I said, I'm happy to learn to be able to troubleshoot the circuit in the future, and any assistance you can provide will be most gratefully accepted.
About the only thing I'll ask is a recommendation on which ESP32 boards to buy so I can start playing with all this myself.
Buy an ESP starter kit (this one looks reasonable) You may need to buy a few additional ESP32 (a 3 pack is plenty); make sure they have pins attached so they fit in the breadboard from the starter kit. And some cheap SG90 servos. Total about $50-$60 US. That should get you started and allow you to test. You may need an external power supply but by the time you do you'll know what to get.
I had started to look at those starter kits, but I don't really need all the LEDs, switches and other sensor boards etc. Is buying the ESPs off Amazon or Aliexpress OK, or should I go to local electronics online stores that I have found who sell the ESPs. And I take it that the WROOM dev board is OK for my needs?
This is one local AU distributor that I have found.
I probably don't need Bluetooth.
And I'll look at getting a breadboard and some jumper leads separately. I already have the servos.
Does this mean you would like me to do this, or will you continue testing the hardware and code?
You can't avoid it. This is your project, so you need to be familiar with all of it. You don't have do circuit design, but you do need to be able to build the circuit. (Don't worry, it's not more difficult than the 3D CAD work you've already done.) You need to see how my implementation actually works and if it really does match you needs and expectations.
I'll be following the thread and will offer advice and thoughts. It would be nice if you had second implementation for comparison purposes.
I don't think I'll have any issues with building the hardware/circuitry, it's just the coding that I see as a steep learning curve.
Thanks again for all your assistance.
Ian
Ian Millard
Port Macquarie, NSW, Australia
ESP32/Arduino etc novice
FYI @tfmccarthy
I bought this ESP32 board from Jaycar some time ago now but it cost a lot of money and you need eight of them. What a rip off. I had thought to buy another to follow along with the @tfmccarthy implementation of the project.
I am loath to shop online.
I think the ESP32 would be an overkill commercially for anything so simple but I realise you are interested in learning how to code and use the ESP32 so the project is something of interest and thus motivation for you.
Learning to code is a steep learning curve and with the Arduino usually starts of with simple projects involving push buttons and LEDs 🙂
I don't think I'll have any issues with building the hardware/circuitry, it's just the coding that I see as a steep learning curve.
I started to reply to this, and I suddenly realized that I don't have the skillset you need.
What I can do is build a breadboard circuit and program it.
What I can't do (and part of the reason I'm on this forum) is translate a breadboard circuit onto a circuit board that can be installed. I can't solder a circuit. I don't even own a soldering iron! I'm trying to develop that skillset and, truth be told, will probably never get there (but enjoy the trip.)
You need someone who can translate the breadboard circuit onto a circuit board that you can screw into your CAD model.
I can help with the programming but there's limit to that. The ESP32 isn't the issue. The issue is the other components; the switch and servo will be specific devices that I won't have. So, I won't be able to build your specific circuit even on a breadboard.
The circuit I've provided is a generic circuit. You can replace the servo and switches with other types or different devices. I can help with how to modify the code to deal with that but can't test it.
Just want to make that clear, OK?
Geez! What's going down there?
This is what I bought
The major attraction of the ESP32 is very low cost for a lot of built-in ability. $3-4 US per device should be the limit.
I think 3 is the right amount to get because apparently a rite of passage is you'll misconnect the power and burn one out. I didn't believe it, but it only took me one day to do it.
I don't think I'll have any issues with building the hardware/circuitry, it's just the coding that I see as a steep learning curve.
I started to reply to this, and I suddenly realized that I don't have the skillset you need.
What I can do is build a breadboard circuit and program it.
What I can't do (and part of the reason I'm on this forum) is translate a breadboard circuit onto a circuit board that can be installed. I can't solder a circuit. I don't even own a soldering iron! I'm trying to develop that skillset and, truth be told, will probably never get there (but enjoy the trip.)
You need someone who can translate the breadboard circuit onto a circuit board that you can screw into your CAD model.
I can help with the programming but there's limit to that. The ESP32 isn't the issue. The issue is the other components; the switch and servo will be specific devices that I won't have. So, I won't be able to build your specific circuit even on a breadboard.
The circuit I've provided is a generic circuit. You can replace the servo and switches with other types or different devices. I can help with how to modify the code to deal with that but can't test it.
Just want to make that clear, OK?
Thanks Tim.
I understand where you're coming from. As I said earlier, I think I'll be able to work out the hardware and circuitry OK, but I'll definitely need further assistance with the programming. I'll try and source all the hardware first and breadboard it all together then come back and look at the code you have done.
I can't thank you enough for what you have done already.
Cheers,
Ian
Ian Millard
Port Macquarie, NSW, Australia
ESP32/Arduino etc novice