B4R Question Connect Android and Esp8266 (NodeMcu) - Solved

derez

Expert
Licensed User
Longtime User
:mad::mad::mad:
I try to connect NodeMcu and Android.
The NodeMcu code as server ( with Cableguy's example):
B4X:
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:
B4X:
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:
AppStart
Access Point Initialisation Successful
SSID: esp
ip: 192.168.4.1

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:

derez

Expert
Licensed User
Longtime 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.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Thanks, that is what I use.

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

derez

Expert
Licensed User
Longtime 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.
 
Upvote 0

derez

Expert
Licensed User
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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):
B4X:
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?
 
Upvote 0

derez

Expert
Licensed User
Longtime 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:
Upvote 0

derez

Expert
Licensed User
Longtime 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:
Upvote 0

derez

Expert
Licensed User
Longtime 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:
Upvote 0
Top