Guest User

Untitled

a guest
Oct 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <SPI.h>
  3. #include <SdFat.h>
  4. #define Ethernet_ss_pin 53
  5. #define Sd_ss_pin 4
  6.  
  7. // On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
  8. /////////////////////////////////////////////////////////////////////////////////
  9. ////////SD Config
  10. /////////////////////////////////////////////////////////////////////////////////
  11.  
  12. SdFat sd;
  13. SdFile myFile;
  14. //const int chipSelect = 4;
  15. const char* fileName="database.txt";
  16.  
  17. /////////////////////////////////////////////////////////////////////////////////
  18. ////////Ethernet Config
  19. /////////////////////////////////////////////////////////////////////////////////
  20. //String server = "rfid.unisec-bd.org"; //ip Address of the server you will connect to
  21.  
  22. //The location to go to on the server
  23. //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
  24. String location = "/getData.php HTTP/1.0";
  25.  
  26.  
  27. // if need to change the MAC address (Very Rare)
  28. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  29. ////////////////////////////////////////////////////////////////////////
  30.  
  31. EthernetClient client;
  32.  
  33. char inString[3200]; // string for incoming serial data
  34. int stringPos = 0; // string index counter
  35. boolean startRead = false; // is reading?
  36.  
  37.  
  38. void setup() {
  39. Serial.begin(9600);
  40. pinMode(53,OUTPUT);
  41. pinMode(4, OUTPUT);
  42. pinMode(10,OUTPUT);
  43. ChipSelect(0);
  44. Ethernet.begin(mac);
  45. String pageValue;
  46. pageValue=connectAndRead(); //connect to the server and read the output
  47. Serial.println(pageValue); //print out the findings.
  48. ChipSelect(1);
  49. // Initialize SdFat or print a detailed error message and halt
  50. // Use half speed like the native library.
  51. // change to SPI_FULL_SPEED for more performance.
  52. if (!sd.begin(Sd_ss_pin, SPI_HALF_SPEED)) {
  53. sd.initErrorHalt("Sd initialization failed");
  54. }else{
  55. Serial.println("Sd card Initialized");
  56. }
  57. ChipSelect(0);
  58.  
  59. }
  60.  
  61. void loop() {
  62. Serial.println("Type any character to start");
  63. while (Serial.read() <= 0) {}
  64. delay(1000); // catch Due reset problem
  65. ChipSelect(2);
  66. String pageValue = connectAndRead(); //connect to the server and read the output
  67. Serial.println(pageValue); //print out the findings.
  68. ChipSelect(1);
  69. writeSd(pageValue);
  70. readSd();
  71. ChipSelect(0);
  72. }
  73. void readSd(){
  74. //const char path=(const char)fileName;
  75. if (!myFile.open(fileName, O_READ)) {
  76. sd.errorHalt("file read error");
  77. }
  78. Serial.println(fileName);
  79. // read from the file until there's nothing else in it:
  80. char data;
  81. int c=0;
  82. while ((data = myFile.read()) >= 0) {
  83. data=char(data);
  84. Serial.print(data);
  85. //Serial.println(c);
  86. c++;
  87. }
  88. // close the file:
  89. myFile.close();
  90. }
  91.  
  92. void writeSd(String storeData){
  93. // open the file for write at end like the Native SD library
  94. if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
  95. sd.errorHalt("opening database.txt for write failed");
  96. }
  97. // if the file opened okay, write to it:
  98. Serial.print("Writing to database.txt...");
  99. myFile.println(storeData);
  100.  
  101. // close the file:
  102. myFile.close();
  103. Serial.println("done.");
  104. }
  105.  
  106. String connectAndRead(){
  107. ///////// Pin 10,11,12,13 are used as SPI Bus of the ethernet Shield
  108. /// MOSI- pin 11
  109. /// MISO- pin 12
  110. /// CLK- pin 13
  111. /// CS for sdCard-pin 4
  112. /// ss- pin 10
  113. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  114. // Note that even if it's not used as the CS pin, the hardware SS pin
  115. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  116. // or the SD library functions will not work.
  117. //digitalWrite(Sd_ss_pin,HIGH); //SD is not active
  118.  
  119. /* USE THIS CODE FOR PROXY SERVER IN BRAC UNIVERSITY
  120. // if you get a connection, report back via serial:
  121. if (client.connect("192.168.1.5", 8080)) { // This is connecting to the proxy
  122. Serial.println("connected");
  123. // Make a HTTP request through proxy:
  124. client.println("GET rfid.unisec-bd.org/getData.php HTTP/1.0");
  125. client.println();
  126. }
  127. */
  128. //connect to the server
  129. Serial.println("connecting...");
  130. while(!client){;}
  131. //port 80 is typical of a www page
  132. if (client.connect("rfid.unisec-bd.org", 80)) {
  133. Serial.println("connected");
  134. client.print("GET ");
  135. client.println(location);
  136. client.println("Host: rfid.unisec-bd.org"); // SERVER ADDRESS HERE TOO
  137. client.println("Content-Type: application/x-www-form-urlencoded");
  138. client.println();
  139. //Connected - Read the page
  140. return readPage(); //go and read the output
  141. }else{
  142. return "connection failed";
  143. }
  144. }
  145.  
  146. String readPage(){
  147. //read the page, and capture & return everything between '<' and '>'
  148. stringPos = 0;
  149. memset( &inString, 0, 3200 ); //clear inString memory
  150. while(true){
  151. if (client.available()) {
  152. char c = client.read();
  153. if (c == '!' ) { //'!' is our begining character
  154. startRead = true; //Ready to start reading the part
  155. }else if(startRead){
  156. if(c != '@'){ //'@' is our ending character
  157. inString[stringPos] = c;
  158. stringPos ++;
  159. }else{
  160. //got what we need here! We can disconnect now
  161. startRead = false;
  162. client.stop();
  163. client.flush();
  164. Serial.println("disconnecting.");
  165. return inString;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. void ChipSelect(int s){
  172. if(s=0){ //Both Device is off
  173. //digitalWrite(53,HIGH);
  174. digitalWrite(10,HIGH);
  175. digitalWrite(4,HIGH);
  176. }else if(s=1){ //Only SD is active
  177. //digitalWrite(53,HIGH);
  178. digitalWrite(10,HIGH);
  179. digitalWrite(4,LOW);
  180. }else if(s=2){ //Only Ethernet is Active
  181. //digitalWrite(53,HIGH);
  182. digitalWrite(10,LOW);
  183. digitalWrite(4,HIGH);
  184. }
  185. Serial.print("Ethernet SS:");
  186. Serial.println(digitalRead(10));
  187. Serial.print("SD SS:");
  188. Serial.println(digitalRead(4));
  189. }
  190.  
  191. https://stackoverflow.com/questions/41277010/how-to-connect-rc522-rfid-module-and-sd-card-adapter-to-one-arduino-board/46797716#46797716
  192.  
  193. `//HEADER FILES##################################
  194. #include <SPI.h>
  195. #include <MFRC522.h>
  196. #include <SoftwareSerial.h>
  197. #include <EEPROM.h>
  198. #include <SD.h>
  199. //ARDUINO MEGA
  200. //MISO 50
  201. //MOSI 51
  202. //SCK 52
  203. //ON/OFF SD CARD READER 2
  204. //SD Card DEfinitions###########################
  205. File root;
  206. const int chipSelect = 4;
  207. const int chipONOFF = 2;
  208. //RF_ID Definitions#############################
  209. #define SS_PIN 7
  210. #define RST_PIN 6
  211. MFRC522 mfrc522(SS_PIN, RST_PIN);
  212.  
  213. void setup()
  214. {
  215. Serial.begin(9600);
  216. SPI.begin();
  217. pinMode(chipONOFF,OUTPUT);
  218. digitalWrite(chipONOFF, LOW);
  219. tc_03_rfIDbasicTest();
  220. digitalWrite(chipONOFF, HIGH);
  221. tc_02_SDcardConnectTest();
  222. digitalWrite(chipONOFF, LOW);
  223. tc_03_rfIDbasicTest();
  224. }
  225.  
  226. void loop()
  227. { }
  228.  
  229. void tc_02_SDcardConnectTest()
  230. {
  231. Serial.println("");
  232. Serial.println("");
  233. Serial.println("-----TEST 2: FUNCIONAMIENTO BASICO DE SD-----");
  234. Serial.println("");
  235. if (!SD.begin(chipSelect))
  236. {
  237. Serial.println("Falla en comunicacion, asegurate que la uSD esté insertada e intenta de nuevo");
  238. Serial.println("FAIL");
  239. }
  240. else
  241. {
  242. Serial.println("Tarjeta uSD Detectada");
  243. Serial.println("PASS");
  244. root = SD.open("/");
  245. printDirectory(root, 0);
  246. }
  247. }
  248.  
  249. int tc_03_rfIDbasicTest()
  250. {
  251. Serial.println("-----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----");
  252. mfrc522.PCD_Init(); // Init MFRC522 chip again per each write attemp
  253. mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
  254.  
  255. if ( ! mfrc522.PICC_IsNewCardPresent())
  256. Serial.println("Targeta fuera de alcance");
  257. else
  258. Serial.println("Targeta dentro de alcance");
  259. if ( ! mfrc522.PICC_ReadCardSerial())
  260. Serial.println("Falla en lectura de tarjeta");
  261. else
  262. {
  263. Serial.println("RFid OK :) !");
  264. Serial.println("PASS");
  265. String nuid="";
  266. for (byte i = 0; i < 4; i++)
  267. nuid+=mfrc522.uid.uidByte[i];
  268. mfrc522.PICC_HaltA();
  269. mfrc522.PCD_StopCrypto1();
  270. Serial.println(nuid);
  271. }
  272. }
  273.  
  274. void printDirectory(File dir, int numTabs)
  275. {
  276. while (true)
  277. {
  278. File entry = dir.openNextFile();
  279. if (! entry)
  280. break;
  281. for (uint8_t i = 0; i < numTabs; i++)
  282. {
  283. Serial.print('t');
  284. }
  285. Serial.print(entry.name());
  286. if (entry.isDirectory())
  287. {
  288. Serial.println("/");
  289. printDirectory(entry, numTabs + 1);
  290. }
  291. else
  292. {
  293. Serial.print("tt");
  294. Serial.println(entry.size(), DEC);
  295. }
  296. entry.close();
  297. }
  298. }`
  299.  
  300. `-----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----
  301. Firmware Version: 0x92 = v2.0
  302. Targeta dentro de alcance
  303. RFid OK :) !
  304. PASS
  305. 215103959
  306.  
  307. -----TEST 2: FUNCIONAMIENTO BASICO DE SD-----
  308.  
  309. Tarjeta uSD Detectada
  310. PASS
  311. BIO/
  312. D70A273B.DB 44
  313. ENROLADO.DB 57
  314. F683B22D.DB 40
  315. A6DE1E49.DB 38
  316. 080732DC.DB 38
  317. 08D1EF01.DB 38
  318. 08267788.DB 38
  319. XWIFI.DB 47
  320. CP0101.DB 26
  321. XFEC.DB 4
  322. CP0102.DB 26
  323. CP0201.DB 26
  324. SYSTEM1/
  325. WPSETT1.DAT 12
  326. INDEXE~1 76
  327. CP0301.DB 26
  328. GP01.DB 33
  329. GP02.DB 33
  330. GP03.DB 33
  331. GP04.DB 32
  332. GP05.DB 32
  333. GS06.DB 35
  334. GS07.DB 37
  335. GS08.DB 36
  336. GS09.DB 36
  337. GS10.DB 36
  338. GE03.DB 24
  339. GE02.DB 20
  340. GE01.DB 23
  341. GS11.DB 38
  342. AP010101.DB 69
  343. AP010102.DB 83
  344.  
  345. -----TEST 3: FUNCIONAMIENTO BASICO DE RFid-----
  346. Firmware Version: 0x92 = v2.0
  347. Targeta dentro de alcance
  348. RFid OK :) !
  349. PASS
  350. 215103959`
Add Comment
Please, Sign In to add comment