First Running OTA

This commit is contained in:
TheJoKlLa 2022-10-31 17:19:20 +01:00
parent 23527a674a
commit 0670e33f12
4 changed files with 252 additions and 331 deletions

View File

@ -1,110 +1,96 @@
// #include "OTAProxy.h"
// #include "NFC.h"
// #include <PubSubClient.h>
#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;
// }
OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id)
{
this->mqtt = mqttClient;
this->nfc = nfc;
this->id = id;
}
// bool OTAProxy::hasActiveOTA()
// {
// return activeOTA;
// }
bool OTAProxy::hasActiveOTA()
{
return activeOTA;
}
// void OTAProxy::startOTA()
// {
// if(!(nfc->selectCard()))
// {
// return;
// }
// activeOTA = true;
void OTAProxy::startOTA()
{
if(!(nfc->hasCardSelected()))
{
return;
}
activeOTA = true;
// char topic[] = "fabreader/00000/startOTA";
// sprintf(topic, "fabreader/%05d/startOTA", id);
// MFRC522::Uid uid = nfc->getUID();
// mqtt->publish(topic, uid.uidByte, uid.size);
char topic[] = "fabreader/00000/startOTA";
sprintf(topic, "fabreader/%05d/startOTA", id);
MFRC522::Uid uid = nfc->getUID();
mqtt->publish(topic, uid.uidByte, uid.size);
// Serial.println("Start OTA");
// }
Serial.println("Start OTA");
}
// void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
// {
// char topic_requestOTA[] = "fabreader/00000/requestOTA";
// sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id);
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_responseOTA[] = "fabreader/00000/responseOTA";
sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id);
// char topic_stopOTA[] = "fabreader/00000/stopOTA";
// sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id);
// char topic_restartOTA[] = "fabreader/00000/restartOTA";
// sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
char topic_restartOTA[] = "fabreader/00000/restartOTA";
sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
// if(!strcmp(topic, topic_requestOTA))
// {
// Serial.println("Request OTA");
// byte response[APDU_BUFFER_SIZE] = {0};
// byte response_len;
if(!strcmp(topic, topic_requestOTA))
{
Serial.println("Request OTA");
byte response[APDU_BUFFER_SIZE] = {0};
byte response_len;
// MFRC522::StatusCode status;
// while (true)
// {
// if(nfc->isCardLost())
// {
// Serial.println("Card Lost");
// return;
// }
// Serial.println("Run Transceive");
// status = nfc->Transceive(payload, length, response, &response_len);
// Serial.printf("PICC_Tranceive: 0x%02x\n", status);
// if(status == MFRC522::STATUS_OK)
// {
// break;
// }
// }
MFRC522::StatusCode status;
// for(int i = 0; i < response_len; i++)
// {
// char hexCar[2];
Serial.println("Run Transceive");
status = nfc->Transceive(payload, length, response, &response_len);
Serial.printf("PICC_Tranceive: 0x%02x\n", status);
if(status != MFRC522::STATUS_OK)
{
cancelOTA();
return;
}
// sprintf(hexCar, "%02X", response[i]);
// Serial.print(hexCar);
// }
// Serial.println();
mqtt->publish(topic_responseOTA, response, response_len);
Serial.println("Response OTA");
}
else if(!strcmp(topic, topic_stopOTA))
{
Serial.println("Stop OTA");
while(!(nfc->disconnectCard()));
activeOTA = false;
}
// else if(!strcmp(topic, topic_restartOTA))
// {
// Serial.println("Restart OTA");
// while(!(nfc->deselectCard()));
// // if(nfc->hasNewCard())
// // {
// // startOTA();
// // }
// }
}
// mqtt->publish(topic_responseOTA, response, response_len);
// Serial.println("Response OTA");
// }
// else if(!strcmp(topic, topic_stopOTA))
// {
// Serial.println("Stop OTA");
// while(!(nfc->deselectCard()));
// activeOTA = false;
// }
// else if(!strcmp(topic, topic_restartOTA))
// {
// Serial.println("Restart OTA");
// while(!(nfc->deselectCard()));
// // if(nfc->hasNewCard())
// // {
// // startOTA();
// // }
// }
// }
void OTAProxy::cancelOTA()
{
char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id);
MFRC522::Uid uid = nfc->getUID();
mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size);
// void OTAProxy::cancelOTA()
// {
// char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
// sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id);
// MFRC522::Uid uid = nfc->getUID();
// mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size);
while(!(nfc->disconnectCard()));
activeOTA = false;
// while(!(nfc->deselectCard()));
// activeOTA = false;
// Serial.println("Cancel OTA");
// }
Serial.println("Cancel OTA");
}

