B4R Question Read NIST server time

Erel

B4X founder
Staff member
Licensed User
Longtime User
Works fine here. Tested with ESP32:
B4X:
AppStart
Connected: 1
Connecting to NIST server...
Failed to connect to NIST server
Connecting to NIST server...
Failed to connect to NIST server
Connecting to NIST server...
Date: 25-05-18
Time (UTC): 05:15:41
Date: 25-05-18
Time (UTC): 05:15:42
Date: 25-05-18
Time (UTC): 05:15:43
Date: 25-05-18
Time (UTC): 05:15:44
Date: 25-05-18
Time (UTC): 05:15:45
Date: 25-05-18
Time (UTC): 05:15:46
Date: 25-05-18
Time (UTC): 05:15:47
Date: 25-05-18
Time (UTC): 05:15:48
Date: 25-05-18
Time (UTC): 05:15:49
Date: 25-05-18
Time (UTC): 05:15:50
Date: 25-05-18
Time (UTC): 05:15:51
Date: 25-05-18
Time (UTC): 05:15:52
Date: 25-05-18
Time (UTC): 05:15:53
Date: 25-05-18
Time (UTC): 05:15:54
Date: 25-05-18
Time (UTC): 05:15:55
Date: 25-05-18
Time (UTC): 05:15:56
Date: 25-05-18
Time (UTC): 05:15:57
Date: 25-05-18
Time (UTC): 05:15:58
Date: 25-05-18
Time (UTC): 05:15:59

Which error do you get?
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Good morning
If you check the ERROR routine, you'll see that it's triggered every NIST read cycle.

Nist.Initialize(socket.Stream, "NIST_NewData", "NIST_Error")
Nist_NewData is triggered with the NIST values, but NIST_ERROR is also triggered.

Checking my application, when I get a NIST error, I re-issue the request and overload the application.
I took measures in my application to not request NIST again before a minute has passed, but I still get the error response in "Nist_Error".

Thank you, and if you can fix this minor issue, I'd appreciate it.

Congratulations on your great work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you referring to this sub:
B4X:
Private Sub AStream_Error
    'Log("Disconnected")
End Sub
It is expected to be raised when the connection is closed. TimeNIST will try to reconnect after 1 second if it failed to connect:
B4X:
Private Sub Connect(u As Byte)
    Log("Connecting to NIST server...")
    If socket.ConnectHost("time.nist.gov", 13) Then
        astream.Initialize(socket.Stream, "astream_NewData", "astream_Error")
    Else
        Log("Failed to connect to NIST server")
        CallSubPlus("Connect", 1000, 0)
    End If
End Sub

You don't need to change anything...
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
If I'm referring to that "Private Sub AStream_Error" module, it always runs whether or not you connect to NIST.

This application uses Wi-Fi and Bluetooth, but not simultaneously, of course.
Every so often, the application turns on Wi-Fi, connects to the NIST server, takes the date and time, and sends information to a data server using PUT and GET.
After a while, the application activates Bluetooth and waits for the connection. If they connect via Bluetooth, it remains connected, and when the time has passed, it reconnects to Bluetooth and repeats the process.
This is the module that performs this operation.

'
' Dato que el ESP32 no puede tener la WIFI y el BLUETOOTH a la vez aqui
' se gestiona cuando usa la WIFI y cuando utiliza el BLUETOOTH
' Si la conexion BlueTooth esta en uso no cambia a WIFI
' Si esta Actualizando por WIFI no pasa a BlueTooth


Sub Gestion_Red
Dim Ayuda As String
Dim Ok_Wifi As Boolean = True ' Condicion
Dim En_Uso As Boolean = False
Dim Name As String = ""
Dim Pass As String = ""

If PLC.Hay_Alguna_Orden = True Then Ok_Wifi = False ' Alguna Orden en la Puerta.
If PLC.M_Hay_Alguna_Orden = True Then Ok_Wifi = False ' Memoria Alguna Orden en la Puerta.

If PLC.M_Hay_Alguna_Orden = True Then En_Uso = True ' Indicacion Puerta en Uso
If PLC.Hay_Alguna_Orden = True Then En_Uso = True ' Indicacion Puerta en Uso

If En_Uso = True Then ' Si la Puerta esta en Uso Termina
Return
End If

'Log(Paso_Red," ... ",Espera_Paso)
Select Case Paso_Red
Case 0 ' Inicia la RED WIFI
Name = EEPROM.EEPROM_Lee_Nombre_WIFI
Pass = EEPROM.EEPROM_Lee_Password_WIFI

