Search code examples
pythonsocketsarduinoethernet

Arduino EthernetServer receiving unexpected values from Python client


I'm attempting to talk an Arduino with an ethernet shield from a Python client. I'm getting (what seem to be) junk values on the Arduino server side as soon as the Python client tries to connect. I receive these values before even trying send any data on the Python side.

I've made a minimal example, based on the EthernetServer example from the Arduino docs. The only thing I've changed (other than the network specs) is I've made the server push the data it receives over the Serial connection instead of back to the client.

Arduino server code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xCD, 0x38};  
byte ip[] = {10, 1, 30, 210};
byte gateway[] = { 10, 1, 30, 0 };
byte subnet[] = { 255, 255, 0, 0 };

EthernetServer server = EthernetServer(50000);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();

  Serial.begin(9600);
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    Serial.write(client.read());
  }
}

Python client code, which I run once the Arduino has been given time to start up:

import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('10.1.30.210', 50000))
client_socket.close()

I get nothing on the Serial stream before running the Python code. As soon as it has run, I get a (seemingly) never ending stream of bytes down the Serial. Here's a sample:

j!ðé¸Xta¼#ò´çÈÂlÊC`»­\S513ÙCJèÙÒ Í([Ôø#Q_$(Ìs.)RlO&BÏ$6êçÁFøå¨ÝõVé]üEº_Tà*4V[¡¬íÆVÓñÿpQDÒÐþoñ«.çbþÐS-8&ÓÒøHüZ¡ª£¸öÙ ÂÛÖ¨{R{&<(]$¿*PXøMÃ>i]Û¿Ãké2E)¢4WuKÕÎ%CsÉ9ïÓàä-fZàH5N6ºÞ¹A¬}CaY,Ä]Dîj!ðé¸Xta¼#ò´çÈÂlÊC`»­\S513ÙCJèÙÒ Í([Ôø#Q_

Does anyone know what could cause this? Where is the Arduino reading these values from? How can I make it stop doing that, and start reading what I send with client_socket.send() instead?


Solution

  • It turned out to be an issue with the version of the Arduino dev environment in Fedora's package manager. I tried to update to the latest version from source, but was stymied by some errors from the Java part of the build process. In the end, I loaded the same code I was using onto the Arduino after compiling in a Windows VM; it worked perfectly.

    Not the best solution, but it got the job done.