Skip to content

Commit 56d2d52

Browse files
committed
Make UC2Client serial-only; deprecate WiFi
Remove/disable WiFi/HTTP communication and favor serial-only communication for UC2Client. Key changes: - Added/renamed binary builds in binaries/latest. - examples/can_ota_streaming_example.py: changed canid to 40 and appended commented JSON OTA start messages for canid 11 and 40. - uc2rest/UC2Client.py: requests kept as optional import; explicit deprecation/warning for host-based (WiFi) usage; removed wifi/http code paths and made serial the primary channel; improved logging and PyScript/SerialManager handling. - uc2rest/motor.py: simplify is_blocking check to rely on serial connection only. - uc2rest/mserial.py: store discovered serial port, set serialport from opened device, clear is_connected on thread stop, and add clarifying comments to device readiness handling. - uc2rest/rotator.py: remove obsolete wifi-related blocking branch. - uc2rest/wifi.py: mark methods deprecated and replace implementations with pass/docstrings to disable HTTP functionalities. These changes simplify the codebase by removing legacy WiFi support and make serial the supported communication path. Adjustments to examples and serial handling ensure compatibility with the serial-only workflow.
1 parent fc65cac commit 56d2d52

9 files changed

Lines changed: 51 additions & 103 deletions
1.23 MB
Binary file not shown.

binaries/latest/esp32_seeed_xiao_esp32s3_can_slave_motor.bin renamed to binaries/latest/esp32_seeed_xiao_esp32s3_can_slave_motor.bin_

File renamed without changes.
781 KB
Binary file not shown.

examples/can_ota_streaming_example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def main():
4848

4949
port = "/dev/cu.SLAB_USBtoUART"
5050
canid = 11
51+
canid = 40
5152
firmware = "/Users/bene/Dropbox/Dokumente/Promotion/PROJECTS/UC2-REST/binaries/latest/esp32_seeed_xiao_esp32s3_can_slave_motor.bin"
5253
baud = 921600
5354

@@ -95,7 +96,10 @@ def main():
9596
print("=" * 70)
9697

9798
return 0 if success else 1
99+
'''
100+
{"task": "/can_ota_stream", "canid": 11, "action": "start", "firmware_size": 876784, "page_size": 4096, "chunk_size": 512, "md5": "43ba96b4d18c010201762b840476bf83"}
101+
{"task": "/can_ota_stream", "canid": 40, "action": "start", "firmware_size": 876784, "page_size": 4096, "chunk_size": 512, "md5": "43ba96b4d18c010201762b840476bf83"}
98102
99-
103+
'''
100104
if __name__ == "__main__":
101105
sys.exit(main())

uc2rest/UC2Client.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
from .can import CAN
3131
from .canota import CANOTA
3232
from .camera_trigger import CameraTrigger
33+
34+
# requests is no longer used for direct ESP communication (serial-only).
35+
# Kept as optional import for other modules that may need HTTP (e.g. firmware downloads).
3336
try:
3437
import requests
35-
except:
36-
print("No requests available - running on pyscript?")
38+
except Exception:
39+
requests = None
3740

3841
class UC2Client(object):
39-
# headers = {'ESP32-version': '*'}
4042
headers={"Content-Type":"application/json"}
4143
getmessage = ""
4244
is_connected = False
4345

44-
is_wifi = False
4546
is_serial = False
4647
BAUDRATE = 115200
4748

@@ -52,12 +53,13 @@ def __init__(self, host=None, port=31950, serialport=None, identity="UC2_Feather
5253
This client connects to the UC2-REST microcontroller that can be found here
5354
https://github.com/openUC2/UC2-REST
5455
56+
Communication is via USB/serial only.
57+
The host/port parameters are deprecated and will be ignored.
58+
5559
generally speaking you send/receive JSON documents that will cause an:
5660
1. action => "/XXX_act"
5761
2. getting => "/XXX_get"
5862
3. setting => "/XXX_set"
59-
60-
you can send commands through wifi/http or usb/serial
6163
'''
6264
if True: #logger is None:
6365
self.logger = Logger()
@@ -67,28 +69,27 @@ def __init__(self, host=None, port=31950, serialport=None, identity="UC2_Feather
6769
# perhaps we are in the browser?
6870
self.isPyScript = isPyScript
6971

70-
# initialize communication channel (# connect to wifi or usb)
72+
# Deprecation notice for WiFi mode
73+
if host is not None and serialport is None and SerialManager is None:
74+
self.logger.warning(
75+
"WiFi/HTTP communication has been removed. "
76+
"Please use serialport= instead. Ignoring host parameter."
77+
)
78+
79+
# initialize communication channel (serial only)
7180
if serialport is not None:
7281
# use USB connection
7382
self.serial = Serial(serialport, baudrate, parent=self, identity=identity, DEBUG=DEBUG, skipFirmwareCheck=skipFirmwareCheck)
7483
self.is_serial = True
7584
self.is_connected = self.serial.is_connected
7685
self.serial.DEBUG = DEBUG
77-
elif host is not None:
78-
# use client in wireless mode
79-
self.is_wifi = True
80-
self.host = host
81-
self.port = port
82-
83-
# check if host is up
84-
self.logger.debug(f"Connecting to microscope {self.host}:{self.port}")
85-
#self.is_connected = self.isConnected()
8686
elif SerialManager is not None:
87-
# we are trying to access the controller from .a web browser
87+
# we are trying to access the controller from a web browser
8888
self.serial = SerialManagerWrapper(SerialManager, parent=self)
8989
self.isPyScript = True
90+
self.is_serial = True
9091
else:
91-
self.logger.error("No ESP32 device is connected - check IP or Serial port!")
92+
self.logger.error("No ESP32 device is connected - please provide a serialport!")
9293

9394

9495
# import libraries depending on API version
@@ -169,19 +170,12 @@ def post_json(self, path, payload, getReturn=True, nResponses=1, timeout=1):
169170
return self.serial.post_json(path, payload, getReturn=getReturn, nResponses=nResponses, timeout=timeout)
170171

171172
def get_json(self, path, getReturn=True, timeout=1):
172-
if self.is_wifi:
173-
# FIXME: this is not working
174-
url = f"http://{self.host}:{self.port}{path}"
175-
r = requests.get(url, headers=self.headers, timeout=timeout)
176-
return r.json()
177-
elif self.is_serial or self.isPyScript:
178-
# timeout is not used anymore
179-
if timeout <=0:
173+
if self.is_serial or self.isPyScript:
174+
if timeout <= 0:
180175
getReturn = False
181176
return self.serial.post_json(path, payload=None, getReturn=getReturn, nResponses=1, timeout=timeout)
182-
#return self.serial.read_json()<
183177
else:
184-
self.logger.error("No ESP32 device is connected - check IP or Serial port!")
178+
self.logger.error("No ESP32 device is connected - serial not initialized!")
185179
return None
186180

187181
def setDebugging(self, debug=False):

uc2rest/motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
630630

631631
# drive motor
632632
self.isRunning = True
633-
is_blocking = not self._parent.is_wifi and is_blocking and self._parent.serial.is_connected
633+
is_blocking = is_blocking and self._parent.serial.is_connected
634634
timeout = timeout if is_blocking else 0
635635
nResponses = len(payload["motor"]["steppers"]) + 1
636636
# if we get a return, we will receive the latest position feedback from the driver by means of the axis that moves the longest

uc2rest/mserial.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def findCorrectSerialDevice(self):
181181
self.is_connected = True
182182
self.manufacturer = port.manufacturer
183183
self._logger.debug(f"Found correct USB device: {port.device} - {port.description}")
184+
self.serialport = port
184185
return self.serialdevice
185186

186187
self.is_connected = False
@@ -292,7 +293,7 @@ def _process_commands(self):
292293

293294
# device not ready yet
294295
if self.serialdevice is None:
295-
self.is_connected = False
296+
self.is_connected = False # TODO: We have to indicate if the device is not connected or if it is just not ready yet - otherwise we will have a lot of "Failed to Send" messages in the beginning
296297
continue
297298
else:
298299
self.is_connected = True
@@ -389,6 +390,7 @@ def _process_commands(self):
389390
except Exception as e:
390391
self._logger.error("Error in processing serial commands: "+str(e))
391392
self.running = False
393+
self.is_connected = False
392394

393395
def get_json(self, path, timeout=1):
394396
message = {"task":path}
@@ -519,7 +521,9 @@ def reconnect(self, baudrate=None):
519521
except:
520522
pass
521523
self.serialdevice = self.openDevice(port = self.serialport, baud_rate = self.baudrate)
522-
if self.serialdevice: return True
524+
if self.serialdevice:
525+
self.serialport = self.serialdevice.port
526+
return True
523527
return False
524528

525529

uc2rest/rotator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
283283
steppersRunning = isAbsoluteArray
284284
else:
285285
steppersRunning = np.abs(np.array(steps))>0
286-
if False: #not self._parent.is_wifi and is_blocking and self._parent.serial.is_connected:
286+
if False:
287287
while True:
288288
time.sleep(0.05) # don'T overwhelm the CPU
289289
# see if already done

uc2rest/wifi.py

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,33 @@ def __init__(self, parent):
55
self._parent = parent
66

77
def isConnected(self):
8-
# check if client is connected to the same network
9-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10-
try:
11-
s.connect((self.host, int(self.port)))
12-
s.settimeout(2)
13-
s.shutdown(2)
14-
return True
15-
except:
16-
return False
17-
8+
'''
9+
deprecated
10+
'''
11+
pass
1812

1913
def get_json(self, path):
20-
if self.is_connected and self.is_wifi:
21-
if not path.startswith("http"):
22-
path = self.base_uri + path
23-
try:
24-
r = requests.get(path)
25-
r.raise_for_status()
26-
self.is_connected = True
27-
28-
self.getmessage = r.json()
29-
self.is_sending = False
30-
return self.getmessage
31-
32-
except Exception as e:
33-
if IS_IMSWITCH: self.__logger.error(e)
34-
self.is_connected = False
35-
self.is_sending = False
36-
# not connected
37-
return None
14+
'''
15+
deprecated
16+
'''
17+
pass
3818

3919
def post_json(self, path, payload={}, headers=None, isInit=False, timeout=1):
40-
"""Make an HTTP POST request and return the JSON response"""
41-
if self.is_connected and self.is_wifi:
42-
if not path.startswith("http"):
43-
path = self.base_uri + path
44-
if headers is None:
45-
headers = self.headers
46-
try:
47-
r = requests.post(path, json=payload, headers=headers,timeout=timeout)
48-
r.raise_for_status()
49-
r = r.json()
50-
self.is_connected = True
51-
self.is_sending = False
52-
return r
53-
except Exception as e:
54-
if IS_IMSWITCH: self.__logger.error(e)
55-
self.is_connected = False
56-
self.is_sending = False
57-
# not connected
58-
return None
59-
20+
'''
21+
deprecated
22+
'''
23+
pass
6024

6125
@property
6226
def base_uri(self):
6327
return f"http://{self.host}:{self.port}"
6428

6529

6630
def send_jpeg(self, image):
67-
if is_cv2:
68-
temp = NamedTemporaryFile()
69-
70-
#add JPEG format to the NamedTemporaryFile
71-
iName = "".join([str(temp.name),".jpg"])
72-
73-
#save the numpy array image onto the NamedTemporaryFile
74-
cv2.imwrite(iName,image)
75-
_, img_encoded = cv2.imencode('test.jpg', image)
76-
77-
content_type = 'image/jpeg'
78-
headers = {'content-type': content_type}
79-
payload = img_encoded.tostring()
80-
path = '/uploadimage'
81-
82-
#r = self.post_json(path, payload=payload, headers = headers)
83-
#requests.post(self.base_uri + path, data=img_encoded.tostring(), headers=headers)
84-
files = {'media': open(iName, 'rb')}
85-
if self.is_connected:
86-
requests.post(self.base_uri + path, files=files)
87-
88-
31+
'''
32+
deprecated
33+
'''
34+
pass
8935

9036
def scanWifi(self, timeout=3):
9137
path = '/wifi/scan'

0 commit comments

Comments
 (0)