B4R Question BROADCAST STOPWATCH TIME THROUGH UDP ESP8266 12E

embedded

Member
Licensed User
I have running a timer every second for making a stopwatch. Three variables sec, minute and hour are taken. Sec is incremented with every timer tick and minute is incremented when sec value reaches 60. Everything is working fine.
Now I want to broadcast the value of time in 00:00:00 format with prefix "A" AND SUFFIX "Z".
LIKE A00:00:01Z when sec=1,minute=0 and hour=0. I am unable to do it. Please help.

MY CODE:

Sub Process_Globals
Public Serial1 As Serial
Private Timer1 As Timer
Private pin2 As Pin
Public sec As Int
Public minute As Int
Public hour As Int
Private usocket As WiFiUDP
Private wifi As ESP8266WiFi
Private ip() As Byte = Array As Byte(192, 168, 1, 101)
Private port As UInt = 5000
End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If wifi.Connect2("JioFi2_AB4119","htcatrii72") = False Then
Log("Error connecting to network")
Return
Else
Log("Connected to network")
End If
pin2.Initialize(2, pin2.MODE_OUTPUT)
usocket.Initialize(5000, "usocket_PacketArrived")
Timer1.Initialize("Timer1_Tick", 1000)
Timer1.Enabled = True 'don't forget to enable it
sec=0
minute=0
hour=0
End Sub

Private Sub Timer1_Tick
Dim s As String
Dim buffer(8) As Byte
pin2.DigitalWrite(Not(pin2.DigitalRead))
sec=sec+1
If sec=60 Then
minute=minute+1
sec=0
End If
If minute=60 Then
hour=hour+1
minute=0
End If
'label2_time.Text=str(hour) & ":" & str(Minute) & ":" & str(sec)
's=NumberFormat(hour,2,0) & ":" & NumberFormat(minute,2,0) & ":" & NumberFormat(sec,2,0)
'Log(sec)
'Log(minute)
Log(hour,":",minute,":",sec)

buffer(0)="A" 'when sec=1 then UDP broacsat IS A00:00:01Z
buffer(1)=hour 'when sec=2 then UDP broadcast is A00:00:02Z
buffer(2)=":"
buffer(3)=minute
buffer(4)=":"
buffer(5)=sec
buffer(6)="Z"
buffer(7)=48
usocket.BeginPacket(ip, port)
usocket.Write(buffer)
usocket.SendPacket
Log("buffer=",buffer)
End Sub

Private Sub usocket_PacketArrived (Data() As Byte, ip1() As Byte, port1 As UInt)

Log("Packet arrived")
Log(Data) 'Received data send here
Log(ip)
End Sub
 

tigrot

Well-Known Member
Licensed User
One question: do you have a realtime clock to initialize hour/Minute/Second ? I don't see any time support.
 

embedded

Member
Licensed User
SOLVED.....HERE IS MY CODE

usocket.BeginPacket(ip, port)
usocket.Write("A".GetBytes)


udpdata = NumberFormat(hour, 2, 0)
usocket.Write(udpdata.GetBytes)
usocket.Write(":".GetBytes)
udpdata = NumberFormat(minute, 2, 0)
usocket.Write(udpdata.GetBytes)
usocket.Write(":".GetBytes)
udpdata = NumberFormat(sec, 2, 0)
usocket.Write(udpdata.GetBytes)
usocket.Write("Z".GetBytes)
usocket.SendPacket
 
Top