0

I am trying to connect my Arduino Nano Connect RP2040 with my machine using Python (3.8.8). I want to use WiFi communication protocol and I am currently working on a client-server socket.
Since I am not so familiar with this communication protocol I am following this thread which looks to perform good results. Of course, this thread talks about Ethernet and I am readapting using WiFiNina library which has been developed by Arduino in order to use the module.

Here the code I am running on Arduino:

#include <SPI.h>
#include <WiFiNINA.h>

#include <ArduinoJson.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0x08, 0x3A, 0xF2, 0xB1, 0x9D, 0xE0 };
IPAddress ip(192,168,43,98);

// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,0,4); 

// Initialize the WiFi client library
// with the IP address and port of the server 
WiFiClient client;

char ssid[] = "xxxxx";     // the name of your network
char username[] = "xxxxxxx";
char pass[] = "xxxxxxx";

void setup() {
  // start the Wifi connection:
  WiFi.beginEnterprise(ssid, username, pass);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 13380)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  //JSON stuff
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["sensor"] = "gps";
  root["time"] = 42;

  JsonArray& data = root.createNestedArray("data");
  data.add(48.756080);
  data.add(2.302038);  

  char json[100];
  root.printTo(json);
  Serial.print(json);

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  if (client.connected()) {
      client.print(json);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

I have used some functions from WiFiNina (for example .beginEnterprise() in order to connect Arduino to the network). For what server concerns, here the code in Python:

import socket
import sys
import json

# host = '127.0.0.1'
host = '192.168.0.4'
port = 13380
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)
# server_socket.connect(address)
print ("waiting for a connection . . .")
conn, address = server_socket.accept()
print ("Connection established: "), address

while True:
        output = conn.recv(84048);
        if output.strip() == "disconnect":
                conn.close()
                sys.exit("Disconnect message received - terminate the connection")
                conn.send("dack")
        elif output:
                print(output)
                data = json.load(output)
                print(data)

I am not interested in sending JSON data, like I have already mentioned I am just readapt the code in thread. I actually need to send data in real-time, but step-by-step :-)

The problem is that I cannot establish the connection between Arduino (client) and Python (server).

From server part, I actually got this error:

Traceback (most recent call last):
  File "server_arduino.py", line 18, in <module>
    server_socket.bind(address)
OSError: [WinError 10049] The requested address is not valid in its context

For some reasons which I actually did not understand yet, if I would change host in 127.0.0.1 (following suggestions here) the server part starts to work, and it stucks in waiting for a connection . . . printed in my console, because it needs a client to connect.

At this stage, running my Arduino code I got this:

connecting...
connection failed
{"sensor":"gps","time":42,"data":[48.75608,2.302038]}
disconnecting.

So, it does mean to me that I was able to connect to WiFi, but not to server.

Does anyone give me any suggestions?
I know I am really beginner and any suggestions would be really appreciated.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
valio
  • 33
  • 8
  • You can check the WiFI connection status with [WiFi.status()](https://www.arduino.cc/en/Reference/WiFiNINAStatus). And binding server to `127.0.0.1` makes it only accept connection from localhost i.e. the same computer. Are you sure `192.168.0.4` is the actual IP of the computer? Try `0.0.0.0`. – gre_gor Nov 26 '21 at 20:44
  • I have checked WiFi Status and I got `3` which should means `WL_CONNECTED` and it sounds good to me. By changing `192.168.0.4` in `0.0.0.0` nothing changed. Also, checking with `ipconfig` from cmd, what I got as IP is `IPv4 Address. . . . . . . . . . . : 10.200.9.36` . Using this IP I again got `Connection Failed` – valio Nov 29 '21 at 09:45
  • Some updates: I have changed WiFi connection from Uni connection to hotspot mobile one from my smartphone. I got connection, and I have some problems in unpacking data (it is a minor problem for now). Then, I think I found my bottleneck: Enterprise connection. Has anyone idea how to handle socket with this scenario? – valio Nov 29 '21 at 13:44
  • 1
    Could be that the university is filtering the traffic, so that devices on the WiFi network can't see each other. – gre_gor Nov 29 '21 at 14:00
  • Yes, I actually think this is a bottleneck. Any ideas? – valio Nov 29 '21 at 16:01

0 Answers0