Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Using an AZDelivery W5100 Ethernet Shield (Read 143 times)
Bob4K
Junior Member
**
Offline


Posts: 16
Joined: Dec 30th, 2015
Using an AZDelivery W5100 Ethernet Shield
Jan 25th, 2025 at 4:47am
Print Post  
Hi,

I need to replicate with an Arduino Uno Rev3 and an AZDelivery W5100 Ethernet Shield the following code that work with VS2022 on Windows 10:

Code
Select All
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
                                                                                                               
//#pragma comment(lib, "ws2_32.lib") // Link with ws2_32.lib
                                                                                                                
int main(int argc, char **argv)
{
  WSADATA wsaData;
  const char *hostname = "barco";
  struct hostent *host_entry;
  char ip_buffer[INET_ADDRSTRLEN];
  int port = 43748;
  int sock, send_result, rc;
  struct sockaddr_in server_addr;
                                                                                                                                                                                                                     
  if (argc != 2)
   { printf("SendBarco usage: SendBarco \"string\"\n");
     return 1;
   }
                                                                                                                
  // Initialize Winsock
  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
   { printf("SendBarco: WSAStartup failed with error: %d\n", WSAGetLastError());
     return 1;
   }
                                                                                                                
  // Retrieve host information
  host_entry = gethostbyname(hostname);
  if (host_entry == NULL)
   { printf("SendBarco: gethostbyname failed with error: %d\n", WSAGetLastError());
     WSACleanup();
     return 1;
   }
                                                                                                                
  // Convert the address from binary to text
  inet_ntop(AF_INET, host_entry->h_addr_list[0], ip_buffer, sizeof(ip_buffer));
  //printf("IP address of %s: %s\n", hostname, ip_buffer);
                                                                                                                
  // Create a socket
  sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock == INVALID_SOCKET)
   { printf("Socket creation failed with error: %d\n", WSAGetLastError());
     WSACleanup();
     return 1;
   }
                                                                                                                
  // Set up the server address struct
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = inet_addr(ip_buffer);
                                                                                                                
  // Connect to the server
  if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
   { printf("Connection failed with error: %d\n", WSAGetLastError());
     closesocket(sock);
     WSACleanup();
     return 1;
   }
                                                                                                                
  // Send the message
  usleep(500000);  // closesocket() too fast prevents working
  send_result = send(sock, argv[1], (int)strlen(argv[1]), 0);
  usleep(500000);  // closesocket() too fast prevents working
  if (send_result == SOCKET_ERROR)
   {  printf("Send failed with error: %d\n", WSAGetLastError());
      rc = 1;
   }
  else rc = 0;
                                                                                                                
  // Receive response from server
  /* char buffer[1024];
  int recv_result;
                                                                                                                
  recv_result = recv(sock, buffer, sizeof(buffer) - 1, 0);
  if (recv_result == SOCKET_ERROR)
    printf("Receive failed with error: %d\n", WSAGetLastError());
  else
   { buffer[recv_result] = '\0';  // Null-terminate the received data
     printf("Received: %s\n", buffer);
   } */
                                                                                                                
  // Cleanup
  closesocket(sock);
  WSACleanup();
  return 0;
}
 



The purpose is to send an arbitrary string (it would not be main() with the Arduino but a subroutine) to a Barco projector.

That would be triggered by an IR signal, that part already works using the IRLib-master library.

Stuff could be hardcoded, no need like above to retrieve the IP address from the hostname, unless it's easy.

I have installed the following libraries:

Code
Select All
WIZ_Ethernet_Library-IDE1.6.x-master
WIZ_Ethernet_Library-master
W5200-Ethernet-Shield-master
 



I don't know how to start and what to use for that kind of code.

Any suggestions?
« Last Edit: Jan 25th, 2025 at 4:48am by Bob4K »  
Back to top
 
IP Logged
 
Bob4K
Junior Member
**
Offline


Posts: 16
Joined: Dec 30th, 2015
Re: Using an AZDelivery W5100 Ethernet Shield
Reply #1 - Jan 25th, 2025 at 2:46pm
Print Post  
I got an answer from ChatGPT.

In case anybody is interested:

Code
Select All
#include <SPI.h>
#include <Ethernet.h>

// Network and server settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Replace with your shield's MAC address
IPAddress server(192, 168, 1, 100); // Replace with the IP of the server
int port = 1234;                    // Replace with the server's port number

EthernetClient client;

void setup() {
  Serial.begin(115200);

  // Start Ethernet connection
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true);
  }

  delay(1000); // Allow some time for the Ethernet shield to initialize
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  // Connect to the server
  if (client.connect(server, port)) {
    Serial.println("Connected to server!");

    // Message to send
    const char* message = "Hello, server!";
    client.print(message); // Send the message
    Serial.print("Sent message: ");
    Serial.println(message);
  } else {
    Serial.println("Connection to server failed.");
  }
}

void loop() {
  // Keep the connection alive
  if (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.write(c); // Print server response to Serial Monitor
    }
  } else {
    client.stop();
    Serial.println("Disconnected from server.");
    while (true);
  }
}
 

  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint