Need help getting data to computer via ethernet

#include <SPI.h>
#include <Ethernet.h>
#include <Encoder.h>
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 229);
EthernetServer server(3000);
// Create Encoder objects
#define ENCODER_DO_NOT_USE_INTERRUPTS
Encoder encoder1(22, 23);
Encoder encoder2(24, 25);
Encoder encoder3(26, 27);
Encoder encoder4(28, 29);
Encoder encoder5(30, 31);
Encoder encoder6(32, 33);
Encoder encoder7(34, 35);
Encoder encoder8(36, 37);
Encoder encoder9(38, 39);
Encoder encoder10(40, 41);
Encoder encoder11(42, 43);
Encoder encoder12(44, 45);

const int interval = 1000;

unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print ("server is at");
  Serial.println(Ethernet.localIP());
encoder2.write(2);
encoder1.write(2);
encoder2.write(2);
encoder3.write(2);
encoder4.write(2);
encoder5.write(2);
encoder6.write(2);
encoder7.write(2);
encoder8.write(2);
encoder9.write(2);
encoder10.write(2);
encoder11.write(2);
encoder12.write(2);
}

void loop() {
  EthernetClient client = server.available();
 // Read the values of the rotary encoders
long encoder1Value = encoder1.read();
long encoder2Value = encoder2.read();
long encoder3Value = encoder3.read();
long encoder4Value = encoder4.read();
long encoder5Value = encoder5.read();
long encoder6Value = encoder6.read();
long encoder7Value = encoder7.read();
long encoder8Value = encoder8.read();
long encoder9Value = encoder9.read();
long encoder10Value = encoder10.read();
long encoder11Value = encoder11.read();
long encoder12Value = encoder12.read();

// Check if the value of any of the encoders is greater than 722 or less than 2
if (encoder1Value > 722) encoder1.write(2);
if (encoder2Value > 722) encoder2.write(2);
if (encoder3Value > 722) encoder3.write(2);
if (encoder4Value > 722) encoder4.write(2);
if (encoder5Value > 722) encoder5.write(2);
if (encoder6Value > 722) encoder6.write(2);
if (encoder7Value > 722) encoder7.write(2);
if (encoder8Value > 722) encoder8.write(2);
if (encoder9Value > 722) encoder9.write(2);
if (encoder10Value > 722) encoder10.write(2);
if (encoder11Value > 722) encoder11.write(2);
if (encoder12Value > 722) encoder12.write(2);
//reverse
if (encoder1Value < 2) encoder1.write(722);
if (encoder2Value < 2) encoder2.write(722);
if (encoder3Value < 2) encoder3.write(722);
if (encoder4Value < 2) encoder4.write(722);
if (encoder5Value < 2) encoder5.write(722);
if (encoder6Value < 2) encoder6.write(722);
if (encoder7Value < 2) encoder7.write(722);
if (encoder8Value < 2) encoder8.write(722);
if (encoder9Value < 2) encoder9.write(722);
if (encoder10Value < 2) encoder10.write(722);
if (encoder11Value < 2) encoder11.write(722);
if (encoder12Value < 2) encoder12.write(722);



  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    
    String packet = "Simulator encoder Packet: \nEncoder 1: " + String(encoder1Value/2) + "\nEncoder 2: " + String(encoder2Value/2)
    + "\nEncoder 3: " + String(encoder3Value/2) + "\nEncoder 4: " + String(encoder4Value/2) + "\nEncoder 5: " + String(encoder5Value/2)
    + "\nEncoder 6: " + String(encoder6Value/2)+ "\nEncoder 7: " + String(encoder7Value/2)
   + "\nEncoder 8: " + String(encoder8Value/2)+ "\nEncoder 9: " + String(encoder9Value/2)+ "\nEncoder 10: " + String(encoder10Value/2)
   + "\nEncoder 11: " + String(encoder11Value/2)+ "\nEncoder 12: " + String(encoder12Value/2) + "\nPacket Close";

    // Send the message
    client.println(packet);
    Serial.println(packet);

    //Update time
    previousMillis = currentMillis;
  }
}

so pretty much i need to upload the data from 12 encoders so it can be read by an application. i have 0 experience in networking. i got an ethernet shield today, thought this would work but ive tried changing the mac, ip, and port. tried the built it libraries and tried following tutorials.

i got the sunfounder shield, tried 2 different cables aswell.

im wondering if im missing a step. ive tried reading the data through a python script, and through telnet on cmd. im just at a loss and my brain hurts, if i could get some help or explanation that would be great.

im using an arduino mega 2560

what TCP client program are you using on the mac? have you setup the firewall?
can you upload the Serial Monitor output as text (not an image)
can you ping the ethernet board IP from your mac
try running File>Examples>EthernetGeneric>webserver

