atanisoft / ESP32CommandStation

An ESP32 based DCC Command Station with integrated OpenLCB (LCC) --- NOTE: this project is not under active development.
https://atanisoft.github.io/ESP32CommandStation/
GNU General Public License v3.0
90 stars 34 forks source link

Adding auto-configuration selection and initial documentation #105

Closed atanisoft closed 2 years ago

atanisoft commented 2 years ago

Adding option for automatic configuration of basic options.

Fixes https://github.com/atanisoft/ESP32CommandStation/issues/42 Fixes https://github.com/atanisoft/ESP32CommandStation/issues/103

atanisoft commented 2 years ago

@sarusso Can you review the state of the docs? I still need to migrate more of the content to here but the config should be considerably simpler now.

sarusso commented 2 years ago

The docs are way more clear, I would suggest to add something like "Pin mapping and WiFi Info are included in the pre-build binaries zip as text files" here: https://github.com/atanisoft/ESP32CommandStation/blob/doc_updates/docs/precompiled.md

So, tried to flash this one: https://github.com/atanisoft/ESP32CommandStation/suites/5108691645/artifacts/153066673 from here: https://github.com/atanisoft/ESP32CommandStation/actions/runs/1766655440 (why there are three of them with the same name?)

The WiFi network name and password do not match (mine is "ESP_58AF05", not "esp32csap" as stated in the docs), and I still can't get it to work. I double checked everything, the CONFIG_DCC_TRACK_SIGNAL_PIN and CONFIG_OPS_TRACK_ENABLE_PIN are just nearly silent (1.6V and 0 V respectively) and do not power up the H-bridge. I don't know what to say.

atanisoft commented 2 years ago

I would suggest to add something like "Pin mapping and WiFi Info are included in the pre-build binaries zip as text files"

Thanks, I'll add that to the page as it will certainly help.

why there are three of them with the same name?

That is very odd, I'll dig into the logs and see what might have gone awry there.

I double checked everything, the CONFIG_DCC_TRACK_SIGNAL_PIN and CONFIG_OPS_TRACK_ENABLE_PIN are just nearly silent (1.6V and 0 V respectively) and do not power up the H-bridge

The DCC_TRACK_SIGNAL_PIN looks about right, it will read around that since it is closer to an AC signal than a DC signal (58uS / 100uS high/low periods depending on 0/1 bits).

Let me take a look at the binary you used and see what happens on the test bench. I suspect it is something very basic that is incorrect in there somewhere.

mine is "ESP_58AF05", not "esp32csap" as stated in the docs

That is a bit odd, did you erase flash?

sarusso commented 2 years ago

Yes I did erase flash. Looking forward to hear what happens on your test bench! :)

atanisoft commented 2 years ago

Yes I did erase flash.

Any chance you can capture the console output? It may provide some clues as to where the WiFi name is coming from, also where are you seeing this name?

atanisoft commented 2 years ago

The WiFi network name and password do not match (mine is "ESP_58AF05", not "esp32csap" as stated in the docs)

I think I've identified the issue causing this:

00:00:00.495:: heap: 246.39kB/321.32Kb (max block size: 107.99kB), mainBufferPool: 0.23kB
[WiFi] Initializing WiFi stack
[WiFi] SoftAP password is blank, using OPEN auth mode.
[WiFi] Configuring SoftAP (SSID: )
[WiFi] Starting WiFi stack

I'll need to check what happened to cause it to record a blank SSID in the OpenLCB config file.

sarusso commented 2 years ago

Yes I did erase flash.

Any chance you can capture the console output? It may provide some clues as to where the WiFi name is coming from, also where are you seeing this name?

I am seeing this name on any WiFi enabled device (computer, phone, tablets)

