Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The ENC28J60 is an Ethernet controller with SPI interface, commonly used to add Ethernet connectivity to microcontrollers like Arduino. To get started, you'll need to install the "UIPEthernet" library, which provides support for the ENC28J60 Ethernet controller.
- You can install the library using the Arduino IDE by going to Sketch > Include Library > Manage Libraries, then search for "UIPEthernet" and click Install.
- Here's a simple example of using the ENC28J60 with an Arduino to obtain an IP address through DHCP and make an HTTP GET request:
- Replace "example.com" with the domain name or IP address of the server you want to connect to. This example code will print the HTTP response to the serial console.
- */
- #include <UIPEthernet.h>
- #include <utility/logging.h> // Optional, for logging purposes
- // Pins for the ENC28J60 (change these if needed)
- #define ENC28J60_CS 10 // Chip Select pin
- #define ENC28J60_SCK 13 // SPI Clock pin
- #define ENC28J60_MISO 12 // SPI MISO pin
- #define ENC28J60_MOSI 11 // SPI MOSI pin
- // Create an instance of the Ethernet client
- EthernetClient client;
- void setup() {
- // Initialize the serial connection for logging
- Serial.begin(9600);
- // Configure the SPI pins
- pinMode(ENC28J60_CS, OUTPUT);
- pinMode(ENC28J60_SCK, OUTPUT);
- pinMode(ENC28J60_MISO, INPUT);
- pinMode(ENC28J60_MOSI, OUTPUT);
- // Initialize the ENC28J60 Ethernet controller
- Ethernet.init(ENC28J60_CS);
- // Enable logging for the UIPEthernet library
- // (Optional, comment this line if you don't need logging)
- uip_set_loglevel(UIP_LOG_LEVEL);
- // Start the Ethernet connection using DHCP
- if (Ethernet.begin() == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- for (;;) { // Do nothing if the DHCP configuration fails
- }
- }
- // Print the local IP address
- Serial.print("Local IP: ");
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // Attempt to connect to the server
- if (client.connect("example.com", 80)) {
- // Send an HTTP GET request to the server
- client.println("GET / HTTP/1.1");
- client.println("Host: example.com");
- client.println("Connection: close");
- client.println();
- } else {
- Serial.println("Connection failed");
- }
- // Read the server's response
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- }
- // Close the connection
- client.stop();
- // Wait for a while before making another request
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement