dr_spock

ESP8266 WIFI and LEGO Trains

Recommended Posts

Hi everyone.

I'm starting a topic for the ESP8266 and LEGO trains to discuss and share experiences of using ESP8266 to control LEGO trains and other layout functions like switches, signal lights, train detection, etc. ESP8266 can also have a lot of uses in the making of IoT (Internet of Things) devices.

ESP8266 is a low cost WIFI chip with TCP/IP protocol and microcontroller. You could run a simple web server on it and control your device through a web browser.

Features:

  • 32-bit RISC CPU: Tensilica Xtensa LX106 running at 80 MHz
  • 64 KiB of instruction RAM, 96 KiB of data RAM
  • External QSPI flash - 512 KiB to 4 MiB (up to 16MiB is supported)
  • IEEE 802.11 b/g/n Wi-Fi
  • Integrated TR switch, balun, LNA, power amplifier and matching network
  • WEP or WPA/WPA2 authentication, or open networks
  • 16 GPIO pins
  • SPI, I²C,
  • I²S interfaces with DMA (sharing pins with GPIO)
  • UART on dedicated pins, plus a transmit-only UART can be enabled on GPIO2
  • 1 10-bit ADC

I picked up a ESP8266 ESP-12 module last year for under $5 USD with free shipping. The low cost makes it affordable to experiment (and if you fried it, not a great loss). I haven't really had a chance to work with it. It's confusing with all the different firmware you can run on it, such as AT-commands with include firmware, NODEMCU with LUA, etc. It is hard for a noob like me to figure out which one to use.

As you can see the module is about the size of a 3x2 LEGO plate.

wifidalek_zpsl33zkwwp.jpg

Daleks could use a MESH network of ESP8266 modules to link together :wink:

Share this post


Link to post
Share on other sites

Hi, thanks for creating another thread.

I had played with an ESP8266 like yours but gave up shortly after. It's great for very small and specific purposes but I found it difficult to use for my own purposes - just 2 digital I/O, not a proper power supply, need to use a FTDI cable to program...

So I got myself a NodeMCU v2 (or ESP-12-E... naming is not quite standard around the several ESP modules):

19667436609_72f3e3433d.jpg

It's twice as bigger but has more I/O pins and can be USB powered or directly with a 3.7V LiPo. And it already has the FTDI part onboard so when I connect it to my laptop I can use Arduino IDE directly - the IDE changes the firmware so it no longer runs LUA and after that I ran some some examples like WiFi, http server and RC servo.

I'm using it to control my micro-car (a RC servo for steering and a micro DC geared motor for traction) from my Android phone with UDP messages. For a guy like me with almost no previous Arduino experience, was like "plug and play".

Share this post


Link to post
Share on other sites

They look to be around $2.50 these days! A nice price if you need 20-30 of them around the layout. Man if you could get these working it'd be very cool for qussi-automated layouts.

I can easily imagine points (switch track) controller. If each pt controller is addressable then a smart phone app can be used to switch points at a distance!

Steve

Edited by SteveB

Share this post


Link to post
Share on other sites

Hi, thanks for creating another thread.

I had played with an ESP8266 like yours but gave up shortly after. It's great for very small and specific purposes but I found it difficult to use for my own purposes - just 2 digital I/O, not a proper power supply, need to use a FTDI cable to program...

So I got myself a NodeMCU v2 (or ESP-12-E... naming is not quite standard around the several ESP modules):

It's twice as bigger but has more I/O pins and can be USB powered or directly with a 3.7V LiPo. And it already has the FTDI part onboard so when I connect it to my laptop I can use Arduino IDE directly - the IDE changes the firmware so it no longer runs LUA and after that I ran some some examples like WiFi, http server and RC servo.

I'm using it to control my micro-car (a RC servo for steering and a micro DC geared motor for traction) from my Android phone with UDP messages. For a guy like me with almost no previous Arduino experience, was like "plug and play".

The NodeMCU version looks like a much easier module to work with. I see eBay has some versions for around $6 USD. I'm going to get a couple to play with.

What type of motor driver do you use? Is the L293D suitable to run the LEGO PF train motor?

Share this post


Link to post
Share on other sites

What type of motor driver do you use? Is the L293D suitable to run the LEGO PF train motor?

I use Pololu DRV8835 (dual) and DRV8838 (single) driver, not with trains - small motors so no heat problems. I think the L293D will work with the PF train motor with a proper heatsink for common usage but perhaps can't give enough current for heavt usage.

With the Raspberry Pi I use L298 modules (mostly from Itead but they are almost all designed from the same reference circuit) because they can handle more power and came with an heatsink, But it's perhaps to big to put in a train.

Share this post


Link to post
Share on other sites

The ESP8266 is a (largely overlooked) revolution! An ESP-12E goes for $1.90 on AliExpress, including shipping.

The setup MajorAlvega uses is the best for tinkerers/makers imho: an assembled PCB with everything you need and then using the Arduino IDE to program it directly. The more widely used setup where the ESP8266 (a $2 80MHz RISC processor) runs an AT interpreter so that it can be controlled with complex AT commands from an underpowered $20 16MHz AVR Arduino is just ridiculous!

Cheers

TomKraut

Share this post


Link to post
Share on other sites

This all sounds very interesting. I'm confident to handle the software part (webserver, firmware, GUI on a touch screen, maybe an app for smart phones). But I do have no experience in making custom hardware. I put together an arduino with a "motor shield". This works with PF motors (I did not test them under heavy load) but it's far to big to be integrated into a real layout. That's why I ended up with SBricks.

So if anyone can give guidelines or instructions how to build a SBrick replacement, I would be one of the first "beta testers".

Edited by Asper

Share this post


Link to post
Share on other sites

Asper, no promises but I will look into it. I have a document somewhere detailing all the PF stuff, so it shouldn't be too hard to put a motor controller together. I haven't gotten around to building my own PF components because I always shied away from the software work involved (which is ironic, since I'm a software engineer by trade :laugh: ).

Share this post


Link to post
Share on other sites

I use Pololu DRV8835 (dual) and DRV8838 (single) driver, not with trains - small motors so no heat problems. I think the L293D will work with the PF train motor with a proper heatsink for common usage but perhaps can't give enough current for heavt usage.

With the Raspberry Pi I use L298 modules (mostly from Itead but they are almost all designed from the same reference circuit) because they can handle more power and came with an heatsink, But it's perhaps to big to put in a train.

It seems like the pololu can handle 3.3v logic without converters for 3.3/5V?

The ESP8266 is a (largely overlooked) revolution! An ESP-12E goes for $1.90 on AliExpress, including shipping.

The setup MajorAlvega uses is the best for tinkerers/makers imho: an assembled PCB with everything you need and then using the Arduino IDE to program it directly. The more widely used setup where the ESP8266 (a $2 80MHz RISC processor) runs an AT interpreter so that it can be controlled with complex AT commands from an underpowered $20 16MHz AVR Arduino is just ridiculous!

Cheers

TomKraut

ESP8266 ESP-01 is a cheap way to add WIFI to the Arduino. With the more recent modules like ESP-12E with more GPIO pins and interfaces exposed, it seems like the Arduino may not be required for some devices.

This all sounds very interesting. I'm confident to handle the software part (webserver, firmware, GUI on a touch screen, maybe an app for smart phones). But I do have no experience in making custom hardware. I put together an arduino with a "motor shield". This works with PF motors (I did not test them under heavy load) but it's far to big to be integrated into a real layout. That's why I ended up with SBricks.

So if anyone can give guidelines or instructions how to build a SBrick replacement, I would be one of the first "beta testers".

There are motor shields for the ESP8266 NodeMCU board, it is like putting together an Arduino with motor shield. The hard part might be finding PF power connectors cheaply.

Share this post


Link to post
Share on other sites

It seems like the pololu can handle 3.3v logic without converters for 3.3/5V?

(...)

There are motor shields for the ESP8266 NodeMCU board, it is like putting together an Arduino with motor shield. The hard part might be finding PF power connectors cheaply.

Yes, inputs are 3V- and 5V-compatible

I'm not aware of PF connectors on the market. I've been using LEGO PF cables, cut at the middle and solder the wire to other connectors or directly to the boards I use. Pretty expensive (Bricklink doesn't help here) and also a capital sin, I know :)

Share this post


Link to post
Share on other sites

This all sounds very interesting. I'm confident to handle the software part (webserver, firmware, GUI on a touch screen, maybe an app for smart phones). But I do have no experience in making custom hardware. I put together an arduino with a "motor shield". This works with PF motors (I did not test them under heavy load) but it's far to big to be integrated into a real layout. That's why I ended up with SBricks.

So if anyone can give guidelines or instructions how to build a SBrick replacement, I would be one of the first "beta testers".

Hi Asper, I'm about to start a project integrating Brickster modules into a webserver. So proxying bluetooth commands to multiple Brickster modules on a webserver running on something like the Pi or GuruPlug. Currently Brickster is limited to an Android App. Should be easy enough for the Brickster modules but also want to be able to integrate other Arduino type modules for switches and sensors (maybe the ESP8266?). Was thinking nodejs for the web server, websockets and HTML5 canvas for the GUI to keep it responsive. Let me know if you'd like to do a joint project?

If interested, you can get in touch with Legoman666, he may be able to help you by making up some Brickster modules.

I'm not aware of PF connectors on the market. I've been using LEGO PF cables, cut at the middle and solder the wire to other connectors or directly to the boards I use. Pretty expensive (Bricklink doesn't help here) and also a capital sin, I know :)

You can get a single PF plug made in Shapeways

, but may not be worth it considering it's probably easier to chop the end off a wire to get one. May also be worth looking at the Brickster case as that has PF plug contacts integrated on both the top and bottom :- https://www.shapeway...tionId=43490793

Edited by bricks n bolts

Share this post


Link to post
Share on other sites

I'm not aware of PF connectors on the market. I've been using LEGO PF cables, cut at the middle and solder the wire to other connectors or directly to the boards I use. Pretty expensive (Bricklink doesn't help here) and also a capital sin, I know :)

I know what you mean about cutting good PF extension cables. I have a couple of failing M-motors from running GBCs at events. I could probably unsolder the PF cable from those. Finding people with dead PF motors could be a source.

.With the Raspberry Pi I use L298 modules (mostly from Itead but they are almost all designed from the same reference circuit) because they can handle more power and came with an heatsink, But it's perhaps to big to put in a train.

A L298N module could fit into a 6 wide train. You may have to design your MOC around it to allow proper air flow and cooling for the heatsink. Working radiators for LEGO trains? :classic:

l298n_zpshrqc22za.jpg

Hi Asper, I'm about to start a project integrating Brickster modules into a webserver. So proxying bluetooth commands to multiple Brickster modules on a webserver running on something like the Pi or GuruPlug. Currently Brickster is limited to an Android App. Should be easy enough for the Brickster modules but also want to be able to integrate other Arduino type modules for switches and sensors (maybe the ESP8266?). Was thinking nodejs for the web server, websockets and HTML5 canvas for the GUI to keep it responsive. Let me know if you'd like to do a joint project?

That sounds interesting. How many modules will you be able to communicate with?

Share this post


Link to post
Share on other sites

So if anyone can give guidelines or instructions how to build a SBrick replacement, I would be one of the first "beta testers".

With a little bit of rerouting, you could probably drop one of these into the driver boards I've designed.

It looks like it has the same footprint as the HM-06/11/13 Bluetooth modules I've been using.

https://github.com/BTbricks

http://www.eurobricks.com/forum/index.php?showtopic=87340

Edit: you'd probably want to omit the ATtiny841, but I'm not 100% sure that'd be possible; it looks like the ESP8266 only has three PWM outputs, which is one less than is needed for two motor outputs.

Edited by codefox421

Share this post


Link to post
Share on other sites

I think you can get PWM on all GPIOs on the ESP8266. http://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations

Different ESP8266 module boards expose different numbers of GPIO pins. The later boards like ESP-12E give you access to more pins plus FCC approval in the USA. An early ESP-01 board only exposes 2 GPIO pins and it could work as in place of the BT module for WIFI connectivity. The ATtiny841 could talk to it through the exposed RX, TX lines. You could probably replace the ATtiny841 with a later ESP8266 board that has more GPIO pins exposed to control the motor driver.

Share this post


Link to post
Share on other sites

There is some confusion with the PWM pins: you can have PWM function in almost all pins but it seems to be "software PWM" - documentation isn't very clear.

NodeMCU functions documentation says "Only 6 pins can be set to PWM mode at the most."

But even only 2 PWM can be enough, you can use 2 PWM for 2 motors - 1 for each "enable" pin, and use 2 normal GPIO to control the direction.

And if you use modified RC servos (continuous rotation instead of just 0-180 degrees range, sometimes used in robotics) you can use just the PWM pins, no need for direction pins (but code gets more complex).

Share this post


Link to post
Share on other sites

Hi Guys,

I just completed a big project to control my Horizon Express train with a NodeMCU (ESP8266). I also use a Raspberry Pi running Node-RED for the dashboard and IoT logic. I agree that the ESP8266 is awesome for IoT and LEGO projects, but it was annoying to program and build the supporting circuit, etc. I did it, but I felt like it was a time suck. I then stumbled on the NodeMCU, which is the lazy mans approach.. and that's me ;)

I bought the NodeMCU with motor shield on eBay for under $10. It works really well, but I think the motor shield was a little difficult to cram in the train. I was able to do it, but I would go for an external H-bridge motor driver, for flexibility.

The complete write-up is on my Internet of LEGO blog:

http://www.interneto...odemcu-esp8266/

post-158657-0-62672000-1460193129_thumb.jpeg

Let me know what you think! I also am working on sensing the train location using reed switches. Since the train has magnetic couplers, the reed switch works perfectly for detecting the train.

Share this post


Link to post
Share on other sites

Let me know what you think! I also am working on sensing the train location using reed switches. Since the train has magnetic couplers, the reed switch works perfectly for detecting the train.

Welcome to aboard. Awesome work. :classic:

I ordered a NodeMCU a few weeks ago after stumbling on them too. I am still waiting for it to arrive from China. It would make development work (programming and testing) much easier.

I found the

also can pick up the magnetic field from the PF train motor.

How do you mount your servo motors to the track switches?

Share this post


Link to post
Share on other sites

I have had a hard time convincing my Lug that blue tooth is the way to go for large layouts. I can't imagine trying to get them to use this. You people are nuts!

Share this post


Link to post
Share on other sites

I switch the tracks with a 5v hobby servo and a single technic stud, attached by a thin piece of wire. These servos are not very powerful and the LEGO track switches require a bit of torque. I tried several methods while trying to keep it as minimal as possible. The final idea is documented in a previous project article. I also go through the Arduino/Raspberry Pi logic as well.

http://www.internetoflego.com/train-track-switch-servos-leds-pubnub-and-johnny-five-io/

@legoman666, I am a little nuts. But naturally just like building things, LEGO, electronics, computers; its all the same to me :)

12473607_1671388736466656_1134200146847620376_o.jpg

Share this post


Link to post
Share on other sites

Nice work, I played with bluetoothbfor this a while a go but think the way you used off the shelf components and pulled it all together is way cooler.

Share this post


Link to post
Share on other sites

I know what you mean about cutting good PF extension cables. I have a couple of failing M-motors from running GBCs at events. I could probably unsolder the PF cable from those. Finding people with dead PF motors could be a source.

A L298N module could fit into a 6 wide train. You may have to design your MOC around it to allow proper air flow and cooling for the heatsink. Working radiators for LEGO trains? :classic:

l298n_zpshrqc22za.jpg

That sounds interesting. How many modules will you be able to communicate with?

Did you actually got it running or is it just a mock-up? If so, which voltage did you use for the logic supply? (it has to be between 4.5 V and 7 V according to the datasheet)

Share this post


Link to post
Share on other sites

Did you actually got it running or is it just a mock-up? If so, which voltage did you use for the logic supply? (it has to be between 4.5 V and 7 V according to the datasheet)

It's currently a mock up. You can use a 3.3V voltage converter to supply the Vin for the ESP8266. Also a 3.3V to 5V logic converter for communicating with motor controller. I ran into a problem with a couple of Ebay orders from China. The ESP8266 NodeMCU and L293D motor controller ICs have gone missing. I ordered from two different sellers too.

Share this post


Link to post
Share on other sites

I have had a hard time convincing my Lug that blue tooth is the way to go for large layouts. I can't imagine trying to get them to use this. You people are nuts!

I just spent a weekend discussing this at Philly BrickFest (after having a few conversations with Peter from S-Brick) which is making me feel like a behind the times schmuck because I am just now getting the devices to work on my layout with the latest iOS9 release along with the latest version of the S-Brick app. My personal goal with the S-Brick is that I will be able to control multiple motors in multiple locomotives in a single consist without worrying if the IR Receivers are getting the same signal and set to the same power levels. Using synchronized, powered units at both ends of my tender on the SpaceTrain is the only way that I can keep the inertia of the heavier car from totaling the MOC everytime I attempt to slow down the locomotive. Inertia is simply a mean and cruel witch.

Back to the Bluetooth concept on a large LUG layout is that typically, the Bluetooth signal from many of the cellphones and tablets on the market are not strong enough to maintain a connection with the S-Brick devices as they travel around the layout. Also, some devices (like my iPadAir2) have a very strong Bluetooth signal which could interfere with the connection with my iPhone 6S Plus connected to my locomotive on the layout. It would be pretty nasty to have someone walk by one end of the layout with a Bluetooth connected Apple Watch and have that device interfere with the connection between the SmartPhone/Tablet controlling the trains as they pass along that end of the layout. WiFi may be the better option if I cannot work out the issues with interference on the devices. I will let everyone know my progress with my experiments on the S-Brick and hopefully I will have some solutions running by the end of the year. If I get lucky, I might be able to run some full scale experiments when we have out layout at a train show late this year or very early next year.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.