Go Down

Topic: Send time from OS X terminal to Arduino via serial (Read 237 times) previous topic - next topic

mattybigback

Hi there

I've built a basic DS1307-based clock using the DS1307 library, and I want to add the capability to send the Unix timestamp over serial.

I know you can use "date +%s" to print the Unix timestamp within the terminal, and I can save that output to a txt file using date +%s > out.txt. Is there a way I can pipe it to the serial port? I've tried the following, but it doesn't work, and I have to exit using ctrl+c. Ideally I'd like to terminate the string with a newline, as that's how my code is set up at the moment. It just means it's easier to tell the end of the incoming message.

date +%s > /dev/tty.usbserial

Here's the code if anyone is interested. It works  when receiving numbers over serial, so at least that's working.

Any help would be greatly appreciated!

#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <Adafruit_CharacterOLED.h>

// initialize the library with the numbers of the interface pins
Adafruit_CharacterOLED lcd(6, 7, 8, 9, 10, 11, 12);

char *separator[2] = {" ", ":"};
const char *monthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
byte tickToggle;
byte separatorSwitcher;

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() {
 
    while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    unsigned long timeFromComputer = Serial.parseInt();


    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      if  (RTC.set(timeFromComputer) == true)
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Sync Successful!");
        delay(1500);
        lcd.clear();
    }
  }
 
 
  tmElements_t tm;
  if (RTC.read(tm)) {
   

    if (tickToggle != tm.Second) {
      if (separatorSwitcher < 1){
        separatorSwitcher++;
      } else {
        separatorSwitcher = 0;
      }
    }
     
   
    lcd.setCursor(0, 0);
    lcd.print(tm.Day);
    lcd.print(' ');
    lcd.print(monthNames[tm.Month - 1]);
    lcd.print(' ');
    lcd.print(tmYearToCalendar(tm.Year));
   
    lcd.setCursor(0, 1);
   
    print2digits(tm.Hour);
    lcd.print(separator[separatorSwitcher]);

    print2digits(tm.Minute);
    lcd.print(separator[separatorSwitcher]);

    print2digits(tm.Second);
    tickToggle = tm.Second;

   
  } else {
    if (RTC.chipPresent()) {
      lcd.setCursor(0, 0);
      lcd.print("No Time Set!");
      lcd.setCursor(0, 1);
      lcd.print("Upload SetTime");
    } else {
      lcd.setCursor(0, 0);
      lcd.print("Circuit Error");
    }
    delay(9000);
   
  }
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.print("0");
  }
  lcd.print(number);
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy