Skip to content

update to use adafruit_requests #31

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 8 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions adafruit_espatcontrol/adafruit_espatcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ class ESP_ATcontrol:
MODE_SOFTAP = 2
MODE_SOFTAPSTATION = 3
TYPE_TCP = "TCP"
TCP_MODE = "TCP"
TYPE_UDP = "UDP"
TYPE_SSL = "SSL"
TLS_MODE = "SSL"
STATUS_APCONNECTED = 2
STATUS_SOCKETOPEN = 3
STATUS_SOCKETCLOSED = 4
Expand Down
189 changes: 0 additions & 189 deletions adafruit_espatcontrol/adafruit_espatcontrol_requests.py

This file was deleted.

15 changes: 11 additions & 4 deletions adafruit_espatcontrol/adafruit_espatcontrol_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
if type != SOCK_STREAM:
raise RuntimeError("Only SOCK_STREAM type supported")
self._buffer = b''
self.settimeout(0)

def connect(self, address, conntype=None):
"""Connect the socket to the 'address' (which should be dotted quad IP). 'conntype'
Expand All @@ -40,7 +41,8 @@ def connect(self, address, conntype=None):
raise RuntimeError("Failed to connect to host", host)
self._buffer = b''

def write(self, data): # pylint: disable=no-self-use

def send(self, data): # pylint: disable=no-self-use
"""Send some data to the socket"""
_the_interface.socket_send(data)

Expand All @@ -53,12 +55,12 @@ def readline(self):
firstline, self._buffer = self._buffer.split(b'\r\n', 1)
return firstline

def read(self, num=0):
def recv(self, num=0):
"""Read up to 'num' bytes from the socket, this may be buffered internally!
If 'num' isnt specified, return everything in the buffer."""
if num == 0:
# read as much as we can
ret = self._buffer + _the_interface.socket_receive(timeout=1)
ret = self._buffer + _the_interface.socket_receive(timeout=self._timeout)
self._buffer = b''
else:
ret = self._buffer[:num]
Expand All @@ -68,6 +70,11 @@ def read(self, num=0):
def close(self):
"""Close the socket, after reading whatever remains"""
# read whatever's left
self._buffer = self._buffer + _the_interface.socket_receive(timeout=1)
self._buffer = self._buffer + _the_interface.socket_receive(timeout=self._timeout)
_the_interface.socket_disconnect()

def settimeout(self, value):
"""Set the read timeout for sockets, if value is 0 it will block"""
self._timeout = value

# pylint: enable=unused-argument, redefined-builtin, invalid-name
5 changes: 3 additions & 2 deletions adafruit_espatcontrol/adafruit_espatcontrol_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

# pylint: disable=no-name-in-module

import adafruit_espatcontrol.adafruit_espatcontrol_requests as requests
import adafruit_espatcontrol.adafruit_espatcontrol_socket as socket
import adafruit_requests as requests

class ESPAT_WiFiManager:
"""
Expand All @@ -50,7 +51,7 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2):
self.debug = False
self.secrets = secrets
self.attempts = attempts
requests.set_interface(self._esp)
requests.set_socket(socket, esp)
self.statuspix = status_pixel
self.pixel_status(0)

Expand Down
3 changes: 0 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
.. automodule:: adafruit_espatcontrol.adafruit_espatcontrol
:members:

.. automodule:: adafruit_espatcontrol.adafruit_espatcontrol_requests
:members:

.. automodule:: adafruit_espatcontrol.adafruit_espatcontrol_socket
:members:
19 changes: 3 additions & 16 deletions examples/esp_atcontrol_aio_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
import board
import busio
from digitalio import DigitalInOut
from digitalio import Direction

# ESP32 AT
from adafruit_espatcontrol import adafruit_espatcontrol, adafruit_espatcontrol_wifimanager

#Use below for Most Boards
import neopixel
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
#Uncomment below for ItsyBitsy M4#
#import adafruit_dotstar as dotstar
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)


# Get wifi details and more from a secrets.py file
Expand All @@ -22,24 +17,16 @@
raise


# With a Metro or Feather M4
uart = busio.UART(board.TX, board.RX, timeout=0.1)
resetpin = DigitalInOut(board.D5)
rtspin = DigitalInOut(board.D6)

# With a Particle Argon
"""
RX = board.ESP_TX
TX = board.ESP_RX
resetpin = DigitalInOut(board.ESP_WIFI_EN)
rtspin = DigitalInOut(board.ESP_CTS)
uart = busio.UART(TX, RX, timeout=0.1)
esp_boot = DigitalInOut(board.ESP_BOOT_MODE)
from digitalio import Direction
esp_boot.direction = Direction.OUTPUT
esp_boot.value = True
"""

status_light = None

print("ESP AT commands")
esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200,
Expand All @@ -58,7 +45,7 @@
response = wifi.post(
"https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data",
json=payload,
headers={bytes("X-AIO-KEY", "utf-8"):bytes(secrets['aio_key'], "utf-8")})
headers={"X-AIO-KEY":secrets['aio_key']})
print(response.json())
response.close()
counter = counter + 1
Expand Down
16 changes: 2 additions & 14 deletions examples/esp_atcontrol_cheerlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import board
import busio
from digitalio import DigitalInOut
from digitalio import Direction

# ESP32 SPI
from adafruit_espatcontrol import adafruit_espatcontrol, adafruit_espatcontrol_wifimanager
Expand All @@ -19,30 +20,17 @@
raise


#Use below for Most Boards
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
#Uncomment below for ItsyBitsy M4
#import adafruit_dotstar as dotstar
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)

# With a Metro or Feather M4
uart = busio.UART(board.TX, board.RX, timeout=0.1)
resetpin = DigitalInOut(board.D5)
rtspin = DigitalInOut(board.D6)

# With a Particle Argon
"""
RX = board.ESP_TX
TX = board.ESP_RX
resetpin = DigitalInOut(board.ESP_WIFI_EN)
rtspin = DigitalInOut(board.ESP_CTS)
uart = busio.UART(TX, RX, timeout=0.1)
esp_boot = DigitalInOut(board.ESP_BOOT_MODE)
from digitalio import Direction
esp_boot.direction = Direction.OUTPUT
esp_boot.value = True
"""

status_light = None

print("ESP AT commands")
esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200,
Expand Down
Loading