Skip to content

New function get_remote_data() to expose NINA getRemoteData() #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2022
Merged

New function get_remote_data() to expose NINA getRemoteData() #161

merged 4 commits into from
Mar 24, 2022

Conversation

anecdata
Copy link
Member

@anecdata anecdata commented Mar 22, 2022

Expose NINA getRemoteData() function
https://github.com/adafruit/nina-fw/blob/d73fe315cc7f9148a0918490d3b75430c8444bf7/main/CommandHandler.cpp#L779
This returns the IP address and port number of the remote host. Especially useful for UDP server.

Adafruit CircuitPython 7.2.0 on 2022-02-24; Adafruit PyPortal with samd51j20 UDP server code:

import time
import board
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
from secrets import secrets

PORT = 5000

spi = board.SPI()

esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
socket.set_interface(esp)

esp.connect_AP(secrets["ssid"], secrets["password"])
print("Connected:", "RSSI =", esp.rssi)

print("Create UDP Server Socket")
s = socket.socket(type=socket.SOCK_DGRAM)
esp.start_server(PORT, s.socknum, conn_mode=1)  # UDP_MODE = const(1)
while True:
    numbytes_avail = esp.socket_available(s.socknum)
    if numbytes_avail:
        print(numbytes_avail)
        bytes_read = esp.socket_read(s.socknum, numbytes_avail)
        print(bytes_read)

        remote_data = esp.get_remote_data(s.socknum)
        remote_ip = esp.pretty_ip(remote_data["ip_addr"])
        remote_port = remote_data["port"]
        print("REMOTE:", remote_ip, remote_port)

Result:

code.py output:
Connected: RSSI = -60
Create UDP Server Socket
12
b'Hello, world'
REMOTE: 192.168.5.32 14290
12
b'Hello, world'
REMOTE: 192.168.5.32 59860
12
b'Hello, world'
REMOTE: 192.168.5.32 47857

CPython UDP client code:

#!/usr/bin/env python3
import time
import socket

# edit host and port to match server
HOST = "192.168.6.39"
PORT = 5000
TIMEOUT = 5
INTERVAL = 5

while True:
    print("Create UDP Client Socket")
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.settimeout(TIMEOUT)

    size = s.sendto(b"Hello, world", (HOST, PORT))
    print("Sent", size, "bytes")

    s.close()

    time.sleep(INTERVAL)

@anecdata anecdata requested a review from a team March 22, 2022 01:25
@tekktrik
Copy link
Member

Is there any benefit to returning a dictionary as opposed to a tuple of the two values?

@anecdata
Copy link
Member Author

anecdata commented Mar 24, 2022

Not particularly, I was just mimicking the return from network_data, which is also a dictionary with an "ip_addr" element. Readability vs. byte count? I don't have a strong opinion either way. At the next layer down (sockets), there is a commonly-used tuple for address which is (host, port), but in that case host is a string like "localhost" or "127.0.0.1". In the present case, the "ip_addr" value for both network data and get_remote_datais a bytes. The only other return in the module that isn't just a single value is the return from scan_networks, which is a list of dictionaries.

Copy link
Member

@tekktrik tekktrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Didn't test but looks good to me!

@tekktrik tekktrik merged commit ddd26eb into adafruit:main Mar 24, 2022
@anecdata anecdata deleted the getRemoteData branch March 24, 2022 14:55
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 26, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_DotStar to 2.2.0 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#60 from dhalbert/no-pypixelbuf

Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 4.2.0 from 4.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#161 from anecdata/getRemoteData

Updating https://github.com/adafruit/Adafruit_CircuitPython_Fingerprint to 2.2.4 from 2.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_Fingerprint#47 from ajiekurniawansaputra/main
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3741 to 1.3.0 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_IS31FL3741#16 from dhalbert/no-pypixelbuf

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel to 6.3.0 from 6.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#124 from dhalbert/no-pypixelbuf
  > Fixed readthedocs build

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI to 1.0.0 from 0.9.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel_SPI#33 from dhalbert/pixelbuf-only
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.6.2 from 5.6.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#84 from adafruit/fix_isconnected
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf to 1.1.3 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Pixelbuf#5 from tekktrik/doc/fix-lib-name
  > Fixed readthedocs build
  > Post-patch cleanup
  > Consolidate Documentation sections of README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants