B4R Question WeMos and B4a reception problems with UDPSocket

D

Deleted member 103

Guest
Hello,

Can anyone tell me whether something is wrong in my B4x codes?
I am sending a string via WeMos, but in Android (2 devices) is not always received (see pictures).

WeMos_HTC-one.png
WeMos_Nexus-7.png
 

Attachments

  • WeMos_b4r.zip
    1.5 KB · Views: 287
  • WeMos_b4a.zip
    8.8 KB · Views: 288
D

Deleted member 103

Guest
I have changed the title.
I'm sorry, it's not using MQTT but UDPSocket. :(
 
Upvote 0
D

Deleted member 103

Guest
So, now I use AsyncStreams, and no package is lost. :)
Since problem is now however the the impulses do not arrive simultaneously at both devices (see picture below). :(
Question: can something be improved on the code so that the impulses arrive at the same time?
If this is not possible, my WeMos can not be used for this purpose. :mad:

WeMos_HTC-one.png
WeMos_Nexus-7.png
 

Attachments

  • WeMos_b4r.zip
    1.6 KB · Views: 282
  • WeMos_b4a.zip
    9.2 KB · Views: 302
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You can measure the time on the ESP8266 board and send it to the other devices. It will be very accurate.
Exactly!
The way you have implemented it you are adding the network's latency time to the time measured!
Make the needed calculations inside the Wemos board and then sed the result, this will be latency free, even if the receiving devices receive their messages at different times.
 
Upvote 0
D

Deleted member 103

Guest
You can measure the time on the ESP8266 board and send it to the other devices. It will be very accurate.
This idea I have already had, the problem is that I then very much of my code would have to change, but it would certainly be the best solution.
Thank Erel, Thanks Cableguy!
 
Upvote 0

cas6678

Active Member
Licensed User
Longtime User
Hi,

I used for my project the Filipo's code post # 4. The one diference is that in my code then count ++ is in B4R not in device B4A. I'm load B4R code in a NODEMCU

This code is to measure the time a wheel turns.

The problem is that does not count all the times.

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public WiFi As ESP8266WiFi
    Private bc As ByteConverter
    Dim MacArray(6) As Byte
    Private server(2) As WiFiServerSocket
    Private astream(2) As AsyncStreams
    Private pinSensor As Pin
    Private pin1 As D1Pins
    Private contador As Long
    Private codigo As Int
End Sub

Private Sub AppStart
    Log("AppStart")
    Serial1.Initialize(115200)
    Dim Passwd As String = bc.HexFromBytes(bc.SubString2(MacAddress, 0, 4))
    Log("StartAP: ", WiFi.StartAccessPoint2("fgWifi", Passwd))
    Log("My AP ip: ", WiFi.AccessPointIp)
    Log(Passwd)
    IntiServer
End Sub

#Region "server-Subs"
Public Sub IntiServer   
    server(0).Initialize(51041, "Server1_NewConnection")
    server(0).Listen
    server(1).Initialize(51042, "Server2_NewConnection")
    server(1).Listen
    pinSensor.Initialize(pin1.D1, pinSensor.MODE_INPUT_PULLUP)
    pinSensor.AddListener("pinSensor_StateChanged")
End Sub

Sub Server1_NewConnection (NewSocket As WiFiSocket)
    Log("Server1_NewConnection=" , NewSocket.Connected)
    If server(0).Socket.Connected Then
        astream(0).Initialize(NewSocket.Stream, "astream_NewData", "astream1_Error")
        astream(0).Write(bc.StringToBytes("OK"))
    End If
End Sub

Sub Server2_NewConnection (NewSocket As WiFiSocket)
    Log("Server2_NewConnection=" , NewSocket.Connected)
    If server(1).Socket.Connected Then
        astream(1).Initialize(NewSocket.Stream, "astream_NewData", "astream2_Error")
        astream(1).Write(bc.StringToBytes("OK"))
    End If
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim cod As String = bc.StringFromBytes(Buffer)
    codigo=cod
    SendValue
End Sub

Sub astream1_Error
    Log("astream1_Error")
    If Not(server(0).Socket.Connected) Then
        Log("server(0).Listen=", 0)
        server(0).Listen
    End If
End Sub

Sub astream2_Error
    Log("astream2_Error")
    If Not(server(1).Socket.Connected) Then
        Log("server(1).Listen=", 1)
        server(1).Listen
    End If
End Sub
#End Region

Sub pinSensor_StateChanged (State As Boolean)
    If Not(State) Then
        contador=contador+1
    Else
        SendValue
    End If   
End Sub

Sub SendValue
        Dim key As String
        key=NumberFormat(contador*100+codigo,0,0)
        If server(0).Socket.Connected Then
            astream(0).Write(key).Write(",")
        End If
        If server(1).Socket.Connected Then
            astream(1).Write(key).Write(",")
        End If
End Sub

Sub MacAddress() As Byte()
    RunNative("getMac", Null)
    Return MacArray
End Sub

#if C
  #include <ESP8266WiFi.h>
  void getMac(B4R::Object* u) {
  WiFi.macAddress((Byte*)b4r_main::_macarray->data);
  }
#end if

If I coment the sub SendValue and dont send, work fine. I can see after the number of time wheel turned. But if the sub SendValue is active this dificult the count (contador=contador+1) and lose a large number of turns.

I've my old proyect like this but in arduino and whit BT and work fine ever.

In arduino el code for send is very simple.

B4X:
#include <SoftwareSerial.h>
SoftwareSerial SSBT(7,8);       
SSBT.begin(38400);    
SSBT.print(pasos*100+num); 
SSBT.print(",");
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I don't understand this code :

B4X:
Sub pinSensor_StateChanged (State As Boolean)
    If Not(State) Then
        contador=contador+1
    Else
        SendValue
    End If
End Sub

If the state is false, then your condition is true and you then add to the counter, is the state is true, then your condition is false, and you send the counter value, is this correct?

I guess you are detecting when the pin goes to 0v (logical 0) to count...
I would change a bit this sub :

B4X:
    If Not(State) Then
        contador=contador+1
        SendValue ' either here form every counter add
    End If
       SendValue 'or here for every state change
 
Last edited:
Upvote 0

cas6678

Active Member
Licensed User
Longtime User
It is a bicycle sensor (bicycle speed) with a magnet.
The normal state is HIGH, when the magnet passes the sensor status is LOW and when leave is HIGH again.
What I do is add it passes and send when it leaves.

If use your code would send me the same number twice time.

It could be done in multiple ways.

If
C ++
else
send
End If

If
Send
else
C ++
End If

If
C ++
send
else
End If

If
else
C ++
send
End If

Its the same.

I use the first method for the send either longer period, which is when the sensor is not excited.

This part the code work fine. If not send I can read the correct number the turns.

Something happens when send that does not let the Sub pinSensor_StateChanged (State As Boolean) run good.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
please do note that I comented my code with 'either here.... or here
this means you should delete one of them, whichever suites you best.
 
Upvote 0

cas6678

Active Member
Licensed User
Longtime User
sorry, my English would not let me see your comment correctly. :(:(:(

Anyway, my problem is not there.

I'm testing, it takes approx. 150-200 micros for send. It's fast. But somehow there is something that is not running well when send.
 
Upvote 0

cas6678

Active Member
Licensed User
Longtime User
I changed my code for...
B4X:
Sub pinSensor_StateChanged (State As Boolean)
    If State Then
            contador=contador+1
            SendValue
        End If   
End Sub
thaks @Cableguy , is best code, but the problem is the same
 
Upvote 0

cas6678

Active Member
Licensed User
Longtime User
No, but now that you say I will do it tomorrow.

What is your idea for that? You suggest some special pin? (nodemcu)

I am surprised because the sub pinsensor_statechange only fails if the sub sendvalue occurs.

I thought the sub sendvalue was slow, but it is not. 150/200 micros. Then I do not understand why interrumhinders the normal run the app
the sub was slow, but it is not. Then I do not understand why interrupots the normal run app.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
almost every pin has double function, also, the way you define your pins is very important.
The same is true for the schematics, check if you need to add a pull-up or pull-down resistor...
All these are just thoughts...
 
Upvote 0
Top