Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

WIFI Library for Feather Huzzah ESP8266
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

WIFI Library for Feather Huzzah ESP8266

by Robonerd on Thu May 25, 2017 10:01 pm

I am running CircuitPython on my Feather Huzzah ESP8266 and would like to attempt wifi communication but I can't figure where to begin. I look at GitHub and the API reference docs and did not find a module that looked like it addressed wifi. Can anyone point me in the right direction?

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Sun May 28, 2017 12:35 am

I just found:
>>> import esp
>>>
>>> # Connect to a WiFi network.
>>> esp.connect('YOUR WIFI SSID NAME', 'YOUR WIFI SSID PASSWORD')
>>>
>>> # Define function to print data received from socket.
>>> def socket_printer(socket, data):
>>> print(data)
>>>
>>> # Create a socket and setup the print function.
>>> soc = esp.socket()
>>> soc.onrecv(socket_printer)
>>>
>>> # Connect to adafruit.com at port 80.
>>> soc.connect(('207.58.139.247', 80))
>>>
>>> # Send a request for the wifi test page.
>>> soc.send('GET /testwifi/index.html HTTP/1.0\r\n\r\n')
37
>>> b'HTTP/1.1 200 OK\r\nDate: Tue, 12 May 2015 18:44:49 GMT\r\nServer: Apache\r\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Authorization, Referer, User-Agent\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Max-Age: 1728000\r\nAccept-Ranges: bytes\r\nX-Mod-Pagespeed: 1.9.32.3-4448\r\nVary: Accept-Encoding\r\nCache-Control: max-age=0, no-cache\r\nContent-Length: 74\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nThis is a test of the CC3000 module!\nIf you can read this, its working :)\n'

This was in Building and Running MicroPython on the ESP8266 by Tony D. If there is any other info out there let me know, thanks

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Mon May 29, 2017 11:57 pm

Just when I thought that I had look all through the docs I found the Quick Reference for the ESP8266 and it shows the network module for micropython but I do not find it in circuitpython. Still looking.

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by jerryn on Tue May 30, 2017 5:11 am

The network module behaves the same in both. If examples use the machine module then you must convert to digitalio or busio as needed but the network modules work there same.

edited - I misspoke -(mistyped) If you are using an esp8266 then CircuitPyhton will support the use of the machine module. It also allows you to use the "io" modes instead. Again = no impact on the network module. At least not for the basic examples. I have not used it very far.

jerryn
 
Posts: 1328
Joined: Sat Sep 14, 2013 9:05 am

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Tue May 30, 2017 6:31 pm

Thanks, I will give it try. A point of ignorance, if I am running circuitpython and I say
>>> from micropython import network
I get "cannot import name network"
Do I need to establish some kind of pathway before this will work, or am I just using the wrong syntax?

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by tannewt on Wed May 31, 2017 2:20 pm

Hi Robonerd,
Jerry is correct, the network access methods are currently the same between MicroPython and CircuitPython. That doesn't mean you need to import the module from micropython though. Simply do:

Code: Select all | TOGGLE FULL SIZE
import network


At some point I'd like to refactor the network modules in the same way as the *io modules. However, its low on my priority list.

I hope that helps. I'm glad you got it working. :-)

~Scott

tannewt
 
Posts: 1845
Joined: Thu Oct 06, 2016 8:48 pm

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Mon Jun 05, 2017 1:08 pm

Thanks for the help.

This morning I followed Tony D's MicroPython Basics: ESP8266 WebREPL and was able to make it all work. I don't know why I had so much difficulty finding the right documentation/tutorial but for a while I felt like I was in a death spiral. Now things are working, it is all so simple once you get pointed to the starting point.

In the 60's I was writing machine and assembler language programs and it was all magical and fun. After that, with the advent of high level languages, the magic was gone and programming was a tool for getting work done. Now, with the development of MicroPython and the direction that you are taking the Feather boards and CircuitPython, the fun is back and I am using it to introduce my grandchildren to building and coding machines. Thanks for all your hard work and inspiration that Lady Ada, et. al. are for the next generation.

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by tannewt on Mon Jun 05, 2017 2:18 pm

Sorry for the trouble finding the right docs. Its pretty early still and my focus has been on the hardware API rather than the network APIs. My intention is to do the same reorganization with the network APIs that I did with the hardware APIs after 1.0 when I'll be diversifying the boards that CircuitPython runs on. (I'm looking at the NRF52 and ESP32.)

I'm glad MicroPython and CircuitPython and have brought the fun back. I feel the same way. I spent six years doing software in the cloud and doing it on microcontrollers is way more fun. I love bare metal. Its gotten me looking at processor design and RISC-V.

Please keep the feedback coming, I want to continue to improve CircuitPython. In particular, I want to improve it for beginning programmers. Thanks!

tannewt
 
Posts: 1845
Joined: Thu Oct 06, 2016 8:48 pm

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Fri Jun 09, 2017 6:15 pm

