Arduino Uno + W5500 + OMRON FQ2 smart camera /Ethernet Problem

@countrypaul

Okay I will try it and write back ! Thank you for your help
That library does not support my w5500 module.
Found some on github that should work but it is not, like I am not sure if I don't ping my computer or the library is not working :smiley:

#include <SPI.h>
#include <Ethernet2.h>
#include <ICMPPing.h>

#define ETH_CS_PIN 10
#define ETH_RST_PIN 7
const byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
IPAddress pingAddr(10, 5, 5, 101); // ip address to ping

SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup()
{
  // start Ethernet
  Serial.begin(115200);

  initEthernet();
  Serial.println("init success");

}

void loop()
{
  ICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
  }
  Serial.println(buffer);
  delay(500);
}


bool initEthernet() {

  Serial.println(F("Obtaining Local IP..."));
  bool DHCPsuccess = false;

  while (!DHCPsuccess) {
    digitalWrite(ETH_RST_PIN, LOW);
    pinMode(ETH_RST_PIN, OUTPUT);
    delay(100);
    pinMode(ETH_RST_PIN, INPUT);
    digitalWrite(ETH_RST_PIN, HIGH);
    delay(200);

    Ethernet.init(ETH_CS_PIN);

    if (Ethernet.begin(mac)) {
      Serial.print(F("DHCP IP: "));
      Serial.println(Ethernet.localIP());

      DHCPsuccess = true;
    } else {
      //timed out 60 secs.
      Serial.println(F("Timeout."));
      Serial.println(F("Check Ethernet cable."));
      Serial.println(F("Retring DHCP..."));

    }

  }




}

And the serial is:
19:17:28.493 -> Obtaining Local IP...
19:17:28.493 -> ⸮

Anyone have some tips? At this point I have no idea what else can I try to move on

From your latest post it again seems that it is failing in either Ethernet.init or Ethernet.begin. Can you put a printLn between them such as Serial.printLn(F("trying etthernet.Begin...")).

I am beginning to feel as though the Ethernet shield might be faulty. After trying the above, and you omit the wire from the Digital Pin 7 to the Ethernet reset and then retry the sketch?

Have you managed to get anything to work successfully with the Ethernet shield?

Okay i will try to do it and reply to you with the results !
Yes actually I did something like a "webserver" on which I hade 2 buttons ( LED ON and LED OFF) and by clicking a it an LED was turning on and off so I guess the module is working as it should be :smiley:

countrypaul:
From your latest post it again seems that it is failing in either Ethernet.init or Ethernet.begin. Can you put a printLn between them such as Serial.printLn(F("trying etthernet.Begin...")).

I am beginning to feel as though the Ethernet shield might be faulty. After trying the above, and you omit the wire from the Digital Pin 7 to the Ethernet reset and then retry the sketch?

Have you managed to get anything to work successfully with the Ethernet shield?

Okay so i put the serial print between ethernet init and begin and the serial is:

Zrzut ekranu 2021-04-11 105453.png

Zrzut ekranu 2021-04-11 105453.png

@countrpaul
Okay quick update
I changed the code:

#include <Ethernet2.h>
#include <SPI.h>

  //byte mac[] = { 0xBC, 0x5F, 0xF4, 0xD2, 0xDC, 0x2A }; 
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA };
  byte ip[] = { 10, 5, 5, 15  }; //
  byte server[] = { 10, 5, 5, 10 }; //ip of server (camera)
  int tcp_port = 9876; //port of the server (camerA)

  EthernetClient client;

void setup() {
  Serial.begin(9600);
Ethernet.init(10);
Serial.println("Trying Ethernet Begin");

Ethernet.begin(mac, ip);

delay(1000);
Serial.println("Connecting...");

if (client.connect(server, tcp_port)) { 
  Serial.println("Connected");
  client.println();
} else{
  Serial.println("Error");
}
}
void loop() {

if (client.available()) {
  if(Serial.available()) {
    char s = Serial.read();
    client.write(s); 
    char c = client.read();
    Serial.print(c);
  }
}
if (!client.connected()) {
  Serial.println();
  Serial.println("Disconecting.");
  client.stop();
  for(;;);
}
}

And now the serial is like in the pic below:

Zrzut ekranu 2021-04-11 120629.png

Zrzut ekranu 2021-04-11 120629.png

Can you try changing :

byte server[] = { 10, 5, 5, 10 }; //ip of server (camera)

to:

IPAddress server[] = (10, 5, 5, 10); //ip of server (camera)

I have seen problems reported that a char string can give problems and they appear to be resolved by using IPAddress.

There also seem to be examples where simply calling Client.Connect again after a call to Client.Connected fails and Client.Stop has been called appears to work. Don't really know why but might be worth trying - appears to be a work around rather than a proper solution.

  //byte mac[] = { 0xBC, 0x5F, 0xF4, 0xD2, 0xDC, 0x2A }; 
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA };
  IPAddress ip( 10, 5, 5, 15  ); //
  IPAddress server(10, 5, 5, 10); //ip of server (camera)
  int tcp_port = 9876; //port of the server (camerA)

Still the results are the same...
Where do you want me to put the Client.Connect again?

@countrypaul
Anyway i started to think that maybe i have to create something like a "socket". Because in TCP/IP Builder it says "Create Socket". I am not good at networking but started to think that maybe the way of connecting to the camera is bad and needs something more before being able to listen.

The Client.Connect( server, port) creates a socket on you Uno and links that to the IP address of the server to its socket numbered port. There is no need to try and create an additional socket - in fact I'm not even sure the functions to do so are exported from the Ethernet library!

You currently have this section of code

if (!client.connected()) {
  Serial.println();
  Serial.println("Disconecting.");
  client.stop();
  for(;;);

What I am suggesting is change it to:

if (!client.connected()) {
  Serial.println();
  Serial.println("Disconecting.");
  client.stop();
  if (client.connect(server, tcp_port)) {
  Serial.println("RE-connected");
} else{
  Serial.println("Error reconnecting");
}

See if it helps. You could set a variable to the value returned from client.connect and
print that out after "Error reconnecting" that might at least give us a clue. Something
like:

int Errcode= client.connect(server, tcp_port);
if (ErrCode == SUCCESS){
  Serial.println("RE-connected");
} else{
  Serial.println("Error reconnecting code", ErrCode);
}

Okay so I tried the first part ( chnagning the if(!client.connected).
And now it is doing:
Disconeting
RE-connected
all the time in the loop :smiley:

Zrzut ekranu 2021-04-11 154351.png

Zrzut ekranu 2021-04-11 154351.png

If you type into the window so there is some serial information to send does that alter the output sequence?

I think no, becasue it just gets connected and reconected in a loop.
But anyway maybe i should just try it by UDP, there is an option in FQ2 software to set it as a FQ2 UDP server. But now how can i make an UDP client program?

These are my settings in OMRON software (pic).
So now the network card ip is: 10.5.5.101
camera ip: 10.5.5.10
output udp ip ? (what is that) : 10.5.5.111
111

I have an example code of udp send receive string:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 5, 5, 111);

unsigned int localPort = 9600;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}


How should I edit that ?
random mac for my board
ip adress ? which one ?
and the port should be 9600?

The reason I suggested the test is because the Serial is read between the connect and disconnect, it might tell us if the disconnect happens immediately, or if the serial input is read and processed before the disconnect.

When you say network card ip address is that the network card in the PC or the one for the Arduino?

I can't work out at the moment where that 111 address comes from, can you ping it from the PC?

Network card adress = adress of my pc card (static)
I also don't understand the 111 adress :smiley: but in that program that's the only thing i see about it. What confuses me is that i tried to monitor TCP and UDP on wireshark and on TCP i see when i do an inspection there are several packages that are coming because of the thing i did. But when i switched to UDP i do not see any changes to be honest. Moreover on UDP i could not read the logs even on PC. I will check that 111 adress in the datasheet and write back tomorrow. Anyway on sunday probaly i will get some other ethernet module from my friend so I will try to do the same thing but with this module.

Maybe we missed the obvious, the Arduino is at the 111 address, but how the Omron software got that is a mystery to me?

Not got used to this new forum software, so I miss posts for sometime before discovering them!

@countrypaul
I DID IT !
But not on my module ... :smiley:
I borrowed ENC28J60 and after like 30 minutes i figured it out and got the response of my logs in the serial. BUT that's not over because it's kinda slow like it gets connected and dissconected in a loop and it gets the data from the port each (5 seconds i think (code)). I need to make it without these "connected" "disconected" all the time and:

  1. Make it listening "constantly"
  2. Add the tcp server function, should i just combine tcp server and tcp client examples in one code or it's not that easy ?
/*
 * UIPEthernet TcpClient example.
 *
 * UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
 * Ethernet-shield.
 *
 * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
 *
 *      -----------------
 *
 * This TcpClient example gets its local ip-address via dhcp and sets
 * up a tcp socket-connection to 192.168.0.1 port 5000 every 5 Seconds.
 * After sending a message it waits for a response. After receiving the
 * response the client disconnects and tries to reconnect after 5 seconds.
 *
 * Copyright (C) 2013 by Norbert Truchsess <norbert.truchsess@t-online.de>
 */

#include <UIPEthernet.h>

EthernetClient client;
signed long next;

void setup() {

  Serial.begin(9600);
 IPAddress myIP(10,5,5,105);
  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  Ethernet.begin(mac,myIP);

//  Serial.print("localIP: ");
//  Serial.println(Ethernet.localIP());
//  Serial.print("subnetMask: ");
//  Serial.println(Ethernet.subnetMask());
//  Serial.print("gatewayIP: ");
//  Serial.println(Ethernet.gatewayIP());
//  Serial.print("dnsServerIP: ");
//  Serial.println(Ethernet.dnsServerIP());

  next = 0;
}

void loop() {

  if (((signed long)(millis() - next)) > 0)
    {
      next = millis() + 5000;
      Serial.println("Client connect");
      // replace hostname with name of machine running tcpserver.pl
//      if (client.connect("server.local",5000))
      if (client.connect(IPAddress(10,5,5,10),9876))
        {
          Serial.println("Client connected");
          client.println("DATA from Client");
          while(client.available()==0)
            {
              if (next - millis() < 0)
                goto close;
            }
          int size;
          while((size = client.available()) > 0)
            {
              uint8_t* msg = (uint8_t*)malloc(size);
              size = client.read(msg,size);
              Serial.write(msg,size);
              free(msg);
            }
close:
          //disconnect client
          Serial.println("Client disconnect");
          client.stop();
        }
      else
        Serial.println("Client connect failed");
    }
}

That "-1.000 0.000 " are the logs

I'm not sure what your TCPserver code is so can't really answer your question about combining them at present.

The TCPclient code you have disconnects explicitly at the end of loop. As a test I would change the if (client.connect... code to check if it is already connected using if (client.connected() and only connect if it is not connected, then comment out the client.stop() near the end of loop. This way you can determine if the delay is caused by the connect/disconnect continually happening with minimal other changes.

You are using Serial.write to output the data from the server to the serial port, I think you should probably follow that with Serial.PrintLn (""); just to break things up and make it easier to read.