Skip to content

Commit

Permalink
NFCReaderとPassDetectorを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
csenet committed Nov 19, 2023
1 parent 7ca1b41 commit 6f77b3b
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 88 deletions.
5 changes: 3 additions & 2 deletions hardware/esp32-control/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ lib_deps =
madhephaestus/ESP32Servo@^1.1.0
bblanchon/ArduinoJson@^6.21.3
jandrassy/ArduinoOTA@^1.0.12
miguelbalboa/MFRC522@^1.4.10
board_build.filesystem = littlefs
targets=upload
monitor_speed = 115200
; targets = upload
monitor_speed = 115200
18 changes: 12 additions & 6 deletions hardware/esp32-control/src/IOManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ IOManager::IOManager(PubSubClient client)
POINT_LIST_INDEX = 0;
STOP_LIST_INDEX = 0;
DETECTOR_LIST_INDEX = 0;
// NFC_LIST_INDEX = 0;
NFC_LIST_INDEX = 0;
}

void IOManager::addPoint(uint8_t pin, String point_id)
Expand All @@ -23,10 +23,16 @@ void IOManager::addStop(uint8_t pin, String stop_id)

void IOManager::addDetector(uint8_t pin, String block_id, String target)
{
DETECTOR_LIST[DETECTOR_LIST_INDEX].init(block_id, target, pin, client);
DETECTOR_LIST[DETECTOR_LIST_INDEX].init(block_id, target, pin, &client);
DETECTOR_LIST_INDEX++;
}

void IOManager::addNfc(uint8_t pin, String nfc_id)
{
NFC_LIST[NFC_LIST_INDEX].init(nfc_id, pin, &client);
NFC_LIST_INDEX++;
}

void IOManager::setPointState(String point_id, POINT_STATE state)
{
for (int i = 0; i < POINT_LIST_INDEX; i++)
Expand Down Expand Up @@ -57,8 +63,8 @@ void IOManager::loop()
{
DETECTOR_LIST[i].loop();
}
// for (int i = 0; i < NFC_LIST_INDEX; i++)
// {
// NFC_LIST[i].loop();
// }
for (int i = 0; i < NFC_LIST_INDEX; i++)
{
NFC_LIST[i].loop();
}
}
13 changes: 7 additions & 6 deletions hardware/esp32-control/src/IOManager.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef IOMANAGER_H
#define IOMANAGER_H
#include <Arduino.h>
#include <PassDetector.h>
#include <PointRail.h>
#include <StopRail.h>
#include "PassDetector.h"
#include "PointRail.h"
#include "StopRail.h"
#include "NFCReader.h"

#define MAX_POINT_NUM 5
#define MAX_STOP_NUM 5
Expand All @@ -21,19 +22,19 @@ class IOManager
void setStopState(String stop_id, STOP_STATE state);
void setPointState(String point_id, POINT_STATE state);
void addDetector(uint8_t pin, String block_id, String target);
// addNfc(uint8_t pin, String block_id);
void addNfc(uint8_t pin, String nfc_id);

void loop();

private:
uint8_t POINT_LIST_INDEX;
uint8_t STOP_LIST_INDEX;
uint8_t DETECTOR_LIST_INDEX;
// uint8_t NFC_LIST_INDEX;
uint8_t NFC_LIST_INDEX;
PointRail POINT_LIST[MAX_POINT_NUM];
StopRail STOP_LIST[MAX_STOP_NUM];
PassDetector DETECTOR_LIST[MAX_DETECTOR_NUM];
// NfcReader NFC_LIST[MAX_NFC_NUM];
NFCReader NFC_LIST[MAX_NFC_NUM];
};

#endif
41 changes: 41 additions & 0 deletions hardware/esp32-control/src/NFCReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "NFCReader.h"

NFCReader::NFCReader()
{
}

void NFCReader::init(String nfc_id, uint8_t ss_pin, PubSubClient *client)
{
this->ss_pin = ss_pin;
this->nfc_id = nfc_id;
this->client = client;
reader.PCD_Init(ss_pin, RST_PIN);
}

void NFCReader::loop()
{
if (!reader.PICC_IsNewCardPresent())
{
return;
}
if (!reader.PICC_ReadCardSerial())
{
return;
}
String uid = "";
for (byte i = 0; i < reader.uid.size; i++)
{
uid += String(reader.uid.uidByte[i] < 0x10 ? "0" : "");
uid += String(reader.uid.uidByte[i], HEX);
}
Serial.print("NFC ID: ");
Serial.print(nfc_id);
Serial.print( "UID: ");
Serial.println(uid);
reader.PICC_HaltA();
reader.PCD_StopCrypto1();

// Publish Topic
String topic = "nfc/" + nfc_id + "/update";
client->publish(topic.c_str(), uid.c_str());
}
24 changes: 24 additions & 0 deletions hardware/esp32-control/src/NFCReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef NFCREADER_H
#define NFCREADER_H
#include <MFRC522.h>
#include <PubSubClient.h>

#define RST_PIN 9
#define SS_1_PIN 10
#define SS_2_PIN 8

class NFCReader
{
private:
int ss_pin;
String nfc_id;
MFRC522 reader;
PubSubClient *client;

public:
NFCReader();
void init(String nfc_id, uint8_t ss_pin, PubSubClient *client);
void loop();
};

#endif
7 changes: 3 additions & 4 deletions hardware/esp32-control/src/PassDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

PassDetector::PassDetector() {}

void PassDetector::init(String id, String state, int pin, PubSubClient client)
void PassDetector::init(String id, String state, int pin, PubSubClient *client)
{
this->id = id;
this->pin = pin;
Expand All @@ -18,9 +18,8 @@ void PassDetector::loop()
{
return;
}
char topic[100] = "";
sprintf(topic, "block/%s/update", id.c_str());
client.publish(topic, state.c_str());
String topic = "pass/" + id + "/update";
client->publish(topic.c_str(), state.c_str());
last_switch_time = millis();
}
}
4 changes: 2 additions & 2 deletions hardware/esp32-control/src/PassDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class PassDetector
String state;
int pin;
unsigned long last_switch_time = 0;
PubSubClient client;
PubSubClient *client;

public:
PassDetector();
void loop();
void init(String id, String state, int pin, PubSubClient client);
void init(String id, String state, int pin, PubSubClient *client);
};

#endif
3 changes: 2 additions & 1 deletion hardware/esp32-control/src/SettingLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void loadSetting(char *input, IOManager *manager)
}

// 端末名
const char *name = doc["name"];
const char* host_name = doc["name"];
strcpy(HOST, host_name);

// STOPS
JsonArray stops = doc["stops"];
Expand Down
17 changes: 3 additions & 14 deletions hardware/esp32-control/src/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#include "Settings.h"

const char *ssid = "SPWH_L12_60d5d1";
const char *password = "1df7e4eabee84";
const char *ssid = "SSID";
const char *password = "PASSWORD";

const char *HOST = "sakurajosui";

servo_item ServoSetting[] = {
{"stop", 26, "yamashita_s1"}, // 26
{"stop", 27, "yamashita_s2"}, // 27
{"point", 14, "yamashita_p1"}, // 14
{"stop", 12, "umishita_s1"} // 12
};

switch_item SwitchSetting[] = {
{16, "yamashita_b1", "OPEN"},
{17, "yamashita_b1", "CLOSE"}};
char HOST[25];
45 changes: 1 addition & 44 deletions hardware/esp32-control/src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,9 @@
#define SETTINGS_H
#include <Arduino.h>

typedef struct
{
String target; // stop or point
int pin; // ピン番号
String id; // ID
} servo_item;

typedef struct
{
int pin; // ピン番号
String id; // ID
String state; // OPEN or CLOSE
} switch_item;

/*
Stations:
yamashita_station
umishita_station
StopRail:
yamashita_s1
yamashita_s2
umishita_s1
PointRail:
yamashita_p1
PassDetector:
yamashita_d1
umishita_d1
*/

// 接続されているStopRailとServoの設定

// target: stop, point, pin: [0-9]*, id: str

#define SERVO_SETTING_LENGTH 4
#define SWITCH_SETTING_LENGTH 2

extern servo_item ServoSetting[];

extern switch_item SwitchSetting[];

// WiFi
// const char *ssid = "plarail-2g";
// const char *password = "plarail2023";
extern const char *ssid;
extern const char *password;

extern const char *HOST;
extern char HOST[25];

#endif
30 changes: 21 additions & 9 deletions hardware/esp32-control/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,28 @@ void setup()
}
Serial.printf("\nIP address: %s\n", WiFi.localIP().toString().c_str());

// LittleFS初期化
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED))
{
Serial.println("LittleFS Mount Failed");
return;
}

// 設定ファイルを読み込む
getSetting(&manager);

if (!SETTING_LOADED)
{
Serial.println("Setting File Not Found");
return;
}

// MDNSを開始
MDNS.begin(HOST);

// SPIを初期化
SPI.begin();

espClient.setCACert(root_ca);
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
Expand All @@ -60,19 +80,11 @@ void setup()
}
}

// Subscribe Topics
// 必要なTopicをSubscribeする
client.subscribe("stop/+/get/accepted");
client.subscribe("point/+/get/accepted");
client.subscribe("stop/+/delta");
client.subscribe("point/+/delta");

// LittleFS初期化
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED))
{
Serial.println("LittleFS Mount Failed");
return;
}
getSetting(&manager);
}

void callback(char *topic, byte *payload, unsigned int length)
Expand Down

0 comments on commit 6f77b3b

Please sign in to comment.