Metro M4 express Wifi problems

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Metro M4 express Wifi problems

Post by johantonissen »

Hello there,

My students are having problems with controlling the metro m4 express wifi airlift over esp32. I've tried to reproduce the problem and found out that disableing the firewall and the antivirus-virus in windows helps. However some students are still experiencing problems. Students are working with cirquitpython 6 or 7.

Here is the client code which we run in anaconda spyder:

Code: Select all

## RCBoat_Client_v2.py

import socket
import time
import keyboard

#Host name and port number of server
HOST = '192.168.4.1'
PORT = 80

#Dictionary for coding tuples
PACK_DICT = {}
PACK_DICT[(-1,-1)] = 'a'
PACK_DICT[(-1,0)] = 'b'
PACK_DICT[(-1,1)] = 'c'
PACK_DICT[(0,-1)] = 'd'
PACK_DICT[(0,0)] = 'e'
PACK_DICT[(0,1)] = 'f'
PACK_DICT[(1,-1)] = 'g'
PACK_DICT[(1,0)] = 'h'
PACK_DICT[(1,1)] = 'i'

def key_handler(key_event):
    #redirect to appropiate press or release function
    if key_event.event_type == 'down':key_press_handler(key_event)
    elif key_event.event_type == 'up':key_release_handler(key_event)


def key_press_handler(key_event):
    if key_event.name == 'left': button_state['left'] = 1
    elif key_event.name == 'right': button_state['right'] = 1
    elif key_event.name == 'up': button_state['forward'] = 1
    elif key_event.name == 'down': button_state['backward'] = 1

def key_release_handler(key_event):
    if key_event.name == 'left': button_state['left'] = 0
    elif key_event.name == 'right': button_state['right'] = 0
    elif key_event.name == 'up': button_state['forward'] = 0
    elif key_event.name == 'down': button_state['backward'] = 0

def sendData(data):
    #convert to string, encode and send
    dataString = PACK_DICT[data]
    dataEncoded = dataString.encode()
    s.sendall(dataEncoded)


def checkStateChange(keyDirection):
    if current_direction == keyDirection:
        return 0
    return 1


#Initialize dictionary to keep track of pressed buttons
button_state = {
    'left':0,
    'right':0,
    'forward':0,
    'backward':0
}

#To keep track of the current direction
current_direction = (0,0)


#convert to host and port to address
addr = socket.getaddrinfo(HOST, PORT)[0][4]
print(socket.getaddrinfo(HOST, PORT))

#connect to address
s = socket.socket()
s.connect(addr)
print(s)

#hook keyboard listerer to key_handler function
keyboard.hook(key_handler)


while True:
    #check dictionary
    leftRightDirection = button_state['right']-button_state['left']
    upDownDirection = button_state['forward']-button_state['backward']
    keyDirection = (leftRightDirection,upDownDirection)
    #check if keyDirection is changed
    stateChange = checkStateChange(keyDirection)

    #if it is changed:
    if stateChange:
        current_direction = keyDirection    #change direction
        sendData(current_direction)       #send it
        time.sleep(50/1000)
Here is the code we run on the metro m4:

Code: Select all

## RCBoat_Server_v2.py

##imports
import busio
import digitalio
import time
import board
#import pulseio
import pwmio
from micropython import const

from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_motor import servo


#constant definitions
NO_SOCK_AVAIL = const(255)
PORT = 80
SSID = b'ESP32'
PASSWORD = b'JohanJohan'

##Pin definitions
#DC motor pins
ENA_PIN = board.D13
IN1_PIN = board.D12
IN2_PIN = board.D11
ENB_PIN = board.D8
IN3_PIN = board.D10
IN4_PIN = board.D9

#Servo pin
SERVO_PIN = board.D5

#Dictionary for Decoding data
UNPACK_DICT = {}
UNPACK_DICT['a'] = (0,-1)
UNPACK_DICT['b'] = (0,0)
UNPACK_DICT['c'] = (0,1)
UNPACK_DICT['d'] = (90,-1)
UNPACK_DICT['e'] = (90,0)
UNPACK_DICT['f'] = (90,1)
UNPACK_DICT['g'] = (180,-1)
UNPACK_DICT['h'] = (180,0)
UNPACK_DICT['i'] = (180,1)

#Define DC motor class
class DC_motor:
    def __init__(self, EN_pin, IN1_pin, IN2_pin):
        self.EN = pwmio.PWMOut(EN_pin)
        self.IN1 = digitalio.DigitalInOut(IN1_pin)
        self.IN1.direction = digitalio.Direction.OUTPUT
        self.IN2 = digitalio.DigitalInOut(IN2_pin)
        self.IN2.direction = digitalio.Direction.OUTPUT

    def forward(self, DutyC):
        self.EN.duty_cycle = 0
        self.IN1.value = True
        self.IN2.value = False
        self.EN.duty_cycle = int(DutyC*(2**16-1))

    def backward(self, DutyC):
        self.EN.duty_cycle = 0
        self.IN1.value = False
        self.IN2.value = True
        self.EN.duty_cycle = int(DutyC*(2**16-1))

    def setMotor(self, DutyC):
        if DutyC>0:
            self.forward(DutyC)
        else:
            self.backward(abs(DutyC))


##setup
#initialize servo
#pwm = pulseio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
pwm = pwmio.PWMOut(SERVO_PIN, frequency=50)
pwm.duty_cycle = 2 ** 15

my_servo = servo.Servo(pwm)
my_servo.angle=90

#initialize motors
motor1 = DC_motor(ENA_PIN,IN1_PIN,IN2_PIN)
motor2 = DC_motor(ENB_PIN,IN3_PIN,IN4_PIN)

#Obtain control over esp32
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

#create access point
esp.create_AP(SSID, PASSWORD)

#Host server
socket.set_interface(esp)
server_socket = socket.socket()
esp.start_server(PORT, server_socket.socknum)
ip = esp.pretty_ip(esp.ip_address)
print("Server available at {0}:{1}".format(ip, PORT))

#initiate client socket
client_sock = socket.socket(socknum=NO_SOCK_AVAIL)

while True:

    #wait for client connection
    while client_sock.socknum == NO_SOCK_AVAIL:
        client_sock_num = esp.socket_available(server_socket.socknum)
        client_sock = socket.socket(socknum = client_sock_num)
        if client_sock.socknum != NO_SOCK_AVAIL: print('connected')

    #once connected, recieve data
    if client_sock.connected() and client_sock.available()!= 0:
        byteData = client_sock.recv()           #recieve data
        dataString = byteData.decode()          #decode
        dataTuple = UNPACK_DICT[dataString[-1]] #last char to tuple
        print(dataTuple)
        (rudderAngle, motorvalue) = dataTuple   #unpack tuple

        #set motors
        my_servo.angle = rudderAngle
        motor1.setMotor(motorvalue)
        motor2.setMotor(motorvalue)



    # once disconnected replace client socket for
    # empty socket and wait for new client.
    elif not client_sock.connected():
        print('connection lost, looking for new connection...')
        client_sock = socket.socket(socknum = NO_SOCK_AVAIL)
I've found out that the code is locked in this infinite loop of the server code, connected is never printed:

Code: Select all

    while client_sock.socknum == NO_SOCK_AVAIL:
        client_sock_num = esp.socket_available(server_socket.socknum)
        client_sock = socket.socket(socknum = client_sock_num)
        if client_sock.socknum != NO_SOCK_AVAIL: print('connected')
We have tested the exact code on my device and it works, so my guess the problem is somewhere in windows, hence my fiddling with firewall and antivirus-virus.

Thanx in advance!
Attachments
RCBoat_Client_v2.py
(2.36 KiB) Downloaded 9 times
RCBoat_Server_v2.py
(3.63 KiB) Downloaded 9 times

User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Re: Metro M4 express Wifi problems

Post by johantonissen »

Bump. Any help please? Have a class of 40 students waiting tor a reply :)

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Metro M4 express Wifi problems

Post by adafruit_support_carter »

We have tested the exact code on my device and it works, so my guess the problem is somewhere in windows, hence my fiddling with firewall and antivirus-virus.
This seems like it could be the likely cause. You were able to get things working on one setup by altering these settings?

User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Re: Metro M4 express Wifi problems

Post by johantonissen »

Yes but my students are reporting this does not work for them. Isn't there maybe any help with how to fine the cause of this problem? I guess there are no socets available as seen in the code, but what does that mean? I can't be the first one reporting this problem. Isn't there a faq somewhere that helps me setting up this connection properly? The 'disable all security' might work for me but it doesn't tell me what wrong, therefore the solution can also be just me being lucky.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Metro M4 express Wifi problems

Post by adafruit_support_carter »

Just tested your code here on a Win10 machine and it worked OK. Didn't need to mess with any firewall settings.

What Windows version are the students using? Is there more than one Metro running at a time? Is more than one student trying to connect at a time?

User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Re: Metro M4 express Wifi problems

Post by johantonissen »

I think all my students run windows 10. I made sure only one connection is made at a time.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Metro M4 express Wifi problems

Post by adafruit_support_carter »

It's working here. It's working on your machine. So it must be something specific to the students machines. Who manages the students laptops?


Is the initial connection working OK? Students can see "ESP32" in wifi list and connect to it?

Does the info here get printed as expected?

Code: Select all

#convert to host and port to address
addr = socket.getaddrinfo(HOST, PORT)[0][4]
print(socket.getaddrinfo(HOST, PORT))

#connect to address
s = socket.socket()
s.connect(addr)
print(s)

User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Re: Metro M4 express Wifi problems

Post by johantonissen »

Hi there,

This is the output that students are getting. They use their own laptops. Students are able to make connections to the wifi access point.

The problem gets stuck in an infinite loop in this piece of code:

Code: Select all

    while client_sock.socknum == NO_SOCK_AVAIL:
        client_sock_num = esp.socket_available(server_socket.socknum)
        client_sock = socket.socket(socknum = client_sock_num)
        if client_sock.socknum != NO_SOCK_AVAIL: print('connected')
The loop is never exited because the following condition is not met:

Code: Select all

if client_sock.socknum != NO_SOCK_AVAIL: print('connected')
Attachments
MicrosoftTeams-image.png
MicrosoftTeams-image.png (96.3 KiB) Viewed 1990 times

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Metro M4 express Wifi problems

Post by adafruit_support_carter »

Who manages the students laptops?

Since this appears to be something specific to the Windows configuration on the students' laptops, someone with administrative privileges will need to investigate and determine what settings are preventing the complete connection.

User avatar
adafruit2
 
Posts: 22144
Joined: Fri Mar 11, 2005 7:36 pm

Re: Metro M4 express Wifi problems

Post by adafruit2 »

maybe dont use port 80? use a high port like 8000 if its aviailable? you could try using netcat to verify if the port is available.

User avatar
johantonissen
 
Posts: 7
Joined: Sat Oct 12, 2019 9:06 am

Re: Metro M4 express Wifi problems

Post by johantonissen »

the student manages their own laptops (i'm from the netherlands i dont know if this is uncommon in the US).

the port change worked, amazing thank you! I should have thought of that.

Thanks again!

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Metro M4 express Wifi problems

Post by adafruit_support_carter »

the port change worked, amazing thank you! I should have thought of that.
Me too :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”