65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
#if !defined(DEBUG_FLAG)
|
|
#define DEBUG_FLAG false
|
|
#endif
|
|
|
|
#include <SPI.h>
|
|
#include <Ethernet2.h>
|
|
#if DEBUG_FLAG
|
|
#include <HardwareSerial.h>
|
|
#endif
|
|
|
|
class NetClass {
|
|
private:
|
|
#if DEBUG_FLAG
|
|
HardwareSerial* _debug;
|
|
#endif
|
|
EthernetClass* e;
|
|
void init(void);
|
|
// Mac-Adress of the Ethernet-Shield
|
|
uint8_t mac[6] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0xA4 };
|
|
public:
|
|
#if DEBUG_FLAG
|
|
NetClass(HardwareSerial *debug);
|
|
#else
|
|
NetClass(void);
|
|
#endif
|
|
};
|
|
|
|
#if DEBUG_FLAG
|
|
NetClass::NetClass(HardwareSerial *debug) {
|
|
this->_debug = debug;
|
|
this->init();
|
|
}
|
|
#else
|
|
NetClass::NetClass(void) {
|
|
this->init();
|
|
}
|
|
#endif
|
|
|
|
void NetClass::init(void) {
|
|
pinMode(10, OUTPUT);
|
|
digitalWrite(10, HIGH);
|
|
#if DEBUG_FLAG
|
|
this->_debug->println("Start Network!");
|
|
this->_debug->println("DHCP Request");
|
|
#endif
|
|
this->e = &Ethernet;
|
|
if (this->e->begin(mac) == 0) {
|
|
#if DEBUG_FLAG
|
|
this->_debug->println("DHCP Failed, Use Link-Local Adress");
|
|
#endif
|
|
this->e->begin(mac, IPAddress(169, 254, 1, 177), IPAddress(169, 254, 0, 1), IPAddress(255, 255, 0, 0));
|
|
}
|
|
#if DEBUG_FLAG
|
|
this->_debug->print("Ethernet Started! IP: ");
|
|
IPAddress ip = Ethernet.localIP();
|
|
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
|
// print the value of each byte of the IP address:
|
|
this->_debug->print(ip[thisByte], DEC);
|
|
this->_debug->print(".");
|
|
}
|
|
this->_debug->println();
|
|
#endif
|
|
}
|
|
|