View File

@ -1,58 +1,132 @@
#include <SPI.h>
#include <MFRC522.h>
#include <Desfire.h>
#include <Arduino.h>
#include <ArduinoLog.h>
#include "nfc.h"
#include "config.h"
#include "pins.h"
#include "nfc.h"
#include "otaproxy.h"
#include "helpers.h"
#include "Desfire.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <SSD1306Wire.h>
WiFiClient espClient;
//Config_Data config;
PubSubClient* mqtt;
NFC* nfc;
OTAProxy* ota;
void setup()
void setup_wifi()
{
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
delay(10);
Serial.println("Connecting Wifi ...");
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
randomSeed(micros());
Serial.println("WiFi connected");
}
void reconnect()
{
while (!mqtt->connected())
{
String clientId = "FabReader_";
clientId += String(FABREADERID);
Serial.println("Connecting MQTT ...");
if (mqtt->connect(clientId.c_str()))
{
Serial.println("MQTT connected");
char id[6] = "00000";
sprintf(id, "%05d", FABREADERID);
mqtt->publish("fabreader", id);
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID);
mqtt->subscribe(topic_requestOTA);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID);
mqtt->subscribe(topic_stopOTA);
} 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)
{
Serial.println("Receive Message");
Serial.println(topic);
if(ota->hasActiveOTA())
{
ota->continueOTA(topic, payload, length);
}
}
void setup()
{
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);
digitalWrite(PIN_ETH_SPI_SS, HIGH);
Serial.println("Connecting NFC ...");
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
if(!(nfc->rfid->PCD_PerformSelfTest()))
{
Serial.println("NFC Test failed");
}
Serial.println("NFC connected");
setup_wifi();
mqtt = new PubSubClient(espClient);
mqtt->setServer(MQTT_BROKER, 1883);
mqtt->setCallback(callback);
ota = new OTAProxy(mqtt, nfc, FABREADERID);
}
void loop()
{
if(nfc->checkforCard())
{
if (!mqtt->connected())
{
Serial.println("Card detected");
if(nfc->connecttoCard())
reconnect();
}
mqtt->loop();
if(!ota->hasActiveOTA())
{
if(nfc->checkforCard())
{
Serial.println("Card connected");
MFRC522::StatusCode state;
byte request_buffer[APDU_BUFFER_SIZE] = {0x90, 0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00};
byte request_buffer_size = 9;
byte response_buffer[APDU_BUFFER_SIZE] = {0};
byte response_buffer_size;
printbytes(request_buffer, request_buffer_size);
state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
if (state != MFRC522::STATUS_OK)
Serial.println("Card detected");
if(nfc->connecttoCard())
{
Serial.println("Data Exchange failed");
Serial.println(state);
Serial.println("Card connected");
ota->startOTA();
}
Serial.println("Data Exchange complete");
printbytes(response_buffer, response_buffer_size);
state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
if (state != MFRC522::STATUS_OK)
{
Serial.println("Data Exchange failed");
Serial.println(state);
}
Serial.println("Data Exchange complete");
printbytes(response_buffer, response_buffer_size);
nfc->disconnectCard();
Serial.println("Card disconnected");
}
}
}
}

View File

@ -1,197 +0,0 @@
#include <Arduino.h>
#include <ArduinoLog.h>
#include "config.h"
#include "pins.h"
#include "nfc.h"
#include "otaproxy.h"
#include "helpers.h"
#include "Desfire.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <SSD1306Wire.h>
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(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
randomSeed(micros());
Serial.println("WiFi connected");
}
void reconnect()
{
while (!mqtt->connected())
{
String clientId = "FabReader_";
clientId += String(FABREADERID);
Serial.println("Connecting MQTT ...");
if (mqtt->connect(clientId.c_str()))
{
Serial.println("MQTT connected");
char id[6] = "00000";
sprintf(id, "%05d", FABREADERID);
mqtt->publish("fabreader", id);
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID);
mqtt->subscribe(topic_requestOTA);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID);
mqtt->subscribe(topic_stopOTA);
} 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)
{
Serial.println("Receive Message");
Serial.println(topic);
if(ota->hasActiveOTA())
{
ota->continueOTA(topic, payload, length);
}
}
void setup()
{
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);
digitalWrite(PIN_ETH_SPI_SS, HIGH);
Serial.println("Connecting NFC ...");
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
if(!(nfc->rfid->PCD_PerformSelfTest()))
{
Serial.println("NFC Test failed");
}
Serial.println("NFC connected");
// setup_wifi();
// mqtt = new PubSubClient(espClient);
// mqtt->setServer(MQTT_BROKER, 1883);
// mqtt->setCallback(callback);
// ota = new OTAProxy(mqtt, nfc, FABREADERID);
}
void loop()
{
// if(!(nfc->hasCardSelected()) && nfc->hasNewCard() && nfc->selectCard())
// {
// // DESFire::mifare_desfire_tag tag;
// // tag.pcb = 0x0A;
// // tag.cid = 0x00;
// // DESFire::mifare_desfire_aid_t aid;
// // aid.data[0] = 0x42;
// // aid.data[1] = 0x41;
// // aid.data[2] = 0x46;
// // DESFire::StatusCode status = nfc->rfid->MIFARE_DESFIRE_SelectApplication(&tag, &aid);
// // Serial.printf("%02x\n", status.mfrc522);
// // char command_h[63*2] = "905a00000342414600";
// // byte command[63] = { 0 };
// // byte command_len = strlen(command_h)/2;
// // byte response[63] = { 0 };
// // byte response_len = 0;
// // char response_h[63*2] = { 0 };
// // Serial.print("Command: ");
// // Serial.println(command_h);
// // chars2bytes(command_h, command, true);
// // MFRC522::StatusCode status = nfc->Transceive(command, command_len, response, &response_len);
// // Serial.printf("PICC_Tranceive: 0x%02x\n", status);
// // bytes2chars(response, response_len, response_h, true);
// // Serial.print("Response: ");
// // Serial.println(response_h);
// }
// if(nfc->hasCardSelected() && nfc->isCardLost())
// {
// nfc->deselectCard();
// }
if( nfc->hasCardSelected() ) {
if( nfc->isCardLost() ) {
nfc->deselectCard();
Serial.println("Stop");
}
}
else {
if( nfc->hasNewCard() ) {
if( nfc->selectCard() ) {
Serial.println("Run");
}
}
}
/*
if(!(nfc->hasCardSelected()) && nfc->hasNewCard())
{
if(nfc->selectCard())
{
Serial.println("Run");
}
}
if(nfc->hasCardSelected() && nfc->isCardLost())
{
Serial.println("Stop");
}
*/
// if (!mqtt->connected())
// {
// reconnect();
// }
// mqtt->loop();
// if(ota->hasActiveOTA() && nfc->isCardLost())
// {
// ota->cancelOTA();
// }
// if(!ota->hasActiveOTA() && nfc->hasNewCard())
// {
// ota->startOTA();
// }
}

View File

@ -0,0 +1,58 @@
#include <SPI.h>
#include <MFRC522.h>
#include <Desfire.h>
#include "nfc.h"
#include "pins.h"
#include "helpers.h"
NFC* nfc;
void setup()
{
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
}
void loop()
{
if(nfc->checkforCard())
{
Serial.println("Card detected");
if(nfc->connecttoCard())
{
Serial.println("Card connected");
MFRC522::StatusCode state;
byte request_buffer[APDU_BUFFER_SIZE] = {0x90, 0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00};
byte request_buffer_size = 9;
byte response_buffer[APDU_BUFFER_SIZE] = {0};
byte response_buffer_size;
printbytes(request_buffer, request_buffer_size);
state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
if (state != MFRC522::STATUS_OK)
{
Serial.println("Data Exchange failed");
Serial.println(state);
}
Serial.println("Data Exchange complete");
printbytes(response_buffer, response_buffer_size);
state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
if (state != MFRC522::STATUS_OK)
{
Serial.println("Data Exchange failed");
Serial.println(state);
}
Serial.println("Data Exchange complete");
printbytes(response_buffer, response_buffer_size);
nfc->disconnectCard();
Serial.println("Card disconnected");
}
}
}