Advertisement
microrobotics

Network Module ENC28J60

Apr 5th, 2023
2,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. You can install the library using the Arduino IDE by going to Sketch > Include Library > Manage Libraries, then search for "UIPEthernet" and click Install.
  5.  
  6. 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:
  7.  
  8. 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.
  9. */
  10. #include <UIPEthernet.h>
  11. #include <utility/logging.h> // Optional, for logging purposes
  12.  
  13. // Pins for the ENC28J60 (change these if needed)
  14. #define ENC28J60_CS 10 // Chip Select pin
  15. #define ENC28J60_SCK 13 // SPI Clock pin
  16. #define ENC28J60_MISO 12 // SPI MISO pin
  17. #define ENC28J60_MOSI 11 // SPI MOSI pin
  18.  
  19. // Create an instance of the Ethernet client
  20. EthernetClient client;
  21.  
  22. void setup() {
  23.   // Initialize the serial connection for logging
  24.   Serial.begin(9600);
  25.  
  26.   // Configure the SPI pins
  27.   pinMode(ENC28J60_CS, OUTPUT);
  28.   pinMode(ENC28J60_SCK, OUTPUT);
  29.   pinMode(ENC28J60_MISO, INPUT);
  30.   pinMode(ENC28J60_MOSI, OUTPUT);
  31.  
  32.   // Initialize the ENC28J60 Ethernet controller
  33.   Ethernet.init(ENC28J60_CS);
  34.  
  35.   // Enable logging for the UIPEthernet library
  36.   // (Optional, comment this line if you don't need logging)
  37.   uip_set_loglevel(UIP_LOG_LEVEL);
  38.  
  39.   // Start the Ethernet connection using DHCP
  40.   if (Ethernet.begin() == 0) {
  41.     Serial.println("Failed to configure Ethernet using DHCP");
  42.     for (;;) { // Do nothing if the DHCP configuration fails
  43.     }
  44.   }
  45.  
  46.   // Print the local IP address
  47.   Serial.print("Local IP: ");
  48.   Serial.println(Ethernet.localIP());
  49. }
  50.  
  51. void loop() {
  52.   // Attempt to connect to the server
  53.   if (client.connect("example.com", 80)) {
  54.     // Send an HTTP GET request to the server
  55.     client.println("GET / HTTP/1.1");
  56.     client.println("Host: example.com");
  57.     client.println("Connection: close");
  58.     client.println();
  59.   } else {
  60.     Serial.println("Connection failed");
  61.   }
  62.  
  63.   // Read the server's response
  64.   while (client.connected()) {
  65.     if (client.available()) {
  66.       char c = client.read();
  67.       Serial.print(c);
  68.     }
  69.   }
  70.  
  71.   // Close the connection
  72.   client.stop();
  73.  
  74.   // Wait for a while before making another request
  75.   delay(5000);
  76. }
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement