Clock from Phone

Hello everybody. First of all , I want to thank you for all the great informations posted on this forum !

I made this post because I am working to an app using node red, and I don't really now how to proceed with this :

My app(made with node-red) is running on a Raspberry Pi (main server). This app, it will be accessed and used from a Phone. The problem with Raspberry Pi is the fact that he doesnt keep his clock , so everytime I need to set manually with date command ( my app dont require internet connection in order to set the date automatically with ntp) .

Does everybody know a node or a java Script function which interogate the correct hour and date from the Phone, in order to synchronize the date and hour to Raspbbery Pi?

Till know I tried something like this : Using a function node with the code :

function upTime() {
var d = new Date();
msg.payload=d;
}
return msg;

This function code it returns the epoch format number which I can use it forward to set the date and time for Raspbeery Pi with exec node. But, the problem is THIS NUMBER IS THE EPOCH FORMAT NUMBER from the Raspber Pi, not from the PHONE.

I need somehow to take the correct data and time informations from the PHONE and synchronize with Raspberry PI. Maybe another functions, I don`t know.

If anybody of you have an idea how to fix this, just let me know.

Thanks in advance.

Welcome to the forum.

The Pi does not have a internet connection ?

Hi. Unfortunately no ! :frowning:

You could try one of the RTC add on boards for the Pi. I would recommend one based on the DS3231 chip, it is very accurate.

Hi ghayne. Thanks for your reply. I also have been thinking on this solution, but I want to try first all software methods before I do this.

You could run this MQTT client in conjunction with Tasker and use it to send the time to mosquitto on the pi via MQTT. https://play.google.com/store/apps/details?id=in.dc297.mqttclpro

There are probably other similar solutions. Search for MQTT on google play (assuming it is an Android phone).

For example. Node-red-dashboard clock in toolbar (https://flows.nodered.org/flow/7e4bb517ce3a75add0083642ef03e9d8) its a node which display the hour and the date from phone. But I cant really understand how the code behind works to take the data and hour from PHONE, and not from Raspberry Pi. dashboard - this is what node-red-dashboard clock display me which is the date and hour from Phone . But the date and hour is not the same with the ones from Raspberry Pi.

The code for clock toolbar node is this:

(function() { if(('.md-toolbar-tools').length != 0){
loadClock();
}else setTimeout(loadClock, 500)
});

function loadClock(){
('#clock').remove(); var toolbar = ('.md-toolbar-tools');

var div = $('<div/>');
var p = $('<p/ id="clock">');

div.append(p);
div[0].style.margin = '5px 5px 5px auto';
toolbar.append(div);

function displayTitle(lh) {
    p.text(lh); 
}

function upTime() {
    var d = new Date();
    p.text(d.toLocaleString());
}

if(document.clockInterval){ 
        clearInterval(document.clockInterval);
        document.clockInterval = null;
}
    
document.clockInterval = setInterval(upTime,1000);

}

I need somehow to extract and save from this node a variable with epoch format in order to use later with exec node to synchronize the data and hour to Raspberry Pi.

Thanks !

Thanks for your reply. I will do some researches.

That works because the code that gets the time is running in the browser on the phone (in the dashboard). Someone more knowledgable than myself could add a feature to that to send a message with the time back to the flow. However it would require you to open the dashboard on the phone for it to work, though that may not be an issue for you .

I understand. Thank you Colin for your explanation and implication. My app will always be used from phone, so it`s not gonna be an issue because I will always open the dashboard on it.

Dumb question:

Ok, as is your RasPi doesn't have internet connection. And no RTC.

Couldn't you make your phone a WAP; connect the RasPi to the phone's WAP and it get the time that way?

It won't need to stay connected to the phone's WAP once the time is received.

(Ok, I'll shut up)

Hi Bruno,

I was working on a ui_template example along Colin's suggestions.
That is getting the time from the user browser sending to node-red and after that you have to process it with the exec node you mentioned

Example flow ...

[{"id":"ce3be529.f3d278","type":"ui_template","z":"f54b08eb.e98bb","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":"  <html>\n  <head>\n  <title>Time</title>\n  <script type=\"text/javascript\"> \n  \n  var date = new Date().toString();\n  document.getElementById('ct').innerHTML = date;\n  \n  this.scope.sendDate = function() {\n      console.log(\"in function\")\n   // return {payload: new Date().toString()}; // .valueOf() \n    return {payload: new Date().valueOf()}; // \n  }\n \n</script>\n  \n  </head>\n\n  <body>\n  <div id='ct' style=\"margin-bottom: 40px;\"></div>\n  <br>\n  <br>\n<md-button ng-click=\"send(sendDate())\">\n    Send time to Node-red\n</md-button>\n\n  </body>\n  </html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":720,"y":480,"wires":[["9352a614.0fc998"]]},{"id":"9352a614.0fc998","type":"debug","z":"f54b08eb.e98bb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":900,"y":480,"wires":[]},{"id":"c205e26f.6865a","type":"ui_group","z":"","name":"Default","tab":"20af7edd.a91fca","order":1,"disp":true,"width":"12","collapse":false},{"id":"20af7edd.a91fca","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

My Raspberry Pi is already used as RasAP. With my phone I will connect to Raspberry Pi AP and I will access the node red app.

My app it will be used offline, without internet connection.

Thanks !

Thanks a lot UnborN !!!!! This is the solution.

Just only one little question:

Do you know how can I get rid of the text above the button?

Send me your adress to send you ten beers !
Thanks again mister !

colin

You edit the template and remove html

<div id='ct' style="margin-bottom: 40px;"></div>

and the javascripts that fills it with the date

var date = new Date().toString();
document.getElementById('ct').innerHTML = date;
1 Like

You are the best ! Thank u UnborN !!

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