I am making a web-client with an Arduino mega and Ethernet Shield. I am trying to receive data from a web-server running a php script and saving that data to the SD card located in the Arduino Ethernet shield. I have separately tested to read and write to SD and to Get data from the the php script using the Ethernet. But when i tried to merge those two code into one, i found that Ethernet is unable to connect to the web-server(php Script).But the Ethernet connects fine when i remove the SD Card from its slot. During my research i found that Arduino mega has different SPI pin configuration that the uno. And the libraries are supposed to handle the SPI data-line independently. But i still tried to manually controlled the SPI bus, but it did not work. As far as i know, i am now unable to change the SPI bus using the D4 pin for SD card and D10 pin for Ethernet.
I am using native Ethernet library and SDFat library.(I Even used the native SD library but the result is the same).
#include <Ethernet.h>
#include <SPI.h>
#include <SdFat.h>
#define Ethernet_ss_pin 53
#define Sd_ss_pin 4
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
/////////////////////////////////////////////////////////////////////////////////
////////SD Config
/////////////////////////////////////////////////////////////////////////////////
SdFat sd;
SdFile myFile;
//const int chipSelect = 4;
const char* fileName="database.txt";
/////////////////////////////////////////////////////////////////////////////////
////////Ethernet Config
/////////////////////////////////////////////////////////////////////////////////
//String server = "rfid.unisec-bd.org"; //ip Address of the server you will connect to
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/getData.php HTTP/1.0";
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////
EthernetClient client;
char inString[3200]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
void setup() {
Serial.begin(9600);
pinMode(53,OUTPUT);
pinMode(4, OUTPUT);
pinMode(10,OUTPUT);
ChipSelect(0);
Ethernet.begin(mac);
String pageValue;
pageValue=connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
ChipSelect(1);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(Sd_ss_pin, SPI_HALF_SPEED)) {
sd.initErrorHalt("Sd initialization failed");
}else{
Serial.println("Sd card Initialized");
}
ChipSelect(0);
}
void loop() {
Serial.println("Type any character to start");
while (Serial.read() <= 0) {}
delay(1000); // catch Due reset problem
ChipSelect(2);
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
ChipSelect(1);
writeSd(pageValue);
readSd();
ChipSelect(0);
}
void readSd(){
//const char path=(const char)fileName;
if (!myFile.open(fileName, O_READ)) {
sd.errorHalt("file read error");
}
Serial.println(fileName);
// read from the file until there's nothing else in it:
char data;
int c=0;
while ((data = myFile.read()) >= 0) {
data=char(data);
Serial.print(data);
//Serial.println(c);
c++;
}
// close the file:
myFile.close();
}
void writeSd(String storeData){
// open the file for write at end like the Native SD library
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening database.txt for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to database.txt...");
myFile.println(storeData);
// close the file:
myFile.close();
Serial.println("done.");
}
String connectAndRead(){
///////// Pin 10,11,12,13 are used as SPI Bus of the ethernet Shield
/// MOSI- pin 11
/// MISO- pin 12
/// CLK- pin 13
/// CS for sdCard-pin 4
/// ss- pin 10
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
//digitalWrite(Sd_ss_pin,HIGH); //SD is not active
/* USE THIS CODE FOR PROXY SERVER IN BRAC UNIVERSITY
// if you get a connection, report back via serial:
if (client.connect("192.168.1.5", 8080)) { // This is connecting to the proxy
Serial.println("connected");
// Make a HTTP request through proxy:
client.println("GET rfid.unisec-bd.org/getData.php HTTP/1.0");
client.println();
}
*/
//connect to the server
Serial.println("connecting...");
while(!client){;}
//port 80 is typical of a www page
if (client.connect("rfid.unisec-bd.org", 80)) {
Serial.println("connected");
client.print("GET ");
client.println(location);
client.println("Host: rfid.unisec-bd.org"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connection failed";
}
}
String readPage(){
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset( &inString, 0, 3200 ); //clear inString memory
while(true){
if (client.available()) {
char c = client.read();
if (c == '!' ) { //'!' is our begining character
startRead = true; //Ready to start reading the part
}else if(startRead){
if(c != '@'){ //'@' is our ending character
inString[stringPos] = c;
stringPos ++;
}else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}
void ChipSelect(int s){
if(s=0){ //Both Device is off
//digitalWrite(53,HIGH);
digitalWrite(10,HIGH);
digitalWrite(4,HIGH);
}else if(s=1){ //Only SD is active
//digitalWrite(53,HIGH);
digitalWrite(10,HIGH);
digitalWrite(4,LOW);
}else if(s=2){ //Only Ethernet is Active
//digitalWrite(53,HIGH);
digitalWrite(10,LOW);
digitalWrite(4,HIGH);
}
Serial.print("Ethernet SS:");
Serial.println(digitalRead(10));
Serial.print("SD SS:");
Serial.println(digitalRead(4));
}
-
I suppose you have seen this one: electronics.stackexchange.com/a/67214/140185Jaroslav Záruba– Jaroslav Záruba2017年02月27日 15:04:06 +00:00Commented Feb 27, 2017 at 15:04
1 Answer 1
You have to switch off and then on pin 2 (connected to Vcc on SD reader) using digitalWrite
https://stackoverflow.com/questions/41277010/how-to-connect-rc522-rfid-module-and-sd-card-adapter-to-one-arduino-board/46797716#46797716
`//HEADER FILES##################################
#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include <SD.h>
//ARDUINO MEGA
//MISO 50
//MOSI 51
//SCK 52
//ON/OFF SD CARD READER 2
//SD Card DEfinitions###########################
File root;
const int chipSelect = 4;
const int chipONOFF = 2;
//RF_ID Definitions#############################
#define SS_PIN 7
#define RST_PIN 6
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
pinMode(chipONOFF,OUTPUT);
digitalWrite(chipONOFF, LOW);
tc_03_rfIDbasicTest();
digitalWrite(chipONOFF, HIGH);
tc_02_SDcardConnectTest();
digitalWrite(chipONOFF, LOW);
tc_03_rfIDbasicTest();
}
void loop()
{ }
void tc_02_SDcardConnectTest()
{
Serial.println("");
Serial.println("");
Serial.println("-----TEST 2: FUNCIONAMIENTO BASICO DE SD-----");
Serial.println("");
if (!SD.begin(chipSelect))
{
Serial.println("Falla en comunicacion, asegurate que la uSD esté insertada e intenta de nuevo");
Serial.println("FAIL");
}
else
{
Serial.println("Tarjeta uSD Detectada");
Serial.println("PASS");
root = SD.open("/");
printDirectory(root, 0);
}
}
int tc_03_rfIDbasicTest()
{
Serial.println("-----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----");
mfrc522.PCD_Init(); // Init MFRC522 chip again per each write attemp
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
if ( ! mfrc522.PICC_IsNewCardPresent())
Serial.println("Targeta fuera de alcance");
else
Serial.println("Targeta dentro de alcance");
if ( ! mfrc522.PICC_ReadCardSerial())
Serial.println("Falla en lectura de tarjeta");
else
{
Serial.println("RFid OK :) !");
Serial.println("PASS");
String nuid="";
for (byte i = 0; i < 4; i++)
nuid+=mfrc522.uid.uidByte[i];
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
Serial.println(nuid);
}
}
void printDirectory(File dir, int numTabs)
{
while (true)
{
File entry = dir.openNextFile();
if (! entry)
break;
for (uint8_t i = 0; i < numTabs; i++)
{
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory())
{
Serial.println("/");
printDirectory(entry, numTabs + 1);
}
else
{
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}`
and this what you get on serial monitor.
`-----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----
Firmware Version: 0x92 = v2.0
Targeta dentro de alcance
RFid OK :) !
PASS
215103959
-----TEST 2: FUNCIONAMIENTO BASICO DE SD-----
Tarjeta uSD Detectada
PASS
BIO/
D70A273B.DB 44
ENROLADO.DB 57
F683B22D.DB 40
A6DE1E49.DB 38
080732DC.DB 38
08D1EF01.DB 38
08267788.DB 38
XWIFI.DB 47
CP0101.DB 26
XFEC.DB 4
CP0102.DB 26
CP0201.DB 26
SYSTEM1/
WPSETT1.DAT 12
INDEXE~1 76
CP0301.DB 26
GP01.DB 33
GP02.DB 33
GP03.DB 33
GP04.DB 32
GP05.DB 32
GS06.DB 35
GS07.DB 37
GS08.DB 36
GS09.DB 36
GS10.DB 36
GE03.DB 24
GE02.DB 20
GE01.DB 23
GS11.DB 38
AP010101.DB 69
AP010102.DB 83
-----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----
Firmware Version: 0x92 = v2.0
Targeta dentro de alcance
RFid OK :) !
PASS
215103959`
-
1
-
Por favor, usa la lengua del Imperio.user31481– user314812017年10月17日 22:54:44 +00:00Commented Oct 17, 2017 at 22:54