mirror of
https://gitlab.com/fabinfra/fabhardware/fabreader.git
synced 2025-03-12 22:51:43 +01:00
Added: OTAProxy
This commit is contained in:
parent
a787f0e7dc
commit
844f04ec6a
20
src/FabReader_v2/README.txt
Normal file
20
src/FabReader_v2/README.txt
Normal file
@ -0,0 +1,20 @@
|
||||
## Protocol:
|
||||
READERID is 5 Digits
|
||||
1. Hello: Topic="fabreader", Payload="READERID"
|
||||
1. Start: Topic="fabreader/READERID/startOTA", Payload=NULL
|
||||
1. Request: Topic="fabreader/READERID/requestOTA", Payload=256 Bytes APDU Command
|
||||
1. Response: Topic="fabreader/READERID/responseOTA", Payload=256 Bytes APDU Response
|
||||
1. Stop: Topic="fabreader/READERID/stopOTA", Payload=NULL
|
||||
1. Cancel: Topic="fabreader/READERID/cancelOTA", Payload=NULL
|
||||
|
||||
## Procedure:
|
||||
After Start Reader sends "Hello".
|
||||
|
||||
After new Card is on Reader:
|
||||
"Start" -> to Server
|
||||
"Request" <- from Server
|
||||
"Response" -> to Server
|
||||
|
||||
Repeat Request and Response until:
|
||||
"Stop" <- from Server
|
||||
"Cancel" -> to Server
|
@ -7,12 +7,15 @@ Config::Config()
|
||||
EEPROM.begin(512);
|
||||
}
|
||||
|
||||
Config::Config(int ID, char broker[], char username[], char password[])
|
||||
Config::Config(int ID, char mqtt_broker[], int mqtt_port, char mqtt_username[], char mqtt_password[], char wlan_ssid[], char wlan_password[])
|
||||
{
|
||||
this->Data.ID = ID;
|
||||
strcpy(this->Data.MQTT_Broker, broker);
|
||||
strcpy(this->Data.MQTT_Username, username);
|
||||
strcpy(this->Data.MQTT_Password, password);
|
||||
Data.ID = ID;
|
||||
strcpy(Data.MQTT_Broker, mqtt_broker);
|
||||
Data.MQTT_Port = mqtt_port;
|
||||
strcpy(Data.MQTT_Username, mqtt_username);
|
||||
strcpy(Data.MQTT_Password, mqtt_password);
|
||||
strcpy(Data.WLAN_SSID, wlan_ssid);
|
||||
strcpy(Data.WLAN_Password, wlan_password);
|
||||
|
||||
EEPROM.begin(512);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <MFRC522.h>
|
||||
|
||||
NFC::NFC(int pin_ss, int pin_rst, PubSubClient* mqtt)
|
||||
NFC::NFC(int pin_ss, int pin_rst)
|
||||
{
|
||||
SPI.begin();
|
||||
|
||||
@ -16,22 +16,24 @@ bool NFC::hasNewCard()
|
||||
return rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20;
|
||||
}
|
||||
|
||||
bool NFC::hasActiveOTA()
|
||||
void NFC::selectCard()
|
||||
{
|
||||
return activOTA;
|
||||
rfid->PICC_Select(&(rfid->uid));
|
||||
uid = rfid->uid;
|
||||
cardSelected = true;
|
||||
}
|
||||
void NFC::deselectCard()
|
||||
{
|
||||
rfid->PICC_HaltA();
|
||||
cardSelected = false;
|
||||
}
|
||||
|
||||
void NFC::startOTA()
|
||||
bool NFC::isCardLost()
|
||||
{
|
||||
// TODO
|
||||
return rfid->PICC_ReadCardSerial() && rfid->uid.uidByte != uid.uidByte;
|
||||
}
|
||||
|
||||
void NFC::continueOTA()
|
||||
MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte response_len)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
void NFC::cancelOTA()
|
||||
{
|
||||
|
||||
}
|
||||
return rfid->PCD_TransceiveData(command, command_len, response, &response_len, NULL, 0, true);
|
||||
}
|
@ -8,17 +8,17 @@ class NFC
|
||||
{
|
||||
private:
|
||||
MFRC522* rfid;
|
||||
PubSubClient* mqtt;
|
||||
bool activOTA = false;
|
||||
MFRC522::Uid uid;
|
||||
bool cardSelected;
|
||||
|
||||
public:
|
||||
NFC(int pin_ss, int pin_rst, PubSubClient* mqtt);
|
||||
NFC(int pin_ss, int pin_rst);
|
||||
bool hasNewCard();
|
||||
bool hasActiveOTA();
|
||||
|
||||
void startOTA();
|
||||
void continueOTA();
|
||||
void cancelOTA();
|
||||
void selectCard();
|
||||
void deselectCard();
|
||||
bool isCardLost();
|
||||
|
||||
MFRC522::StatusCode Transceive(byte* command, byte command_len, byte* response, byte response_len);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
61
src/FabReader_v2/src/OTAProxy.cpp
Normal file
61
src/FabReader_v2/src/OTAProxy.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "OTAProxy.h"
|
||||
#include "NFC.h"
|
||||
#include <PubSubClient.h>
|
||||
|
||||
OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id)
|
||||
{
|
||||
this->mqtt = mqttClient;
|
||||
this->nfc = nfc;
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
bool OTAProxy::hasActiveOTA()
|
||||
{
|
||||
return activeOTA;
|
||||
}
|
||||
|
||||
void OTAProxy::startOTA()
|
||||
{
|
||||
nfc->selectCard();
|
||||
activeOTA = true;
|
||||
|
||||
char topic[] = "fabreader/00000/startOTA";
|
||||
sprintf(topic, "fabreader/%05d/startOTA", id);
|
||||
mqtt->publish(topic, "");
|
||||
}
|
||||
|
||||
void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
char topic_requestOTA[] = "fabreader/00000/requestOTA";
|
||||
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id);
|
||||
|
||||
char topic_responseOTA[] = "fabreader/00000/responseOTA";
|
||||
sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id);
|
||||
|
||||
char topic_stopOTA[] = "fabreader/00000/stopOTA";
|
||||
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id);
|
||||
|
||||
if(strcmp(topic, topic_requestOTA))
|
||||
{
|
||||
byte response[APDU_BUFFER_SIZE] = {0};
|
||||
nfc->Transceive(payload, length, response, APDU_BUFFER_SIZE);
|
||||
|
||||
mqtt->publish(topic_responseOTA, response, APDU_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
if(strcmp(topic, topic_stopOTA))
|
||||
{
|
||||
nfc->deselectCard();
|
||||
activeOTA = false;
|
||||
}
|
||||
}
|
||||
|
||||
void OTAProxy::cancelOTA()
|
||||
{
|
||||
char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
|
||||
sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id);
|
||||
mqtt->publish(topic_cancelOTA, "");
|
||||
|
||||
nfc->deselectCard();
|
||||
activeOTA = false;
|
||||
}
|
26
src/FabReader_v2/src/OTAProxy.h
Normal file
26
src/FabReader_v2/src/OTAProxy.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef OTAProxy_H
|
||||
#define OTAProxy_H
|
||||
|
||||
#include <PubSubClient.h>
|
||||
#include "NFC.h"
|
||||
#define MSG_BUFFER_SIZE 50
|
||||
#define APDU_BUFFER_SIZE 256
|
||||
|
||||
class OTAProxy
|
||||
{
|
||||
private:
|
||||
bool activeOTA;
|
||||
PubSubClient* mqtt;
|
||||
NFC* nfc;
|
||||
int id;
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
public:
|
||||
OTAProxy(PubSubClient* mqtt, NFC* nfc, int id);
|
||||
bool hasActiveOTA();
|
||||
|
||||
void startOTA();
|
||||
void continueOTA(char* topic, byte* payload, unsigned int length);
|
||||
void cancelOTA();
|
||||
};
|
||||
|
||||
#endif
|
@ -4,6 +4,7 @@
|
||||
#include "Config.h"
|
||||
#include "Pins.h"
|
||||
#include "NFC.h"
|
||||
#include "OTAProxy.h"
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
@ -14,13 +15,71 @@
|
||||
#include <MFRC522.h>
|
||||
#include <SSD1306Wire.h>
|
||||
|
||||
Config config;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqttClient;
|
||||
WiFiClient espClient;
|
||||
Config_Data config;
|
||||
|
||||
PubSubClient* mqtt;
|
||||
NFC* nfc;
|
||||
OTAProxy* ota;
|
||||
|
||||
void setup_wifi()
|
||||
{
|
||||
delay(10);
|
||||
Serial.println("Connecting Wifi");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(config.WLAN_SSID, config.WLAN_Password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
}
|
||||
|
||||
randomSeed(micros());
|
||||
Serial.println("WiFi connected");
|
||||
}
|
||||
|
||||
void reconnect()
|
||||
{
|
||||
while (!mqtt->connected())
|
||||
{
|
||||
String clientId = "FabReader_";
|
||||
clientId += String(config.ID);
|
||||
if (mqtt->connect(clientId.c_str()))
|
||||
{
|
||||
Serial.println("connected");
|
||||
char id[6] = "00000";
|
||||
sprintf(id, "%05d", config.ID);
|
||||
mqtt->publish("fabreader", id);
|
||||
|
||||
char topic[] = "fabreader/00000/#";
|
||||
sprintf(topic, "fabreader/%05d/#", config.ID);
|
||||
|
||||
mqtt->subscribe(topic);
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(mqtt->state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
if(ota->hasActiveOTA())
|
||||
{
|
||||
ota->continueOTA(topic, payload, length);
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
config.ID = 1;
|
||||
strcpy(config.WLAN_SSID, "");
|
||||
strcpy(config.WLAN_Password, "");
|
||||
strcpy(config.MQTT_Broker, "");
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.print("\n\n\n");
|
||||
Serial.println("Booting ...");
|
||||
@ -31,13 +90,31 @@ void setup()
|
||||
digitalWrite(PIN_ETH_SPI_SS, HIGH);
|
||||
|
||||
Serial.println("NFC ...");
|
||||
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST, &mqttClient);
|
||||
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
|
||||
|
||||
setup_wifi();
|
||||
|
||||
mqtt = new PubSubClient(espClient);
|
||||
mqtt->setServer(config.MQTT_Broker, config.MQTT_Port);
|
||||
mqtt->setCallback(callback);
|
||||
|
||||
ota = new OTAProxy(mqtt, nfc, config.ID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (nfc->hasNewCard())
|
||||
if (!mqtt->connected())
|
||||
{
|
||||
Serial.println("New Card");
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
mqtt->loop();
|
||||
|
||||
if(!ota->hasActiveOTA() && nfc->hasNewCard())
|
||||
{
|
||||
ota->startOTA();
|
||||
}
|
||||
if(ota->hasActiveOTA() && nfc->isCardLost())
|
||||
{
|
||||
ota->cancelOTA();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user