29
29
import struct
30
30
import time
31
31
from micropython import const
32
- from digitalio import Direction
33
32
from adafruit_bus_device .spi_device import SPIDevice
33
+ from digitalio import Direction
34
34
35
35
__version__ = "0.0.0-auto.0"
36
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI.git"
37
37
38
38
_SET_NET_CMD = const (0x10 )
39
39
_SET_PASSPHRASE_CMD = const (0x11 )
40
+ _SET_IP_CONFIG = const (0x14 )
41
+ _SET_DNS_CONFIG = const (0x15 )
42
+ _SET_HOSTNAME = const (0x16 )
40
43
_SET_AP_NET_CMD = const (0x18 )
41
44
_SET_AP_PASSPHRASE_CMD = const (0x19 )
42
45
_SET_DEBUG_CMD = const (0x1A )
67
70
_START_SCAN_NETWORKS = const (0x36 )
68
71
_GET_FW_VERSION_CMD = const (0x37 )
69
72
_SEND_UDP_DATA_CMD = const (0x39 )
73
+ _GET_REMOTE_DATA_CMD = const (0x3A )
70
74
_GET_TIME = const (0x3B )
71
75
_GET_IDX_BSSID_CMD = const (0x3C )
72
76
_GET_IDX_CHAN_CMD = const (0x3D )
124
128
ADC_ATTEN_DB_6 = const (2 )
125
129
ADC_ATTEN_DB_11 = const (3 )
126
130
131
+ # pylint: disable=too-many-lines
132
+
127
133
128
134
class ESP_SPIcontrol : # pylint: disable=too-many-public-methods, too-many-instance-attributes
129
135
"""A class that will talk to an ESP32 module programmed with special firmware
@@ -405,6 +411,46 @@ def scan_networks(self):
405
411
return APs
406
412
return None
407
413
414
+ def set_ip_config (self , ip_address , gateway , mask = "255.255.255.0" ):
415
+ """Tells the ESP32 to set ip, gateway and network mask b"\xFF "
416
+
417
+ :param str ip_address: IP address (as a string).
418
+ :param str gateway: Gateway (as a string).
419
+ :param str mask: Mask, defaults to 255.255.255.0 (as a string).
420
+ """
421
+ resp = self ._send_command_get_response (
422
+ _SET_IP_CONFIG ,
423
+ params = [
424
+ b"\x00 " ,
425
+ self .unpretty_ip (ip_address ),
426
+ self .unpretty_ip (gateway ),
427
+ self .unpretty_ip (mask ),
428
+ ],
429
+ sent_param_len_16 = False ,
430
+ )
431
+ return resp
432
+
433
+ def set_dns_config (self , dns1 , dns2 ):
434
+ """Tells the ESP32 to set DNS
435
+
436
+ :param str dns1: DNS server 1 IP as a string.
437
+ :param str dns2: DNS server 2 IP as a string.
438
+ """
439
+ resp = self ._send_command_get_response (
440
+ _SET_DNS_CONFIG , [b"\x00 " , self .unpretty_ip (dns1 ), self .unpretty_ip (dns2 )]
441
+ )
442
+ if resp [0 ][0 ] != 1 :
443
+ raise RuntimeError ("Failed to set dns with esp32" )
444
+
445
+ def set_hostname (self , hostname ):
446
+ """Tells the ESP32 to set hostname for DHCP.
447
+
448
+ :param str hostname: The new host name.
449
+ """
450
+ resp = self ._send_command_get_response (_SET_HOSTNAME , [hostname .encode ()])
451
+ if resp [0 ][0 ] != 1 :
452
+ raise RuntimeError ("Failed to set hostname with esp32" )
453
+
408
454
def wifi_set_network (self , ssid ):
409
455
"""Tells the ESP32 to set the access point to the given ssid"""
410
456
resp = self ._send_command_get_response (_SET_NET_CMD , [ssid ])
@@ -518,8 +564,7 @@ def connect(self, secrets):
518
564
self .connect_AP (secrets ["ssid" ], secrets ["password" ])
519
565
520
566
def connect_AP (self , ssid , password , timeout_s = 10 ): # pylint: disable=invalid-name
521
- """
522
- Connect to an access point with given name and password.
567
+ """Connect to an access point with given name and password.
523
568
Will wait until specified timeout seconds and return on success
524
569
or raise an exception on failure.
525
570
@@ -552,8 +597,7 @@ def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-n
552
597
def create_AP (
553
598
self , ssid , password , channel = 1 , timeout = 10
554
599
): # pylint: disable=invalid-name
555
- """
556
- Create an access point with the given name, password, and channel.
600
+ """Create an access point with the given name, password, and channel.
557
601
Will wait until specified timeout seconds and return on success
558
602
or raise an exception on failure.
559
603
@@ -806,6 +850,14 @@ def server_state(self, socket_num):
806
850
resp = self ._send_command_get_response (_GET_STATE_TCP_CMD , self ._socknum_ll )
807
851
return resp [0 ][0 ]
808
852
853
+ def get_remote_data (self , socket_num ):
854
+ """Get the IP address and port of the remote host"""
855
+ self ._socknum_ll [0 ][0 ] = socket_num
856
+ resp = self ._send_command_get_response (
857
+ _GET_REMOTE_DATA_CMD , self ._socknum_ll , reply_params = 2
858
+ )
859
+ return {"ip_addr" : resp [0 ], "port" : struct .unpack ("<H" , resp [1 ])[0 ]}
860
+
809
861
def set_esp_debug (self , enabled ):
810
862
"""Enable/disable debug mode on the ESP32. Debug messages will be
811
863
written to the ESP32's UART."""
@@ -814,8 +866,7 @@ def set_esp_debug(self, enabled):
814
866
raise RuntimeError ("Failed to set debug mode" )
815
867
816
868
def set_pin_mode (self , pin , mode ):
817
- """
818
- Set the io mode for a GPIO pin.
869
+ """Set the io mode for a GPIO pin.
819
870
820
871
:param int pin: ESP32 GPIO pin to set.
821
872
:param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
@@ -831,8 +882,7 @@ def set_pin_mode(self, pin, mode):
831
882
raise RuntimeError ("Failed to set pin mode" )
832
883
833
884
def set_digital_write (self , pin , value ):
834
- """
835
- Set the digital output value of pin.
885
+ """Set the digital output value of pin.
836
886
837
887
:param int pin: ESP32 GPIO pin to write to.
838
888
:param bool value: Value for the pin.
@@ -844,8 +894,7 @@ def set_digital_write(self, pin, value):
844
894
raise RuntimeError ("Failed to write to pin" )
845
895
846
896
def set_analog_write (self , pin , analog_value ):
847
- """
848
- Set the analog output value of pin, using PWM.
897
+ """Set the analog output value of pin, using PWM.
849
898
850
899
:param int pin: ESP32 GPIO pin to write to.
851
900
:param float value: 0=off 1.0=full on
@@ -858,8 +907,7 @@ def set_analog_write(self, pin, analog_value):
858
907
raise RuntimeError ("Failed to write to pin" )
859
908
860
909
def set_digital_read (self , pin ):
861
- """
862
- Get the digital input value of pin. Returns the boolean value of the pin.
910
+ """Get the digital input value of pin. Returns the boolean value of the pin.
863
911
864
912
:param int pin: ESP32 GPIO pin to read from.
865
913
"""
@@ -877,8 +925,7 @@ def set_digital_read(self, pin):
877
925
)
878
926
879
927
def set_analog_read (self , pin , atten = ADC_ATTEN_DB_11 ):
880
- """
881
- Get the analog input value of pin. Returns an int between 0 and 65536.
928
+ """Get the analog input value of pin. Returns an int between 0 and 65536.
882
929
883
930
:param int pin: ESP32 GPIO pin to read from.
884
931
:param int atten: attenuation constant
@@ -914,6 +961,7 @@ def get_time(self):
914
961
def set_certificate (self , client_certificate ):
915
962
"""Sets client certificate. Must be called
916
963
BEFORE a network connection is established.
964
+
917
965
:param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
918
966
"""
919
967
if self ._debug :
@@ -936,6 +984,7 @@ def set_certificate(self, client_certificate):
936
984
def set_private_key (self , private_key ):
937
985
"""Sets private key. Must be called
938
986
BEFORE a network connection is established.
987
+
939
988
:param str private_key: User-provided .PEM file up to 1700 bytes.
940
989
"""
941
990
if self ._debug :
0 commit comments