@@ -67,13 +67,10 @@ class ESP_ATcontrol:
67
67
TLS_MODE = "SSL"
68
68
STATUS_APCONNECTED = 2 # CIPSTATUS method
69
69
STATUS_WIFI_APCONNECTED = 2 # CWSTATE method
70
-
71
70
STATUS_SOCKETOPEN = 3 # CIPSTATUS method
72
71
STATUS_SOCKET_OPEN = 3 # CIPSTATE method
73
-
74
72
STATUS_SOCKETCLOSED = 4 # CIPSTATUS method
75
73
STATUS_SOCKET_CLOSED = 4 # CIPSTATE method
76
-
77
74
STATUS_NOTCONNECTED = 5 # CIPSTATUS method
78
75
STATUS_WIFI_NOTCONNECTED = 1 # CWSTATE method
79
76
STATUS_WIFI_DISCONNECTED = 4 # CWSTATE method
@@ -116,7 +113,6 @@ def __init__(
116
113
self ._ifconfig = []
117
114
self ._initialized = False
118
115
self ._conntype = None
119
-
120
116
self ._use_cipstatus = use_cipstatus
121
117
122
118
def begin (self ) -> None :
@@ -400,7 +396,6 @@ def socket_receive(self, timeout: int = 5) -> bytearray:
400
396
i = 0 # reset the input buffer now that we know the size
401
397
elif i > 20 :
402
398
i = 0 # Hmm we somehow didnt get a proper +IPD packet? start over
403
-
404
399
else :
405
400
self .hw_flow (False ) # stop the flow
406
401
# read as much as we can!
@@ -491,29 +486,11 @@ def is_connected(self) -> bool:
491
486
print ("is_connected(): status says not connected" )
492
487
return False
493
488
489
+ # pylint: disable=too-many-branches
490
+ # pylint: disable=too-many-return-statements
494
491
@property
495
492
def status (self ) -> Union [int , None ]:
496
493
"""The IP connection status number (see AT+CIPSTATUS datasheet for meaning)"""
497
- # Note that CIPSTATUS, at least in the esp32-c3 version of espressif AT firmware
498
- # is considered deprecated and you should use AT+CWSTATE for wifi state
499
- # and AT+CIPSTATE for socket connection statuses.
500
- #
501
- # if CWSTATE/CIPSTATE are available, this function uses those and generates
502
- # a return code compatible with CIPSTATUS. For more fine grain control
503
- # you can use status_wifi and status_socket
504
- # if CWSTATE/CIPSTATE are not available, this falls back to using CIPSTATUS
505
- # (e.g. - ILabs Challenger RP2040 Wifi which has an onboard ESP8285 with older
506
- # firmware)
507
- # CIPSTATUS status messages:
508
- #<stat>: status of the ESP32-C3 station interface.
509
- # 0: The ESP32-C3 station is not initialized.
510
- # 1: The ESP32-C3 station is initialized, but not started a Wi-Fi connection yet.
511
- # 2: The ESP32-C3 station is connected to an AP and its IP address is obtained.
512
- # 3: The ESP32-C3 station has created a TCP/SSL transmission.
513
- # 4: All of the TCP/UDP/SSL connections of the ESP32-C3 station are disconnected.
514
- # 5: The ESP32-C3 station started a Wi-Fi connection, but was not connected
515
- # to an AP or disconnected from an AP.
516
-
517
494
if self ._use_cipstatus :
518
495
replies = self .at_response ("AT+CIPSTATUS" , timeout = 5 ).split (b"\r \n " )
519
496
for reply in replies :
@@ -531,12 +508,17 @@ def status(self) -> Union[int, None]:
531
508
for reply in replies :
532
509
if reply .startswith (b"STATUS:" ):
533
510
cipstatus = int (reply [7 :8 ])
534
- print (f"STATUS: CWSTATE: { status_w } , CIPSTATUS: { cipstatus } , CIPSTATE: { status_s } " )
511
+ print (
512
+ f"STATUS: CWSTATE: { status_w } , CIPSTATUS: { cipstatus } , CIPSTATE: { status_s } "
513
+ )
535
514
536
515
# Produce a cipstatus-compatible status code
537
516
# Codes are not the same between CWSTATE/CIPSTATUS so in some combinations
538
517
# we just pick what we hope is best.
539
- if status_w in (self .STATUS_WIFI_NOTCONNECTED , self .STATUS_WIFI_DISCONNECTED ):
518
+ if status_w in (
519
+ self .STATUS_WIFI_NOTCONNECTED ,
520
+ self .STATUS_WIFI_DISCONNECTED ,
521
+ ):
540
522
if self ._debug :
541
523
print (f"STATUS returning { self .STATUS_NOTCONNECTED } " )
542
524
return self .STATUS_NOTCONNECTED
@@ -546,31 +528,26 @@ def status(self) -> Union[int, None]:
546
528
print (f"STATUS returning { self .STATUS_SOCKETOPEN } " )
547
529
return self .STATUS_SOCKETOPEN
548
530
549
- # Sometimes you get a CIPSTATUS=4 when CWSTATE=2/CIPSTATE=4 and sometimes you
550
- # get CIPSTATUS=2 when CWSTATE=2/CIPSTATE=4
551
- # if status_w == self.STATUS_WIFI_APCONNECTED and status_s == self.STATUS_SOCKET_CLOSED:
552
- # if self._debug:
553
- # print(f"STATUS returning {self.STATUS_SOCKETCLOSED}")
554
- # return self.STATUS_SOCKETCLOSED
555
-
556
531
if status_w == self .STATUS_WIFI_APCONNECTED :
557
532
if self ._debug :
558
533
print (f"STATUS returning { self .STATUS_APCONNECTED } " )
559
534
return self .STATUS_APCONNECTED
560
535
561
536
# handle extra codes from CWSTATE
562
- if status_w == 0 : # station has not started any Wi-Fi connection.
537
+ if status_w == 0 : # station has not started any Wi-Fi connection.
563
538
if self ._debug :
564
539
print ("STATUS returning 1" )
565
540
return 1 # this cipstatus had no previous handler variable
566
541
567
542
# pylint: disable=line-too-long
568
- if status_w == 1 : # station has connected to an AP, but does not get an IPv4 address yet.
543
+ if (
544
+ status_w == 1
545
+ ): # station has connected to an AP, but does not get an IPv4 address yet.
569
546
if self ._debug :
570
547
print ("STATUS returning 1" )
571
- return 1 # this cipstatus had no previous handler variable
548
+ return 1 # this cipstatus had no previous handler variable
572
549
573
- if status_w == 3 : # station is in Wi-Fi connecting or reconnecting state.
550
+ if status_w == 3 : # station is in Wi-Fi connecting or reconnecting state.
574
551
if self ._debug :
575
552
print (f"STATUS returning { self .STATUS_NOTCONNECTED } " )
576
553
return self .STATUS_NOTCONNECTED
@@ -585,16 +562,6 @@ def status(self) -> Union[int, None]:
585
562
@property
586
563
def status_wifi (self ) -> Union [int , None ]:
587
564
"""The WIFI connection status number (see AT+CWSTATE datasheet for meaning)"""
588
- # Note that as of 2022-Nov CIPSTATUS is deprecated and replaced with CWSTATE and CIPSTATE
589
- # and the CWSTATE <state> codes are different than the old CIPSTATUS codes.
590
- # CWSTATE:
591
- # <state>: current Wi-Fi state.
592
- # 0: ESP32-C3 station has not started any Wi-Fi connection.
593
- # 1: ESP32-C3 station has connected to an AP, but does not get an IPv4 address yet.
594
- # 2: ESP32-C3 station has connected to an AP, and got an IPv4 address.
595
- # 3: ESP32-C3 station is in Wi-Fi connecting or reconnecting state.
596
- # 4: ESP32-C3 station is in Wi-Fi disconnected state.
597
- # <”ssid”>: the SSID of the target AP.
598
565
replies = self .at_response ("AT+CWSTATE?" , timeout = 5 ).split (b"\r \n " )
599
566
for reply in replies :
600
567
if reply .startswith (b"+CWSTATE:" ):
@@ -609,20 +576,6 @@ def status_wifi(self) -> Union[int, None]:
609
576
@property
610
577
def status_socket (self ) -> Union [int , None ]:
611
578
"""The Socket connection status number (see AT+CIPSTATE for meaning)"""
612
- # +CIPSTATE:<link ID>,<"type">,<"remote IP">,<remote port>,<local port>,<tetype>
613
- # OK
614
- # When there is no connection, AT returns:
615
- # OK
616
- # Parameters
617
- # <link ID>: ID of the connection (0~4), used for multiple connections.
618
- # <”type”>: string parameter showing the type of transmission: “TCP”, “TCPv6”,
619
- # “UDP”, “UDPv6”, “SSL”, or “SSLv6”.
620
- # <”remote IP”>: string parameter showing the remote IPv4 address or IPv6 address.
621
- # <remote port>: the remote port number.
622
- # <local port>: the local port number.
623
- # <tetype>:
624
- # 0: ESP32-C3 runs as a client.
625
- # 1: ESP32-C3 runs as a server.
626
579
replies = self .at_response ("AT+CIPSTATE?" , timeout = 5 ).split (b"\r \n " )
627
580
for reply in replies :
628
581
# If there are any +CIPSTATE lines that means it's an open socket
@@ -740,6 +693,7 @@ def join_AP( # pylint: disable=invalid-name
740
693
return
741
694
742
695
# pylint: disable=invalid-name
696
+ # pylint: disable=too-many-arguments
743
697
def join_AP_Enterprise (
744
698
self ,
745
699
ssid : str ,
@@ -755,7 +709,6 @@ def join_AP_Enterprise(
755
709
# Not sure how to verify certificates so we set that to not verify.
756
710
certificate_security = 0 # Bit0: Client certificate.Bit1: Server certificate.
757
711
758
- # First make sure we're in 'station' mode so we can connect to AP's
759
712
if self ._debug :
760
713
print ("In join_AP_Enterprise()" )
761
714
if self .mode != self .MODE_STATION :
@@ -765,9 +718,6 @@ def join_AP_Enterprise(
765
718
if router and router [0 ] == ssid :
766
719
return # we're already connected!
767
720
reply = self .at_response (
768
- # from https://docs.espressif.com/projects/esp-at/en/latest/
769
- # esp32c3/AT_Command_Set/Wi-Fi_AT_Commands.html#cmd-jeap
770
- # AT+CWJEAP=<ssid>,<method>,<identity>,<username>,<password>,<security>[,<jeap_timeout>]
771
721
'AT+CWJEAP="'
772
722
+ ssid
773
723
+ '",'
@@ -807,9 +757,7 @@ def disconnect(self, timeout: int = 5, retries: int = 3):
807
757
else :
808
758
wait_for_disconnect = False
809
759
if self ._debug is True :
810
- print (
811
- "disconnect(): Not connected, not waiting for disconnect message"
812
- )
760
+ print ("disconnect(): Not connected, not waiting for disconnect message" )
813
761
reply = self .at_response ("AT+CWQAP" , timeout = timeout , retries = retries )
814
762
# Don't bother waiting for disconnect message if we weren't connected already
815
763
# sometimes the "WIFI DISCONNECT" shows up in the reply and sometimes it doesn't.
@@ -835,7 +783,6 @@ def disconnect(self, timeout: int = 5, retries: int = 3):
835
783
print (
836
784
f"disconnect(): Timed out wating for WIFI DISCONNECT: { response } "
837
785
)
838
- return
839
786
840
787
def scan_APs ( # pylint: disable=invalid-name
841
788
self , retries : int = 3
0 commit comments