2929import struct
3030import time
3131from micropython import const
32- from digitalio import Direction
3332from adafruit_bus_device .spi_device import SPIDevice
33+ from digitalio import Direction
3434
3535__version__ = "0.0.0-auto.0"
3636__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI.git"
3737
3838_SET_NET_CMD = const (0x10 )
3939_SET_PASSPHRASE_CMD = const (0x11 )
40+ _SET_IP_CONFIG = const (0x14 )
41+ _SET_DNS_CONFIG = const (0x15 )
42+ _SET_HOSTNAME = const (0x16 )
4043_SET_AP_NET_CMD = const (0x18 )
4144_SET_AP_PASSPHRASE_CMD = const (0x19 )
4245_SET_DEBUG_CMD = const (0x1A )
6770_START_SCAN_NETWORKS = const (0x36 )
6871_GET_FW_VERSION_CMD = const (0x37 )
6972_SEND_UDP_DATA_CMD = const (0x39 )
73+ _GET_REMOTE_DATA_CMD = const (0x3A )
7074_GET_TIME = const (0x3B )
7175_GET_IDX_BSSID_CMD = const (0x3C )
7276_GET_IDX_CHAN_CMD = const (0x3D )
124128ADC_ATTEN_DB_6 = const (2 )
125129ADC_ATTEN_DB_11 = const (3 )
126130
131+ # pylint: disable=too-many-lines
132+
127133
128134class ESP_SPIcontrol : # pylint: disable=too-many-public-methods, too-many-instance-attributes
129135 """A class that will talk to an ESP32 module programmed with special firmware
@@ -405,6 +411,46 @@ def scan_networks(self):
405411 return APs
406412 return None
407413
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+
408454 def wifi_set_network (self , ssid ):
409455 """Tells the ESP32 to set the access point to the given ssid"""
410456 resp = self ._send_command_get_response (_SET_NET_CMD , [ssid ])
@@ -518,8 +564,7 @@ def connect(self, secrets):
518564 self .connect_AP (secrets ["ssid" ], secrets ["password" ])
519565
520566 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.
523568 Will wait until specified timeout seconds and return on success
524569 or raise an exception on failure.
525570
@@ -552,8 +597,7 @@ def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-n
552597 def create_AP (
553598 self , ssid , password , channel = 1 , timeout = 10
554599 ): # 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.
557601 Will wait until specified timeout seconds and return on success
558602 or raise an exception on failure.
559603
@@ -806,6 +850,14 @@ def server_state(self, socket_num):
806850 resp = self ._send_command_get_response (_GET_STATE_TCP_CMD , self ._socknum_ll )
807851 return resp [0 ][0 ]
808852
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+
809861 def set_esp_debug (self , enabled ):
810862 """Enable/disable debug mode on the ESP32. Debug messages will be
811863 written to the ESP32's UART."""
@@ -814,8 +866,7 @@ def set_esp_debug(self, enabled):
814866 raise RuntimeError ("Failed to set debug mode" )
815867
816868 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.
819870
820871 :param int pin: ESP32 GPIO pin to set.
821872 :param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
@@ -831,8 +882,7 @@ def set_pin_mode(self, pin, mode):
831882 raise RuntimeError ("Failed to set pin mode" )
832883
833884 def set_digital_write (self , pin , value ):
834- """
835- Set the digital output value of pin.
885+ """Set the digital output value of pin.
836886
837887 :param int pin: ESP32 GPIO pin to write to.
838888 :param bool value: Value for the pin.
@@ -844,8 +894,7 @@ def set_digital_write(self, pin, value):
844894 raise RuntimeError ("Failed to write to pin" )
845895
846896 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.
849898
850899 :param int pin: ESP32 GPIO pin to write to.
851900 :param float value: 0=off 1.0=full on
@@ -858,8 +907,7 @@ def set_analog_write(self, pin, analog_value):
858907 raise RuntimeError ("Failed to write to pin" )
859908
860909 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.
863911
864912 :param int pin: ESP32 GPIO pin to read from.
865913 """
@@ -877,8 +925,7 @@ def set_digital_read(self, pin):
877925 )
878926
879927 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.
882929
883930 :param int pin: ESP32 GPIO pin to read from.
884931 :param int atten: attenuation constant
@@ -914,6 +961,7 @@ def get_time(self):
914961 def set_certificate (self , client_certificate ):
915962 """Sets client certificate. Must be called
916963 BEFORE a network connection is established.
964+
917965 :param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
918966 """
919967 if self ._debug :
@@ -936,6 +984,7 @@ def set_certificate(self, client_certificate):
936984 def set_private_key (self , private_key ):
937985 """Sets private key. Must be called
938986 BEFORE a network connection is established.
987+
939988 :param str private_key: User-provided .PEM file up to 1700 bytes.
940989 """
941990 if self ._debug :
0 commit comments