Basic Wifi Communication with the ESP8266?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
synimak
Posts: 20
Joined: Sun Feb 28, 2016 5:11 am
Contact:

Basic Wifi Communication with the ESP8266?

Post by synimak » Sun Sep 04, 2016 9:48 am

I am self taught in the programing world so forgive my lack of knowledge. Much better at hardware. I just bought a Adafruit Feather with the ESP8266 and am struggling to work out how to communicate with it over Wifi. For ages I have been using an app called "cmd" on my iphone and python telnetlib on my laptop to connect to xbee RN-XV modules. Basic robot driving and relay control etc.

I have loaded micropython, have the webrepl working and love it!

But now I'm stuck. Been searching for tutorials and information but am going round in circles. At this stage I'm not interested in connecting to web sites or anything like that. Just basic communication between my laptop or iPhone.

Any help would be great :D

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Basic Wifi Communication with the ESP8266?

Post by deshipu » Sun Sep 04, 2016 12:27 pm

If you have webrepl working, then you already have basic communication. Not sure what it is exactly that you need?

quantalume
Posts: 15
Joined: Thu Aug 04, 2016 3:04 pm

Re: Basic Wifi Communication with the ESP8266?

Post by quantalume » Sun Sep 04, 2016 4:15 pm

Why don't you start out with sockets? That's about as basic as you can get for TCP/IP communication. Download some software for your laptop like SocketTest (http://sockettest.sourceforge.net/), open a socket and try sending some text back and forth. There are some examples here http://docs.micropython.org/en/latest/e ... k_tcp.html and here http://hackaday.com/2016/07/21/micropyt ... the-tires/. Make sure you don't have any firewall issues on your laptop; you may need to initiate the connection from your laptop and use socket.listen() on the ESP side.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Basic Wifi Communication with the ESP8266?

Post by pythoncoder » Mon Sep 05, 2016 7:32 am

Peter Hinch
Index to my micropython libraries.

synimak
Posts: 20
Joined: Sun Feb 28, 2016 5:11 am
Contact:

Re: Basic Wifi Communication with the ESP8266?

Post by synimak » Mon Sep 05, 2016 8:06 am

Cheers thanks all.

The TCP socket connection is where I'm heading. (Still learning, getting my head around it all :? ) The app I've been using just requires an IP and port and it connect.

Could someone give me an example of what code to write to get the ESP to wait for and incoming connection then once connected "do what ever" ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Basic Wifi Communication with the ESP8266?

Post by pythoncoder » Mon Sep 05, 2016 8:40 am

Assuming you're using it in station mode something along these lines should work.

Code: Select all

import network
import utime

use_default = True
ssid = 'YourSSID'
pw = 'YourPassword'

sta_if = network.WLAN(network.STA_IF)

if use_default:
    secs = 5
    while secs >= 0 and not sta_if.isconnected():
        utime.sleep(1)
        secs -= 1

# If can't use default, use specified LAN
if not sta_if.isconnected():
    sta_if.active(True)
    sta_if.connect(ssid, pw)
    while not sta_if.isconnected():
        utime.sleep(1)
Explanation: the ESP8266 stores connection information so that it can reconnect to the same network without needing to explicitly enter SSID and password. This should be used where possible, because connecting to a specific network causes this information to be saved, which causes wear on the flash memory. So this code first tries to connect to the saved network. If this fails, it connects using specified parameters. On the first connection you'll need to set use_default False - or just let it fail, time out, and connect to the specified network.
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Basic Wifi Communication with the ESP8266?

Post by SpotlightKid » Mon Sep 05, 2016 10:04 am

I think synimak meant a TCP connection, not a Wifi connection.

Have a look at the HTTP server example in the esp8266 tutorial quantalume linked above. It doesn't get much simpler than that:

* Create a socket.
* bind() to an address.
* listen() on the socket to wait for connections.
* accept() the connection and read() from it.

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

Re: Basic Wifi Communication with the ESP8266?

Post by jms » Mon Sep 05, 2016 10:11 am

Learn network programming with sockets (the socket module) using normal machines not the ESP8266 which has many quirks.

Jon

synimak
Posts: 20
Joined: Sun Feb 28, 2016 5:11 am
Contact:

Re: Basic Wifi Communication with the ESP8266?

Post by synimak » Wed Sep 07, 2016 9:31 am

Thank you everyone. Did some socket tutorials and i have it all working cheers :D

Shivank
Posts: 1
Joined: Thu Mar 16, 2017 10:32 am

Re: Basic Wifi Communication with the ESP8266?

Post by Shivank » Thu Mar 16, 2017 10:40 am

Hi synimak,
Please could you guide me how to communicate esp and laptop via wifi, without webserver or connecting to internet.
I cant get any help from internet. I want just simple communication b/w my window laptop and esp-12e over wifi.

Regards,
Shivank

Thanks

Post Reply