임베디드 보드/아두이노

Arduino NANO + ENC28J60 이더넷 쉴드

ZEROWIN.TECH 2021. 6. 23. 16:21
728x90

 

 

NANO + ENC28J60 ethernet shield

아두이노 나노 쉴드 형태로 쉽게 나노와 결합하여 이더넷만 꽂으면 웹서버 또는 웹클라이언트를 비롯한 IOT 디바이스로 간단히 사용가능합니다.

 

 

enc28j60 MQTT 라이브러리

  • Use of an ENC28J60 shield (or module) with MQTT PubSub

https://github.com/consolacion/ENC28J60-MQTT

 

consolacion/ENC28J60-MQTT

Use of an ENC28J60 shield (or module) with MQTT PubSub - consolacion/ENC28J60-MQTT

github.com

UIPEthernet 라이브러리

https://github.com/UIPEthernet/UIPEthernet

 

UIPEthernet/UIPEthernet

UIPEthernet library for Arduino IDE,Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32, Nordic nRF51, Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network ...

github.com

 

소스경로
e:\PROJECT_SMARTFARM_1908\BOARD_COMPILER\nano_rj45\enc28j60MQTT_uni\

DHCP 테스트

// Arduino demo sketch for testing the DHCP client code
//
// Original author: Andrew Lindsay
// Major rewrite and API overhaul by jcw, 2011-06-07
//
// License: GPLv2

#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];


void setup () {
  Serial.begin(57600);
  Serial.println(F("\n[testDHCP]"));

  Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();

  // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));

  Serial.println(F("Setting up DHCP"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));

  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.netmask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
}
  • EtherCard 라이브러이 샘플파일

mqtt 통신 테스트 

mqtt 서버로 데이터를 올리는 테스트를 합니다.

정상적으로 데이터 업로드됨

  • 샘플소스코드
//#define UIP_CONF_UDP             1
#include <UIPEthernet.h>
#include "PubSubClient.h"
//#include "DHT.h"
#define CLIENT_ID       "NANO_RJ45"
#define TOKEN       ""
#define PUBLISH_DELAY   10* 1000
//#define DHTPIN        3
//#define DHTTYPE     DHT11
bool statusD4 = HIGH;
bool statusD5 = HIGH;
bool statusD6 = HIGH;
bool statusD7 = HIGH;
bool statusD8 =HIGH;
bool statusD9 = HIGH;
bool statusD10 =LOW;
bool statusD11=LOW;
bool statusD12=LOW;
int valueA0;
int valueA1;
float valueA2; //made a float to store a  battery value
int valueA3;
int  upTime = 0;
byte i;
String ip = "192.168";
String MAC = "";
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};


EthernetClient ethClient;
PubSubClient mqttClient;
//DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

void setup() {
  pinMode(4, INPUT_PULLUP);//
  pinMode(5, INPUT_PULLUP);//
  pinMode(6, INPUT_PULLUP);//
  pinMode(7, INPUT_PULLUP);//
  pinMode(8, INPUT_PULLUP);//
  pinMode(9, INPUT_PULLUP);//
  pinMode(10,INPUT_PULLUP);// 
  pinMode(11,INPUT_PULLUP);//
  pinMode(12,INPUT_PULLUP);//

  // setup serial communication
  Serial.begin(9600);
  /*
    while(!Serial) {};
    Serial.println(F("MQTT Arduino Demo"));
    Serial.println();
  */
  // setup ethernet communication using DHCP
  if (Ethernet.begin(mac) == 0) {
    //Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
  }

  // ip="192.168";
  for (i = 2; i < 4; i++) {
    ip = ip + ".";
    ip = ip + String (Ethernet.localIP()[i]);
  }
  
  //---mac
  for (i = 0; i < 6; i++) {
    if ((mac[i]) <= 0x0F) {
      MAC = MAC + "0"; //zet er zonodig een '0' voor
    }
    MAC = MAC + String ((mac[i]), HEX);
    MAC = MAC + ":";
  }
  MAC[(MAC.length()) - 1] = '\0'; //verwijder laatst toegevoegde ":"

  
    Serial.println(F("Ethernet configured via DHCP"));
    Serial.print("IP address: ");
    Serial.println(Ethernet.localIP());
    Serial.println();
  
  // setup mqtt client
  mqttClient.setClient(ethClient);
  //  mqttClient.setServer( "raspberrypi.local",1883);

  mqttClient.setServer("farm1st.ddns.net", 1883);
  Serial.println(F("MQTT client configured"));

  // setup DHT sensor
  // dht.begin();
  //Serial.println(F("DHT sensor initialized"));

  //Serial.println();
  // Serial.println(F("Ready to send data"));
  previousMillis = millis();
}

void loop() {
  /*
  statusD4 = digitalRead(4);//
  statusD5 = digitalRead(5);// 
  statusD6 = digitalRead(6);// 
  statusD7 = digitalRead(7);
  statusD8 = digitalRead(8);
  statusD9 = digitalRead(9);
  statusD10=digitalRead(10);
  statusD11=digitalRead(11);
  statusD12=digitalRead(12);
  upTime = millis() / 60000;
  valueA0 = analogRead(A0);//
  valueA1 = analogRead(A1); 
  valueA2 = analogRead(A2);
  valueA2 = (valueA2 * 5) / 1023.0;
   valueA3 = analogRead(A3); //garage
  // it's time to send new data? */
  if (millis() - previousMillis > PUBLISH_DELAY) {
    sendData();
    previousMillis = millis();
  }

  mqttClient.loop();
}

long randomNumber = 0;

void sendData() {

  char msgBuffer[7];//was 20

  randomSeed(analogRead(A0));
  
  float h = random(100); // dht.readHumidity();
  float t = random(100)-50; // dht.readTemperature();

  Serial.println("sendData");

  if (mqttClient.connect(CLIENT_ID, TOKEN, NULL)) {
    
        char data[64] = { 0x00, };
        sprintf(data, "{\"humidity\" : %d, \"temperature\" : %d }", (int)h, (int)t);
        Serial.print("publish=");
        Serial.println(data);
        
        mqttClient.publish("v1/devices/me/telemetry", data);
    

        //mqttClient.publish("temp", deblank(dtostrf(t, 4, 1, msgBuffer)));//
        //mqttClient.publish("humid", deblank(dtostrf(h, 3, 0, msgBuffer)));
            
    /*
    mqttClient.publish("home/d4", (statusD4 == HIGH) ? "OPEN" : "CLOSED");//
    mqttClient.publish("home/d5", (statusD5 == HIGH) ? "OPEN" : "CLOSED");//
    mqttClient.publish("home/d6", (statusD6 == HIGH) ? "OPEN" : "CLOSED");//
    mqttClient.publish("home/d7", (statusD7 == HIGH) ? "OPEN" : "CLOSED");//
    mqttClient.publish("home/d8", (statusD8 == HIGH) ? "OPEN" : "CLOSED"); //
    mqttClient.publish("home/d9", (statusD9 == HIGH) ? "OPEN" : "CLOSED"); //
    mqttClient.publish("home/d10",(statusD10 == HIGH) ? "OPEN" : "CLOSED"); //
    mqttClient.publish("home/d11",(statusD11== HIGH) ? "OPEN" : "CLOSED"); //
    mqttClient.publish("home/d12",(statusD12== HIGH) ? "OPEN" : "CLOSED"); //
    mqttClient.publish("home/A0", deblank(dtostrf(valueA0, 4, 0, msgBuffer)));
    mqttClient.publish("home/A1", deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
    mqttClient.publish("home/A2", deblank(dtostrf(valueA2, 4, 2, msgBuffer)));
    mqttClient.publish("home/A3", deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
    mqttClient.publish("home/versie", "enc28j60MQTT_uni");
    mqttClient.publish("home/ip", ip.c_str());
    mqttClient.publish("home/u", deblank(dtostrf(upTime, 6, 0, msgBuffer)));//uptime
    mqttClient.publish("home/mac", MAC.c_str()); */
   
    mqttClient.disconnect();
    
  }
}

char * deblank(char *str) {
  char *out = str;
  char *put = str;

  for (; *str != '\0'; ++str) {

    if (*str != ' ') {
      *put++ = *str;
    }
  }
  *put = '\0';
  return out;
}

 

'임베디드 보드 > 아두이노' 카테고리의 다른 글

아두이노 사운드 레벨 감지 센서  (0) 2021.12.31
W5500 Wiznet Ethernet Shield  (0) 2021.07.08
ATMEGA2560 boot Writing  (0) 2021.05.24
유량 패들 스위치 테스트  (0) 2021.05.16
유량센서 YF-S201 테스트  (0) 2021.05.02