• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

HUM: ENC28J60 ethernet

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Croduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

In this tutorial, you will learn about the Ethernet module ENC28J60 which allows us to connect to the Internet using Croduino. We can use the module when we want to control a certain process or a device via Internet (a local network to which the module is connected and a cellphone or computer using which we change the parameters), or if we want to send readings of different sensors to a web server and store them there. To use the module, you must know the basics of HTML in order to make simple things using the module and Croduino. If we want to display sensor data on the site graphically, we will also have to use CSS, and if we only want to display text, HTML is sufficient.

Sensor characteristics:
•Power supply: 3.3V or 5V
•Communication: SPI
•Ethernet connector: HR911105A

HOW DOES THE MODULE WORK?

For starters, we will learn about how the module works and the way we connect it to Croduino and the local network. The module has a built-in ENC28J60 chip that allows the communication between the local network and Croduino. The chip on the microcontroller communicates via SPI communication, which makes it easy to connect and use. For a better understanding of how Internet protocols that are used to connect to the Internet work with this and similar modules, and what IP and MAC addresses are, you can watch our tutorial about the Ethernet module W5100.

 

HOW TO CONNECT THE MODULE?

The module is easy to connect because it uses SPI communication and it is necessary to connect the appropriate pins of the module to the Croduino pins that are in charge of SPI communication. The scheme shows which pin of the module is connected to Croduino, and we have inserted a LED diode to pin 4 so that we could test the code given in the example immediately. In order for the module to work properly, it requires an external 5V power supply, but it is also necessary to connect the GND pin of the power supply to the GND of the Croduino to have a reference point.

ARDUINO CODE

To use the ENC28J60 Ethernet module with Croduino, it is necessary to download a library for using the Ethernet module because the library that is installed with Arduino IDE is not suitable for our module. The library we will use in the example is UIPEthernet. If you do not know how to install the library, check our tutorial explaining the installation process. The example of the code in the web browser opens a web page on which we have two buttons for turning the LED diode connected to the pin 4 on or off. The page also displays the state of the LED so that we know if it is on or off.

 

#include "UIPEthernet.h" //including the UIPEthernet library used for working with ENC28J60 Ethernet shield
int LED1=4;  //the pin to which we connect the LED diode
int val_LED1=0;//the variable to which we save the state of the LED

String readString; //the string that serves for saving the requests of the users visiting the page
EthernetServer server = EthernetServer(80);  //initialization of the library that performs all the functions related to the Web server (receiving requests, processing requests and sending data)
//80 within the brackets signifies the port of our web page (web page, i.e. HTTP always has the port 80)

void setup()
{

 pinMode(LED1, OUTPUT);//setting the pin to which the LED is connected as output
 digitalWrite(LED1, LOW);//setting the pin to LOW, i.e. turning the LED diode off
  Serial.begin(9600);//serial communication initialization 
  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};//setting the MAC address 
  IPAddress myIP(192,168,1,156);//setting the IP address (we enter this address to the web browser to access the page)
  Ethernet.begin(mac,myIP);//initialize the library and Ethernet with the given MAC and IP address
  server.begin();//the server starts working
 Serial.print(F("The server is active on the IP address")); //Display the IP address using which we can access our server
 Serial.print(Ethernet.localIP());
}

//the function in which we check the request from the page and turn the LED diode on or off 
void StateLED(EthernetClient cl)
{  
  //if we have "on1" returned to the string, we turn the LED diode on and display the message on the Serial monitor
          if(readString.indexOf("on1") >0)
          { digitalWrite(LED1, HIGH);   
            Serial.println("The LED diode is on");
          }
 //if we have "off2" returned to the string, we turn the LED diode off and display the message on the Serial monitor         
          if(readString.indexOf("off2") >0)
          { digitalWrite(LED1, LOW);    
            Serial.println("The LED diode is off");
          } 

}
//the function for changing the state of the LED on the page (if we turn it on or off)
void Changetext(EthernetClient cl){
    val_LED1 = digitalRead(LED1);//to the val_LED1 variable, we save the state of the pin to which the LED is connected
    Serial.println(val_LED1);//displaying pin state on the Serial monitor

    if(val_LED1 == HIGH){//checking whether the LED diode is on
      cl.println("ON");//displaying that the LED diode is on, on the page
    }
    else {//if the LED diode is off, displaying that it is off
        cl.println("OFF");
    }  
}

void loop(){
  //
  EthernetClient client = server.available();//checking the state of the server, whether we have someone connected to it 
 //if someone is connected to the server, we check whether that person is connected in the while loop and while being connected, we read the requests of that person
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();//reading the user's request character by character 
       
        if (readString.length() < 100) {
         //saving the character we have read to the String readString
          readString += c; 
       
        } 
        //when the request is completed, we display our page as a response
        if (c == '\n') {     
          client.println("HTTP/1.1 200 OK"); //send a new page
          client.println("Content-Type: text/html");
          client.println(F("Connection: close"));
          client.println();
          client.println(F("<!DOCTYPE HTML>"));
          client.println(F("<html>"));
          client.println(F("<title>Croduino Web server</title></html>"));
          client.println(F("Croduino web server :)"));
          client.println("<head>");
          client.println("<meta http-equiv='refresh'content='3 ;url=$$$$'>");//the part of the code that refreshes the page every 3 seconds
          client.println("</head>");
          client.println("<body>");
          client.println("<h1>Controlling the LED diode using Croduino</h1>");
          // tipke na našoj stranici
          client.println("State of the LED diode:  "); 
          StateLED(client);//the function in which we check the state and turn the LED on/off
          Changetext(client); //the function using which we display the state of the LED diode 
          //the button for turning the LED on
          client.println("<input type=button value=ON onmousedown=location.href='/on1'>");
          client.println(" ");  
          //the button for turning the LED off
          client.println("<input type=button value=OFF onmousedown=location.href='/off2'>");
          client.println(" "); 
          client.println("</body>");
          client.println("</html>");
          delay(1);//wait a moment for the user's cellphone or computer to receive the web page 
          //end the connection with the user
          client.stop();
          readString="";//delete the String in which we had the user's request saved, in order to be able to save another request
        }
      }
    }
  }
}
Leave a Reply