Server Life Tester

Image.png

Image.png
Image.png

COMPONENTS:
-Arduino Uno
-Ethernet ENC28J60
-LCD I2C

CABLING:
ENC28J60 > Arduino
GND -> GND pin
VCC -> 5V pin
CS -> pin 10
SI -> pin 11
SO -> pin 12
SCK -> pin 13

Download and install Ethercard library from https://github.com/jcw/ethercard

CODE:
#include <avr/pgmspace.h>
#include <EtherCard.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define CS_PIN        10
#define pinToToggle   9
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// myip,gwip,mask,dns Only needed if using a Static IP
//static byte myip[]  = { 192,168,2,50 };
//static byte gwip[]  = { 192,168,2,1 };
//static byte mask[]  = { 255,255,255,0 };
//static byte dns[]   = { 8,8,8,8 };
byte Ethernet::buffer[700];
static int32_t timer;
static int polling;
//change these to the IPs that you want to ping
const char srv1[]  PROGMEM = “10.0.10.1”;
const char srv2[]  PROGMEM = “8.8.8.8”;
// Make sure this list matches the above list
const char* const servers[] PROGMEM = {srv1,srv2};
#define NUMSRVRS (sizeof(servers) / sizeof(servers[0]))
#define MAXCYCLES 3
#define PING_DELAY 2000000
static int cycles;
char sbuffer[30];
static int i;
float ms;
long interval              = 500;
unsigned long currentTime  = 0;
unsigned long previousTime = 0;
void blink() {
  currentTime = millis();
  if(currentTime – previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(pinToToggle, !digitalRead(pinToToggle));
  }
}
static void gotPinged (byte* ptr) {
  ether.printIp(“>>> ping from: “, ptr);
}
void setup () {
  pinMode(pinToToggle,OUTPUT);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
// Uncomment to wait till serial port is ready
//  while (!Serial) { }
  Serial.println(F(“Ping Servers\n”));
  if (!ether.begin(sizeof Ethernet::buffer, mymac, CS_PIN))
    Serial.println(F(“Failed to access Ethernet controller\n”));
  strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[0])));
  ether.parseIp(ether.hisip,sbuffer);
// Comment these two lines and uncomment staticSetup
// to use static vs DHCP IP address
  if (!ether.dhcpSetup())
    Serial.println(F(“DHCP failed\n”));
//  if (!ether.staticSetup(myip, gwip, dns, mask))
//    Serial.println(F(“Failed to set Static IP”));
  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println(“Gateway found”);
  ether.printIp(“IP:  “,     ether.myip);
  ether.printIp(“GW:  “,     ether.gwip);
  ether.printIp(“Netmask: “, ether.netmask);
  ether.printIp(“DNS IP: “,  ether.dnsip);
  Serial.println();
  ether.registerPingCallback(gotPinged);
  timer   = -9999999;
  polling = 0;
  i       = NUMSRVRS;
  cycles  = 0;
  digitalWrite(pinToToggle, !digitalRead(pinToToggle));
  delay(2000);
  digitalWrite(pinToToggle, !digitalRead(pinToToggle));
}
void loop () {
// Uncomment to change to no delay
// if ( ( (micros() – timer) >= PING_DELAY) || !polling  ) {
//Comment this next line, if the above line is uncommented
if ((micros() – timer) >= PING_DELAY) {
   if (polling) {
     ether.printIp(“\n”,ether.hisip);
     Serial.println(F(“Timeout\n”));
     lcd.setCursor(0, 1);
     lcd.print(“Timeout”);
     if (cycles >= MAXCYCLES) while (1) blink();
   }
   else {
     polling = 1;
     lcd.clear();
     lcd.setCursor(0, 0);
     ++i;
     if (i >= NUMSRVRS) i = 0;
     strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[i])));
     lcd.print(sbuffer);
     ether.parseIp(ether.hisip,sbuffer);
     ether.printIp(“Pinging: “, ether.hisip);
   }
   cycles += 1;
   ether.clientIcmpRequest(ether.hisip);
   timer = micros();
  }
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
    Serial.print(”  “);
    ms = (micros() – timer) * 0.001;
    Serial.print(ms, 3);
    Serial.println(” ms\n”);
    lcd.setCursor(0, 1);
    lcd.print(ms);
    lcd.print(” ms”);
    polling   = 0;
    cycles    = 0;
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s