B4R Question BROADCAST STOPWATCH TIME THROUGH UDP ESP8266 12E

Discussion in 'B4R Questions' started by embedded, Jun 2, 2018.

Tags:
  1. embedded

    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
     
  2. tigrot

    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.
     
  3. tigrot

    tigrot Well-Known Member Licensed User

  4. embedded

    embedded Member Licensed User

    No..RTC is not used here. Timer is used here which fire a event every second
     
  5. tigrot

    tigrot Well-Known Member Licensed User

    But you must load a starting time.
     
  6. embedded

    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
     
  7. embedded

    embedded Member Licensed User

    STOPWATCH ALWAYS START FROM 00:00:00
     
  8. tigrot

    tigrot Well-Known Member Licensed User

    Ah ok...
    Remember that timer is not much precise.
     
    embedded likes this.
  9. tigrot

    tigrot Well-Known Member Licensed User

    you can write:
    Code:
    usocket.Write(NumberFormat(minute, 20).GetBytes)
     
    Johan Hormaza and embedded like this.
  10. embedded

    embedded Member Licensed User

    Yes....I know...Thanks for your support....Namaste from India
     
    tigrot likes this.
  11. tigrot

    tigrot Well-Known Member Licensed User

    You are welcome, ciao dall'Italia !
     
    embedded likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice