Created
May 21, 2025 01:01
-
-
Save cyrusmeh/381b9c300e41c5fd8178963bdc56de66 to your computer and use it in GitHub Desktop.
Project related to FZA.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SPI.h> | |
| #include <EtherCard.h> | |
| #include <MFRC522.h> | |
| #include <Keypad.h> | |
| // Network configuration | |
| static byte mymac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
| static byte myip[] = {192, 168, 137, 177}; | |
| static byte gwip[] = {192, 168, 137, 1}; | |
| static byte netmask[] = {255, 255, 255, 0}; | |
| static byte hisip[] = {192, 168, 137, 1}; // Your server IP | |
| byte Ethernet::buffer[500]; | |
| // Pin definitions | |
| #define ETH_CS 53 | |
| #define RFID_CS 9 | |
| #define RST_PIN 8 | |
| #define LDR_PIN A0 | |
| // Keypad setup | |
| const byte ROWS = 4; | |
| const byte COLS = 4; | |
| char keys[ROWS][COLS] = { | |
| {'1','2','3','A'}, {'4','5','6','B'}, | |
| {'7','8','9','C'}, {'*','0','#','D'} | |
| }; | |
| byte rowPins[ROWS] = {22, 24, 26, 28}; | |
| byte colPins[COLS] = {30, 32, 34, 36}; | |
| Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); | |
| // RFID setup | |
| MFRC522 mfrc522(RFID_CS, RST_PIN); | |
| // System variables | |
| String inputString = ""; | |
| String lastRFID = "None"; | |
| String lastKeypad = "None"; | |
| int lastLDR = 0; | |
| unsigned long lastRefresh = 0; | |
| const unsigned long refreshInterval = 5000; | |
| void setup() { | |
| Serial.begin(9600); | |
| Serial.println(F("\n[System Initializing]")); | |
| // Initialize SPI and pins | |
| SPI.begin(); | |
| pinMode(ETH_CS, OUTPUT); | |
| pinMode(RFID_CS, OUTPUT); | |
| digitalWrite(ETH_CS, HIGH); // Start with Ethernet disabled | |
| digitalWrite(RFID_CS, LOW); // RFID enabled | |
| // Initialize RFID | |
| mfrc522.PCD_Init(); | |
| // Initialize Ethernet | |
| if (ether.begin(sizeof Ethernet::buffer, mymac, ETH_CS) == 0) { | |
| Serial.println(F("Failed to access Ethernet controller")); | |
| } | |
| // Configure network | |
| ether.staticSetup(myip, gwip, 0, netmask); | |
| Serial.println(F("System ready")); | |
| Serial.print(F("My IP: ")); | |
| ether.printIp(ether.myip); | |
| } | |
| void loop() { | |
| // Handle incoming packets | |
| word len = ether.packetReceive(); | |
| word pos = ether.packetLoop(len); | |
| // Process HTTP requests | |
| if (pos) { | |
| handleHttpRequest(pos); | |
| } | |
| checkRFID(); | |
| checkKeypad(); | |
| if (millis() - lastRefresh >= refreshInterval) { | |
| lastLDR = analogRead(LDR_PIN); | |
| sendToMainServer(); | |
| lastRefresh = millis(); | |
| } | |
| } | |
| void handleHttpRequest(word pos) { | |
| BufferFiller bfill = ether.tcpOffset(); | |
| bfill.emit_p(PSTR( | |
| "HTTP/1.0 200 OK\r\n" | |
| "Content-Type: text/html\r\n" | |
| "Pragma: no-cache\r\n" | |
| "Refresh: 5\r\n\r\n" | |
| "<!DOCTYPE html><html><head><title>Arduino Control Panel</title></head>" | |
| "<body style='font-family:Arial;text-align:center'>" | |
| "<h1>Arduino Sensor Monitor</h1>" | |
| "<div style='display:inline-block;text-align:left'>" | |
| "<table border='1' cellpadding='5' style='border-collapse:collapse'>" | |
| "<tr><th>Sensor</th><th>Value</th></tr>" | |
| "<tr><td>LDR Value</td><td>$D</td></tr>" | |
| "<tr><td>Last RFID</td><td>$S</td></tr>" | |
| "<tr><td>Last Keypad</td><td>$S</td></tr>" | |
| "</table></div></body></html>"), | |
| lastLDR, | |
| lastRFID.c_str(), | |
| lastKeypad.c_str() | |
| ); | |
| ether.httpServerReply(bfill.position()); | |
| } | |
| void checkRFID() { | |
| digitalWrite(RFID_CS, LOW); | |
| digitalWrite(ETH_CS, HIGH); | |
| if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { | |
| lastRFID = ""; | |
| for (byte i = 0; i < mfrc522.uid.size; i++) { | |
| if (mfrc522.uid.uidByte[i] < 0x10) lastRFID += "0"; | |
| lastRFID += String(mfrc522.uid.uidByte[i], HEX); | |
| if (i < mfrc522.uid.size - 1) lastRFID += " "; | |
| } | |
| lastRFID.toUpperCase(); | |
| Serial.print(F("RFID Detected: ")); Serial.println(lastRFID); | |
| // Map specific tags to names | |
| if (lastRFID == "1E FC 42 02") lastRFID += " (Asadi)"; | |
| else if (lastRFID == "13 7F 79 13") lastRFID += " (Sharifi)"; | |
| mfrc522.PICC_HaltA(); | |
| } | |
| } | |
| void checkKeypad() { | |
| char key = keypad.getKey(); | |
| if (!key) return; | |
| if (key == '#') { | |
| if (inputString.length() > 0) { | |
| lastKeypad = inputString; | |
| // Map specific codes to names | |
| if (inputString == "123") lastKeypad += " (Trim Mold)"; | |
| else if (inputString == "456") lastKeypad += " (Mirror Mold)"; | |
| else if (inputString == "789") lastKeypad += " (Handle Mold)"; | |
| Serial.print(F("Keypad Sent: ")); Serial.println(lastKeypad); | |
| inputString = ""; | |
| } | |
| } | |
| else if (key == '*') { | |
| inputString = ""; | |
| Serial.println(F("Input cleared")); | |
| } | |
| else { | |
| inputString += key; | |
| Serial.print(F("Key pressed: ")); Serial.println(key); | |
| } | |
| } | |
| void sendToMainServer() { | |
| digitalWrite(ETH_CS, LOW); | |
| digitalWrite(RFID_CS, HIGH); | |
| if (ether.begin(sizeof Ethernet::buffer, mymac, ETH_CS) == 0) { | |
| Serial.println(F("Ethernet restart failed")); | |
| return; | |
| } | |
| ether.staticSetup(myip, gwip, 0, netmask); | |
| // Prepare URL with all data | |
| char url[200]; | |
| snprintf_P(url, sizeof(url), PSTR("/log?ldr=%d&rfid=%s&keypad=%s"), | |
| lastLDR, | |
| urlEncode(lastRFID).c_str(), | |
| urlEncode(lastKeypad).c_str()); | |
| ether.browseUrl(PSTR("/log"), url, PSTR("192.168.137.1"), [](byte status, word off, word len) { | |
| Serial.println(F("Data sent to main server")); | |
| }); | |
| delay(100); | |
| } | |
| String urlEncode(String str) { | |
| String encodedString = ""; | |
| char c; | |
| for (int i = 0; i < str.length(); i++) { | |
| c = str.charAt(i); | |
| if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { | |
| encodedString += c; | |
| } else if (c == ' ') { | |
| encodedString += '+'; | |
| } else { | |
| encodedString += '%'; | |
| char buf[3]; | |
| sprintf(buf, "%02X", c); | |
| encodedString += buf; | |
| } | |
| } | |
| return encodedString; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is my ipconfig data "C:\Users\pc>ipconfig | |
| Windows IP Configuration | |
| Wireless LAN adapter Local Area Connection* 9: | |
| Media State . . . . . . . . . . . : Media disconnected | |
| Connection-specific DNS Suffix . : | |
| Wireless LAN adapter Local Area Connection* 10: | |
| Media State . . . . . . . . . . . : Media disconnected | |
| Connection-specific DNS Suffix . : | |
| Ethernet adapter Ethernet: | |
| Connection-specific DNS Suffix . : | |
| Link-local IPv6 Address . . . . . : fe80::cea9:e1b1:625f:bbfa%4 | |
| IPv4 Address. . . . . . . . . . . : 192.168.137.1 | |
| Subnet Mask . . . . . . . . . . . : 255.255.255.0 | |
| Default Gateway . . . . . . . . . : | |
| Wireless LAN adapter Wi-Fi: | |
| Connection-specific DNS Suffix . : | |
| IPv6 Address. . . . . . . . . . . : 2a02:4540:64:46a7:7f35:82e8:779e:596f | |
| Temporary IPv6 Address. . . . . . : 2a02:4540:64:46a7:e557:f5e4:1575:b5b | |
| Link-local IPv6 Address . . . . . : fe80::1722:1338:9f51:a76c%13 | |
| Autoconfiguration IPv4 Address. . : 169.254.42.207 | |
| Subnet Mask . . . . . . . . . . . : 255.255.0.0 | |
| Default Gateway . . . . . . . . . : fe80::b4b8:c6ff:fe01:a8d%13".I connected RFID model RC522 and 4*4 matrix keypad to Arduino mega2560. I also connected Ethernet module model ENC28J60 to Arduino mega2560. I want you to write Arduino code which send LDR sensor, keypad and RFID data to HTTP and their confirmation and display in serial monitor. in this project we have two CS. first CS related to Ethernet is connected to pin 53 of Arduino and second CS related to RFID is connected to pin 9 of Arduino. Reset pin related to RFID is connected to pin 8 Arduino. The senario is like this when RFID is active, CS related to RFID should be LOW and CS related to Ethernet should be HIGH. When Ethernet is active, CS related to RFID should be high and CS related to Ethernet should be LOW. I want when I put first tag (1E FC 42 02) in front of rfid, UID of first tag send to HTTP and name of Asadi receive and show in serial monitor. when I put second tag (13 7F 79 13) in front of rfid, UID of second tag send to HTTP and name of Sharifi receive and show in serial monitor. when I type 123 in keypad and press # button, number 123 send to HTTP and name of Trim Mold receive and show in serial monitor. when I type 456 in keypad and press # button, number 456 send to HTTP and name of Mirror Mold receive and show in serial monitor. when I type 789 in keypad and press # button, number 789 send to HTTP and name of Handle Mold receive and show in serial monitor. LDR sensor data also should send to HTTP. HTTP page should be refresh each 5 second. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment