top of page
Crescer (fundo transparente)_Negativo_pn
Você sabia que a Crescer ensina, fornece e faz soluções de automação e IoT com Arduino e ESP32?

Fale com um especialista

2.png
3.png
1.png

PLC32 - Ethernet cabeada

Atualizado: há 7 dias

O PLC32 não possui conexão WiFi. Entretanto, para garantir conectividade, o PLC32 vem equipado com hardware Ethernet integrado, permitindo uma conexão fácil à rede através de cabo.

 

Neste blog, vamos ver como utilizar a conexão Ethernet cabeada do PLC32.



Sumário

 


1. Visão geral

 

Como já mencionado, o PLC32 não possui hardware para conexão WiFi, o que faz com que ele não necessite de homologação pela Anatel. No entanto, se preferir uma conexão WiFi, recomendamos o uso de um roteador/repetidor já homologado pela Anatel, conectado ao PLC32 via cabo. A partir desse ponto, você poderá acessar a rede WiFi com facilidade.

 

Na figura a seguir, podemos ver um exemplo de roteador/repetidor que pode ser utilizado em conjunto com o PLC32.



O circuito utilizado é baseado no módulo LAN8720, sendo plenamente compatível com as bibliotecas existentes para este dispositivo. Na figura a seguir, apresentamos a seção da placa destinada à conexão Ethernet.



Na tabela a seguir, apresentamos as conexões do LAN8720 com os pinos do ESP32.




2. Como utilizar a ethernet cabeada

 

No exemplo deste blog, vamos utilizar a conexão Ethernet para controlar um relé ligado ao PLC32 pelo computador. Na figura a seguir, podemos ver a topologia do sistema montado.



Como base para programar o PLC32, utilizamos os exemplos SimpleWiFiServer e ETH_LAN8720 disponíveis na IDE do Arduino. A seguir, veja o código desenvolvido.


#include <HTTPClient.h>
#include <ETH.h>

static bool eth_connected = false;

#define   rele  13

WiFiServer server(80);


void WiFiEvent(WiFiEvent_t event)
{
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case ARDUINO_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;
    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress());
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed());
      Serial.println("Mbps");
      eth_connected = true;
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default:
      break;
  }
}

void setup()
{
  Serial.begin(115200);
  WiFi.onEvent(WiFiEvent);
  ETH.begin( 1 , 32, 14, 4 , ETH_PHY_LAN8720);
  server.begin();
  pinMode(rele,OUTPUT);
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 13 on.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 13 off.<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c; // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(rele, HIGH);  // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(rele, LOW);    // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

Agora podemos carregar o código na placa. Assim que ela se conectar à rede, podemos obter o IP do dispositivo através da porta serial, conforme mostrado na figura a seguir.



Digitando esse IP na barra de endereço do navegador, poderemos ligar e desligar o relé, clicando na opção indicada, como podemos ver na figura a seguir:



Neste blog, vimos como utilizar a conexão Ethernet do PLC32. Essa interface permite a conexão da placa com redes Ethernet cabeada, sem utilizar WiFi e sem necessidade de homologação na Anatel.


Se você deseja ver mais detalhes sobre o PLC32, acessar o seu manual ou adquirir uma unidade, clique neste link.


Posts recentes

Ver tudo
ícone do whatsapp

Fale com um
especialista:

bottom of page