Go Down

Topic: Where do I post code for help with a large (29K) NTP/UDP and TCP/ip sketch (Read 2 times) previous topic - next topic

solar_eta

I don't have a problem with the idea of sockets being reused provided that they are allocated and released properly and several sockets can be allocated to multiple overlapping activities. There must however be something wrong with the way this is done in the Ethernet library because in my case the UDP code is failing to return anything on the second and subsequent times of asking after there has been a web page request from the browser. The sequence goes.

1  Startup
2  Get NTP time stamp

Then in the loop
A   Do control stuff
B   Record Data
C   Listen for Web browser request

If there is a request
     a)  Serve Page  automatic Refresh every 15 seconds
     b)  React to any POST commands

D   Once a day Get NTP time stamp (This is the bit that fails the second time it is supposed to get the time)
E   Go round again

Activity D always works the first time in the loop even if it has been preceded by a browser request but not the second time round. If there is no browser request as the startup/ first turn of the loop then there is a 50/50 chance that it will return the time a few more times before it fails.

As for my 'nearly always' comment if left in the failed state it continues to fail, but once in a hundred or so times it will actually return the right time!!! I've speeded the requests up to once every 2 minutes recording the outcome with the data to the SD card. There is no pattern that I can ascertain to when it might throw up the right time.

If it did not work at all, or only worked on start up but not in the loop I would know there was a problem with my code, but its the works some of the time condition that is flumoxeing.




SurferTim

I just started my NTP/server sketch again. It has updated four times (every 10 minutes) and the times returned are correct, even after several server requests.

I did notice one thing about UDP. If you have a SD card in the slot, and do not disable or initialize it, the NTP time will be incorrect. I have a 2GB SanDisk in the slot for my test, but I disable it in setup.

I would post my sketch, but it is a bit difficult to understand. I have changed it since I wrote it to automatically adjust the millis time to get the millis "clock" adjusted to stay within a second of the NTP time over a few hours.

solar_eta

I've found a curious thing with my sketch and the Ethernet Library. I now believe that your original thought that I was running out of Sockets is right but not in the normal overload way. Yes I did have the last CRLF left in the buffer as well but once I cured that problem I still had a problem getting UDP time stamp updates.

As originally coded I put a Udp.begin(localPort); at the beginning of the code to get an NTP time stamp and a UDP.stop(); at the end of that segment of the code. As I was only looking to get a time stamp once a day it seemed reasonable to free up the socket that UDP was using for the rest of the time.

Now by doctoring the Server.available() and the UDP begin() to print out the socket number to the serial monitor showed that in on start up Server.available() claimed socket 0 and UDP claimed socket 1. However once running in the loop as soon as there was a browser request Server.available() hogged both sockets 0 &1, I suspect the reason for two sockets is that the browser starts asking for the Style.css file before its finished receiving the web page. If the sketch called for an updated time stamp it might get socket 0 or 1and it seems to be pure chance as to which it gets If it got socket 1 ie its original socket, the new time was returned any other socket and nothing comes back. I even tried loading the system with several browsers open on various PCs on the network. All 4 sockets were in use but UDP code was only interested in socket 1.

Going a stage further I deleted the Udp.stop() line and moved the Udp.begin() into the setup part of the code. Now on start up Server.available gets socket 0 UDP gets socket 1 and keeps it! Make a browser request for a web page and server.available uses sockets 0 and 2. With this set up the time stamps keep on coming with out any hickcups. So good coding practice of releasing resources when you don't need them creates a problem.

It is my belief that there is either something wrong with the way the server releases sockets or there is something wrong with the way UDP aquires a socket. Certainly there is a an incompatibility between the two.

The code has run for 4 days now with out a problem albeit that the UDP code is hogging one of the sockets, however I think this incompatibility between Server.available() and UDP.begin() should be treated as a bug that needs fixing.

SurferTim

Quote
The code has run for 4 days now with out a problem albeit that the UDP code is hogging one of the sockets, however I think this incompatibility between Server.available() and UDP.begin() should be treated as a bug that needs fixing.

OK, what is your suggestion? Would you prefer the UDP function to fail if all sockets are busy with the server?

solar_eta

If all the sockets are in use then what ever is calling for another socket, be it Server or UDP should fail to get one. What I've been showing is that once you top and bottom your code that sends and receives a NTP request with a Udp.begin()/Udp.stop(),  the UDP fails even when there are sockets available and it is allocated one.

I haven't dug deep into the library code yet but I suspect that when a socket is allocated to UDP and that was or is still being used by the server it is not recognised as still being in use.

SurferTim

Quote
If all the sockets are in use then what ever is calling for another socket, be it Server or UDP should fail to get one.

I vote "no" on that. If you need more sockets, may I suggest the w5200.

edit: As a matter of fact, I was already trying to devise a way to reserve a socket for client communication while running a server.

edit2: After a little thought, it would be fine with me if
socket 0 was reserved for client tcp stuff
socket 1 was reserved for UDP stuff
socket 2 and up for the server. 2 if w5100 or 6 if w5200

Go Up