If Name <> "" Then ' Si no Hay nombre de WIFI
If Pass <> "" Then ' Si no Hay Password de la WIFI
Main.Conecta_a_Red_Wifi(0)
Paso_Red = 2 ' Avanza Paso
Espera_Paso = 60 ' Carga Tiempo de Espera a Cumplir el Paso
Else
Paso_Red = 100 ' Salta a Paso 100
End If
Else
Paso_Red = 100 ' Salta a Paso 100
End If

Case 2 ' Espera WIFI Conectada con IP Correcta.
If Main.wifi.IsConnected = True Then ' Si la WIFI esta Conectada
If Main.wifi.LocalIp <> "0.0.0.0" Then ' la IP es Valida
If Ok_Wifi = True Then ' Condiciones de WIFI OK
Paso_Red = 4 ' Avanza de Paso
Radio_en_Modo = 0 ' Radio en Modo WIFI
Ayuda = "Modo WIFI" '***
Test.Log_P(Ayuda,False) '***
End If
Else
Paso_Red = 100 ' Salta a Paso 100
End If
End If

If Espera_Paso = 0 Then ' Si paso el Tiempo de Espera
Paso_Red = 10 ' Pasa a la Conexion WIFI
End If
Case 4 ' Conecta a Servidor NIST
If Ok_Wifi = True Then ' Condiciones de WIFI Ok
RELOJ.Connect_NIST ' Conexion a Servidor NIST
End If
Paso_Red = 6 ' Avanza Paso
Espera_Paso = 6 ' Carga Tiempo de Espera a Cumplir el Paso
Case 6 ' Espera Terminar la Conexion NIST
'If RELOJ.Conectando_a_NIST = False Then ' Si Termino la Conexion NIST
' Paso_Red = 8 ' Avanza de Paso
'End If
If Espera_Paso = 0 Then ' Termino el Tiempo de Espera
Paso_Red = 8 ' Avanza de Paso
End If
Case 8 ' Gestiona con el Servidor
If Ok_Wifi = True Then ' Condiciones de WIFI Ok
Gestion_Actualiza_Servidor ' Actualiza Datos al Servidor
End If
Paso_Red = 10 ' Avanza Paso
Espera_Paso = 6 ' Carga Tiempo de Espera a Cumplir el Paso
Case 10 ' Espera Actualizacion del Servidor
If Espera_Paso = 0 Then ' Termino el Tiempo de Espera
If Actualizando_servidor = False Then ' Si Termino de Actualizar el Servidor
If OTA_en_Curso = False Then ' Si no esta actualizando el FIRMWARE
Paso_Red = 100 ' Avanza de Paso
End If
End If
End If
Case 100 ' Inicializa BlueTooth
Inicializa_Blue(0) ' Lanza el BlueTooth
Radio_en_Modo = 1 ' Radio en Modo BlueTooth
Ayuda="Modo BlueTooth" '***
Test.Log_P(Ayuda,False)'***
Paso_Red = 102 ' Avanza de Paso
Espera_Paso = 300 ' Carga Tiempo de Espera a Cumplir el Paso
Case 102 ' Retorna a Paso Inicial
If Espera_Paso = 0 Then ' Termino el Tiempo de Espera
If BLUE_Conectada = False Then ' Si no esta Conectado por BlueToth
Paso_Red = 0 ' Reinicia el Paso
End If
End If
If Blue_T_Correcto = False Then ' Si el BlueTooth no se inicio Correctamente
Inicializa_Blue(0) ' Lanza el BlueTooth
End If


Case Else
Ayuda = JoinStrings(Array As String("Error en paso Red = ",Paso_Red))
Test.Log_P(Ayuda,True)
Paso_Red = 0 ' Reinicia el Numero de Paso

End Select


' Descuenta Tiempo de Espera
If Espera_Paso > 0 Then
Espera_Paso = Espera_Paso - 1
End If

End Sub

The application is currently running on more than 500 devices, and everything is correct. My client continues to request modules, and at some point, I will have to make some changes, so it's very important to be able to make changes and keep everything working.

The problem is that my hard drive stopped working, so I reinstalled Windows 10, along with all the programs.
I have backups of all the applications, several from different times.

This application was created in 2021 with ESP32 3.0.7, and now the only thing I changed was to ESP32 3.2.0, and the application doesn't work properly.

I've checked everything; if I load version 3.0.7, the application works correctly.

Any suggestions?

Thank you very much.
 
Upvote 0
Top