Android Question Controlling the ESP8266 over wifi

Discussion in 'Android Questions' started by ardhuru, Apr 16, 2015.

  1. ardhuru

    ardhuru Member Licensed 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!
     
    JordiCP likes this.
  2. JordiCP

    JordiCP Well-Known Member Licensed User

    I think UDP is the more straightforward since you also have to define the ESP8266 part. If you are going to use LUA, @alexhi made this

    I have some ESP modules in a box waiting for time to play with them
     
    Cableguy and ardhuru like this.
  3. derez

    derez Expert Licensed User

    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:
    Code:
    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.
     
    ardhuru likes this.
  4. ardhuru

    ardhuru Member Licensed 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.
     
    Derek Johnson likes this.
  5. XorAndOr

    XorAndOr Active Member Licensed User

     

    Attached Files:

    spairo, ellpopeb4a, jparraga and 3 others like this.
  6. ardhuru

    ardhuru Member Licensed 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?
     
  7. XorAndOr

    XorAndOr Active Member Licensed 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.
     
  8. ardhuru

    ardhuru Member Licensed 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!
     
  9. XorAndOr

    XorAndOr Active Member Licensed 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 ...
     

    Attached Files:

    sme114, jparraga and ardhuru like this.
  10. ardhuru

    ardhuru Member Licensed 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 ardhuru@gmail.com

    Regards,

    Anand
     
    XorAndOr likes this.
  11. ardhuru

    ardhuru Member Licensed 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: Apr 21, 2015
  12. XorAndOr

    XorAndOr Active Member Licensed 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())
     
    jparraga and ardhuru like this.
  13. ardhuru

    ardhuru Member Licensed 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...
     
  14. XorAndOr

    XorAndOr Active Member Licensed 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....
     
    ardhuru likes this.
  15. ardhuru

    ardhuru Member Licensed 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
     
    XorAndOr likes this.
  16. XorAndOr

    XorAndOr Active Member Licensed User

    I'm happy but .... it is useful to study not only use copy and paste !!:)
     
  17. ardhuru

    ardhuru Member Licensed User

    Absolutely!!

    Right up top in my to-be-learnt-better list.
     
  18. ardhuru

    ardhuru Member Licensed 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: http://www.b4x.com/android/forum/threads/vbwep-1-0-library-wep.19623/

    Would it be the way to go?
     
  19. freedom2000

    freedom2000 Well-Known Member Licensed 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 !
     
    walterf25 likes this.
  20. XorAndOr

    XorAndOr Active Member Licensed 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:)
     
    freedom2000 likes this.
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