Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Android System Time Stamp to Arduino
#1
Hello, I am using Virtuino 6 on Android to communicate via blue tooth to an Arduino. I'd like to send the phone's time stamp to the Arduino.  It seems like such a simple thing... I want to avoid requiring the user to use the date time picker to select today's date/time (very many clicks).  I've poked around what I thought was all the different widgets, but cannot figure it out.  I can display the current date/time by using the hack of showing the "Updated Value date", but you cannot copy that value to anything.
Reply
#2
Hi, 
no it is not possible. 
This ability is added to the newer app virtuino IoT
Reply
#3
I have a solution for this task.
Pick up the "Text script or Json creator", fill in "Script" field the following Json: {"Day":#value1, "Hour":#value2, "Minutes":#value3}. In the given example the "Day" stands for the day of week. Now choose an output variable on your Virtuino server. For testing that you should at least once in loop use that variable, for example print that on serial terminal.
Then you should chouse input variables by pressing "+" as much time, as you have input variable. The first input variable is the Android device sensor's V12-Day -of the week, next is the V9-Hour and last is V8-Minute. The Widget does not have its own refresh rate definition, so be careful to include the "seconds", which may overload the connection with server.
The following is the received Json on my terminal, sent to V[8] server side variable.
Code:
21:37:01.549 -> Received data: 1234!V08=%7B%22Day%22%3A0%2C+%22Hour%22%3A0%2C+%22Minute%22%3A37%7D$
21:37:01.549 -> %7B%22Day%22%3A0%2C+%22Hour%22%3A0%2C+%22Minute%22%3A37%7D
Reply
#4
I have a newer refined answer to this topic.
The preliminary options are as follows. The server is standalone, thus cannot get the data&time info from Internet. But the server is equipped with RTC module, so the actual time can be requested from smartphone and get the RTC adjusted. For phone and microcontroller time comparision, at least for debugging purpose you should have two value displaying widgets, one for phone time and another for controller time.
If you have used Advanced Value Display widget and have used the value type as "Updated value data" for phone time, you should change it to "Time" value type. Being used the "Updated value data" that blocks most of Android Sensor data usage. As "Server" should be chousen the Android Device Sensor's V6, which returns Unix epoch time in milliseconds.
On server side I have used Adafruit developed RTC library (available here), which can give the epoch time in seconds. The part of code looks like this: 
Code:
  DateTime now = rtc.now();
  V[18] = String(now.unixtime());

Now, lets discuss the actual time data sending to microcontroller. Add an Script widget, in script field write 
Code:
x/1000
Input X needs to be the Android Device Sensor's V6-ms. If you do not divide this by 1000, this data will overflow the Output side variable (which need to be chosen as somewhat variable on microcontroller), even being chosen the unsigned long. On microcontroller side you will get milliseconds as fraction, which can be easily truncated or rounded to integer or since the data are coming as String type, it can be wiped out last 4 symbols, the decimal dot inclusive. The clock adjustmentt on server side needs in seconds data, instead of milliseconds, which sends the phone.
Another observation is that the refresh rate of script widget does not work as expected and it is bombarding the controller as fast as it can.
Reply
#5
A post earlier I wrote that the Script widget ignores the own refresh rate value and floods the connection with microcontroller. To overcome this trouble, I redirect the output of Script widget to some buffer cell in M memory, let's to say, to M30 of Android and planning to push the data to microcontroller on demand. I try to create a system button, which where the action set as something neutral thing, like "Select First Panel", then in "Command" section of widget I add a command, namely, on Click send command to Virtuino Server's V8 variable the content of Emulator's M30 value. On "System button" clicking nothing has been sent to controller.
In meantime for debugging purpose the content of M30 is displayed by value widget. It is proven that the data is available and updates itself automatically.

This post is the final fix of timestamp sending microcontroller subject.
Select "Botton creator widget", 
"Operation": Custom Button
"Server":      Virtuino server1
"Memory":   Somewhat V?

"Click down": Emulator M30 (* See earlier post. This is the buffer memory where the Script widget fires the timestamp)
"Click" up:     Nothing
"Long click:   Nothing
Reply
#6
(08-17-2025, 05:55 PM)Araqel Wrote: I have a solution for this task.
Pick up the "Text script or Json creator", fill in "Script" field the following Json: {"Day":#value1, "Hour":#value2, "Minutes":#value3}. In the given example the "Day" stands for the day of week. Now choose an output variable on your Virtuino server. For testing that you should at least once in loop use that variable, for example print that on serial terminal.
Then you should chouse input variables by pressing "+" as much time, as you have input variable. The first input variable is the Android device sensor's V12-Day -of the week, next is the V9-Hour and last is V8-Minute. The Widget does not have its own refresh rate definition, so be careful to include the "seconds", which may overload the connection with server.
The following is the received Json on my terminal, sent to V[8] server side variable.
Code:
21:37:01.549 -> Received data: 1234!V08=%7B%22Day%22%3A0%2C+%22Hour%22%3A0%2C+%22Minute%22%3A37%7D$
21:37:01.549 -> %7B%22Day%22%3A0%2C+%22Hour%22%3A0%2C+%22Minute%22%3A37%7D

As it is obvious, the application has non-perfection in Json generating, where hex data is mixed with ascii codes. Cause of this it is impossible to feed such a date to app's Json converter widget, for further use or debugging.
I have modified the original sketch for on-the-fly Json correction.
Code:
//==============================================================
void virtuinoRun() {
  short s = 3;
  String tempStr = "";
  WiFiClient client = server.accept();
  if (!client) return;
  //if (debug) Serial.println("Connected");
  unsigned long timeout = millis() + 3000;
  while (!client.available() && millis() < timeout) delay(1);
  if (millis() > timeout) {
    //  Serial.println("timeout");
    client.clear();
    // client.stop();
    return;
  }
  virtuino.readBuffer = "";  // clear Virtuino input buffer. The inputBuffer stores the incoming characters
  while (client.available() > 0) {
    char c = client.read();  // read the incoming data
    if (c == '%') {
      s--;
    }
    if (c != '%' && s == 2) {
      tempStr += c;
      s--;
    } else {
      if (c != '%' && s == 1) {
        s--;
        tempStr += c;
        virtuino.readBuffer += (char)strtol(tempStr.c_str(), NULL, 16);
        tempStr = "";
      }
    }
    if (s == 0) {
      s = 3;
    } else {
      if (s == 3 && c != '%') {
        virtuino.readBuffer += c;  // add the incoming character to Virtuino input buffer
        // if (debug) Serial.write(c);
      }
    }
  }
Now, the Json of "Programmable button wedget's Json on IDE's Serial monitor looks like this, instead of earlier garbage:
Code:
00:33:48.928 -> Received data: 1234!V02={"ID":"schedule","date":"հնգ+2025-08-21+00:33:48","items":[{"D":"0111111","H":8,"M":0,"S":0,"ST":1},{"D":"0111111","H":18,"M":0,"S":0,"ST":0}]}$
00:33:48.928 -> V2 = {"ID":"schedule","date":"հնգ+2025-08-21+00:33:48","items":[{"D":"0111111","H":8,"M":0,"S":0,"ST":1},{"D":"0111111","H":18,"M":0,"S":0,"ST":0}]}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)