Created
October 12, 2015 04:45
-
-
Save daxanya2/5d6c59d40bef15d2f4e6 to your computer and use it in GitHub Desktop.
ESP-WROOM-02とRFID-RC522で非接触Lチカ ref: http://qiita.com/daxanya1/items/b9c2b971a946c8bf063e
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
/* | |
* ESP-WOORM-02とMFRC522を接続する | |
* 2015/10/12 | |
* | |
* 配線(SPI通信): | |
* ESP-WROOM-02 : MFRC522 | |
* GPIO 5 -> SDA(1) - ESP-WROOM-02側はSS_PINで定義 | |
* GPIO14 -> SCK(2) | |
* GPIO13 -> MOSI(3) | |
* GPIO12 <- MISO(4) | |
* RQ(5) - 未使用 | |
* GND(6) - GNDにつなぐ | |
* GPIO16 -> RST(7) - ESP-WROOM-02側はRST_PINで定義 | |
* 3V3(8) - 電源につなぐ | |
* | |
* LED: GPIO 4 | |
* | |
*/ | |
#include <SPI.h> | |
#include <MFRC522.h> | |
#include <ESP8266WiFi.h> | |
#define RST_PIN 16 // MFRC522用リセットピンGPIO NO | |
#define SS_PIN 5 // MFRC522用SS(SDA)ピンGPIO NO | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // MFRC522のインスタンスを作成 | |
#define CARD_PRESENT_PIN 4 // MFRC522にカードが検知されたら光らせるGPIO NO | |
bool cardset; // MFRC522にカードが検知されているかどうか | |
int timeoutcount; // MFRC522からカードが連続で離れた回数を記録 | |
const char* ssid = "xxxxxxxx"; // ESP-WROOM-02用 Wifi SSID(各自設定してください) | |
const char* password = "xxxxxxxxxxxx"; // ESP-WROOM-02用 Wifi パスワード(各自設定してください) | |
void setup() { | |
// UART接続初期化 | |
Serial.begin(115200); // UARTの通信速度を変更したい場合はこちらを変更 | |
delay(10); | |
// カード検出用のGPIO初期化 | |
pinMode(CARD_PRESENT_PIN, OUTPUT); | |
digitalWrite(CARD_PRESENT_PIN, LOW); | |
// MFRC522用変数初期化 | |
cardset = false; | |
timeoutcount = 0; | |
// MFRC522のスケッチのDumpInfoを参考にした。ref) https://github.com/miguelbalboa/rfid | |
SPI.begin(); // SPI初期化 | |
mfrc522.PCD_Init(); // MFRC522初期化 | |
ShowReaderDetails(); // MFRC522 Card Readerのバージョンを返す。00かFFなら配線間違いの可能性 | |
// とりあえずWifiにつなぐ | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
// Print the IP address | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
// カードの検出 | |
if ( !mfrc522.PICC_IsNewCardPresent()) { | |
// 検出されなかった場合の処理 | |
if (cardset) { | |
// すでにカードが検出されていた場合で、連続4回未検出になったらLEDをLOWにする | |
// 検出後、タイムアウトと検出を繰り返すのでその対策 | |
if (timeoutcount > 4) { | |
digitalWrite(CARD_PRESENT_PIN, LOW); | |
Serial.println("LED LOW"); | |
cardset = false; | |
timeoutcount = 0; | |
} else { | |
// 4回以内なら連続未検出回数を増やす | |
timeoutcount++; | |
} | |
} | |
delay(5); | |
return; | |
} | |
// カードが初めてor改めてセットされた場合、LEDをHIGHにする | |
if (!cardset) { | |
digitalWrite(CARD_PRESENT_PIN, HIGH); | |
Serial.println("LED HIGH"); | |
cardset = true; | |
} | |
timeoutcount = 0; | |
delay(100); | |
} | |
// MFRC522のスケッチのDumpInfoのまま。ref) https://github.com/miguelbalboa/rfid | |
// LICENSE:https://github.com/miguelbalboa/rfid/blob/master/UNLICENSE | |
void ShowReaderDetails() { | |
// Get the MFRC522 software version | |
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); | |
Serial.print(F("MFRC522 Software Version: 0x")); | |
Serial.print(v, HEX); | |
if (v == 0x91) | |
Serial.print(F(" = v1.0")); | |
else if (v == 0x92) | |
Serial.print(F(" = v2.0")); | |
else | |
Serial.print(F(" (unknown)")); | |
Serial.println(""); | |
// When 0x00 or 0xFF is returned, communication probably failed | |
if ((v == 0x00) || (v == 0xFF)) { | |
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment