ESP8266 Micropython TCP Socket Simple Sample Code

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jgouy
Posts: 1
Joined: Mon Jan 30, 2017 7:00 pm

ESP8266 Micropython TCP Socket Simple Sample Code

Post by jgouy » Mon Jan 30, 2017 7:13 pm

Hi Team,
I am trying to program a simple socket connection between the ESP8266 board (Adafruit Feather HUZZAH with ESP8266), client side, to a Raspberry Pi 3, server side. Can you help me getting any directions to create this simple application?

Thanks a lot and very best regards,

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

Re: ESP8266 Micropython TCP Socket Simple Sample Code

Post by SpotlightKid » Wed Feb 01, 2017 10:08 pm

On the Raspberry Pi run a TCP server listening on port 8000 (lets assume its IP address visible to the ESP8266 is 192.168.4.2) in a terminal:

Code: Select all

$ netcat -l --tcp --local-port=8000
On the ESP8266 run the following to connect to the server and send the string "Hello World!":

Code: Select all

import socket
sock = socket.socket()
addrinfos = socket.getaddrinfo('192.168.4.2', 8000)
# (host and port to connect to are in 5th element of the first tuple in the addrinfos list
sock.connect(addrinfos[0][4])
sock.send("Hello World!")
sock.close()
The terminal output on the Raspberry Pi should now show "Hello World!".

For further information see https://docs.python.org/3/howto/sockets.html.

Post Reply