Skip to content

ESP32 as wifi co processor with sockets & requests library #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Feb 11, 2019

Conversation

ladyada
Copy link
Member

@ladyada ladyada commented Feb 9, 2019

ready for consumption!

@ladyada ladyada requested a review from a team February 9, 2019 05:43
@jerryneedell
Copy link
Contributor

@ladyada Where can we get the SPI WiFi firmware for the ESP32?

@ladyada
Copy link
Member Author

ladyada commented Feb 9, 2019

@jerryneedell
Copy link
Contributor

jerryneedell commented Feb 9, 2019

Can this be used with a feather nrf52840 express or feather m4 express and an external ESP32 or dos it have to be used with a NINA-102 module? If so ,
what pins on the ESP32 are used for the interface?
MISO
MOSI
SCK
CS
BUSY
RESET
GPIO0

@jerryneedell
Copy link
Contributor

It's not clear how I can try this out with out the pyportal board or without a way to generate the ESP32 firmware for the WROOOM board. If you have any suggestions, please let me know and I'll be happy to try.

@jerryneedell
Copy link
Contributor

I am testing this on a feather_nrf52840 connected to an ESPRESSIF ESP32 devkit (WROOM) board.
Off to a good start.
A few first observations.
I am able to get it to communicate and connect to my network.

I tried the simpletest and get this error on the Ping

IP lookup adafruit.com: 104.20.38.240
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "esp32spi_nrf.py", line 32, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 469, in ping
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 295, in _send_command_get_response
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 183, in _send_command
TypeError: object of type 'int' has no len()
>>> 

I commented out the ping and it passes the other tests:



Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-beta.2-23-g72f0783b6 on 2019-02-07; Adafruit Feather nRF52840 Express with nRF52840
>>> 
>>> import esp32spi_nrf
ESP32 SPI webclient test
ESP32 found and in idle mode
Firmware vers. bytearray(b'1.2.1\x00')
MAC addr: ['0x84', '0xb6', '0xdf', '0xa4', '0xae', '0x30']
SSID names: [bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'xfinitywifi'), bytearray(b' A31A')]
APs:  [{'ssid': bytearray(b'Needell Airport'), 'rssi': -35, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -58, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -72, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -72, 'encryption': 4}, {'ssid': bytearray(b'xfinitywifi'), 'rssi': -80, 'encryption': 7}, {'ssid': bytearray(b' A31A'), 'rssi': -90, 'encryption': 4}]
Connecting to AP...
Connected to bytearray(b'Needell Airport') RSSI -34
My IP address is 10.0.0.72
IP lookup adafruit.com: 104.20.38.240
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
This is a test of the CC3000 module!
If you can read this, its working :)
Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json
{'time': {'updated': 'Feb 10, 2019 13:08:00 UTC', 'updatedISO': '2019-02-10T13:08:00+00:00', 'updateduk': 'Feb 10, 2019 at 13:08 GMT'}, 'disclaimer': 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'description': 'United States Dollar', 'rate_float': 3621.382, 'rate': '3,621.3817'}}}
Done!

For some reason - every other time I try to connect, it has trouble finding my network, but if I reboot and try again it works.
This appears to be repeatable... always works on the second try.

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-beta.2-23-g72f0783b6 on 2019-02-07; Adafruit Feather nRF52840 Express with nRF52840
>>> 
>>> 
>>> import esp32spi_nrf
ESP32 SPI webclient test
ESP32 found and in idle mode
Firmware vers. bytearray(b'1.2.1\x00')
MAC addr: ['0x84', '0xb6', '0xdf', '0xa4', '0xae', '0x30']
SSID names: [bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'Needell Airport'), bytearray(b'xfinitywifi'), bytearray(b'xfinitywifi'), bytearray(b'Jquighome'), bytearray(b' A31A')]
APs:  [{'ssid': bytearray(b'Needell Airport'), 'rssi': -36, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -57, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -71, 'encryption': 4}, {'ssid': bytearray(b'Needell Airport'), 'rssi': -76, 'encryption': 4}, {'ssid': bytearray(b'xfinitywifi'), 'rssi': -83, 'encryption': 7}, {'ssid': bytearray(b'xfinitywifi'), 'rssi': -86, 'encryption': 7}, {'ssid': bytearray(b'Jquighome'), 'rssi': -87, 'encryption': 4}, {'ssid': bytearray(b' A31A'), 'rssi': -88, 'encryption': 4}]
Connecting to AP...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "esp32spi_nrf.py", line 28, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 437, in connect_AP
RuntimeError: ('No such ssid', b'Needell Airport')
>>> 

@jerryneedell
Copy link
Contributor

jerryneedell commented Feb 10, 2019

the ping fails because the "params" sent is a 4 integer bytearray - returned by get_host_by_name and when the array is enumerated each value is an integer so the len(param) fails in send_command.

there were recent changes at line 183 (send_command)
was something changed to break ping?

@jerryneedell
Copy link
Contributor

regarding the connect failures, it is repeatable -- If it is connected then reset, the next attempt to connect fails. Attempting to connect again passes...

>>> esp.connect_AP(b'Needell Airport', b'mypass')
3
>>> esp.connect_AP(b'Needell Airport', b'mypass')
3
>>> esp.reset()
>>> esp.connect_AP(b'Needell Airport', b'mypass')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 437, in connect_AP
RuntimeError: ('No such ssid', b'Needell Airport')
>>> esp.connect_AP(b'Needell Airport', b'mypass')
3
>>> esp.connect_AP(b'Needell Airport', b'mypass')
3
>>> esp.reset()
>>> esp.connect_AP(b'Needell Airport', b'mypass')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 437, in connect_AP
RuntimeError: ('No such ssid', b'Needell Airport')
>>> esp.connect_AP(b'Needell Airport', b'mypass')
3
>>> 

@jerryneedell
Copy link
Contributor

cool! cheerlights works!

Fetching json from http://api.thingspeak.com/channels/1417/feeds.json?results=1
*** Get host by name
*** Get socket
Allocated socket #0
*** Socket connect mode 0
*** Open socket
Writing: b'GET /channels/1417/feeds.json?results=1 HTTP/1.0\r\n'
Writing: b'Host: api.thingspeak.com\r\n'
Writing: b'User-Agent: Adafruit CircuitPython\r\n'
Writing: b'\r\n'
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 0 bytes available
ESPSocket: 1236 bytes available
Reading 1236 bytes from ESP socket with status 4
ESPSocket: 0 bytes available
{'channel': {'field1': 'Last CheerLights Command', 'last_entry_id': 532346, 'longitude': '-80.22', 'updated_at': '2018-07-20T01:05:15Z', 'name': 'CheerLights', 'created_at': '2011-12-01T03:30:04Z', 'id': 1417, 'field2': 'CheerLights HEX Color', 'latitude': '40.5', 'description': 'CheerLights is an \u201cInternet of Things\u201d project created by Hans Scharler that allows people\u2019s lights all across the world to synchronize to one color set by Twitter. This is a way to connect physical things with social networking experiences.'}, 'feeds': [{'created_at': '2019-02-10T17:01:03Z', 'field2': '#FFC0CB', 'field1': 'pink', 'entry_id': 532346}]}
[{'created_at': '2019-02-10T17:01:03Z', 'field2': '#FFC0CB', 'field1': 'pink', 'entry_id': 532346}]
{'created_at': '2019-02-10T17:01:03Z', 'field2': '#FFC0CB', 'field1': 'pink', 'entry_id': 532346}
#FFC0CB
Done!

@ladyada
Copy link
Member Author

ladyada commented Feb 11, 2019

latest commit removes need for GPIO0 and also fixes ping

@jerryneedell
Copy link
Contributor

I don't get errors from ping, but I also don't get responses...

Ping google.com: 65535 ms
Ping google.com: 65535 ms
Ping google.com: 65535 ms

@jerryneedell
Copy link
Contributor

jerryneedell commented Feb 11, 2019

hmmm -- nevermind

Ping adafruit.com: 20 ms
Ping google.com: 30 ms

used the updated example -- not sure what changed, but it works now ...

@jerryneedell
Copy link
Contributor

FYI -- I started using this to workaround the failed connects -- seems to work well:

while not esp.is_connected:
    try:
        print("Connecting to AP...")
        esp.connect_AP(b'Needell Airport', b'browndog')
    except (ValueError, RuntimeError) as e:
        print("Failed to connect, retrying\n", e)
        continue

@ladyada
Copy link
Member Author

ladyada commented Feb 11, 2019

@jerryneedell kk you feel this is ok to merge/release for now then?

@jerryneedell
Copy link
Contributor

Yes. Go for it! I’ve done some posts to AIO and have been running cheer lights for hours.

@ladyada ladyada merged commit b671a80 into adafruit:master Feb 11, 2019
jadudm added a commit to jadudm/Adafruit_CircuitPython_ESP32SPI that referenced this pull request Jul 14, 2019
This is a complete round trip. At least... for pinMode and digitalWrite.

However, I'm now sending a command and getting the correct response.

Wrote:  ['0xe0', '0x51', '0x2', '0x1', '0xd', '0x1', '0x0', '0xee']
                Read: 0xe0
                Read: 0xd1
                Read: 0x1
                Read: 0x1
        Parameter #0 length is 1
                Read: ['0x1']
                Read: 0xee
Read 1:  [bytearray(b'\x01')]
        Sending param #0 is 1 bytes long
        Sending param adafruit#1 is 1 bytes long
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants