Android Question Controlling the ESP8266 over wifi

ardhuru

Member
Licensed User
Longtime User
Please bear with me, I am a complete novice with B4A.

A few months back, after lookimg at a lot of samples, I designed an app that shows a numeric keypad, and using asyncstreams over bluetooth, transmits a number corresponding to the key that was pressed.

Now, I would like to implement the same thing, but with Wifi. On the reception side, I'll probably use an ESP8266 serial to Wifi module. I feel fairly confident of writing the code for the ESP8266.

So, how do I go about it on the Android? For starters, what would be used in place of asyncstreams? I suspect it would be httputils, but I have absolutely no idea where to start.

I can probably make it happen using a browser, but I'd like to create an app that has my custom buttons, and an interface designed in the abstract designer.

Any pointers would be highly appreciated!
 

derez

Expert
Licensed User
Longtime User
what would be used in place of asyncstreams?
You can use the same code that operated with asyncstream over bluetooth, using wifi as the connection means.
For example, I have an application to transfer data between two devices. After connection has been established the code looks like this:
B4X:
If AStream.IsInitialized = False Then
    Select Common.channelflag
        Case "BT"
            AStream.InitializePrefix(BT.serial1.InputStream,False,  BT.serial1.OutputStream, "AStream")
        Case "WiFi"
            AStream.InitializePrefix(WiFi.socket1.InputStream,False, WiFi.socket1.OutputStream, "AStream")
        Case "WiFiDir"
            AStream.InitializePrefix(WFDService.socket1.InputStream,False, WFDService.socket1.OutputStream, "AStream")
    End Select
End If

The flag say what connection is working. After that you work with Astream without caring what is the connection's media.
Remember that you'll have to use AStream.Initialize, not Prefix.
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
JordiCP, thanks for that pointer. In fact this is very similar to what I'd like to achieve. Unfortunately, Alex had only released the app, not the code, so I'm unclear as to how he achieved it. He has released the code for the ESP, written in LUA. I too tried LUA, but since then there's been a development. People have now written plugins to the Arduino IDE to be able to directly program the ESP8266 in Arduino fashion, without invoving an actual Arduino. I feel this opens up immense possibilities, and is much simpler to use than LUA, or the stock AT commands.

Derez, thank you so much! Didnt realize it would be that straight-forward! Are there any examples demonstrating this? For example, where do I define the ip address of the target ESP module? Am very excited with this suggestion, hope to have a working code soon.

Thanks again, guys.
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
JordiCP, thanks for that pointer. In fact this is very similar to what I'd like to achieve. Unfortunately, Alex had only released the app, not the code, so I'm unclear as to how he achieved it. He has released the code for the ESP, written in LUA. I too tried LUA, but since then there's been a development. People have now written plugins to the Arduino IDE to be able to directly program the ESP8266 in Arduino fashion, without invoving an actual Arduino. I feel this opens up immense possibilities, and is much simpler to use than LUA, or the stock AT commands.

Derez, thank you so much! Didnt realize it would be that straight-forward! Are there any examples demonstrating this? For example, where do I define the ip address of the target ESP module? Am very excited with this suggestion, hope to have a working code soon.

Thanks again, guys.
 

Attachments

  • B4A + LUA ESP8266 + PIC16F876A Mikrobasic Usart.zip
    416 KB · Views: 1,334
Upvote 0

ardhuru

Member
Licensed User
Longtime User
XorAndOr, thank you very much for sharing your code, I really appreciate it.

This makes things a lot clearer.

As per my understanding, your app can

1) Control the 2 IO pins on the ESP8266 running LUA (independant of the 16F876A)

2) Control6 IO pins on the 16F876A (running Mikrobasic), with serial signals received from the ESP8266.

3) MoiIP is the Androi's IP and IP Server is the IP of the ESP8266, acting as a server (soft AP, router not in the picture).

Is this correct?
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
Hi, All correct, the attached example was scheduled for a module esp8266 as an access point, I just needed the part of the pic16F876A, the functions of the module to drive the GPIO GPIO 0 E 2 are only tests. If you want it as a (station) you have to change the parameters in the file lua. If you need the code for the pic I'll attach. excuse my english.
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
Yes please, I would absolutely love to have that. Actually, would you mind sharing the LUA code as well?

Thanks!

BTW, your English is way better than my Italian!
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
hello, I am attaching the code lua (station) and (access point), I think you know how to send it to esp8266, I use the firmware nodemcu and esplorer to program in lua.
you have the hex code for the pic, if you need the source pic code, give me the email that you send it. The code B4A perhaps not the best for communication between B4A and esp8266 but there are several ways, I currently I am fine with the socket B4A, although at the moment I am experiencing other ways of connection, such as udp, MQTT etc ...
 

