Basic Wifi Communication with the ESP8266?
Basic Wifi Communication with the ESP8266?
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
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
Re: Basic Wifi Communication with the ESP8266?
If you have webrepl working, then you already have basic communication. Not sure what it is exactly that you need?
-
- Posts: 15
- Joined: Thu Aug 04, 2016 3:04 pm
Re: Basic Wifi Communication with the ESP8266?
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Basic Wifi Communication with the ESP8266?
Another option is to look at MQTT http://forum.micropython.org/viewtopic.php?f=16&t=2239.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Basic Wifi Communication with the ESP8266?
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" ?
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" ?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Basic Wifi Communication with the ESP8266?
Assuming you're using it in station mode something along these lines should work.
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.
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)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Basic Wifi Communication with the ESP8266?
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.
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.
Re: Basic Wifi Communication with the ESP8266?
Learn network programming with sockets (the socket module) using normal machines not the ESP8266 which has many quirks.
Jon
Jon
Re: Basic Wifi Communication with the ESP8266?
Thank you everyone. Did some socket tutorials and i have it all working cheers
Re: Basic Wifi Communication with the ESP8266?
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
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