kode untuk membaca RFID di NodeMCU dan mengirimkan data ke Google Spreadsheet
Berikut adalah contoh kode untuk membaca RFID di NodeMCU dan mengirimkan data ke Google Spreadsheet menggunakan protokol HTTP POST:
Kode Arduino pada NodeMCU:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <MFRC522.h>
// Replace with your network credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
// Replace with your Google Spreadsheet Script URL
const char* url = "https://script.google.com/macros/s/Your_SCRIPT_URL/exec";
// Define pins for RFID reader
#define SS_PIN D4
#define RST_PIN D3
// Create instance of MFRC522 RFID reader
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Define variables for storing RFID tag data
byte tagData[4];
char tagString[16];
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print message to indicate setup is complete
Serial.println("Ready to scan RFID tags");
// Initialize RFID reader
SPI.begin();
mfrc522.PCD_Init();
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
}
void loop() {
// Check if a tag is present
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// Read tag data
for (byte i = 0; i < 4; i++) {
tagData[i] = mfrc522.uid.uidByte[i];
}
// Convert tag data to a string
sprintf(tagString, "%02X%02X%02X%02X", tagData[0], tagData[1], tagData[2], tagData[3]);
Serial.println(tagString);
// Send tag data to Google Spreadsheet
String postData = "rfid=" + String(tagString);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
http.end();
// Print HTTP response code to serial monitor
Serial.println(httpResponseCode);
// Halt PICC and prepare for new card
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
// Wait for 5 seconds before scanning for a new tag
delay(5000);
}
}
0 Response to "kode untuk membaca RFID di NodeMCU dan mengirimkan data ke Google Spreadsheet"
Post a Comment