B4R Question Connect Android and Esp8266 (NodeMcu) - Solved

Discussion in 'B4R Questions' started by derez, Aug 21, 2016.

  1. derez

    derez Expert Licensed User

    :mad::mad::mad:
    I try to connect NodeMcu and Android.
    The NodeMcu code as server ( with Cableguy's example):
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private Server As WiFiServerSocket
       
    Private Wstream As AsyncStreams

        
    Private APSSID As String = "esp"
        
    Public WiFi As ESP8266WiFi
    End Sub

    Private Sub AppStart

       Serial1.Initialize(
    115200)
       
    Log("AppStart")
        
    Select WiFi.StartAccessPoint(APSSID)
           
    Case 1
                
    Log("Access Point Initialisation Successful")
                
    Log("SSID: ",APSSID)
                
    Log("ip: ", WiFi.AccessPointIp)
                
    Server.Initialize(80,"Server_NewConnection")
                
    Server.Listen
            
    Case 0
                
    Log("Something Went wrong!")
                WiFi.Disconnect
       
    End Select
    End Sub

    Sub Server_NewConnection(NewSocket As WiFiSocket)
       
    Log("new connection")
       Wstream.Initialize(NewSocket.Stream,
    "Wstream_NewData""Wstream_Error")
       Wstream.Write(
    "ok".GetBytes())
    End Sub
    The B4A code as client:
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        Client.Initialize(
    "Client")
    ...
    End Sub

    Sub contmr_tick
        
    If Client.Connected = False Then
            Client.Connect(
    "192.168.4.1",80,1000)
        
    End If
    End Sub

    Sub Client_Connected (Successful As Boolean)
        
    If Successful Then
            wtmr.Enabled = 
    True
            contmr.Enabled = 
    False
            
    Log("Connected to Car")
        
    Else
            
    Log("Failed to connect to Car")
        
    End If
    End Sub

    Sub wtmr_tick
        Astream.Initialize(Client.InputStream, Client.OutputStream,
    "Astream")
        wtmr.Enabled = 
    False
    End Sub
    The problem is that I start the NodeMcu and get this log:
    In the B4A log I get the log "Connected to Car" which testify that the Client is connected to the server, but in the NodeMcu the event of NewConnection is not fired, I don't get the log "new connection".

    Can someone explain ?
     
    Last edited: Aug 21, 2016
    ocalle likes this.
  2. derez

    derez Expert Licensed User

    I am using B4R version 1.2 , not Beta#3 - is this the problem ? I have that and Beta#2...can't find #3.
     
  3. positrom2

    positrom2 Active Member Licensed User

  4. derez

    derez Expert Licensed User

    Thanks, that is what I use.

    Another input - the NodeMcu connects nicely to a server on the home LAN, as a client.
     
  5. derez

    derez Expert Licensed User

  6. Erel

    Erel Administrator Staff Member Licensed User

  7. derez

    derez Expert Licensed User

    The example above is not the same, it is based on connecting to existing network while I try to connect to the ESP's Access Point.
    I wrote above that it connects to the home LAN as a client, I'll check what is the case when it is a server.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Start with the example I posted and the home network. Assuming that it works, modify it to be based on the ad hoc network.
     
  9. derez

    derez Expert Licensed User

    Checked the example of ESP as server using home network - works.
    Modified it (the B4R code in post #1) with the B4A the same except IP and port - the situation is like I describe in the first post: B4A reports connected but in B4R it does not go to the NewConnection sub.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    It works properly here when I the example it on the WeMos with this code (also change the port to 80 in the B4A code):
    Code:
    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    If wifi.StartAccessPoint("esp"Then
         
    server.Initialize(80"server_NewConnection")
         
    server.Listen
         
    Log("Waiting for connection.")
       
    Else
         
    Log("Failed to connect to Wifi.")
       
    End If
       timer1.Initialize(
    "timer1_Tick"1000)
       pin.Initialize(d1.D6, pin.MODE_INPUT)
    End Sub

    Have you tested it with a different port?
     
  11. derez

    derez Expert Licensed User

    It does not work, also when I change port.
    I also tried to use the phone as server on the ESP access point (found using b4a-bridge that the IP is 192.168.4.2) and the ESP as a client but it did not connect...
    May be the NodeMcu is not the same as Wemos D1.
    I'll let it rest for a while, I'll use the NodeMcu as server on the home lan.

    upload_2016-8-23_22-18-36.png

    The car is run by Arduino connected to NodeMcu which gets instructions fron a phone.
    Steering is by a stepper motor and there is MPU which enables the car to go either manually by the phone's control or by a plan in the memory which gives time and azimuth for each step.
    I have found it very easy to build the car from kapa board.
     
    Last edited: Aug 23, 2016
    Erel and inakigarm like this.
  12. derez

    derez Expert Licensed User

    After receiving few WEMOS from Aliexpress I checked again the behavior with Access Point.
    I have succeeded (once after many trials) to connect B4A device to the Wemos on its AP but only when the B4A device is the server.
    When Wemos is server - it refuses to show the server connection even though the B4A device is connecting successfully.
    I don't understand why, maybe the chineese copies are different (I already noted that the pins are not the same).
    If others had succeeded in connecting Wemos or Nodemcu as server on its own AP please tell me.

    Note: the Wemos must be connected to power in addition to the USB, otherwise the signal is weak.
    I used a laptop on the Wemos network and B4A-bridge connection to the B4A device. It works but with many disconnections of the bridge.
     
    Last edited: Oct 4, 2016
  13. derez

    derez Expert Licensed User

  14. derez

    derez Expert Licensed User

    Today I was lucky and got B4a and ESP connected on AP !
    I don't know why, it the same code, port 80, maybe another board...

    Edit:
    Now I understand it - the problem is that I used a laptop to work with the phone on the ESP network. That inhibits the connection to the server. When I use the phone without debugging (only the phone and the ESP) - it works !
     
    Last edited: Nov 24, 2016
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