Set current time on Pi using Hologram

I’m looking at the Hologram commands and do not see a simple way to get the current time. This would be nice if my pi reboots.

My Pi does not have a Real Time Clock so it would be nice to pull the current time from my cellular connection. I am using Hologram Nova and have the SDK installed. I can successfully send messages to my dashboard and route them.

Edit:

I used dmesg and it appears that I can see the tty which the modem is being assigned to:

[ 9.455987] usb 1-1.2: New USB device found, idVendor=1546, idProduct=1102
[ 9.455995] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 9.456000] usb 1-1.2: Product: u-blox Wireless Module
[ 9.456003] usb 1-1.2: Manufacturer: u-blox
[ 9.456007] usb 1-1.2: SerialNumber: 358887093407490
[ 9.501464] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device
[ 9.504178] cdc_acm 1-1.2:1.2: ttyACM1: USB ACM device
[ 9.506608] cdc_acm 1-1.2:1.4: ttyACM2: USB ACM device
[ 9.508719] cdc_acm 1-1.2:1.6: ttyACM3: USB ACM device
[ 9.510830] cdc_acm 1-1.2:1.8: ttyACM4: USB ACM device
[ 9.514155] cdc_acm 1-1.2:1.10: ttyACM5: USB ACM device
[ 9.516553] cdc_acm 1-1.2:1.12: ttyACM6: USB ACM device

When I cat’ed it the first time it seemed like ttyACM0 had some timestamps in it but ever since I did that I am not getting anything out of the tty’s. Still learning about Linux…

Any suggestions would be appreciated!

Figured it out, I needed to use a serial console application to query the tty correctly. Pyserial allows this to be automated.

This Python 2.7 code attempts to read the time from the modem at 2/10 of a second intervals for 20 seconds. If unsuccessful it sleeps for 20 seconds and then tries again. It will return the datetime string that is provided to the modem. That can be parsed from the “response” variable.

import serial
import time, sys
import datetime
import subprocess

i=0
time_found=False
response=''
SERIAL_PORT="/dev/ttyACM0"
ser=serial.Serial(SERIAL_PORT, baudrate = 9600, timeout = 15)

while time_found==False:
        ser.write('AT+CCLK?\r')
        response = ser.readline()
        while "CCLK:" not in response:
                response=ser.readline()
                time.sleep(0.2)
                ++i
                if i==200:
                        break
        if "CCLK:" in response:
                time_found=True
        else:
                time.sleep(20)

split_date=response.split("\"")
date_time_str = split_date[1]
date_time_str = date_time_str.replace(",", " ")
date_time_str = date_time_str.replace("-", ".")
date_time_obj = datetime.datetime.strptime(date_time_str, '%y/%m/%d %H:%M:%S.%f')
subprocess.call(['sudo', 'date', '-s', date_time_obj.isoformat(" ")])

This uses subprocess to execute a date set command. I added this to the rc.local script and used it to send a message to my hologram account letting me know that the system was online with a datetime stamp. If you want to do this be sure to add a long time.sleep() at the top of the script - I used 120 seconds in order for the modem to fully establish connectivity.

If there is any way to have this added to the SDK so that it can be done without running a silly script to query the TTY that would be much appreciated.

2 Likes

Thanks for posting the example code. I’ll talk to the team about adding a function that wraps around the network time call in the next release.

1 Like

Awesome! Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.