Ethernet OFF sdcard On-> sdcard On ethernet OFF

Hi all,
I use an ethernet shield on an Arduino UNO...

I want to use Ethernet to communicate with a computer via a client socket and under certain conditions (if the network is not connected) the Arduino will retrieve the data on the SD card...

how can i sort out those conditions to run the line of code..

sorry for asking, I'm new to this...

here are the lines of code that I have..

/**********************************
 * Ethernet Server Arduino WIZ5100
 * www.ardutech.com
 * 
 *********************************/
#include <Ethernet.h>
#include <WiegandMulti.h>
#include <SdFat.h>
#include <WiegandMulti.h>
#include <string.h>

String pch;
WIEGANDMULTI wg;
WIEGANDMULTI wg2;

SdFat sd;
SdFile file;
const uint8_t SD_CHIP_SELECT = 4;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,189);
EthernetServer server(80);
char str;
const int relay1 = 8; //pin2
const int relay2 = 9; //pin3

int relayON = HIGH; //relay nyala
int relayOFF = LOW; //relay mati

void Reader1D0Interrupt(void)
{
  wg.ReadD0();
}

void Reader1D1Interrupt(void)
{
  wg.ReadD1();
}

void Reader2D0Interrupt(void)
{
  wg2.ReadD0();
}

void Reader2D1Interrupt(void)
{
  wg2.ReadD1();
}

bool findStringInFile(const char *filename, const char *searchString, size_t &pos) {
  File file = sd.open("data.txt", FILE_READ);
  if (!file) return false; // File not found

  size_t searchLen = strlen(searchString);
  size_t index = 0;
  pos = 0;

  while (file.available()) {
    char ch = file.read();
    if (ch == searchString[index]) {
      index++;
      if (index == searchLen) { // Match found
        pos = file.position() - searchLen;
        file.close();
        return true;
      }
    } else {
      index = (ch == searchString[0]) ? 1 : 0; // Reset index or start new match (not foolproof)
    }
  }

  file.close();
  return false; // String not found
}


void setup() {
  Serial.begin(115200);
  wg.begin(2,3,Reader1D0Interrupt,Reader1D1Interrupt);
  wg2.begin(4,5,Reader2D0Interrupt,Reader2D1Interrupt);
  //wg2.begin(4,5,Reader2D0Interrupt,Reader2D1Interrupt);
 pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay1, relayOFF);
  digitalWrite(relay2, relayOFF);
  Ethernet.begin(mac, ip);
 pinMode(10, OUTPUT);
  server.begin();

//ACCESS TO SD
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) 
  {
    if (sd.card()->errorCode()) {
      Serial.println("SD initialization failed.");
    } else if (sd.vol()->fatType() == 0) {
      Serial.println("Can't find a valid FAT16/FAT32 partition.");
    } else {
      Serial.println("Can't determine error type");
    }
    return;
  }
}

void loop() 
{

  EthernetClient client = server.available();
      if (client.available() > 0) 
      {
         if(wg.available())
            {
          client.print("A");
          client.print(wg.getCode());
          Serial.print(wg.getCode());
        
        delay(100);    
        }
        
        if(wg2.available())
        {
          
        Serial.print(wg2.getCode());
        client.print("B");
        client.print(wg2.getCode());
        
          delay(100); 
        }
            }
      
            if (client.available() > 0) 
            {
              str = client.read();       
              if(str=='1')
              { digitalWrite(relay1, relayON);
                delay(10);
                digitalWrite(relay1, relayOFF);
                delay(10);
                client.print("AOK");
                      //client.println("Pintu Masuk dibuka!");
              }
              else if(str=='3') 
              {
                digitalWrite(relay2, relayON);
                delay(10);
                digitalWrite(relay2, relayOFF);
                delay(10);
                client.print("BOK");
              }                          
            }
          if (!(client.connected())) 
          {
            client.stop();
          }

  
      //Access SD card 
    if(wg.available())
	{
		Serial.print(wg.getCode());
    size_t position;
  char wgText[15]; // large enough to store the ASCII representation 
  snprintf(wgText, sizeof wgText, "%lu", wg.getCode() ); // see https://cplusplus.com/reference/cstdio/snprintf/
  Serial.print(snprintf(wgText, sizeof wgText, "%lu", wg.getCode() ));
  if (findStringInFile("data.txt", wgText, position)) {
    Serial.print(" String found ");
    digitalWrite(relay1, relayON);
    delay(10);
    digitalWrite(relay1, relayOFF);
    
    //Serial.println(position);
  } else {
    Serial.println(" String not found");
  }
      
  }
    	
	if(wg2.available())
	{
				Serial.print(wg2.getCode());
		size_t position;
  char wgText[15]; // large enough to store the ASCII representation 
  snprintf(wgText, sizeof wgText, "%lu", wg.getCode() ); // see https://cplusplus.com/reference/cstdio/snprintf/
  Serial.print(snprintf(wgText, sizeof wgText, "%lu", wg.getCode() ));
  if (findStringInFile("data.txt", wgText, position)) {
    Serial.print(" String found ");
    digitalWrite(relay2, relayON);
    delay(10);
    digitalWrite(relay2, relayOFF);
    
    //Serial.println(position);
  } else {
    Serial.println(" String not found");
    //wg.getCode().close;
  }
}
}

This is what works for me. If you don't disable the SPI interfaces on both devices (both CS pins HIGH), they will crosstalk when you try to initialize one or the other, causing a fail.
This is the only time you need to control the CS pins. After the init on each, the libraries manage the CS pins.

void setup() {
  Serial.begin(115200);
  # disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  # disable ethernet SPI
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
# rest of your code.

hi, thanks for responding..
I will try it :slightly_smiling_face:

If that doesn't help, let me know by posting the result of the fail.

for enable SD SPI change value HIGH to LOW ?

CS pins are active LOW. HIGH is disabled.

oke then... i wil try first

hi bro, it seems the code is working...
Now I can turn off and on the Ethernet or SD card

but I'm confused about where to put the code...

Is there a way to find out when the Ethernet connection is lost or there is no client?

as information, Arduino IP: 192.168.1.189

computer ip: 192.168.1.50

Please advise

Are you planning on a server? I have server code, but last I checked, it was too big to fit in an Uno. I use a Mega2560.

My server code serves a bunch of file types. HTML, CSS, bunch of image types from the SD.

Unfortunately I use an Arduino Uno...

I'm trying to find a line of code to be able to place the code where I will turn off ethernet and switch to SD CARD mode

thanks for the help bro..

The libraries take care of that. You can have an ethernet connection open and an SD card file open at the same time. Read from SD, write to ethernet.

what library is used?
Is there an example sketch?

This is what I use.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

You can try SdFat.h if you want longer filenames.
I had examples in the Arduino Playground, but it seems to have disappeared.
I'll check.

Edit: Here are some examples.
https://docs.arduino.cc/hardware/ethernet-shield-rev2/#tutorials