ESP8266 Wifi control using LocoNet UDP

Moderator: Moderators

ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 07.08.2016, 20:37

I have been working on my version of Liviu's RFID sensor code to make a "Mobile" "Loconet Single Loco Command Station".
The code runs entirely in the ESP8266, and responds to conventional Loconet Slot requests and set slot speed etc commands for a single slot (which is set at the Loco Address (CV[1]) of the ESP8266).

You can set CV settings using the Rocrail programming functions, and the ESP8266 also responds to LocoIO programming via the Rocrail "programming/Loconet/LocoIO" tabs.
In the "Mobile" version, only ports 0 3, 4 and 8 are "spare", as the rest are used by the RFID reader.
In the current version, Port 8 is used to send "servo" commands for the locomotive motor,
Port 3 is set/cleared by Function F3 ; Port 0 is set / cleared by Function F4 (eventually these will be lights and respond to direction and F0).
Hopefully I will use the others later for smoke...

In use, RFID tabs are set in the track, and each Loco will has an ESP8266 and a RFID reader(and a motor controlled by a simple RC type motor servo controller).
When the loco passes over a Tag, it sends a "General sensor input codes" message with an address unique to the tag (in a 0-2047) range. Thus any loco passing over the tag will see the same "tag" address, and Rocrail can use this as a conventional block sensor to automate the railway with up to 127 locos, although I would expect the limit to be a lot less in practice.. The code also sends a "tag gone" message when the tag is removed from the reader, so the block sensor works correctly. (This was more difficult to code than you would have expected!).

My previous static" version of the code without the RFID reader gives 8 ports to provide turnout controls or to switch lights on the trackside etc.

Now the bad news... I have been testing this and the code does sometimes miss some of the "Set Slot" messages (A0,A1,A2).

As far as I can deduce from my tests, the RFID code is taking about 30ms and during this time the ESP8266 is blind to WiFi.
I only check the RFID reader when there are no WiFi commands to parse, but during the RFID reading, it appears that WiFi commands can still be lost. My timing suggests that a full set of A0 to A2 commands are sent within about 4 ms, using timing similar to Loconet when its is running on physical electrical connections.
The ESP8266 seems to have a hidden stack for WiFi messages that is about 4 deep, and provided that the ESP8266 is allowed to check the WiFi regularly, most timing issues seem to be dealt with inside the ESP8266.

There are a couple of possible ways to solve this, an this is why I am starting this new topic...

One: +Liviu, is it possible to speed up the RFID code reading? or allow the ESP8266 to update its WiFi reception? The "mfrc522.PICC_IsNewCardPresent()" command seems to take about 25ms or so to complete, and I cannot see a way to speed it up.. Is this due to the SPI, or are there delays other than my test 5ms delays? hidden in the MFRC code?? I have an ESP8266 set up as a "Monitor" but without the RFID, and it never seems too lose any messages....
Code: Select all
void checkRFID(){
  if(bReaderActive){
          if ( mfrc522.PICC_IsNewCardPresent()&& mfrc522.PICC_ReadCardSerial()){
               delay(5); //test delay allow wifi background
               if(!compareUid( mfrc522.uid.uidByte, oldUid, mfrc522.uid.size)){// NEW  (NOT same UID)
                          copyUid(mfrc522.uid.uidByte, oldUid, mfrc522.uid.size); //save in oldUID
                          SendUID(mfrc522.uid.uidByte,1,1);
                          uiStartTime = millis();
                          bRFIDactive=true;
                          bRFIDquiet=false;
                                                                              }//  end NOT same UID same
                                      else{uiStartTime = millis();  //last time card was seen
                                          }                                              //same card..
                   } else { //  nothing read..
                            delay(5);       //test delay     allow wifi background                                        //
                               //if(compareUid( mfrc522.uid.uidByte, oldUid, mfrc522.uid.size)){//same UID 
                             
                             if((!bRFIDquiet)& (millis()>= uiStartTime + uiDelayTime)){ // wait delay before declaring the card gone
                                 SendUID(oldUid,0,3);
                                 bRFIDquiet=true; } //if((uiActTime
                                                                                             }// nothing read 
                                 
                                                                                }//ifmrfc
}


Second, and this is a question for Rob V... Would it be possible to add an option in the LocoNet Controller options so that the "Set Slot" commands (A0,A1,A2) can be replaced by (say) a Single 6 byte "A3" command setting Slot, speed, snd and dirf, but which expects a Long_ACK (sending the A3 command and perhaps "Slot" as ACK1 ) back from the command stations? And then make Rocrail resend the message if the Long_Ack was not seen within a set time limit such as 100ms?.

The third and cruder approach would be to have a dedicated "ESP8266" Static Control station that reads all the slot commands but then sends them out via the A3 message as described above to the Mobile Esp8266's. ?

Many thanks, I hope this ESP8266 code will be of interest to others!...

The raw code is here: https://github.com/dagnall53/WiFiMobile
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Liviu M » 08.08.2016, 09:15

Hi Dagnall,

if you have a free pin on the ESP8266, you can take a look to my interrupt example on the Miguel Balboa's github repo.
I haven't tested it with the ESP8266, but I suppose it can be made to work and I hope it will be faster.

Regards,
Liviu
----------------------------------------------
System: DCC N-Scale
CS: Profiboss (Loconet)
Server: RaspberryPi/OdroidC1/OpenSuse
Client: OpenSuse/Windows(?)
----------------------------------------------
Liviu M
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 08.08.2016, 12:50

With regards to interrupts, I don't think that will help, as I see the Tags as soon as I need to.--- Its just that the code that does the ""mfrc522.PICC_IsNewCardPresent()" seems to take a lot longer than I expected, and does not allow the ESP to do its "background" WiFi things...Also I am running out of pins :?

However, this morning I discovered the "Poll Info" button in the Loco interface. and It looks like that does what I need,... it seems to set Rocrail to repeatedly sending A0-A2 to the loco. So any lost packets are resent.
Its going to make the message space more crowded, but I think this should be a long term solution to the "UDP lost messages" issue.
-----I am not sure if or where there is a variable to change the poll frequency, but perhaps Rob V will point out where it is!

I have also discovered the DC servo motor controller has significantly different characteristics depending on the motor direction. (at least with the Small "Arduinio TT gearmotors" I use on my Tram.
I am going to modify my code to use some CV's to try and linearise this.

Happy coding
:coding:
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Liviu M » 08.08.2016, 13:40

Hi Dagnall,
Dagnall wrote:Its just that the code that does the ""mfrc522.PICC_IsNewCardPresent()" seems to take a lot longer than I expected

The interrupt approach has (unfortunately) nothing to do with the card detection - to detect a card in the reader's range, a command from the ESP is needed.
What I've tried to do by using the interrupts, was the minimization of the overhead in the IsNewCardPresent() function.
I haven't done any measurement, by I'm expecting a faster behavior using the interrupt approach.

But as long as you don't have any free pins...

Cheers,
Liviu
Last edited by Liviu M on 13.08.2016, 09:34, edited 1 time in total.
----------------------------------------------
System: DCC N-Scale
CS: Profiboss (Loconet)
Server: RaspberryPi/OdroidC1/OpenSuse
Client: OpenSuse/Windows(?)
----------------------------------------------
Liviu M
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 08.08.2016, 16:09

Very interesting,, I might "sacrifice a pin" to try this out..
But to show the general idea, I put the electronics in an early version of my 3D printed tram, and set it running as a simple reversing track; With "poll info" set in the locomotive settings, the repeated sending of the A0-A2 commands means that even if one message is lost, others get through. Stopping on the track can be a bit variable, but the tram is now definitely under control..
https://youtu.be/a_Ar0VceEMo.

The reader was only "taped" to one end of the tram, so is not optimised for height, but I found that if I went too fast, it could miss the tags.
However I feel that the concept is at least proved!.

I think that some form of feedback is really needed for the speed, as it seems to vary quite a lot.. Mostly "20" was a nice slow but reasonable speed, but sometimes it would not start until I send "30"... Other times it will start nicely at "10"....
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 13.09.2016, 19:37

Quick update, with a 8MHz SPI clock speed, the RFID read takes about 16ms, including a 2ms delay I have in the main loop. (Measured by Oscilloscope!)
Even with this improvement, I still seem to get data dropouts due to the UDP nature of the WiFi interface. My shuttle will run OK for quite a while, and then miss a crucial command like a speed change from 0 to vcruise, and the loco will stay in the station... Grrr! Running slowly into the buffers because it misses a "Speed = 0" is less troublesome, but still annoying.

The trouble mostly seems to happen when Rocrail sends a set of 6 command bytes , three for Speed=0 then three more for speed at the new demanded rate.. The NodeMCU only seems to have a 3 or 4 deep message buffer....so I think most of the commands I miss are in these fast 6 message bursts.

Despite that it works long enough for videos! Not bad for a total electronics cost of circa £20... but just not reliable enough to be "Really Useful Engine"...
https://youtu.be/st7UfQos2Oc

I would very much like to get Rob to add some sort of selectable periodic repeat transmission of the Loco commands into the Rocrail UDP interface, or hold off transmission of the second set of loco commands for just a very short period, say 5ms? ... but I am not sure where to put this question as a topic, DIY Hardware or Command Stations?
This data loss must be fundamental issue for anyone using LocoNet over UDP !, the Loconet protocol requires monitoring of the state of the transmission medium, but this is not possible in WiFi implementation. I wait for no transmissions before sending anything on the bus, but this method cannot absolutely prevent collisions..

Help Please Rob? Where should I set up a new topic...
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby rjversluis » 14.09.2016, 06:24

If you encounter dataloss you must have a network problem. (Bad WiFi connection...)
UDP in the same ethernet segment are save.

There is no known dataloss with the GCA101.

There is already a gap of 10ms between LocoNet commands.
Best Regards, Rob.
:!: PS: Do not forget to attach the usual files.
:!: PS: Nicht vergessen die übliche Dateien an zu hängen.
[ macOS - Linux] - [ N: CBus - CAN-GCA ] - [ 0: RocNetNode - GCA-Pi ]
rjversluis
Site Admin
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 14.09.2016, 10:51

Rob, thanks,
Most network specialists accept that UDP means there is the potential for data loss and therefore it is only used for repetitive communications where some loss can be accepted, such as audio or video, but I am sure you knew this. In a quiet Wifi environment, very low loss rates can be expected, and where only a single station is being communicated with , I would agree that only few packets should be lost. and the broadcast nature of UDP makes it ideal for a network such as we need for the railways. I have thought of implementing a mesh topology, but I would prefer to try and sort out the remaining issues with this Loconet WiFi experiment.

However, you say
There is already a gap of 10ms between LocoNet commands.

I have a WiFi monitor that is running with a cycle time of well less than 1ms, which identifies UDP packets and repeats them onto a serial bus (very fast) for monitor and test purposes. It has not missed a message that I can tell so far in testing..
Every message it sees is timestamped (us from board turn on) and repeated onto the serial monitor so I can see what is happening. This monitor shows me that I have messages with about 1-2ms between them, not the 10ms that you say should be the minimum. The longer timings (not shown) between block messages (not shown on this captured example) indicate that there is no x10 error in the microseconds timing..
Code: Select all
3791311066 us. From:192.168.0.11 Full Message is: A0 03 00 5C
3791312798 us. From:192.168.0.11 Full Message is: A1 03 31 6C
3791314277 us. From:192.168.0.11 Full Message is: A2 03 00 5E
3791316526 us. From:192.168.0.11 Full Message is: A0 03 20 7C
3791322044 us. From:192.168.0.11 Full Message is: A1 03 31 6C
3791327346 us. From:192.168.0.11 Full Message is: A2 03 00 5E

This was some code I captured whilst my loco was doing a shuttle. if you subtract the raw us time form each previous message time, you will see that the inter message gaps are
Code: Select all
1732
1479
2249
5518
5302

all in microseconds, so the minimum gap is ~1.5ms not 10ms..... My module seems to have problems mostly when this sequence of 6 loco commands are sent, caused, i am assuming, by a mix between the smallish message buffer size in the ESP8266 and the fast sending of the messages.

Was the 10ms delay a new addition? I am using revision 11206... or is it possible to switch it off? How did you test it?

Thanks for your patience!.
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby rjversluis » 14.09.2016, 11:11

Was the 10ms delay a new addition? I am using revision 11206...

no, thats more or less standard.
Best Regards, Rob.
:!: PS: Do not forget to attach the usual files.
:!: PS: Nicht vergessen die übliche Dateien an zu hängen.
[ macOS - Linux] - [ N: CBus - CAN-GCA ] - [ 0: RocNetNode - GCA-Pi ]
rjversluis
Site Admin
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Liviu M » 14.09.2016, 11:13

Hi Rob,
Because of missing hardware handshake, to avoid packages loss, for the ULNI CS you have implemented the echoing of the messages.
Maybe you can implement such a echoing procedure for the UDP CS also?
Just an idea.

Regards,
Liviu
----------------------------------------------
System: DCC N-Scale
CS: Profiboss (Loconet)
Server: RaspberryPi/OdroidC1/OpenSuse
Client: OpenSuse/Windows(?)
----------------------------------------------
Liviu M
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Liviu M » 14.09.2016, 11:16

Hi Rob,

Me again.
Forget about my idea, it is a completely different situation.

regards,
Liviu
----------------------------------------------
System: DCC N-Scale
CS: Profiboss (Loconet)
Server: RaspberryPi/OdroidC1/OpenSuse
Client: OpenSuse/Windows(?)
----------------------------------------------
Liviu M
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 14.09.2016, 12:37

Hi Rob, Liviu...
Regarding 10ms delay, is there a way to extend this, so I can see if I can see any effects on my setup? -- Or does the A0,A1,A2 message count as a single "packet"?? This would at least explain why I see the set of three packets so fast,.

Also, I have been exploring other protocols to see if they are perhaps better optimised for this experiment.. If I changed to RocNet, is there a way of demanding a response from the mobile decoder? - At least RocNet sends the direction, speed and functions as a single packet!!. Or is RocNet intended only for Rasberry PI..?

PS Rob, I got distracted and found your user page and saw your layout.. Most impressive..

Dagnall
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby rjversluis » 14.09.2016, 13:47

Hi Dagnall,
Dagnall wrote:Or does the A0,A1,A2 message count as a single "packet"??

those packets are send, under certain conditions, without a pause in between.
I could add a little pause between those commands.
Best Regards, Rob.
:!: PS: Do not forget to attach the usual files.
:!: PS: Nicht vergessen die übliche Dateien an zu hängen.
[ macOS - Linux] - [ N: CBus - CAN-GCA ] - [ 0: RocNetNode - GCA-Pi ]
rjversluis
Site Admin
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 14.09.2016, 15:18

Adding a small pause might be very helpful, but a longer pause between sending each set of three might be more helpful..
But before you do that (which might cause others problems), I have been looking closer at RocNet, and am wondering if I should transfer the project over to that protocol..
I know this is the "wrong" place to ask :oops: .. but is there any documentation for RocNet other than in http://wiki.rocrail.net/doku.php?id=rocnet:rocnet-prot-en ?
I am especially looking for some examples of send and receive messages.. I already have an initial version of RocNet working on my ESP8266 and receiving a mobile decode actions, and the protocol looks quite elegant.

All the best
Dagnall
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Re: ESP8266 Wifi control using LocoNet UDP

Postby Dagnall » 28.09.2016, 15:22

Rob, I am going to continue to try to make the RocNet modules work reliably, so can you "close" this topic? :eot:
ESP8266 Based RocNet Universal 8 i/o decoder and Mobile Decoder. http://wiki.rocrail.net/doku.php?id=users:dagnall53:description
G Scale 3D printed Locos and wagons : (on Thingyverse)
Dagnall
 

Next

Return to Loconet