He's using the arduino as a UDP client, talking to a remote server, my attempt was to use the (in my case) Teensy 3.0 and CC3000 as a UDP server, responding to requests from one or more clients.
When I first got the CC3000, your library was strictly client based, with TCP server code added later. I haven't checked back lately, but there was no UDP server code in the library when I started this thread.
CC3000 UDP Server
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: CC3000 UDP Server
Fair enough. But UDP client stuff is easy. Even some of the example sketches use UDP.
- fmbfla
- Posts: 110
- Joined: Fri Jun 08, 2012 6:48 pm
Re: CC3000 UDP Server
I am no programmer by any means how ever I can say for sure that the CC3000 BoB stops werking after a bit and needs to RE ACQUIRE? to the router. This can be achieved in the sketch by making a running sample and after a certain "Sample++ " or "TIME" has been reached you can disconnect and reconnect to the DHCP server of the router. I have tried this method and have only been able to achieve 70 posts in a row (@ 1 per 3kmillis)before the button has to be pressed to reset the MPU. I have Tried with the UNO and the MEGA, How ever, I am not giving in to defeat, and from time to time I screw with the code and library's to see if I can "stumble upon a fix". (and it is sloooooooooow to TX the DATA)
I Have no education in this field (or any other)and have taught my self quite a bit through using this forum.
Someone will be able to help you given some time an patients.
Dementia is a triad of problems: Wanna go Stone Crabbing?.
I Have no education in this field (or any other)and have taught my self quite a bit through using this forum.
Someone will be able to help you given some time an patients.
Dementia is a triad of problems: Wanna go Stone Crabbing?.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: CC3000 UDP Server
Sounds like maybe the DHCP lease is expiring on you. That is a router setting - see if you can set the DHCP lease time to 'infinite'fmbfla wrote:I am no programmer by any means how ever I can say for sure that the CC3000 BoB stops werking after a bit and needs to RE ACQUIRE? to the router.
- sh00ter
- Posts: 2
- Joined: Tue Jan 14, 2014 2:48 pm
Re: CC3000 UDP Server
I tackled this (FWIW) by making the UDP client on CC3000 tcp-esque, meaning not have it listen outright (like a true server) - rather having it send out a UDP "heartbeat" that clients can then reply to with information / commands, etc ( Similar to the wifi relay idea). So it's kind of like a syn, synack, ack type of relationship. The client need only listen for a special heart beat message and reply to it with a UDP message (the payloads are customized). I use python (for portability, ease of coding) on the client side (scapy or straight python) for the packet crafting. The CC3000 only need know the host names, or IP addresses of pontential clients (in order to send them the heartbeat). Hope that makes sense.
I imagine this works because the source port generated by the CC3000 when sending a UDP packet stays open waiting for replies for a period of time, this seemed simpler than trying to invent or modify a true UDP server port, which I've read / heard people having some difficulty.
I've had great luck with this. I also periodically refresh the DHCP lease ( I do this in an interval less than my DHCP server's default lease time ) - and have code to automatically reconnect to the AP. I have it running 24/7 (test bench right now), and it works perfectly.
Side note: I did have to reflash the CC3000 firmware after my very first connection out of the box, as it wouldn't refresh the DNS server from DHCP when connecting to different APs, no matter what I tried. After the reflash I've had no issues.
I imagine this works because the source port generated by the CC3000 when sending a UDP packet stays open waiting for replies for a period of time, this seemed simpler than trying to invent or modify a true UDP server port, which I've read / heard people having some difficulty.
I've had great luck with this. I also periodically refresh the DHCP lease ( I do this in an interval less than my DHCP server's default lease time ) - and have code to automatically reconnect to the AP. I have it running 24/7 (test bench right now), and it works perfectly.
Side note: I did have to reflash the CC3000 firmware after my very first connection out of the box, as it wouldn't refresh the DNS server from DHCP when connecting to different APs, no matter what I tried. After the reflash I've had no issues.
- fmbfla
- Posts: 110
- Joined: Fri Jun 08, 2012 6:48 pm
Re: CC3000 UDP Server
while posting to Wunderground, Using "GET" I would like it to connect via UDP to the NTP server during the loop (after 10).
Using the MEGA, the UNO wont have the memory.
I have tried using this code with my CC3K and (#include <EthernetUdp.h>) to Wunderground but it STOPS>>>>>>>at the moment I call for the UDP.
Am I using the wrong LIB?
Using the MEGA, the UNO wont have the memory.
I have tried using this code with my CC3K and (#include <EthernetUdp.h>) to Wunderground but it STOPS>>>>>>>at the moment I call for the UDP.
Am I using the wrong LIB?
Code: Select all
/*
* NAME: NTP2RTC
* DATE: 2012-02-19
* URL: http://playground.arduino.cc/Main/DS1307OfTheLogshieldByMeansOfNTP
*
* PURPOSE:
* Get the time from a Network Time Protocol (NTP) time server
* and store it to the RTC of the adafruit logshield
*
* NTP is described in:
* http://www.ietf.org/rfc/rfc958.txt (obsolete)
* http://www.ietf.org/rfc/rfc5905.txt
*
* based upon Udp NTP Client, by Michael Margolis, mod by Tom Igoe
* uses the RTClib from adafruit (based upon Jeelabs)
* Thanx!
* mod by Rob Tillaart, 10-10-2010
*
* This code is in the public domain.
*
*/
// libraries for ethershield
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> // New from IDE 1.0
// libraries for realtime clock
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888; // local port to listen for UDP packets
// find your local ntp server http://www.pool.ntp.org/zone/europe or
// http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers
// byte timeServer[] = {192, 43, 244, 18}; // time.nist.gov NTP server
//byte timeServer[] = {132, 163, 4, 101}; // ntp1.nl.net NTP server
// byte timeServer[] = {132, 163, 4, 102}; // time-b.timefreq.bldrdoc.gov NTP server
byte timeServer[] = {132, 163, 4, 103};
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte pb[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp; // New from IDE 1.0
///////////////////////////////////////////
//
// SETUP
//
void setup()
{
Serial.begin(115200);
Serial.println("NTP2RTC 0.5");
// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
Udp.begin(localPort);
int packetSize = Udp.parsePacket(); //<<<<<<<<<<<<NEW LINE HERE
Serial.println("network ...");
// init RTC
Wire.begin();
RTC.begin();
Serial.println("rtc ...");
Serial.println();
}
///////////////////////////////////////////
//
// LOOP
//
void loop()
{
Serial.print("RTC before: ");
PrintDateTime(RTC.now());
Serial.println();
int packetSize = Udp.parsePacket(); //<<<<<<<<<<<<NEW LINE HERE
// send an NTP packet to a time server
sendNTPpacket(timeServer);
// wait to see if a reply is available
delay(1000);
if ( Udp.available() ) {
// read the packet into the buffer
Udp.read(pb, packetSize); // New from IDE 1.0 <<<<<<<<<<<<<MODIFIED LINE
// NTP contains four timestamps with an integer part and a fraction part
// we only use the integer part here
unsigned long t1, t2, t3, t4;
t1 = t2 = t3 = t4 = 0;
for (int i=0; i< 4; i++)
{
t1 = t1 << 8 | pb[16+i];
t2 = t2 << 8 | pb[24+i];
t3 = t3 << 8 | pb[32+i];
t4 = t4 << 8 | pb[40+i];
}
// part of the fractional part
// could be 4 bytes but this is more precise than the 1307 RTC
// which has a precision of ONE second
// in fact one byte is sufficient for 1307
float f1,f2,f3,f4;
f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0;
f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0;
f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0;
f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0;
// NOTE:
// one could use the fractional part to set the RTC more precise
// 1) at the right (calculated) moment to the NEXT second!
// t4++;
// delay(1000 - f4*1000);
// RTC.adjust(DateTime(t4));
// keep in mind that the time in the packet was the time at
// the NTP server at sending time so one should take into account
// the network latency (try ping!) and the processing of the data
// ==> delay (850 - f4*1000);
// 2) simply use it to round up the second
// f > 0.5 => add 1 to the second before adjusting the RTC
// (or lower threshold eg 0.4 if one keeps network latency etc in mind)
// 3) a SW RTC might be more precise, => ardomic clock :)
// convert NTP to UNIX time, differs seventy years = 2208988800 seconds
// NTP starts Jan 1, 1900
// Unix time starts on Jan 1 1970.
const unsigned long seventyYears = 2208988800UL;
t1 -= seventyYears;
t2 -= seventyYears;
t3 -= seventyYears;
t4 -= seventyYears;
/*
Serial.println("T1 .. T4 && fractional parts");
PrintDateTime(DateTime(t1)); Serial.println(f1,4);
PrintDateTime(DateTime(t2)); Serial.println(f2,4);
PrintDateTime(DateTime(t3)); Serial.println(f3,4);
*/
PrintDateTime(DateTime(t4)); Serial.println(f4,4);
Serial.println();
// Adjust timezone and DST... in my case substract 4 hours for Chile Time
// or work in UTC?
t4 -= (5 * 3600L); // Notice the L for long calculations!!
t4 += 1; // adjust the delay(1000) at begin of loop!
if (f4 > 0.4) t4++; // adjust fractional part, see above
RTC.adjust(DateTime(t4));
Serial.print("RTC after : ");
PrintDateTime(RTC.now());
Serial.println();
Serial.println("done ...");
// endless loop
while(1);
}
else
{
Serial.println("No UDP available ...");
}
// wait 1 minute before asking for the time again
// you don't want to annoy NTP server admin's
delay(30000L);
}
///////////////////////////////////////////
//
// MISC
//
void PrintDateTime(DateTime t)
{
char datestr[24];
sprintf(datestr, "%04d-%02d-%02d %02d:%02d:%02d ", t.year(), t.month(), t.day(), t.hour(), t.minute(), t.second());
Serial.print(datestr);
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(byte *address)
{
// set all bytes in the buffer to 0
memset(pb, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
pb[0] = 0b11100011; // LI, Version, Mode
pb[1] = 0; // Stratum, or type of clock
pb[2] = 6; // Polling Interval
pb[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
pb[12] = 49;
pb[13] = 0x4E;
pb[14] = 49;
pb[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
#if ARDUINO >= 100
// IDE 1.0 compatible:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(pb,NTP_PACKET_SIZE);
Udp.endPacket();
#else
Udp.sendPacket( pb,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
#endif
}
///////////////////////////////////////////
//
// End of program
//
- sjfreeland
- Posts: 29
- Joined: Sat Dec 21, 2013 2:18 pm
Re: CC3000 UDP Server
My adaptation will run correctly for hours, then hangs. I feel there is some minor loose end that when fixed will make the system robust. It boils down to sending a short (40) character string to a UDP client 20 times per second. Here is the code (I have added diagnostic print statements to determine where the hang occurs):
and in the Server class code:
My diagnostic printout at the hang is "...EacfFQGEac" which tells me the hang is in the IF statement:
Can you shed any light on this? Can you suggest a path that will shed light?
Code: Select all
DiagSerial.print("E");
if (UDPServer[i].connected())
{
DiagSerial.print("F");
UDPServer[i].write(str, strlen(str), 0);
}
DiagSerial.print("Q");
Code: Select all
bool Adafruit_CC3000_Server::connected(void) {
Serial3.print("a");
if (_socket < 0) {
// if (CC3KPrinter != 0) CC3KPrinter->println("connected() - _socket < 0");
Serial3.print("b");
return false;
}
Serial3.print("c");
if (! available() && closed_sockets[_socket] == true) {
// if (CC3KPrinter != 0) CC3KPrinter->println("No more data, and closed!");
Serial3.print("d");
closesocket(_socket);
closed_sockets[_socket] = false;
_socket = -1;
Serial3.print("e");
return false;
}
Serial3.print("f");
return true;
}
Code: Select all
Serial3.print("c");
if (! available() && closed_sockets[_socket] == true) {
Last edited by adafruit_support_rick on Fri Apr 04, 2014 6:39 pm, edited 1 time in total.
Reason: please use code tags when posting code
Reason: please use code tags when posting code
Please be positive and constructive with your questions and comments.