Attachments

  • Code Lua hex.zip
    3.8 KB · Views: 593
Upvote 0

ardhuru

Member
Licensed User
Longtime User
Wow, works like a charm! Absolutely flawless.

I have been struggling with this for a long time, and within hours of posting on this forum, I get such an elegant, complete solution.

Cant thank you enough.

And yes, I'd love to have a look at the pic source code as well; my email is [email protected]

Regards,

Anand
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
Hello, I'm back for some more advice.

I used XorAndOr's B4A code successfully with both versions of the ESP (mode station, and soft AP).

My application is a keypad that would send characters to the ESP8266.

Now, my observations are as follows;

1) ESP in station mode: both the ESP and the Android connect to my router, works well, no issues. Potential problem: the ESP could get assigned a different IP by the router, and then, of course, the setup wopuldnt work.

2) ESP in soft AP mode: ESP has its own SSID, password and an apparently fixed IP. Android connects directly to the ESP module, router nowhere in the picture. Works well by itself. BUT, the android now keeps connecting to the ESP module, even when the task is done. Will not connect to the router, unless the ESP module is shut off.

So, my qyuestion is, how would it be best to proceed? Can the B4A app be modified to connect to the soft AP (case 2), do its job, and then somehow 'forget' that hot-spot, so that any further use of wifi would connect the Android to the router, instead of the ESP soft AP?

Or, somehow assign a fixed IP (not sure whether it is possible) to the ESP module in the station mode (case 1)?
Whew, I hope I have conveyed my thoughts clearly enough in this post.

Regards,

Anand
 
Last edited:
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
hello, if you want that mode (station) your router does not change the ip dell'esp8266 every time you turn off and turn on the module, you must assign a static IP to your router, then enters the setup in router and assign an ip, then the file lua enter the ip that you set on the router. As for the access point, is a little more complex, because the module acts as server then you are connected to him and then he goes on when you turn on automatically. If you're good with B4A you can write an app that disconnects from the module esp8266 and connects to your router. I wrote an app much more complex that makes many more features than I have attached as an example, but for copyright reasons I can not attach. Studied a bit of code that B4A get there you too to the solution.

File Lua:

--IP STATIC STATION (YOUR ROUTER DON'T CAN CHANGE THE IP)

wifi.setmode(wifi.STATION)
wifi.sta.config("name","pass")
cfg =
{
ip="192.168.1.50", --<<<<<<<<<<< CHANGE = router
netmask="255.255.255.0",
gateway="192.168.1.90" --<<<<<<<<<<< CHANGE = router
}
wifi.sta.setip(cfg)
print(wifi.sta.getip())
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
Thanks for the response.

I'm not sure I completely understand;

The code you listed in the last post would always assign the same IP to the module, in the station mode?

Sorry for being so dense...
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
hello, The router can sometimes change the ip module, with this code you tell the router not to change ip !! ;). and always use this. Go into the setup menu of your router in DHCP and set a new ip for esp8266 after write them on row lua (ip, submask, gateway). Reboot the router, and the module will always have the same esp8266 ip.You need to know as you set your router to assign a new ip.:D Regards....
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
Thanks a ton, XorAndOr!

That worked a treat. Just pasted those lines in the existing code, and the module gets assigned the same IP every time!

Best Regards,

Anand
 
Upvote 0

ardhuru

Member
Licensed User
Longtime User
"As for the access point, is a little more complex, because the module acts as server then you are connected to him and then he goes on when you turn on automatically. If you're good with B4A you can write an app that disconnects from the module esp8266 and connects to your router."

I'm desperately trying to achieve exactly this, but I'm afraid I made no headway.

This is the closest solution I found: https://www.b4x.com/android/forum/threads/vbwep-1-0-library-wep.19623/

Would it be the way to go?
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi Ardhuru and XorAndOr,

I am as well playing a lot with B4A, PICs and ESP8266 under ATcommand and NodeMCU
I have also published the MQTT free library for B4A
This lib is using TCP sockets to connect and can work with ESP8266 under nodeMCU which has also the MQTT lib. Try it it's fun

So I subscribe to this thread !
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
hi Ardhuru
sorry but I have not tried this lib, I do not know exactly what to do. I do not know exactly what use do in your project esp8266 + Android, but the module esp has different types of connections, if you want to use Android + router + esp8266 then you must use the solution lua (station). If you want to use Android + esp No router, then follow the solution lua (Access Point). If we explain which use other than that I have listed then both I and other people in the forum will try to give you a hand.

hi freedom2000, welcome, I got to try your lib, great job:)
 
Upvote 0
Top