Advertisement
pleasedontcode

Network Communication rev_04

Jun 20th, 2025
352
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 compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-06-20 07:57:53
  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. /****** Ethernet connection variables *****/
  55. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
  56. IPAddress ip(192, 168, 0, 177); // Static IP address
  57. const char* server = "www.google.com"; // Server to connect
  58. EthernetClient client;
  59.  
  60. void setup(void)
  61. {
  62.   // Initialize serial communication for debugging
  63.   Serial.begin(9600);
  64.   while (!Serial) {
  65.     ; // wait for serial port to connect. Needed for Leonardo only
  66.   }
  67.  
  68.   // Initialize the Ethernet reset pin
  69.   pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
  70.   digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH); // Keep W5500 out of reset
  71.  
  72.   // Initialize SPI pins
  73.   pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
  74.   SPI.begin();
  75.  
  76.   // Detect and initialize Ethernet connection
  77.   Serial.println("Initializing Ethernet...");
  78.   if (Ethernet.begin(mac) == 0) {
  79.     Serial.println("Failed to configure Ethernet using DHCP");
  80.     Serial.println("Trying static IP...");
  81.     Ethernet.begin(mac, ip);
  82.   }
  83.   delay(1000); // Allow Ethernet hardware to initialize
  84.  
  85.   // Retrieve and display the assigned IP address
  86.   IPAddress localIP = Ethernet.localIP();
  87.   Serial.print("Ethernet IP Address: ");
  88.   Serial.println(localIP);
  89. }
  90.  
  91. void loop(void)
  92. {
  93.   // put your main code here, to run repeatedly:
  94.   updateOutputs();
  95.  
  96.   // Optional: Add code to check connection or send data
  97.   // For demonstration, attempt to connect to server periodically
  98.   static unsigned long lastAttempt = 0;
  99.   if (millis() - lastAttempt > 10000) { // every 10 seconds
  100.     lastAttempt = millis();
  101.     Serial.println("Connecting to server...");
  102.     if (client.connect(server, 80)) {
  103.       Serial.println("Connected to server");
  104.       client.println("GET / HTTP/1.1");
  105.       client.println("Host: www.google.com");
  106.       client.println("Connection: close");
  107.       client.println();
  108.     } else {
  109.       Serial.println("Connection to server failed");
  110.     }
  111.   }
  112.  
  113.   // Read server response
  114.   if (client.available()) {
  115.     char c = client.read();
  116.     Serial.print(c);
  117.   }
  118.  
  119.   // Check if server disconnected
  120.   if (!client.connected()) {
  121.     Serial.println();
  122.     Serial.println("Disconnecting.");
  123.     client.stop();
  124.   }
  125. }
  126.  
  127. void updateOutputs()
  128. {
  129.   digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement