Introducing DCC++ ---a complete open-source DCC station and interface

Gregg Aug 25, 2015

  1. HVT

    HVT TrainBoard Member

    74
    93
    15
    Steve, Thanks for the explanation. Looking forward to more info about your implementation.
    Dave
     
    Scott Eric Catalano likes this.
  2. HVT

    HVT TrainBoard Member

    74
    93
    15
    Steve,
    I think a simple viable application would be a handheld throttle with a digital encoder for speed and a button or two for horn and perhaps lights. I know Jason is working on a throttle, albeit a bit more complex. A limit of 4 of them would be more than adequate.
    Dave
     
    Scott Eric Catalano likes this.
  3. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    All,

    OK, here's a copy of the 'webpage' that will be the front end of our Throttle application. It is by no means finished !
    Download the whole folder "indexDCC++" to your chosen device. Then just open indexDCC++.html in the device web browser. Note that you should keep all the files together as the others enable correct rendering of the page when you're not connected to the internet. In fact internet dependency is not required. This feature will enable us to host the page directly from storage on the ESP8266 in a later version. So you would just log your device on to the 8266 domain and call the page straight to your browser in a secure mini network, thus eliminating this step when a new device wants to connect to an existing setup.

    I would really like everyone interested to test how it renders (if at all) on various mobile devices. I've written the page (and don't forget this is my first go at HTML) using a modern structure called Bootstrap.

    For the non technical users you don't need to be concerned about that. But it would be great if some of you could help with future code developments as Html is not that hard to learn. I wrote the basic structure over a couple of evenings with the help of a template or two. If you know Javascript as well, then we'll be flying. Even if you can't code, have a look at it in a basic text editor like notepad (windows) or the Github file viewer. I'm not used to the Github protocol just yet so forgive me if something is wrong.

    The beauty of Bootstrap is that it automatically sorts rendering of the page from small mobiles to a large desktop screen. Try it out on both you'll hopefully see what I mean.

    So what we have so far is a login box where you just type in the esp8266 server address eg. 192.168.22.10:81 This can be omitted for an auto login in a later version if you would always be addressing the same system.
    Then by default the main power switch is set OFF. Just tap that to switch ON.
    Type in a cab # in the next box, choose forward or reverse and move the throttle slider.
    At the bottom we have some function keys (yet to be configured) just to give an idea of layout.

    Over to you for what you'd like to see added.

    I have a little tidying up of the server code to but will get that posted shortly. You will need a cheap esp8266 development board for that stage (wait for the photo suggestion before ordering).

    For now lets get any bugs out of the html and throw around a few ideas.

    Steve.
     
  4. Davemil

    Davemil New Member

    4
    2
    1
    New technology, New techniques. I started playing with a Particle Photon ($19) with on chip Wifi. Its DMA supported SPI allows DCC signal generation completely independent of the CPU once an output buffer has been loaded. and dma initiated. A single Photon is easily capable of supporting 8, 16 ...or how many channels, The same 4 wire SPI interface can also simultaneously supply low resolution pwm channels for servo control and lamp dimmers and discrete on/off controls, assuming a custom SPI interfaced motor shield . Is there room here for a Photon port? Thoughts??
     
    sboyer2 and Scott Eric Catalano like this.
  5. HVT

    HVT TrainBoard Member

    74
    93
    15
    Steve,
    Out of town until Saturday night so won't get much testing done, but if I understand correctly I can download the indexDCC++ folder to my Android tablet, open indexDCC++.html in Chrome and see the throttle window you posted on post#906?

    Dave
     
    Scott Eric Catalano likes this.
  6. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Yes Dave,

    Spot on. Catch you later.
     
    Scott Eric Catalano likes this.
  7. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Dave,

    Sounds awesome. I'll check it out when I'm done with phase one of my server code.

    Regards

    Steve.
     
    Scott Eric Catalano likes this.
  8. HVT

    HVT TrainBoard Member

    74
    93
    15
    Steve,
    This is the top half of the page that I get on the laptop. ESP8266 Throttle.jpg
    Haven't been able to figure out how to download the folder on the S5 phone.
    Dave
     
    Scott Eric Catalano likes this.
  9. w8one

    w8one TrainBoard Member

    89
    109
    5
    Appears fine on my windows 10 (x64) machine using chrome (1600 x 1200 resolution). Base station is not done yet, life is getting in the way of playing with my trains.
     
    Scott Eric Catalano likes this.
  10. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    @HVT

    Strange behaviour for Chrome. Did you open the file from within its parent folder. I think I mentioned the other files are dependencies which would normally be called from the internet.
    If you can't download to your phone, perhaps do a file transfer from your laptop. Worked for me on a Kindle Fire 7HD in the oddball Silk browser, displayed perfectly.

    @w8one

    Thank you Sir, that's the info we need, will you be trying tablet/phone? Hope you get some playtime soon.

    More tablets/phones please everyone, especially interested in IOS and Mac compatibility.

    Regards

    Steve.
     
    Scott Eric Catalano likes this.
  11. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    sboyer2 and Scott Eric Catalano like this.
  12. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Dave

    Excellent Thank You. Shame we got some issues there.

    I was rather hoping you'd chime in and have another look at the ESP8266, I note from your website that you had a brief experiment in its earlier days.

    You will be pleased to know that a working example of the Server sketch should be up on Github later today. I'm just tinkering with it now.

    Hope you can jump right in and get it working straight away as you already have a chip. You will just have to power it up re- flash and connect Tx(8266) > Rx(Arduino) same as your IR throttle, a one way serial connection.

    Regards

    Steve.

    Edit; I can see the switches are not rendered properly on the Galaxy Note. Please make sure all the files are together in the same folder and that the .html file is initiated from there. The other files are dependencies that would normally be collected from the internet. This shouldn't be an issue later as we will be serving the page from the esp8266 memory. Thanks.
     
    Last edited: Mar 10, 2016
    Scott Eric Catalano likes this.
  13. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Steve - I do have a number of the ESP8266 boards and have done a good bit of experimentation with them - please let us know when the code is posted and I'll give it a try.

    I revisited the files on my phone and, as you suggested, all were not available as I was using Dropbox to view the HTML and it only downloaded the main file - when I copied the folder to the phone it worked properly from Firefox and Chrome (see revised images here: http://trainelectronics.com/DCC_Arduino/DCC++/WebThrottle/throttle_screen_shots.htm )
    The image below is from Chrome on the Note 4 - it only shows the top of the file - the bottom is on the web page.

    thanks

    dave
    [​IMG]
     
    Scott Eric Catalano likes this.
  14. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Dave,

    Almost fixed then. You should be able to just slide it into view. But I agree it would be better if both sliders were there by default. No matter we can fix that very easily.

    Open the .html file in Notepad (windows) and look for this bit of code near the top of the page

    }

    .container-fluid {
    padding-top: 20px;
    padding-bottom: 20px;
    }
    </style>

    Change the px values to say 10. That will narrow each 'band' by 2o pixels for starters.

    Then scroll down to the <body> section till you find this

    <body>
    <div class="container-fluid bg-1 text-center">
    <h3>DCC++ Mobile Controller</h3>
    <h3>Beta</h3>
    </div>

    Deleting the line <h3>Beta</h3> will remove the word Beta from the header and narrow that bit considerably.

    And while you're in there, I found a bug earlier. Scroll back up to the script functions section looking for this

    }

    function Cabmod1() {
    var text = document.getElementById('Cab1').value;
    var toSend = "CAB1" + text;
    connection.send(toSend);

    In your version it says "CAB" just add the 1 as above. Save and exit you're done.

    Give me another 10 and I'll post some code for the esp. Hopefully on here.

    Thanks for now.

    Steve
     
    Scott Eric Catalano likes this.
  15. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    That did the trick getting both throttles on the screen.

    thanks

    dave

    [​IMG]
     
    Scott Eric Catalano likes this.
  16. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Dave,

    Here's a Proof of Concept version of the esp8266 server sketch. Sorry, not got round to activating the function buttons just yet. But as you'll see it's so beautifully simple, you might want to play around with it yourself.

    So what are websockets all about? They are about as near as we can get to a true TCP/IP connection from a web browser. Built-in browser security stops us short of that, however unofficial solutions have seen some workarounds.

    For our application we really don't need to go there. A browser websocket once opened, provides an 'open till closed pipe' that we can move data up and down at relatively high speed with minimal overhead.
    So on both the client and server we only need very minimal 'processing' to output our serial commands to the Base Station. Applications on the Web include the ever more popular live chat help windows, that most of you will have seen in recent times.

    Those of you that want to play with the code should find it easy to set up processing of a user input event (button pressed triggers "onchange") send some identifying code to the server which constructs an instruction string for the Base Station.

    All the server is doing is listening for those codes and then acting accordingly. If we want to develop this further then we can set up bi-directional messages so the client page can 'see' what's going on, amongst other things.

    Anyway over to you guys.

    Code:
    //*** IMPORTANT *** Most of the libraries used are included in the ESP8266 Core package which you must install to the Arduino IDE for this sketch to compile.
    //Please use the Git method here https://github.com/esp8266/Arduino and NOT the File > Preferences > Additional Boards Manager URL.
    //WebSocketsServer.h appears to be broken using the later. And DO include the Python requirement (correct at Feb 15 2016) The author is aware of this issue.
    //You will need to install Git to simplify that operation.
    //If the sketch compiles OK but won't upload properly, then go Sketch > Export compiled Binary. A .bin file will be created in this sketch's folder.
    //Then upload the binary with your favorite flashing tool.
    
    //Please remember to fill in your home network SSID and PASSWORD. In this version when you reboot your esp8266 it will automatically log on to your WLAN.
    //Make a note of the IP address assigned by your router by logging on to its home page. Your PC or Mobile also need to be on this same network.
    //With the "Webpage" open on your chosen device, type the IP and Port in the following format eg. 192.168.11.55:81 (port == @line 28) press Enter(desktop) or tap the screen outside the box (mobile).
    //With luck you are now connected enjoy. A message will confirm in later versions.
    //Console Debugging is available in Chrome windows desktop for the html wireless side. And at the other end (Arduino) a serial monitor will let you know whats happening there.
    //You can also see serial out from esp8266 if you connect by usb and use a serial monitor there. (Debug from this sketch shows up there)
    
    //In tests I hooked up 2 seperate clients at the same time so multi-client is confirmed. However as certain values are fixed assignment things start to go wrong quickly. We can work that out ;)
    //There is room for many more functions on the page, this is just Proof of Concept.
    
    //Special thanks to Rahul 27 who created the original sketch on which this sketch is based. And also Markus Sattler for the WebSocketsServer library.
    
    //This software is Free. Enjoy. Steve Lowry(indev2)2016.
    
    #include <Arduino.h>
    #include <ESP8266WiFi.h>
    #include <WebSocketsServer.h>
    #include <Hash.h>
    #include <EEPROM.h>
    WebSocketsServer webSocket = WebSocketsServer(81);
    
    String c;
    String d;
    int cInt;
    int dInt;
    const char* ssid     = "YOUR SSID";
    const char* password = "YOUR PASSWORD";
    const int redPin = 15;  //Debug LED function not needed ***All debug lines can be deleted once you understand the code and you have it working, we like tidy code ;)***
    const int greenPin = 12; //Debug LED function not needed
    void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
    
      switch (type) {
        case WStype_DISCONNECTED:
    
          break;
        case WStype_CONNECTED:
          {
            IPAddress ip = webSocket.remoteIP(num);
    
    
          }
          break;
        case WStype_TEXT:
          {
    
            String text = String((char *) &payload[0]);
            if (text == "PON") {
    
              digitalWrite(12, HIGH);//Visual Debug - Green LED will come on at Power ON
              digitalWrite(15, LOW);//Debug - not needed
              Serial.println("<1>");
    
            }
    
            if (text.startsWith("CAB1")) {
    
              String c = (text.substring(text.indexOf("CAB1") + 4, text.length()));
              int cInt = c.toInt();
              EEPROM.write (4, cInt);
              EEPROM.commit();
              delay (20);
              Serial.print("Cab# Entered t1 ");//Debug
              Serial.println(cInt);//Debug
    
            }
    
            if (text.startsWith("CAB2")) {
    
              String c = (text.substring(text.indexOf("CAB2") + 4, text.length()));
              int cInt = c.toInt();
              EEPROM.write (12, cInt);
              EEPROM.commit();
              delay (20);
              Serial.print("Cab# Entered t2 ");//Debug
              Serial.println(cInt);//Debug
    
            }
    
            if (text == "FOR1") {
    
              dInt = (1);
              EEPROM.write (0, dInt);
              EEPROM.commit();
              int Cab1 = EEPROM.read(4);
              Serial.print("Forward Entered t1 ");//Debug
              Serial.println(dInt);//Debug
              Serial.print("<t1 ");
              Serial.print(Cab1);
              Serial.println(" 0 1>");//Stops train (if not stopped) on direction change
              delay (20);
    
            }
    
            if (text == "FOR2") {
    
              dInt = (1);
              EEPROM.write (8, dInt);
              EEPROM.commit();
              int Cab2 = EEPROM.read(12);
              Serial.print("Forward Entered t2 ");//Debug
              Serial.println(dInt);//Debug
              Serial.print("<t2 ");
              Serial.print(Cab2);
              Serial.println(" 0 1>");
              delay (20);
    
            }
    
            if (text == "REV1") {
    
              dInt = (0);
              EEPROM.write (0, dInt);
              EEPROM.commit();
              int Cab1 = EEPROM.read(4);
              Serial.print("Reverse Entered t1 ");//Debug
              Serial.println(dInt);//Debug
              Serial.print("<t1 ");
              Serial.print(Cab1);
              Serial.println(" 0 0>");
              delay (20);
    
    
            }
    
            if (text == "REV2") {
    
              dInt = (0);
              EEPROM.write (8, dInt);
              EEPROM.commit();
              int Cab2 = EEPROM.read(12);
              Serial.print("Reverse Entered t2 ");//Debug
              Serial.println(dInt);//Debug
              Serial.print("<t2 ");
              Serial.print(Cab2);
              Serial.println(" 0 1>");
              delay (20);
    
    
            }
    
            if (text.startsWith("x")) {
    
              String xVal = (text.substring(text.indexOf("x") + 1, text.length()));
              int xInt = xVal.toInt();
              analogWrite(greenPin, xInt); //Debug - not needed, Green "ON" LED will brighten/dim according to throttle up/down
              int dvalue = EEPROM.read(0);
              int cvalue = EEPROM.read(4);
              Serial.print("<t1 ");
              Serial.print(cvalue);
              Serial.print(" ");
              Serial.print(xInt);
              Serial.print(" ");
              Serial.print(dvalue);
              Serial.println(">");
    
            }
    
            if (text.startsWith("y")) {
    
              String yVal = (text.substring(text.indexOf("y") + 1, text.length()));
              int yInt = yVal.toInt();
              int dvalue = EEPROM.read(8);
              int cvalue = EEPROM.read(12);
              Serial.print("<t2 ");
              Serial.print(cvalue);
              Serial.print(" ");
              Serial.print(yInt);
              Serial.print(" ");
              Serial.print(dvalue);
              Serial.println(">");
    
            }
    
            if (text == "RESET") {
    
              EEPROM.write (0, 0);
              EEPROM.write (4, 0);
              EEPROM.write (8, 0);
              EEPROM.write (12, 0);
              EEPROM.commit (); //All stored values set zero
              delay (20);
              digitalWrite(15, HIGH);//Visual Debug - not needed - Red LED will come on at Power OFF
              analogWrite(greenPin, 0); //LED function - not needed
              Serial.println("<0>");
    
            }
            break;
    
          }
    
          webSocket.sendTXT(num, payload, lenght);
          webSocket.broadcastTXT(payload, lenght);
          break;
    
        case WStype_BIN:
    
          hexdump(payload, lenght);
    
          // echo data back to browser
          webSocket.sendBIN(num, payload, lenght);
          break;
      }
    
    }
    
    
    void setup() {
    
      Serial.begin(115200);
      pinMode(15, OUTPUT);
      pinMode(12, OUTPUT);
      EEPROM.begin(512);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(100);
      }
      Serial.println(WiFi.localIP());
      webSocket.begin();
      webSocket.onEvent(webSocketEvent);
    }
    
    void loop() {
      webSocket.loop();
    }
     
    Last edited: Mar 10, 2016
    Scott Eric Catalano likes this.
  17. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Sorry Dave

    If you just copied that code before I posted this update then you need to amend it or copy again, I have amended above.

    More bugs at if (text == "FOR") {

    AND

    if (text == "REV") {

    Need to be FOR1 and REV1 with corresponding alterations in the html aswell.

    Reason being there was only one throttle in the code to start but now a FOR2 text event would also trigger a FOR response and likewise for REV.

     
    Last edited: Mar 10, 2016
    Scott Eric Catalano likes this.
  18. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Steve - I can't find a place to download WebSocketsServer.h (or its library) - I find WebSocketServer.h (singular Socket) but not the plural - am I missing something?

    thanks

    dave
     
    Scott Eric Catalano likes this.
  19. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hi Dave,

    I had much the same problems when looking for the same.

    The instructions I've written at the top of the sketch pretty much cover it. That and other important libraries are found in the ESP8266 Core package which gives the full support when using the Arduino IDE.
    If you click the Github link in those instructions and follow the instructions about halfway down the page for Git method install you should be up and running in no time. Don't forget the python bit.

    This bit is a pain I know, but its not going to work without it. For a one time install you'll have a whole raft of esp libraries at your disposal.

    I'll be on hand for a while yet.

    Regards

    Steve
     
    Scott Eric Catalano likes this.
  20. David Bodnar

    David Bodnar TrainBoard Member

    264
    481
    13
    Steve - I just spent some time installing and trying to figure out how to use GIT in Windows and had no success - I did read the documentation but it seems to be a royal pain to just get a file or two into the Arduino environment. If we are just looking for a library can you email it to me? I really don't want to spend time on such a confusing operation.
    thanks
    dave

    PS my direct email is dave@davebodnar.com
    PPS I am fairly familiar with Linux and use the Raspberry Pi for many projects and the Git info is still way too cryptic! Many other readers of this forum are likely to be in the same boat
     

Share This Page