as i said im brand new to networking, also im not on a mac (windows), ive tried using telnet, wrote a script on python

import socket

# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.0.229', 3000))

while True:
    # Receive data from the server
    data = client_socket.recv(1024).decode()
    print(data)

client_socket.close()

i have not setup a firewall, the output is all text and it prints to serial fine, i cant ping it, webserver does not work.

i should add im connecting my arduino shield directly to my system not the router.

you may require a crossover ethernet cable
many interfaces will automatically sort out the problem some will not
why not connect to the router - if you do make sure you use the the same IP addressing range

what are the ethernet adapter settings in the Control Panel? e.g.

you will probably have to setup the firewall to allow your client program to transmit outbound packets

in loop() you need to wait for a client to connect then send your packets, e.g. try

#include <SPI.h>
#include <Ethernet.h>
//#include <Encoder.h>
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 44);  //0, 229);
EthernetServer server(10000);
// Create Encoder objects
/*#define ENCODER_DO_NOT_USE_INTERRUPTS
Encoder encoder1(22, 23);
Encoder encoder2(24, 25);
Encoder encoder3(26, 27);
Encoder encoder4(28, 29);
Encoder encoder5(30, 31);
Encoder encoder6(32, 33);
Encoder encoder7(34, 35);
Encoder encoder8(36, 37);
Encoder encoder9(38, 39);
Encoder encoder10(40, 41);
Encoder encoder11(42, 43);
Encoder encoder12(44, 45);*/

const int interval = 1000;

unsigned long previousMillis = 0;

void setup() {
  Serial.begin(115200);  //9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at");
  Serial.println(Ethernet.localIP());
  /*encoder2.write(2);
encoder1.write(2);
encoder2.write(2);
encoder3.write(2);
encoder4.write(2);
encoder5.write(2);
encoder6.write(2);
encoder7.write(2);
encoder8.write(2);
encoder9.write(2);
encoder10.write(2);
encoder11.write(2);
encoder12.write(2);*/
}

EthernetClient client;

void loop() {
  if (!client) {
    // Listen for incoming client requests.
    client = server.available();
    if (!client) return;
    Serial.println("Client connected");
  }
  // Loop while the client is connected.
  if (client.connected()) {
    // Read available bytes.
    if (client.available()) {
      {  // Read the values of the rotary encoders
         /*long encoder1Value = encoder1.read();
long encoder2Value = encoder2.read();
long encoder3Value = encoder3.read();
long encoder4Value = encoder4.read();
long encoder5Value = encoder5.read();
long encoder6Value = encoder6.read();
long encoder7Value = encoder7.read();
long encoder8Value = encoder8.read();
long encoder9Value = encoder9.read();
long encoder10Value = encoder10.read();
long encoder11Value = encoder11.read();
long encoder12Value = encoder12.read();

// Check if the value of any of the encoders is greater than 722 or less than 2
if (encoder1Value > 722) encoder1.write(2);
if (encoder2Value > 722) encoder2.write(2);
if (encoder3Value > 722) encoder3.write(2);
if (encoder4Value > 722) encoder4.write(2);
if (encoder5Value > 722) encoder5.write(2);
if (encoder6Value > 722) encoder6.write(2);
if (encoder7Value > 722) encoder7.write(2);
if (encoder8Value > 722) encoder8.write(2);
if (encoder9Value > 722) encoder9.write(2);
if (encoder10Value > 722) encoder10.write(2);
if (encoder11Value > 722) encoder11.write(2);
if (encoder12Value > 722) encoder12.write(2);
//reverse
if (encoder1Value < 2) encoder1.write(722);
if (encoder2Value < 2) encoder2.write(722);
if (encoder3Value < 2) encoder3.write(722);
if (encoder4Value < 2) encoder4.write(722);
if (encoder5Value < 2) encoder5.write(722);
if (encoder6Value < 2) encoder6.write(722);
if (encoder7Value < 2) encoder7.write(722);
if (encoder8Value < 2) encoder8.write(722);
if (encoder9Value < 2) encoder9.write(722);
if (encoder10Value < 2) encoder10.write(722);
if (encoder11Value < 2) encoder11.write(722);
if (encoder12Value < 2) encoder12.write(722);*/



        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {

          /*  String packet = "Simulator encoder Packet: \nEncoder 1: " + String(encoder1Value/2) + "\nEncoder 2: " + String(encoder2Value/2)
    + "\nEncoder 3: " + String(encoder3Value/2) + "\nEncoder 4: " + String(encoder4Value/2) + "\nEncoder 5: " + String(encoder5Value/2)
    + "\nEncoder 6: " + String(encoder6Value/2)+ "\nEncoder 7: " + String(encoder7Value/2)
   + "\nEncoder 8: " + String(encoder8Value/2)+ "\nEncoder 9: " + String(encoder9Value/2)+ "\nEncoder 10: " + String(encoder10Value/2)
   + "\nEncoder 11: " + String(encoder11Value/2)+ "\nEncoder 12: " + String(encoder12Value/2) + "\nPacket Close";
*/
          // Send the message
          client.println("hello client");  //packet);
          Serial.println("hello");         //packet);

          //Update time
          previousMillis = currentMillis;
        }
      }
    }
  }
}

