Advertisement
pleasedontcode

Network Communication rev_03

Jun 20th, 2025
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Network Communication
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-06-20 07:50:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Detect and initialize the Ethernet connection */
  21.     /* using the Ethernet2 library and the W5500 module. */
  22.     /* Retrieve and display the assigned IP address to */
  23.     /* confirm successful connection. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <SPI.h>
  30. #include <Ethernet2.h> //https://github.com/adafruit/Ethernet2
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF SPI PINS *****/
  41. const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
  42. const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
  43. const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
  44. const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool ethernet_W5500_RST_PIN_D2_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float ethernet_W5500_RST_PIN_D2_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. EthernetClass Ethernet;
  56.  
  57. /****** Ethernet connection variables *****/
  58. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
  59. IPAddress ip(192, 168, 0, 177); // Static IP address
  60. const char* server = "www.google.com"; // Server to connect
  61. EthernetClient client;
  62.  
  63. void setup(void)
  64. {
  65.   // Initialize serial communication for debugging
  66.   Serial.begin(9600);
  67.   while (!Serial) {
  68.     ; // wait for serial port to connect. Needed for Leonardo only
  69.   }
  70.  
  71.   // Initialize the Ethernet reset pin
  72.   pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
  73.   digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH); // Keep W5500 out of reset
  74.  
  75.   // Initialize SPI pins
  76.   pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
  77.   SPI.begin();
  78.  
  79.   // Detect and initialize Ethernet connection
  80.   Serial.println("Initializing Ethernet...");
  81.   if (Ethernet.begin(mac) == 0) {
  82.     Serial.println("Failed to configure Ethernet using DHCP");
  83.     Serial.println("Trying static IP...");
  84.     Ethernet.begin(mac, ip);
  85.   }
  86.   delay(1000); // Allow Ethernet hardware to initialize
  87.  
  88.   // Retrieve and display the assigned IP address
  89.   IPAddress localIP = Ethernet.localIP();
  90.   Serial.print("Ethernet IP Address: ");
  91.   Serial.println(localIP);
  92. }
  93.  
  94. void loop(void)
  95. {
  96.   // put your main code here, to run repeatedly:
  97.   updateOutputs();
  98.  
  99.   // Optional: Add code to check connection or send data
  100.   // For demonstration, attempt to connect to server periodically
  101.   static unsigned long lastAttempt = 0;
  102.   if (millis() - lastAttempt > 10000) { // every 10 seconds
  103.     lastAttempt = millis();
  104.     Serial.println("Connecting to server...");
  105.     if (client.connect(server, 80)) {
  106.       Serial.println("Connected to server");
  107.       client.println("GET / HTTP/1.1");
  108.       client.println("Host: www.google.com");
  109.       client.println("Connection: close");
  110.       client.println();
  111.     } else {
  112.       Serial.println("Connection to server failed");
  113.     }
  114.   }
  115.  
  116.   // Read server response
  117.   if (client.available()) {
  118.     char c = client.read();
  119.     Serial.print(c);
  120.   }
  121.  
  122.   // Check if server disconnected
  123.   if (!client.connected()) {
  124.     Serial.println();
  125.     Serial.println("Disconnecting.");
  126.     client.stop();
  127.   }
  128. }
  129.  
  130. void updateOutputs()
  131. {
  132.   digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
  133. }
  134.  
  135. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement