ESP32 Command Station

Atani Dec 10, 2017

  1. esfeld

    esfeld TrainBoard Member

    442
    382
    17
    Mike
    Following along with Norm on an Arduino sketch. If you could email me a copy of index.html I would be most appreciative. esfeld@cyberdude.com
     
    Scott Eric Catalano likes this.
  2. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    Sent. As soon as I figure out how to do this within the IDE itself I will add it to a readme. Likely a custom plugin will be needed for this, similar to the SPIFFS upload plugins.
     
    Scott Eric Catalano likes this.
  3. esfeld

    esfeld TrainBoard Member

    442
    382
    17
    Mike
    Thank you ....added index_html ...... compile shows a missing md5.h which I can not locate from your SRC file ... any ideas?
    Steve
     
    Scott Eric Catalano likes this.
  4. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    Unfortunately no. I don't know what issue you ran into. I went through the following steps to get the code compiling under Arduino IDE 1.8.5:
    1) https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/windows.md
    2) download zip of these libraries:
    https://github.com/squix78/esp8266-oled-ssd1306
    https://github.com/me-no-dev/ESPAsyncWebServer
    https://github.com/me-no-dev/AsyncTCP
    https://github.com/bblanchon/ArduinoJson
    https://github.com/mathertel/LiquidCrystal_PCF8574
    3) exploded zips for above libraries to
    $USER/Documents/Arduino/libraries
    4) renamed DCCppESP32/src folder to DCCppESP32/DCCppESP32.
    5) renamed DCCppESP32/DCCppESP32/DCCppESP32.cpp to DCCppESP32.ino
    6) Copied index_html.h from DCCppESP32/.pioenvs/esp32 to DCCppESP32/DCCppESP32
    7) Started Arduino IDE 1.8.5
    8) Selected all files in DCCppESP32/DCCppESP32 to the IDE.
    9) Clicked Verify button which took a long time to compile everything and showed this output:
    Code:
    Sketch uses 574706 bytes (43%) of program storage space. Maximum is 1310720 bytes.
    Global variables use 38796 bytes (13%) of dynamic memory, leaving 256116 bytes for local variables. Maximum is 294912 bytes.
     
  5. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    @Atani -- you mentioned in the other thread the need for current-sense lines to be available on the H-bridge. That doesn't see, to be the case on the L298N board I have, but all the pins are easily accessible on the board to solder on an extra wire somewhere. But what about chips that might not have this feature, is there another (easy?) way to add current-sensing to the circuit? I read somewhere about using a resistor and capacitor for this function but couldn't find any sample circuits.

    This is the L298N (2A) board I have: https://www.ebay.com/itm/381992069310
    That should be enough for my test loop that will only run 1 loco at a time, but I have a 5A board on the way to test with:
    https://www.ebay.com/itm/291966896712
    I also have my eye on a 10A board that could be of use once I get my layout together: https://www.ebay.com/itm/232164773123

    Note that none of these options mention current sensing or seem to have a pin available to read it from. I also noticed neither the 5A or 10A boards have heatsinks of any kind... Ack! Obviously would have to correct that, but otherwise they both seem like decent options at first glance?
     
    Scott Eric Catalano likes this.
  6. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    Current Sense is critical for the DCC++ base station to cut track power when a short occurs. With the L298N board you have linked to, it does not have current sense exposed where the base station can read it that I can tell. I have a few of the same boards on the way from China which I will be modifying to add a connection for the current sense pins coming off of the L298 chip itself (#1 and #15 IIRC). I will share pictures of that once I have them in hand and can do some testing.

    For the 5A and 10A boards you link to, these do not have current sense capability that I can tell. I am not sure these can easily be used with DCC++ without additional hardware. Perhaps using a current transformer on one of the output lines in a differential setup might work. That is how I plan on doing block detection (CT, I2C ADC, Arduino) which will feed back to the base station for signals etc.
     
    Scott Eric Catalano likes this.
  7. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Well it seems current sensing is a rather minor task. I found some Hall-effect based sensors for under $2, built on the ACS712 and available in 5A, 20A, and 30A varieties (note some warnings about the 30A version having problems with the solder melting at high current!). I went ahead and grabbed one of the 5A versions to pair with the 5a H-bridge I already ordered, should be easy enough to add this to the circuit although I'll have to use a voltage divider to reduce the 5V output from the sensor.

    I took a look at the L298N chip, and the chinese boards are not going to work directly for current sensing. It appears you need to add a couple of resistors sized to handle the amperage at about 2VDC (so probably about a 4-5W resistor), but I think that's all that is needed. There's a PDF datasheet here that talks a bit about current sensing on this chip: http://www.datasheetcatalog.org/datasheet/SGSThomsonMicroelectronics/mXxzuw.pdf

    Will try and get an ESP32 loaded up with the software this week and start to learn the code and more about how DCC works. I also put in an order for one of the Lolin boards, looks like a nice setup with its little OLED onboard. Maybe by the time all the new parts get here I'll have some idea of what I'm doing :)
     
    Scott Eric Catalano likes this.
  8. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    The ACS712 may work, I have a few of them and with an Arduino Pro Mini I could not accurately detect trains with the DCC signal present. With DC it is very doable though. The Arduino only has a 10bit ADC so that played a factor as well.

    I moved to current transformers for detecting now and can detect an idle engine or resistor wheelsets.

    As for the L298N and current sense, I believe it should work with a resistor and OpAmp, same as the Arduino shield does. But it would require a few extra parts for sure.
     
    Scott Eric Catalano likes this.
  9. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Hmm now I'm confused. I thought the current detection was solely for killing the power in the case of a short across the tracks. Is it also required for DCC to communicate with the locos?

    I think the ESP32 also only has a 10-bit ADC, so we'll be in the same boat there.
     
    Scott Eric Catalano likes this.
  10. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    It is not used as part of the DCC signal. It is used by DCC++ to detect shorts which will result in the signal being shut off.

    The ESP32 has a 12bit SAR ADC, or 4096 possible values. The Arduino has 10bit or 1024 values.
     
    Scott Eric Catalano likes this.
  11. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Hmm might have to check into that. I knew the esp32 had a new 12-bit DAC, but I thought the ADCs were still 10-bit like the 8266s were.
    -- Nope, you're right, they HAVE moved to 12-bit ADC. Awesome! I was also reading about the difficulties of using and arduino to detect cars with resistors on the wheels, but with the extra resolution it might be more practical now.

    So can you clarify something for me regarding how DCC++ works? I know standard DCC uses an AC voltage across the track, but I'm not seeing any components to create a sine wave in DCC++. Do they instead create a square wave by switching the H-bridge? If so, do you know what the frequency is?
     
    Scott Eric Catalano likes this.
  12. Jimbo20

    Jimbo20 TrainBoard Member

    274
    178
    11
    I think I'm right in stating that as well as power overload, the current sense is also used for confirming programming operations and reading CV's on the programming track (if used) too.
     
    Scott Eric Catalano likes this.
  13. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    Yes, the base station looks for at least a 60mA "pulse" on the PROG track as an ACK from the decoder in response to a programming packet.

    It isn't exactly AC but similar to that yes. With DCC++ the enable pin (PWM-A or PWM-B on the Arduino motor shield) is held HIGH or LOW based on the power state (<0> or <1> being sent to the base station). DCC++ uses the DIR pin to control the direction of the wave when PWM is HIGH, ie: PWM=HIGH & DIR=HIGH => top portion of wave, PWM=HIGH & DIR=LOW => bottom portion of wave. When PWM is LOW there won't be a sine wave.

    In DCCppESP32 you can see the full cycle and direction flip of the wave. At the end of the full cycle ISR the direction pin is set HIGH (create top half of sine wave) and at the halfway point the direction flip ISR is invoked to set the direction pin to LOW to switch to the bottom half of the sine wave. At no point does the PWM pin get changed, that is set externally to the sine wave generation. The timers are configured here, this code is called twice (MAIN timerIndex=0, PROG timerIndex=1), and sets up two hardware timers to call the above ISRs initially around 116us and 58us. After the first call the values will be dependent on the pending DCC packets.

    In DCC++ (Arduino) this is done differently as the hardware has a way to flip the state of the direction pin via hardware timer. You can see the timing for that here which is similar to the full cycle timer above. OCRA controls the full cycle and OCRB controls the direction flip. Configuring the timer is done here (MAIN only, PROG is further down).
     
    Last edited: Dec 20, 2017
    Scott Eric Catalano likes this.
  14. Jimbo20

    Jimbo20 TrainBoard Member

    274
    178
    11
    DCC is not truly AC; There is never zero volts across the rails as would be the case if it was sinusoidal ac. It is a rapidly reversing voltage (square wave). Also note that neither rail is connected to ground. Gregg explains it quite well here:

    (Start watching from about 3:50 in)


    Jim
     
    Scott Eric Catalano and Atani like this.
  15. Atani

    Atani TrainBoard Member

    1,466
    1,736
    37
    almost, if you send <0> then the rails will have zero power going to them as the PWM signal is cut off.
     
    Scott Eric Catalano likes this.
  16. Jimbo20

    Jimbo20 TrainBoard Member

    274
    178
    11
    ^^ Very true!
     
    Scott Eric Catalano likes this.
  17. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Yep, definitely got a lot to read into for figuring out all this stuff. Give me a week. :) And thanks for all the info above, I'm going through that as well.

    I also noted that the sketch isn't using up a huge amount of space, which is cool. Will certainly be fun to work with once I get my head around it.
     
    Scott Eric Catalano likes this.
  18. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Hi Mike, great to see this, thanks for your hard work. I’ll look forward to trying this out over the next few weeks. I’m hoping this will enable me to get my mobile WiFi decoders working, in conjunction with a simple java chat server sitting with JMRI on my RPi3.
     
    Scott Eric Catalano and Atani like this.
  19. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Well I'm hoping to be following everyone else soon. I installed the latest Arduino IDE (1.8.5) last night and loaded up the code from github, but unfortunately something in the ESP32 code requires glibc 2.14 to compile, and I only have 2.13 available. Got a bug open with them, hopefully hear something soon on how to get around this issue.

    In the meantime, my new lolin32 arrived today! Quick shipping, for some reason it only took 10 days to get here from China, but I plugged it in and it's working fine with the default code. Still waiting for the current sensors to arrive but I have everything else needed to hook up to the track once I can get the software to compile.
     
    Scott Eric Catalano and Atani like this.
  20. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    @Atani -- I created a new clean install of Arduino 1.8.5 and downloaded the list of libraries you mentioned above in post #24, however I'm getting the following errors when I try to compile:

    sketch/WebServer.cpp: In lambda function:
    WebServer.cpp:76: error: 'indexHtmlGz' was not declared in this scope
    AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", indexHtmlGz, indexHtmlGz_size);
    ^
    WebServer.cpp:76: error: 'indexHtmlGz_size' was not declared in this scope
    AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", indexHtmlGz, indexHtmlGz_size);
    ^
    Multiple libraries were found for "WiFi.h"
    Used: /pub/Projects/arduino/arduino-1.8.5-DCCpp/hardware/espressif/esp32/libraries/WiFi
    Not used: /pub/Projects/arduino/arduino-1.8.5-DCCpp/libraries/WiFi
    exit status 1
    'indexHtmlGz' was not declared in this scope


    For the WiFi.h error, I'm not sure if I should remove the pre-installed library?
    The other error for indexHtmlGz appears to be a show-stopper though. Hopefully this is just an error in the code currently in git?
     
    Scott Eric Catalano likes this.

Share This Page