Fixed Config

This commit is contained in:
TheJoKlLa 2022-09-30 21:21:34 +02:00
parent 984bc9d873
commit 3122fabc36
8 changed files with 100 additions and 49 deletions

View File

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
src/config.h

View File

@ -0,0 +1,32 @@
#ifndef CONFIG_H
#define CONFIG_H
struct Config_Data
{
int ID = 0;
char MQTT_Broker[256];
int MQTT_Port = 1883;
char MQTT_Username[64];
char MQTT_Password[64];
char WLAN_SSID[65];
char WLAN_Password[64];
};
class Config {
public:
const char WLANAP_SSID[32] = "FabReader";
const char WLANAP_Password[64] = "FabReader";
Config_Data Data;
Config();
Config(int ID, char mqtt_broker[], int mqtt_port, char mqtt_username[], char mqtt_password[], char wlan_ssid[], char wlan_password[]);
void Load();
void Save();
bool IsEmpty();
};
#endif

View File

@ -15,25 +15,26 @@ bool NFC::hasNewCard()
{
if(rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial())
{
byte atqaLen = sizeof(atqa);
MFRC522::StatusCode status = rfid->PICC_RequestA(atqa, &atqaLen);
Serial.printf("PICC_RequestA: 0x%02x\n", status);
if(status == MFRC522::STATUS_OK && atqa[0] == 0x44)
{
return true;
}
return true;
// byte atqaLen = sizeof(atqa);
// MFRC522::StatusCode status = rfid->PICC_RequestA(atqa, &atqaLen);
// Serial.printf("PICC_RequestA: 0x%02x\n", status);
// if(status == MFRC522::STATUS_OK && atqa[0] == 0x44)
// {
// return true;
// }
}
return false;
}
bool NFC::selectCard()
{
MFRC522::StatusCode status = rfid->PICC_Select(&(rfid->uid));
Serial.printf("PICC_Select: 0x%02x\n", status);
if(status != MFRC522::STATUS_OK)
{
return false;
}
// MFRC522::StatusCode status = rfid->PICC_Select(&(rfid->uid));
// Serial.printf("PICC_Select: 0x%02x\n", status);
// if(status != MFRC522::STATUS_OK)
// {
// return false;
// }
cardSelected = true;
uid = rfid->uid;
@ -43,12 +44,12 @@ bool NFC::selectCard()
bool NFC::deselectCard()
{
MFRC522::StatusCode status = rfid->PICC_HaltA();
Serial.printf("PICC_HaltA: 0x%02x\n", status);
if(status != MFRC522::STATUS_OK)
{
return false;
}
// MFRC522::StatusCode status = rfid->PICC_HaltA();
// Serial.printf("PICC_HaltA: 0x%02x\n", status);
// if(status != MFRC522::STATUS_OK)
// {
// return false;
// }
cardSelected = false;
Serial.println("Card Deselected");

View File

@ -44,22 +44,39 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
char topic_restartOTA[] = "fabreader/00000/restartOTA";
sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
if(strcmp(topic, topic_requestOTA))
if(!strcmp(topic, topic_requestOTA))
{
Serial.println("Request OTA");
byte response[APDU_BUFFER_SIZE] = {0};
nfc->Transceive(payload, length, response, APDU_BUFFER_SIZE);
MFRC522::StatusCode status;
do
{
status = nfc->Transceive(payload, length, response, APDU_BUFFER_SIZE);
Serial.printf("PICC_Tranceive: 0x%02x\n", status);
delay(100);
}
while (status != MFRC522::STATUS_OK);
for(int i = 0; i < APDU_BUFFER_SIZE; i++)
{
char hexCar[2];
sprintf(hexCar, "%02X", response[i]);
Serial.print(hexCar);
}
Serial.println();
mqtt->publish(topic_responseOTA, response, APDU_BUFFER_SIZE);
Serial.println("Response OTA");
}
else if(strcmp(topic, topic_stopOTA))
else if(!strcmp(topic, topic_stopOTA))
{
Serial.println("Stop OTA");
while(!(nfc->deselectCard()));
activeOTA = false;
}
else if(strcmp(topic, topic_restartOTA))
else if(!strcmp(topic, topic_restartOTA))
{
Serial.println("Restart OTA");
while(!(nfc->deselectCard()));

View File

@ -0,0 +1,4 @@
#define WLAN_SSID ""
#define WLAN_PASS ""
#define MQTT_BROKER ""
#define FABREADERID 1

View File

@ -1,10 +1,10 @@
#include <Arduino.h>
#include <ArduinoLog.h>
#include "Config.h"
#include "Pins.h"
#include "NFC.h"
#include "OTAProxy.h"
#include "config.h"
#include "pins.h"
#include "nfc.h"
#include "otaproxy.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
@ -16,7 +16,7 @@
#include <SSD1306Wire.h>
WiFiClient espClient;
Config_Data config;
//Config_Data config;
PubSubClient* mqtt;
NFC* nfc;
@ -28,7 +28,7 @@ void setup_wifi()
Serial.println("Connecting Wifi ...");
WiFi.mode(WIFI_STA);
WiFi.begin(config.WLAN_SSID, config.WLAN_Password);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
@ -44,22 +44,22 @@ void reconnect()
while (!mqtt->connected())
{
String clientId = "FabReader_";
clientId += String(config.ID);
clientId += String(FABREADERID);
Serial.println("Connecting MQTT ...");
if (mqtt->connect(clientId.c_str()))
{
Serial.println("MQTT connected");
char id[6] = "00000";
sprintf(id, "%05d", config.ID);
sprintf(id, "%05d", FABREADERID);
mqtt->publish("fabreader", id);
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", config.ID);
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID);
mqtt->subscribe(topic_requestOTA);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", config.ID);
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID);
mqtt->subscribe(topic_stopOTA);
} else {
Serial.print("failed, rc=");
@ -72,7 +72,8 @@ void reconnect()
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.println("Recive Message");
Serial.println("Receive Message");
Serial.println(topic);
if(ota->hasActiveOTA())
{
ota->continueOTA(topic, payload, length);
@ -81,18 +82,13 @@ void callback(char* topic, byte* payload, unsigned int 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 ...");
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_ETH_SPI_SS, OUTPUT);
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_ETH_SPI_SS, OUTPUT);
digitalWrite(PIN_ETH_SPI_SS, HIGH);
Serial.println("Connecting NFC ...");
@ -101,16 +97,15 @@ void setup()
{
Serial.println("NFC Test failed");
}
//nfc->rfid->PCD_SetAntennaGain(MFRC522::RxGain_max);
Serial.println("NFC connected");
setup_wifi();
mqtt = new PubSubClient(espClient);
mqtt->setServer(config.MQTT_Broker, config.MQTT_Port);
mqtt->setServer(MQTT_BROKER, 1883);
mqtt->setCallback(callback);
ota = new OTAProxy(mqtt, nfc, config.ID);
ota = new OTAProxy(mqtt, nfc, FABREADERID);
}
void loop()
@ -121,12 +116,13 @@ void loop()
}
mqtt->loop();
if(!ota->hasActiveOTA() && nfc->hasNewCard())
{
ota->startOTA();
}
if(ota->hasActiveOTA() && nfc->isCardLost())
{
ota->cancelOTA();
}
if(!ota->hasActiveOTA())
{
ota->startOTA();
}
}