Time for additional Ethernet add-on with PWM output

Expansion modules and attachments
Post Reply
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Is is possible to use UdpNtpClient at the same time as the web server? I've been debating with myself the best and easiest way for my ethernet board to know the "time."
I can do:

Code: Select all

/*

 Udp NTP Client
 
 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket 
 For more on NTP time servers and the messages needed to communicate with them, 
 see http://en.wikipedia.org/wiki/Network_Time_Protocol
 
 Warning: NTP Servers are subject to temporary failure or IP address change.
 Plese check 

    http://tf.nist.gov/tf-cgi/servers.cgi

 if the time server used in the example didn't work.

 created 4 Sep 2010 
 by Michael Margolis
 modified 9 Apr 2012
 by Tom Igoe
 
 This code is in the public domain.

 */

#include <SPI.h>         
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

unsigned int localPort = 8888;      // local port to listen for UDP packets

IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() 
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // 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);
}

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server

    // wait to see if a reply is available
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);               

    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;     
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;  
    // print Unix time:
    Serial.println(epoch);                               


    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');  
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':'); 
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch %60); // print the second
  }
  // wait ten seconds before asking for the time again
  delay(10000); 
}

// send an NTP request to the time server at the given address 
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE); 
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49; 
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:         
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket(); 
}
Or would you suggest that I tap into the RTC of the RA?
I'm not sure what code I could use to do this once I got the RTC hooked up to whatever input PIN.


Overall, the goal of this is to have a combination of the PWM expansion offered by RA with the ethernet option for people who don't need/want the wifi. I could gain an additional output for four PWM(max) or just two(for me) to control my LED drivers based on "time."

Thanks!! Thoughts?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

I should mention. The stepping of 0-255 is the easy part and I've got that down. Just need to figure out syncing time so my LEDs don't ramp all weird and don't match up with the RA cycle.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Time for additional Ethernet add-on with PWM output

Post by lnevo »

How does the head unit communicate with the pwm module? I would simulate that instead...if possible
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Right now it just reads the serial output to converts that output to a string pointed at the RA server. I'm not sure IF the "time" is also output through the serial pins or if that's possible to make it do that.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

I miss read your posting. Sorry. That is a good question. I'm assuming the HUB allows for RTC sync. I'd be using either serial output or any theory to make it work.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Time for additional Ethernet add-on with PWM output

Post by lnevo »

Why not just send the serial commands todo what you want and let ra head figure out the schedule?
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Can the RA do this I wonder? There would need to be some modification no? In this way...the RA would have to issue the commands to tell the ethernet arduino what to do for stepping 0-255?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

I think you could add the code you posted just fine, but you don't want to keep querying the ntp all the time...
Just once is sufficient.
Once you get the unix time, you should sync it every day or so, just to make sure the clocks match.
The other alternative is to connect SDA and SCL to the expansion bus. This way you can talk directly to the RA RTC.
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

My main concern is that the Webserver and the ntp might get a little heavy on the RAM? I need just a simple way to get the time off the RA I suppose? I'm noticing with my testing that if I do the LED fade in my existing webserver loop that everything past the fade functions is getting NULL'ed
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Time for additional Ethernet add-on with PWM output

Post by binder »

DrewPalmer04 wrote:My main concern is that the Webserver and the ntp might get a little heavy on the RAM? I need just a simple way to get the time off the RA I suppose? I'm noticing with my testing that if I do the LED fade in my existing webserver loop that everything past the fade functions is getting NULL'ed
To pull the time off the controller, just use the /d command.

Code: Select all

http://IP:PORT/d
That sends out the date and time from the controller but does not send the seconds. Of course you would have to process the XML code that is returned. Just offering some input.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Thanks!!! It's a start. Of course I just finish one thing and move onto another. Roberto is going to ban me lol
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Time for additional Ethernet add-on with PWM output

Post by binder »

DrewPalmer04 wrote:Thanks!!! It's a start. Of course I just finish one thing and move onto another. Roberto is going to ban me lol
Welcome. I doubt he will....he hasn't banned me yet. :)
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Oh learned something. Delay(); terminates loop function. I think my headache will be getting it to output a PWM slope while checking the RA time :S. all within the webserver's if / else statements. Oh boy I'll need help :(
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Roberto. The easiest way to connect into the RTC on the head unit is? I was going to set up my own RTC on the Ethernet module but it seems silly to do it this way when I could just read the RTC off the RA and save some kb on the Ethernet module
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

Curt already gave you the answer... :)
GET /d
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

That does work but it is a string. I noticed in the RA headers that the RTC uses the wire commands to comm to the RTC and get data. ??
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

Yeah, just convert string to something you can use.
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

No better way with wire.h?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Sorry. I knew you had told me that. I wasn't sure about the implementation. My RF expansion takes up my only bus slot. I should splice this or I must have the expansion bus module?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

Try splicing... if you start getting hiccups, you may want to think about hub
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Any way to splice into the head unit versus in the relay box? the jumper maybe?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

yeap... jumpers
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Thanks for the help. Next ? Any way to read the internal memory with my Ethernet module? So I can use the RA PWM parabola functions?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

GET /mbxxx
Or you talk to jsclownfish...
He needs the same thing for his remote screen
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Here's the plan: use RTC to get the time on the Ethernet board. Then use GET/mbxxx to get the internal memory location values. Then define the existing RA code for parabola onTime OffTime= to output the parabola. What are the locations for the start end etc for the actinic value?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

Look at the Globals.h
Roberto.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

I mean the internal memory locations for me to point GET/mbxxx from what are all the values?
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

That takes me here lol
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
Post Reply