Update:
I now have a socket server running on the Huzzah ESP8266 and a socket client running on my Mac. The Huzzah will serve as the comm node for my small bot with the M0 Express being the brain.

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by tannewt on Fri Jun 09, 2017 6:40 pm

Woohoo! Any other tips you can give for others trying to do it?

tannewt
 
Posts: 1845
Joined: Thu Oct 06, 2016 8:48 pm

Re: WIFI Library for Feather Huzzah ESP8266

by Robonerd on Sat Jun 10, 2017 12:41 pm

Screen Shot 2017-06-10 at 9.55.24 AM.png
Screen Shot 2017-06-10 at 9.55.24 AM.png (224.6 KiB) Viewed 700 times
I am a noob to every aspect of this project so asking if I have any tips is a little intimidating, so instead of tips let me just show what I have done.

I wanted to use my Feather Huzzah ESP8266 as the communications node for a robot and after fumbling around watching one of Tony D's tutorials, the Huzzah was talking thru my router. More fumbling around and I realized that I needed to use sockets to be able to pass data to and from the Huzzah. The socket research was overwhelming, too many methods, I needed a simple example that I could test, dissect and learn from. I found (https://shakeelosmani.wordpress.com/201 ... g-example/) an example of a simple server and a simple client written in Python 3. I copied the code and added as much documentation as my ignorance could produce. The server loaded and ran without issue on the Huzzah, the client is loaded on my Mac and first try it works. To me this is real testimony to how well Circuit Python is retaining the essence of Python 3. My current challenge is fully decoding the how the program was written. I have not figured out how to see the content of imported functions and until than I am using the Python 3 docs to better understand the methods and functions.

Sorry for the wonky image placement, this is my first attempt at posting code.
Attachments
Screen Shot 2017-06-10 at 9.48.08 AM.png
Screen Shot 2017-06-10 at 9.48.08 AM.png (157.51 KiB) Viewed 700 times

Robonerd
 
Posts: 40
Joined: Tue Apr 18, 2017 9:54 pm

Re: WIFI Library for Feather Huzzah ESP8266

by tannewt on Sun Jun 11, 2017 2:53 pm

Thats awesome that you could use a Python3 tutorial for it! I haven't done any network stuff different in CircuitPython so the credit for that should go to the MicroPython devs. :-)

Thanks for posting more info! You may think you are a newbie but you are now more knowledgeable about the network stuff than I am. That makes you the expert. :D

tannewt
 
Posts: 1845
Joined: Thu Oct 06, 2016 8:48 pm

Re: WIFI Library for Feather Huzzah ESP8266

by flexypepo on Sun Jul 16, 2017 3:32 pm

Thank you @Robonerd and @tannewt2 for the example code and explanation. I was able to understand the socket communication and used your example-code (socket_server/_client) to control a robot car via socket (client-Mac, server-Huzzah ESP8266). Robot-commands based on the bot.py code of @TonyDiCola.
Its not rocket science and work-in-progress.

Some extended micropython code, added to the socket-server:
Code: Select all | TOGGLE FULL SIZE
 
......
       # parse user command
        data = str(data).upper()
        if data == 'H':
            help(conn) # print available commands on client
        elif data == '+':
            speed += 500
            parse_command_echo(conn, 'speed={0}'.format(speed))
        elif data == '-':
            speed -= 500
            parse_command_echo(conn, 'speed={0}'.format(speed))
        elif data == '/':
            dt += 1
            parse_command_echo(conn, 'duration={0}'.format(dt))
        elif data == ';':
            dt -= 1
            parse_command_echo(conn, 'duration={0}'.format(dt))
        elif data == 'S':
            parse_command_echo(conn, 'robot: speed={0}, duration={1}\n'.format(speed, dt))
        else:
            parse_command_bot(robot, data, speed, dt)
            data = 'OK'
            parse_command_echo(conn, data)

    conn.close()
    print('Connection closed')

# Car command services
def parse_command_bot(robot, data, speed, dt):
    '''parse client-commands to robot-actions'''
    if data == 'F':
        robot.forward(speed, dt) #turn left
    if data == 'B':
        robot.backward(speed, dt) #backwards
    if data == 'L':
        robot.left(speed, dt) # turn right
    if data == 'R':
        robot.right(speed, dt) # turn right

# default ECHO-service
def parse_command_echo(conn, data):
    print('parse_command_echo() - sending: ' + str(data) )
    conn.send(data.encode())
........

Hope this might help someone ;-)
Huzzah ESP8266 with micropython 1.91 and PWM servo/motor wing.

Next step is to figure out how to reopen the socket communication after it was closed (command 'q').

flexypepo
 
Posts: 14
Joined: Sun Jul 16, 2017 3:14 pm

Re: WIFI Library for Feather Huzzah ESP8266

by tannewt on Sun Jul 16, 2017 7:13 pm

Awesome @flexypepo! Thanks for checking in!

tannewt
 
Posts: 1845
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.


cron