when I connect a client serial monitor displays

server is at192.168.1.44
Client connected
hello
hello
hello
hello

and client displays

TCPchatClient: IP address BB-DELL2/192.168.1.68 port 10000
Connecting to TCPcharServer on to 192.168.1.44 socket 10000
Server contacted OK sending 
Connected OK, you can send and receive messages
Client received: hello client
Client received: hello client
Client received: hello client
Client received: hello client
Client received: hello client
Client received: hello client
Client received: hello client
Client received: hello client

Edit: running the following Python program

import socket

host_ip, server_port = "192.168.1.44", 10000
data = " Hello how are you?\n"

# Initialize a TCP client socket using SOCK_STREAM
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Establish connection to TCP server and exchange data
tcp_client.connect((host_ip, server_port))
tcp_client.sendall(data.encode())

# Read data from the TCP server and close the connection
while True:
    received = tcp_client.recv(1024)
    print ("Bytes Received: {}".format(received.decode()))

it displays

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

========================= RESTART: C:/Python311/xx1.py =========================
Bytes Received: hello client
Bytes Received: 

Bytes Received: hello client
Bytes Received: 

Bytes Received: hello client
Bytes Received: 

Bytes Received: hello client
Bytes Received: 


here are the adapter settings, not connecting to router because it is far away from my project and wouldnt be a good solution to have a cable running across the house. how would i setup the firewall to allow my client program to transmit outbound packets?

follow the instructions how-do-i-allow-games-and-apps-through-my-firewall
on my PC the program to allow thru the firewall was C:\Python311\python.exe

tried it, still no connection. ive even tried bridging the connection between my wifi and arduino shield in control panel and setting an ip for my shield, still nothing.

image
i pinged it after all this stuff and i get this, but cant connect and actually get anything from it.

This program is using the Ethernet library to create a server on the Arduino and connect it to the Ethernet network. The program reads the values of 12 rotary encoders and sends them over the network to a connected client.

It looks like you are trying to ensure that the encoder values stay within a certain range by checking if the values are greater than 722 or less than 2 and resetting them accordingly. This is done inside the loop function and that run constantly in the loop.

It is not recommended to use 12 encoders all at the same time, since it could overload the Arduino's available resources, and the arduino will slow down, this could affect the accuracy and speed of the encoders reading.

Also, you might want to try using an interrupt-based library to read the encoders instead of polling them continuously, this will help you to save some processing power and could have a more precise reading.

In addition, the if statement that check the value of each encoder 12 times is not very efficient, you can use an array to store the encoders and use a for loop to iterate through the array and check the value of each encoder instead.

I suggest you to first test your project with a few encoders to make sure that everything is working as expected and then add the remaining encoders incrementally

looks as though you are attempting to access a device on a different subnet
can you provide details of your network, e.g. on windows run cmd (Command prompt) and enter ipconfig
if you a using a company/university/college network it may check the connected devices mac address and if it is not on its allowed list it will not allow connection - you have to apply to be allowed connection

The getting encoders part of thos project is not what im worried about, ive tested it- it works fine. I just need to be able to read them over ethernet (just using serial and sending the packet it displays correct values and i want to send the exact same string (packet) via ethernet. Ill test the options others have provided tomorrow when i have the chance.

So i just had a thought-

So for some more context this project is for my dad, and hes building a plane sim. He wants to use 12 encoders to control certain values on his flight sim. Ideally i just plug the ethernet into a computer and he makes the program for it, but i wanted to make sure that the ethernet connection actually works before handing it off.

So my dad has our network setup so that omly certain mac addresses can access it. This is probably it huh? Ill talk to him about it on thursday when i get the chance, but any other ideas would be appreciated. This completely flew over my head.

if you are not connecting to a router but connecting the Arduino Ethernet directly in the Ethernet port of a PC you need to change the Ethernet Adapter settings
have a look at share-internet-connection-windows-10

it works! thank you so much for the help. i read somewhere else that to connect directly you need to turn off sharing and create a bridge, but i guess it was the opposite lol

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.