Here is the console output, where I tried the latest version of the firmware (https://github.com/atanisoft/ESP32CommandStation/actions/runs/1784724099, firmware ESP32CommandStation-esp32-idfv44-l298), then connected to the AP and enabled the power on tracks (that is still not working):

ets Jun  8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4704
load:0x40078000,len:14252
load:0x40080400,len:3464
entry 0x40080638
ESP32 Command Station starting up...
Compiled on Feb  2 2022 15:39:05 using IDF v4.4-98-geb3797dc3f
Running from: ota_0
ESP32 Command Station uses the OpenMRN library
Copyright (c) 2019-2021, OpenMRN
All rights reserved.
[SNIP] version:4, manufacturer:atanisoft.github.io, model:ESP32CommandStation, hw-v:1.0.0 esp32, sw-v:v2.0.0-alpha1-45-gac
[CDI] Size: 17683, Version:0200
[SoC] reset reason:1 - power on reset
[SoC] model:ESP32,rev:1,cores:2,flash:No,WiFi:Yes,BLE:Yes,BT:Yes
[SoC] Heap: 280.86kB / 321.60kB
[SoC] App running from: ota_0
[NVS] Initializing RTC vars
[NVS] Initializing NVS
[NVS] Loading configuration
[NVS] Node ID: 05.02.01.03.ff.fe
[NVS] WiFi Mode: SoftAP (2)
[NVS] Hostname Prefix: esp32cs_
[NVS] SoftAP SSID: esp32csap
[NVS] SoftAP Auth: open (0)
[NVS] SNTP: Off
[NVS] FastClock: Disabled
[NVS] LED brightness: 128
[NVS] Bootloader flag is false, start the stack
[FS] SPIFFS usage: 5.15/463.52 KiB
[FS] SPIFFS will be used for persistent storage.
[FS] Dumping content of filesystem: /fs
[FS] /fs/train.xml (3155 bytes) mtime: Thu Jan  1 00:00:04 1970
[FS] /fs/tmptrain.xml (566 bytes) mtime: Thu Jan  1 00:00:04 1970
[FS] /fs/olcb_config (410 bytes) mtime: Wed Dec 31 23:59:59 1969
[ULP-ADC] Registering ULP wakeup ISR
[ULP-ADC] Loading ULP ADC monitoring binary
[ULP-ADC] OPS Short threshold: 3686/4095 (1799.80 mA), Warning threshold: 2764/4095 (1349.61 mA), Shutdown threshold: 4090/4095 (1997.07 mA), Pin: 35 (ADC1:7)
[ULP-ADC] PROG Ack threshold: 122/4095 ( 59.57 mA), Short threshold: 512/4095 (250.00 mA), Pin: 34 (ADC1:6)
[ULP-ADC] Starting the ULP FSM
[TrainDB] Refreshing train CDI files...
[CDI] Checking /fs/train.xml...
[CDI] File /fs/train.xml appears up-to-date (len 3155 vs 3155)
[CDI] Checking /fs/tmptrain.xml...
[CDI] File /fs/tmptrain.xml appears up-to-date (len 566 vs 566)
[TrainDB] /fs/trains.json does not exist, skipping loading.
[TrainDB] Found 0 persistent roster entries.
[Track] Registering /dev/track VFS interface
[DCC-RMT-0] DCC config: zero:100uS, one:58uS, preamble-bits:11/22, wave:high,low, signal pin:19, RMT-mem:80 (blocks:2), RMT-CLK:APB (1)
[DCC-RMT-0] Starting signal generator
[OPS] EN/PWM: 25, Current Sense Pin: 35 (ADC1:7)
[PROG] EN/PWM: 23, Current Sense Pin: 34 (ADC1:6)
[Track:0] Connecting track interface
[Track Power] Registering OpenLCB event consumer (On:01.00.00.00.00.00.ff.fe, Off:01.00.00.00.00.00.ff.ff)
[eStop] Registering OpenLCB event consumer (On:01.00.00.00.00.00.ff.fd, Off:01.00.00.00.00.00.ff.fc)
[AccessoryDecoderDB] Initializing
[AccessoryDecoderDB] /fs/decoders.json does not exist, skipping loading.
[AccessoryDecoderDB] Loaded 0 accessory decoder(s)
[NodeIdMemCfg:aa] NodeID: 05.02.01.03.ff.fe
[WiFiMemCfg:ab] register_numeric(wifi_mode): 250
[WiFiMemCfg:ab] register_string(hostname_prefix): 251
[WiFiMemCfg:ab] register_string(station_ssid): 272
[WiFiMemCfg:ab] register_string(station_pass): 304
[WiFiMemCfg:ab] register_string(softap_ssid): 432
[WiFiMemCfg:ab] register_string(softap_pass): 464
[WiFiMemCfg:ab] register_numeric(softap_auth): 592
[WiFiMemCfg:ab] register_numeric(softap_channel): 593
[WiFiMemCfg:ab] register_numeric(sntp_enabled): 594
[WiFiMemCfg:ab] register_string(sntp_server): 595
[WiFiMemCfg:ab] register_string(sntp_timezone): 659
[WiFiMemCfg:ab] registered
[FastClockMemCfg:ac] register_numeric(enabled): 1000
[FastClockMemCfg:ac] register_string(id): 1001
[FastClockMemCfg:ac] register_numeric(year): 1033
[FastClockMemCfg:ac] register_numeric(month): 1035
[FastClockMemCfg:ac] register_numeric(day): 1036
[FastClockMemCfg:ac] register_numeric(hour): 1037
[FastClockMemCfg:ac] register_numeric(minute): 1038
[FastClockMemCfg:ac] register_numeric(rate): 1039
[FastClockMemCfg:ac] registered
[RealTimeClockMemCfg:ad] register_numeric(enabled): 1500
[RealTimeClockMemCfg:ad] register_string(id): 1501
[RealTimeClockMemCfg:ad] registered
[OpenLCB] DCC signal output: Enabled
[OpenLCB] Brownout event publishing: Enabled
[OpenLCB] DCC Accessory Decoder Listener: Enabled
[OpenLCB] Train Search Listener: Enabled
00:00:00.444:: heap: 246.79kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.23kB
[WiFi] Initializing WiFi stack
[WiFi] SoftAP password is blank, using OPEN auth mode.
[WiFi] Configuring SoftAP (SSID: )
[WiFi] Starting WiFi stack
[SoftAP] MAC Address: 24:0a:c4:58:af:05
[SoftAP] Setting ESP32 hostname to "esp32cs_5020103fffe".
[SoftAP] IP address: 192.168.4.1
[mDNS] Initializing mDNS system
[mDNS] Setting mDNS hostname to "esp32cs_5020103fffe"
[httpd] Starting DNS listener
[httpd] Starting HTTP listener on port 80
[dnsd] Listening on port 53, fd 48, using 192.168.4.1 for local IP
Listening on port 80, fd 49
[mDNS] mdns_service_add(_http._tcp:80): ESP_OK.
[mDNS] Advertising _http._tcp:80.
Allocating new alias 436 for node 05020103fffe
[SoftAP aid:1] c8:69:cd:91:c2:e4 connected.
W (26226) wifi:<ba-add>idx:2 (ifx:1, c8:69:cd:91:c2:e4), tid:6, ssn:0, winSize:64
W (27490) wifi:<ba-add>idx:3 (ifx:1, c8:69:cd:91:c2:e4), tid:0, ssn:0, winSize:64
00:00:15.447:: heap: 178.59kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
Incoming connection from 192.168.4.2, fd 50.
Incoming connection from 192.168.4.2, fd 51.
Incoming connection from 192.168.4.2, fd 50.
Incoming connection from 192.168.4.2, fd 52.
Incoming connection from 192.168.4.2, fd 50.
Incoming connection from 192.168.4.2, fd 51.
[Track Power] Clearing global emergency off
[Track] Usage: 0/3686, 0 mA
00:00:30.450:: heap: 175.46kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:00:45.453:: heap: 175.46kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:01:00.457:: heap: 175.46kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:01:15.460:: heap: 175.46kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
W (89431) wifi:<ba-del>idx
W (89431) wifi:<ba-del>idx
[SoftAP aid:1] c8:69:cd:91:c2:e4 disconnected.
[Track] Usage: 0/3686, 0 mA
00:01:30.463:: heap: 176.63kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:01:45.465:: heap: 176.64kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:02:00.468:: heap: 176.63kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 28/3686, 13 mA
00:02:15.471:: heap: 176.64kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
[Track] Usage: 0/3686, 0 mA
00:02:30.473:: heap: 176.64kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB
atanisoft commented 2 years ago

enabled the power on tracks (that is still not working): ... Incoming connection from 192.168.4.2, fd 51. [Track Power] Clearing global emergency off [Track] Usage: 0/3686, 0 mA 00:00:30.450:: heap: 175.46kB/321.60Kb (max block size: 107.99kB), mainBufferPool: 0.45kB

Based on this output the OPS EN pin (25) should be HIGH (3v3). I'm able to see the same on my bench which doesn't make much sense entirely. I'll do some digging.

atanisoft commented 2 years ago

@sarusso Please give the latest a go. I've put in a fix for the EN pin not enabling correctly.

sarusso commented 2 years ago

@sarusso Please give the latest a go. I've put in a fix for the EN pin not enabling correctly.

Ok, I got it working!!! 🎉

Just FYI, following the pin configuration here: https://github.com/atanisoft/ESP32CommandStation/issues/103#issuecomment-1013704722

Lights and functions seems working properly (-ish, read on).

Still some issues:

The connection to ws://192.168.4.1/ws was interrupted while the page was loading. [192.168.4.1:818:13](http://192.168.4.1/)
WS closed, reconnecting. Reason: undefined [192.168.4.1:804:15](http://192.168.4.1/)
WS closed, reconnecting. Reason: <empty string> [192.168.4.1:804:15](http://192.168.4.1/)

Do you have any comments on these points?

Thanks for the help so far!!

sarusso commented 2 years ago

p.s. I checked on the console and even with a Python websocket client: no matter what speed I set to the loco, the response is always zero and the loco does not move at all:

WS-TX: {"req":"loco","addr":3,"spd":43,"id":52}
WS-RX: {"res":"loco","addr":3,"spd":0,"dir":false,"id":52}
atanisoft commented 2 years ago

@sarusso WiThrottle is not yet implemented. I'm trying to figure out what is breaking the OpenLCB tab, it is used for configuration settings primarily. I've got a lead on it but haven't found the exact failure yet.

For the WS disconnect, that looks likely that the CS crashed during the OpenLCB load possibly. I'll check further as to cause.

For the throttle/functions, I've got an idea of where it is failing and will confirm it via the console output. The speed coming back as zero indicates that the speed update got dropped, cause TBD. I'll see if I can find what is going on here, can you check the console output when accessing the loco from the browser?

For functions, can you give me a bit more info? 12v 2A should be sufficient for Z/N and possibly HO, but may provide around 10.5v to the track as the L298 will sink up to 1.5V from the supply voltage to the tracks. For my test setup (and layout) I'm using a 15v 5A supply.

sarusso commented 2 years ago

@atanisoft I think functions issues are a bug of the loco and how it handles digital couplings, and by this I mean that if you do not deactivate the front one and then use the back one it kind of goes crazy, there is something mentioned on the manual but I won't bother you with this. I would say that it is not a bug and that after a few tests everything including the couplers is working fine.

For the WiThrottle, TrainzLuvr here https://github.com/atanisoft/ESP32CommandStation/issues/103 suggested to give it a try, I probably misunderstood what he/she meant. I do not intend to use OpenLCB (which I am not even sure what is ti for), I just thought it was necessary for using WiThrottle.

For the impossibility of changing speed: here are logs of the Firefox console of a fresh session. Rebooted the Esp32, reconnected, selected loco, powered on track, turned lights on, tried to change speed:

WS-RX: {"res":"info","timestamp":"Feb  3 2022 21:21:40","ota":"ota_0","snip_name":"ESP32CommandStation","snip_hw":"1.0.0 esp32","snip_sw":"v2.0.0-alpha1-53-g31","node_id":"5020103fffe","statusLED":false,"statusLEDBrightness":128,"id":1} 192.168.4.1:607:19
WS-TX: {"req":"status","id":2} 192.168.4.1:793:15
WS-RX: {"res":"status","id":2,"track":"Off"} 192.168.4.1:607:19
WS-TX: {"req":"loco","addr":3,"spd":0,"dir":false,"id":3} 192.168.4.1:793:15
WS-RX: {"res":"loco","addr":3,"spd":0,"dir":false,"id":3} 192.168.4.1:607:19
WS-TX: {"req":"event","evt":"01.00.00.00.00.00.FF.FE","id":4} 192.168.4.1:793:15
WS-RX: {"res":"event","evt":"01.00.00.00.00.00.FF.FE","id":4} 192.168.4.1:607:19
WS-TX: {"req":"status","id":5} 192.168.4.1:793:15
WS-RX: {"res":"status","id":5,"track":"On","usage":0} 192.168.4.1:607:19
WS-TX: {"req":"function","addr":3,"fn":0,"state":true,"id":6} 192.168.4.1:793:15
WS-RX: {"res":"function","id":6,"fn":0,"state":true} 192.168.4.1:607:19
WS-TX: {"req":"loco","addr":3,"spd":21,"id":7} 192.168.4.1:793:15
WS-RX: {"res":"loco","addr":3,"spd":0,"dir":false,"id":7} 192.168.4.1:607:19
WS-TX: {"req":"status","id":8} 192.168.4.1:793:15
WS-RX: {"res":"status","id":8,"track":"On","usage":0}
TrainzLuvr commented 2 years ago

For the WiThrottle, TrainzLuvr here #103 suggested to give it a try, I probably misunderstood what he/she meant. I do not intend to use OpenLCB (which I am not even sure what is ti for), I just thought it was necessary for using WiThrottle.

We have been focusing on LCC all this time, and to be honest in my mind I did not even consider that WiThrottle protocol might not be implemented at all in the ESP32CS. I just sort of assumed it was there.

Sorry about that, if I have misled you.

atanisoft commented 2 years ago

Well, there is code for it in OpenMRN but I don't know if it is compliant with the latest protocol updates. It's a pretty basic text protocol so it shouldn't take long to add a translation layer. Almost all aspects of the protocol are available now as well

sarusso commented 2 years ago

@TrainzLuvr no problem, I was just trying to understand.

@atanisoft FYI the WiThrottle App did connect at some point, but buttons and loco selection seemed unresponsive. As I think you would expect.

Looking forward to see if you can fix the speed update problem! :)

atanisoft commented 2 years ago

FYI the WiThrottle App did connect at some point

That is interesting since I don't expose any mDNS entry that WiThrottle normally looks for (unless it looks specifically for an http endpoint now). I'll see if I can get something working for basic integration pretty soon.

if you can fix the speed update problem!

Pull the latest commit as it should fix it.

sarusso commented 2 years ago

It works! 🎉 That's great! 😄

atanisoft commented 2 years ago

@sarusso The OpenLCB tab should now work in this latest commit.

I'll also be starting on WiThrottle support this weekend.

sarusso commented 2 years ago

So, I tested the ESP32CommandStation over the weekend. Everything seems to work quite nice, but I think there is a small bug in the Web UI so that after function 9 they get looped (i.e. function 15 activates function 5, function 21 activates function 1 and so on).

I also have a problem with the digital couplers. When I activate them on my Roco 72001, the H-bridge behave strangely, and if the loco is a bit far from the track power plugs (say, 50 cm away) it shuts down and starts behaving randomly.

I captured the issue with the loco near the tracks power plugs in a video: https://youtu.be/eqLwZ35wEDg. See the H-bridge leds: they get more intense and start flickering when the coupler is activated and stop when it is deactivated. The power supply is a 18,5 V and 3.5A.

What do you think is causing this? Have you ever experienced something similar?

atanisoft commented 2 years ago

See the H-bridge leds: they get more intense and start flickering when the coupler is activated and stop when it is deactivated. The power supply is a 18,5 V and 3.5A.

Is that a magnetic decoupler you are activating or a function on the car that might be similar? Is the power supply shared with other devices (accessories such as magnetic decouplers)?

Have you ever experienced something similar?

I can't say that I have ever seen that before, it is certainly odd. Normally the only way those LEDs should change intensity is if there is a voltage/current change on the power supply to the shield. As example, if I connect the 5v pin to the shield POS power input the LEDs are quite dim but on the 15v supply they are similar to yours.

Everything seems to work quite nice

Glad to hear that it is working well for you so far! I still need to get more documentation written, specifically the user guide section. And then one more review of the docs to confirm they are accurate.

I think there is a small bug in the Web UI so that after function 9 they get looped (i.e. function 15 activates function 5, function 21 activates function 1 and so on).

Interesting, I don't use most functions beyond the 0-8 typically. I'll do some digging and see if it is an encoding issue for the DCC packet or some other sort of bug in the UI.

sarusso commented 2 years ago

Is that a magnetic decoupler you are activating or a function on the car that might be similar? Is the power supply shared with other devices (accessories such as magnetic decouplers)?

I suspect that the decoupler coming built-in with the loco out from the factory is magnetic, yes. Do you think I could do something to mitigate the issue from and electronics point of view (a capacitor, a solenoid, a diode, something)?

Glad to hear that it is working well for you so far! I still need to get more documentation written, specifically the user guide section. And then one more review of the docs to confirm they are accurate.

👍

Interesting, I don't use most functions beyond the 0-8 typically. I'll do some digging and see if it is an encoding issue for the DCC packet or some other sort of bug in the UI.

If I send the commands using a Python websocket client directly to the Command Station (i.e. {"req":"function","addr":3,"fn":20,"state":true,"id":1}), I can access functions above the 9 with no issues. I think this is a trivial bug in the UI.

atanisoft commented 2 years ago

Do you think I could do something to mitigate the issue from and electronics point of view (a capacitor, a solenoid, a diode, something)?

Possibly a larger decoupling capacitor. I don't know the correct sizing for that though.

I can access functions above the 9 with no issues. I think this is a trivial bug in the UI.

Ok, if it works from other WS clients but not the web it is very likely a trivial bug in the rendition of the UI.

atanisoft commented 2 years ago

I think this is a trivial bug in the UI.

Turns out it was a VERY subtle bug, the regex matching for /.*(\d+).*/ which should match multiple digits is only catching the last one. So I've changed it over to use split(/_/) which will take care of the splitting of the ID correctly.

sarusso commented 2 years ago

Turns out it was a VERY subtle bug, the regex matching for /.*(\d+).*/ which should match multiple digits is only catching the last one. So I've changed it over to use split(/_/) which will take care of the splitting of the ID correctly.

Ah, regexs 😂