Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Print data from TCP or UDP socket using esp.socket_receive
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Print data from TCP or UDP socket using esp.socket_receive

by Bruddy on Tue Mar 09, 2021 5:16 pm

I have a got a socket connection using "esp.socket_connect" working. I know the socket I am connecting is outputting data as I have connected to it by other means.
I am now trying to print the data using "esp.socket_receive"
The code I am trying is as follows:
Code: Select all | TOGGLE FULL SIZE
while True:
   
    data = esp.socket_receive()
    print (data)


This results in an output of:

ESP AT commands
Connected to Study
bytearray(b'')
bytearray(b'')
bytearray(b'')
....

Am I using the correct code to print the data (it is actually GPS NMEA sentences) ?

Bruddy
 
Posts: 14
Joined: Sat Feb 20, 2021 1:12 pm

Re: Print data from TCP or UDP socket using esp.socket_recei

by tannewt on Wed Mar 10, 2021 9:07 pm

Ya, the print code looks correct.

tannewt
 
Posts: 2390
Joined: Thu Oct 06, 2016 8:48 pm

Re: Print data from TCP or UDP socket using esp.socket_recei

by Bruddy on Thu Mar 11, 2021 3:18 pm

As a beginner trying to learn Circuitpython and how to use a Pico, I really apprectate the help.
The machine producing the UDP data is a Raspberry Pi
I am happy that the UDP socket is streaming data as I can connect to it directly using Python 3.7.3 on the Pi and see data lines as follows:
received message: b'$GPRMC,,A,5951.3940,N,02323.3190,E,5.6,96.7,,,E*6E\r\n'
received message: b'$GPRMC,,A,5951.3930,N,02323.3250,E,5.6,96.7,,,E*66\r\n'
received message: b'$GPRMC,,A,5951.3930,N,02323.3250,E,5.6,99.9,,,E*67\r\n'

Circuitpython is giving me
bytearray(b'')
bytearray(b'')
bytearray(b'')
bytearray(b'')
bytearray(b'')
bytearray(b'')

I seem to be nearly there, but missing something small.

The full code I am using is the example given in the Docs, but slightly modified with my ip and ports etc.
Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from digitalio import DigitalInOut
from digitalio import Direction
import adafruit_requests as requests
import adafruit_espatcontrol.adafruit_espatcontrol_socket as socket
from adafruit_espatcontrol import adafruit_espatcontrol

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise



RX = board.GP0
TX = board.GP1
resetpin = DigitalInOut(board.GP4)


uart = busio.UART(board.GP0, board.GP1, baudrate=115200, timeout=15)



print("ESP AT commands")
esp = adafruit_espatcontrol.ESP_ATcontrol(
    uart, 115200, reset_pin=resetpin, debug=False)


esp.hard_reset
 
requests.set_socket(socket, esp)
esp.connect(secrets)
esp.socket_connect("UDP", "192.168.1.154", 1111, keepalive=20, retries=3)


while True:
    data = esp.socket_receive()
    print (data)
   


Any further suggestions on what I can try?

Thanks,
Bruddy

Bruddy
 
Posts: 14
Joined: Sat Feb 20, 2021 1:12 pm

Re: Print data from TCP or UDP socket using esp.socket_recei

by tannewt on Fri Mar 12, 2021 4:10 pm

I've never used the ATControl library so I don't have any specific suggestions for it. I know on our Airlift products we use the ESP32SPI library instead.

tannewt
 
Posts: 2390
Joined: Thu Oct 06, 2016 8:48 pm

Re: Print data from TCP or UDP socket using esp.socket_recei

by Bruddy on Fri Mar 12, 2021 6:14 pm

I am using an ESP-01S connected to the Pico UART. It finds my WiFi and connects perfectly and even binds to the socket.
There is also a possibility of using :
"readline()
Attempt to return as many bytes as we can up to but not including ‘ ‘"

But that needs:
classadafruit_espatcontrol.adafruit_espatcontrol_socket.socket(family=2, type=1, proto=0, fileno=None).

I can't find any example code for that and my knowledge of Circuitpython is not sufficient to use that class and readline().
I'll keep reading docs!

Bruddy
 
Posts: 14
Joined: Sat Feb 20, 2021 1:12 pm

Please be positive and constructive with your questions and comments.