Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Network Communication
- - Source Code NOT compiled for: Arduino Nano 33 BLE
- - Source Code created on: 2025-06-20 07:50:07
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Detect and initialize the Ethernet connection */
- /* using the Ethernet2 library and the W5500 module. */
- /* Retrieve and display the assigned IP address to */
- /* confirm successful connection. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <Ethernet2.h> //https://github.com/adafruit/Ethernet2
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
- const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
- const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
- const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ethernet_W5500_RST_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ethernet_W5500_RST_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EthernetClass Ethernet;
- /****** Ethernet connection variables *****/
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
- IPAddress ip(192, 168, 0, 177); // Static IP address
- const char* server = "www.google.com"; // Server to connect
- EthernetClient client;
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // Initialize the Ethernet reset pin
- pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
- digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH); // Keep W5500 out of reset
- // Initialize SPI pins
- pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
- SPI.begin();
- // Detect and initialize Ethernet connection
- Serial.println("Initializing Ethernet...");
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- Serial.println("Trying static IP...");
- Ethernet.begin(mac, ip);
- }
- delay(1000); // Allow Ethernet hardware to initialize
- // Retrieve and display the assigned IP address
- IPAddress localIP = Ethernet.localIP();
- Serial.print("Ethernet IP Address: ");
- Serial.println(localIP);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs();
- // Optional: Add code to check connection or send data
- // For demonstration, attempt to connect to server periodically
- static unsigned long lastAttempt = 0;
- if (millis() - lastAttempt > 10000) { // every 10 seconds
- lastAttempt = millis();
- Serial.println("Connecting to server...");
- if (client.connect(server, 80)) {
- Serial.println("Connected to server");
- client.println("GET / HTTP/1.1");
- client.println("Host: www.google.com");
- client.println("Connection: close");
- client.println();
- } else {
- Serial.println("Connection to server failed");
- }
- }
- // Read server response
- if (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- // Check if server disconnected
- if (!client.connected()) {
- Serial.println();
- Serial.println("Disconnecting.");
- client.stop();
- }
- }
- void updateOutputs()
- {
- digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement