Android Question ESP8266 with B4A ?

funker

Member
Licensed User
Hi good afternoon.:

I have a robot car, which has the ESP8266 it.
The Pc Program (PureBasic) sends regularly every 400 ms there:
http://192.168.2.102/?pin=leer with a timer loop.

From Pc (PureBasic) I can also control signals for driving passed with a button:
http://192.168.2.102/?pin=vor or
http://192.168.2.102/?pin=stop etc.

Each Send to ESP822 is also a value back to PureBasic either when the button is pressed or when the timer loop is tuned.

As such things go, contact the B4A?


---------------------------------------------
---------------------------------------------
Dieses wird zb von Purebasic gesendet in der url zum Roboter wenn ein Button gedrückt wurde:
http://192.168.2.102/?pin=vor

In einer Timerschleife von Purebasic wird dauernd dieses gesendet in der url ca alle 400 ms wenn kein Button gedrückt wurde. (ersichtlich aus dem Programmcode Purebasic)
http://192.168.2.102/?pin=leer
In der Rückantwort vom Roboter ESP.. ist dann ein bestimmter Wert drin.
Wenn keine bestimmten Daten anliegen ist dort alle 400 ms "0" drin. Wenn von Purebasic nichts gesendet wird, kommt auch keine Rückantwort vom Roboter.

...................................................................................
If Event = #PB_Event_Timer And EventTimer() = 123
Buffer = ReceiveHTTPMemory(url)
If buffer
string=Str(PeekC(Buffer))
EndIf
URL = SetURLPart(URL, "pin", "leer")
EndIf
..............................................................


Thank you.

Greeting
 

KMatle

Expert
Licensed User
Longtime User
Yes, of course. It's a simple post request. Use the HttpUtils library. Very easy.


PS: I have some ESP8266 and I use it the other way. The ESP sends post requests and my B4A/B4J app receives it (via php). FCM would be possible, too.
 
Upvote 0

funker

Member
Licensed User
job1.Download("http://192.168.2.102/?pin=leer") in the Timer 400 ms ?

greeting


B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: HttpUtils2
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #LibraryVersion: 2.01
    #LibraryName: HttpUtils2
#End Region

Sub Process_Globals
   
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=rechts")
End Sub

Sub JobDone (Job As HttpJob)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Remove these lines:
B4X:
#LibraryVersion: 2.01
#LibraryName: HttpUtils2
They are only needed when you compile your project into a library.

2. You should use OkHttpUtils2 library (check it in the libraries tab).

3. Move the code to a Timer_Tick event if you want to call it every 400ms.

4. HttpUtils2 tutorial: https://www.b4x.com/android/forum/threads/18992/#content

Note that you can use B4R to program the ESP8266 module.
 
Upvote 0

funker

Member
Licensed User
I now have a success.
It works the robot (with ESP8266) according to the commands that are listed in the program with my "Medion Livetab".

How now does please a regular Timer recall of 400 ms for this call: ????
job1.Download ("http://192.168.2.102/?pin=leer")

And how can I please grab the string value of a string, in a print text window ????
strwert = job1.Download ("http://192.168.2.102/?pin=leer")

--------------------------------------------------------
Ich hab jetzt einen Erfolg.
Es funktioniert der Roboter (mit ESP8266) gemäß den Befehlen, die im Programm aufgeführt sind mit meinem "Medion Livetab" .

Wie funktioniert jetzt bitte ein regelmäßiger Timeraufruf von 400 ms für diesen Aufruf:
job1.Download("http://192.168.2.102/?pin=leer")

Und wie kann ich bitte den Stringwert davon in einen String packen und in einem
Textfenster ausgeben?
strwert=job1.Download("http://192.168.2.102/?pin=leer") ????
-------------------------------------------

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: HttpUtils2
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #LibraryVersion: 2.20
    #LibraryName: OkHttpUtils2
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Dim job1 As HttpJob

    Private hlinks As Button
    Private links As Button
    Private hrechts As Button
    Private mitte As Button
    Private rechts As Button
    Private vor As Button
    Private zurueck As Button
    Private v0 As Button
    Private v1 As Button
    Private v2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
      Activity.LoadLayout("esp8266")
      job1.Initialize("Job1", Me)
End Sub

Sub JobDone (job As HttpJob)
    Log("JobName = " & job.JobName & ", Success = " & job.Success)

    If job.Success = True Then
        Select job.JobName
            Case "Job1"
        End Select
    End If
    job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub rechts_Click
    job1.Download("http://192.168.2.102/?pin=rechts")
    Delay(400)
End Sub

Sub hrechts_Click
    job1.Download("http://192.168.2.102/?pin=hrechts")
    Delay(400)
End Sub

Sub mitte_Click
    job1.Download("http://192.168.2.102/?pin=mitte")
    Delay(400)
End Sub

Sub hlinks_Click
    job1.Download("http://192.168.2.102/?pin=hlinks")
    Delay(400)
End Sub

Sub links_Click
    job1.Download("http://192.168.2.102/?pin=links")
    Delay(400)
End Sub

Sub v0_Click
    job1.Download("http://192.168.2.102/?pin=v0")
    Delay(400)
End Sub

Sub v1_Click
    job1.Download("http://192.168.2.102/?pin=v1")
    Delay(400)
End Sub

Sub v2_Click
    job1.Download("http://192.168.2.102/?pin=v2")
    Delay(400)
End Sub

Sub zurueck_Click
    job1.Download("http://192.168.2.102/?pin=zur")
    Delay(400)
End Sub

Sub vor_Click
    job1.Download("http://192.168.2.102/?pin=vor")
    Delay(400)
End Sub

Sub Delay(nMilliSecond As Long)
    Dim nBeginTime As Long
    Dim nEndTime As Long
    nEndTime = DateTime.Now + nMilliSecond
    nBeginTime = DateTime.Now

    Do While nBeginTime < nEndTime
        nBeginTime = DateTime.Now
        Log(nBeginTime)
        If nEndTime < nBeginTime Then
            Return
        End If
          DoEvents
    Loop
End Sub
 
Upvote 0

funker

Member
Licensed User
Mein Roboter.
 

Attachments

  • roboter.jpg
    roboter.jpg
    132.9 KB · Views: 310
Upvote 0

funker

Member
Licensed User
Hi good afternoon.
I get it, unfortunately not out.
It will be returned no data.

can you tell me that please even change?

thank you.
greeting

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: esp8266
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #LibraryVersion: 2.20
    #LibraryName: OkHttpUtils2
#End Region

Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Globals
    Dim daten As String
      Dim job1 As HttpJob
   
      Private hlinks As Button
      Private links As Button
    Private hrechts As Button
    Private mitte As Button
    Private rechts As Button
    Private vor As Button
    Private zurueck As Button
    Private v0 As Button
    Private v1 As Button
    Private v2 As Button
    Private ser0 As Button
    Private ser1 As Button
    Private sharp As Button
    Private edit1 As EditText
   
    Timer1.Initialize("Timer1", 700) 
    Timer1.Enabled = True   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)   
      Activity.LoadLayout("esp8266")
      job1.Initialize("Job1", Me)   
    edit1.text ="hallo"
End Sub

Sub JobDone (job As HttpJob)
    Log("JobName = " & job.JobName & ", Success = " & job.Success)

    If job.Success = True Then
        Select job.JobName
            Case "Job1"
              daten = job.getString
             edit1.text=daten
        End Select
    End If
    job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Timer1_Tick
  Dim job1 As HttpJob 
  job1.Initialize("Job1", Me)
  job1.Download("http://192.168.2.102/?pin=leer")
  Delay(400)
End Sub

Sub rechts_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=rechts")
    Delay(200)
End Sub

Sub hrechts_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=hrechts")
    Delay(200)
End Sub

Sub mitte_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=mitte")
    Delay(200)
End Sub

Sub hlinks_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=hlinks")
    Delay(200)
End Sub

Sub links_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=links")
    Delay(200)
End Sub

Sub v0_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=v0")
    Delay(200)
End Sub

Sub v1_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=v1")
    Delay(200)
End Sub

Sub v2_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=v2")
    Delay(200)
End Sub

Sub zurueck_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=zur")
    Delay(200)
End Sub

Sub vor_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=vor")
    Delay(200)
End Sub

Sub ser0_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=ser0")
    Delay(200)
End Sub

Sub ser1_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)  
    job1.Download("http://192.168.2.102/?pin=ser1")
    Delay(200)
End Sub

Sub sharp_Click
    Dim job1 As HttpJob 
    job1.Initialize("Job1", Me)
    job1.Download("http://192.168.2.102/?pin=sharp")
    Delay(200)
End Sub

Sub Delay(nMilliSecond As Long)
    Dim nBeginTime As Long
    Dim nEndTime As Long
    nEndTime = DateTime.Now + nMilliSecond
    nBeginTime = DateTime.Now
   
    Do While nBeginTime < nEndTime
        nBeginTime = DateTime.Now
        Log(nBeginTime)
        If nEndTime < nBeginTime Then
            Return
        End If
          DoEvents
    Loop
End Sub
 
Upvote 0
Top