11 import ast |
11 import ast |
12 import json |
12 import json |
13 |
13 |
14 from PyQt6.QtCore import QUrl, pyqtSlot |
14 from PyQt6.QtCore import QUrl, pyqtSlot |
15 from PyQt6.QtNetwork import QNetworkRequest |
15 from PyQt6.QtNetwork import QNetworkRequest |
16 from PyQt6.QtWidgets import QMenu |
16 from PyQt6.QtWidgets import QDialog, QMenu |
17 |
17 |
18 from eric7 import Globals, Preferences |
18 from eric7 import Globals, Preferences |
19 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
19 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
20 from eric7.EricWidgets import EricMessageBox |
20 from eric7.EricWidgets import EricMessageBox |
21 from eric7.EricWidgets.EricApplication import ericApp |
21 from eric7.EricWidgets.EricApplication import ericApp |
358 rtc = machine.RTC() |
358 rtc = machine.RTC() |
359 rtc.datetime(rtc_time[:7] + (0,)) |
359 rtc.datetime(rtc_time[:7] + (0,)) |
360 """ |
360 """ |
361 |
361 |
362 ################################################################## |
362 ################################################################## |
363 ## Methods below implement network related methods |
363 ## Methods below implement WiFi related methods |
364 ################################################################## |
364 ################################################################## |
|
365 |
|
366 def addDeviceWifiEntries(self, menu): |
|
367 """ |
|
368 Public method to add device specific entries to the given menu. |
|
369 |
|
370 @param menu reference to the context menu |
|
371 @type QMenu |
|
372 """ |
|
373 menu.addSeparator() |
|
374 menu.addAction(self.tr("Set Country"), self.__setCountry).setEnabled( |
|
375 self._deviceData["wifi_type"] == "picow" |
|
376 ) |
365 |
377 |
366 def hasWifi(self): |
378 def hasWifi(self): |
367 """ |
379 """ |
368 Public method to check the availability of WiFi. |
380 Public method to check the availability of WiFi. |
369 |
381 |
408 command = """ |
420 command = """ |
409 def wifi_status(): |
421 def wifi_status(): |
410 import ubinascii |
422 import ubinascii |
411 import ujson |
423 import ujson |
412 import network |
424 import network |
|
425 import rp2 |
413 |
426 |
414 wifi = network.WLAN(network.STA_IF) |
427 wifi = network.WLAN(network.STA_IF) |
415 station = { |
428 station = { |
416 'active': wifi.active(), |
429 'active': wifi.active(), |
417 'connected': wifi.isconnected(), |
430 'connected': wifi.isconnected(), |
418 'status': wifi.status(), |
431 'status': wifi.status(), |
419 'ifconfig': wifi.ifconfig(), |
432 'ifconfig': wifi.ifconfig(), |
420 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), |
433 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), |
421 'channel': wifi.config('channel'), |
434 'channel': wifi.config('channel'), |
422 'txpower': wifi.config('txpower'), |
435 'txpower': wifi.config('txpower'), |
|
436 'country': rp2.country(), |
423 } |
437 } |
424 print(ujson.dumps(station)) |
438 print(ujson.dumps(station)) |
425 |
439 |
426 wifi = network.WLAN(network.AP_IF) |
440 wifi = network.WLAN(network.AP_IF) |
427 ap = { |
441 ap = { |
431 'ifconfig': wifi.ifconfig(), |
445 'ifconfig': wifi.ifconfig(), |
432 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), |
446 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), |
433 'channel': wifi.config('channel'), |
447 'channel': wifi.config('channel'), |
434 'txpower': wifi.config('txpower'), |
448 'txpower': wifi.config('txpower'), |
435 'essid': wifi.config('essid'), |
449 'essid': wifi.config('essid'), |
|
450 'country': rp2.country(), |
436 } |
451 } |
437 print(ujson.dumps(ap)) |
452 print(ujson.dumps(ap)) |
438 |
453 |
439 wifi_status() |
454 wifi_status() |
440 del wifi_status |
455 del wifi_status |
476 @type str |
491 @type str |
477 @return tuple containing the connection status and an error string |
492 @return tuple containing the connection status and an error string |
478 @rtype tuple of (bool, str) |
493 @rtype tuple of (bool, str) |
479 """ |
494 """ |
480 if self._deviceData["wifi_type"] == "picow": |
495 if self._deviceData["wifi_type"] == "picow": |
|
496 country = Preferences.getMicroPython("WifiCountry").upper() |
481 command = """ |
497 command = """ |
482 def connect_wifi(ssid, password): |
498 def connect_wifi(ssid, password): |
483 import network |
499 import network |
|
500 import rp2 |
|
501 import ujson |
484 from time import sleep |
502 from time import sleep |
485 import ujson |
503 |
|
504 rp2.country({2}) |
486 |
505 |
487 wifi = network.WLAN(network.STA_IF) |
506 wifi = network.WLAN(network.STA_IF) |
488 wifi.active(False) |
507 wifi.active(False) |
489 wifi.active(True) |
508 wifi.active(True) |
490 wifi.connect(ssid, password) |
509 wifi.connect(ssid, password) |
491 max_wait = 100 |
510 max_wait = 140 |
492 while max_wait: |
511 while max_wait: |
493 if wifi.status() < 0 or wifi.status() >= 3: |
512 if wifi.status() < 0 or wifi.status() >= 3: |
494 break |
513 break |
495 max_wait -= 1 |
514 max_wait -= 1 |
496 sleep(0.1) |
515 sleep(0.1) |
497 status = wifi.status() |
516 status = wifi.status() |
498 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
517 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
499 |
518 |
500 connect_wifi({0}, {1}) |
519 connect_wifi({0}, {1}) |
501 del connect_wifi |
520 del connect_wifi |
502 """.format(repr(ssid), repr(password if password else "")) |
521 """.format( |
|
522 repr(ssid), |
|
523 repr(password if password else ""), |
|
524 repr(country if country else "XX"), |
|
525 ) |
503 elif self._deviceData["wifi_type"] == "pimoroni": |
526 elif self._deviceData["wifi_type"] == "pimoroni": |
504 # TODO: not yet implemented |
527 # TODO: not yet implemented |
505 pass |
528 pass |
506 else: |
529 else: |
507 return super().connectWifi(ssid, password) |
530 return super().connectWifi(ssid, password) |
555 out, err = self._interface.execute(command) |
578 out, err = self._interface.execute(command) |
556 if err: |
579 if err: |
557 return False, err |
580 return False, err |
558 |
581 |
559 return out.decode("utf-8").strip() == "True", "" |
582 return out.decode("utf-8").strip() == "True", "" |
|
583 |
|
584 @pyqtSlot() |
|
585 def __setCountry(self): |
|
586 """ |
|
587 Private slot to configure the country of the connected RP2040 device. |
|
588 |
|
589 The country is the two letter country code. |
|
590 """ |
|
591 from ..WifiDialogs.WifiCountryDialog import WifiCountryDialog |
|
592 |
|
593 dlg = WifiCountryDialog() |
|
594 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
595 country, remember = dlg.getCountry() |
|
596 if remember: |
|
597 Preferences.setMicroPython("WifiCountry", country) |
|
598 |
|
599 command = """ |
|
600 import rp2 |
|
601 rp2.country({0}) |
|
602 """.format(repr(country)) |
|
603 |
|
604 out, err = self._interface.execute(command) |
|
605 if err: |
|
606 self._showError("rp2.country()", err) |
560 |
607 |
561 |
608 |
562 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
609 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
563 """ |
610 """ |
564 Function to instantiate a MicroPython device object. |
611 Function to instantiate a MicroPython device object. |