Fixed
update touch sensor connection logic to correctly identify connection status
This commit is contained in:
+6
-3
@@ -3,7 +3,7 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "commonFS.h"
|
#include "commonFS.h"
|
||||||
|
|
||||||
bool spoolman_connected = false;
|
volatile spoolmanApiStateType spoolmanApiState = API_INIT;
|
||||||
String spoolmanUrl = "";
|
String spoolmanUrl = "";
|
||||||
bool octoEnabled = false;
|
bool octoEnabled = false;
|
||||||
String octoUrl = "";
|
String octoUrl = "";
|
||||||
@@ -85,7 +85,8 @@ JsonDocument fetchSingleSpoolInfo(int spoolId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sendToApi(void *parameter) {
|
void sendToApi(void *parameter) {
|
||||||
SendToApiParams* params = (SendToApiParams*)parameter;
|
spoolmanApiState = API_TRANSMITTING;
|
||||||
|
SendToApiParams *params = (SendToApiParams *)parameter;
|
||||||
|
|
||||||
// Extrahiere die Werte
|
// Extrahiere die Werte
|
||||||
String httpType = params->httpType;
|
String httpType = params->httpType;
|
||||||
@@ -120,6 +121,7 @@ void sendToApi(void *parameter) {
|
|||||||
// Speicher freigeben
|
// Speicher freigeben
|
||||||
delete params;
|
delete params;
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
|
spoolmanApiState = API_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateSpoolTagId(String uidString, const char* payload) {
|
bool updateSpoolTagId(String uidString, const char* payload) {
|
||||||
@@ -477,7 +479,8 @@ bool checkSpoolmanInstance(const String& url) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
spoolman_connected = true;
|
spoolmanApiState = API_IDLE;
|
||||||
|
oledShowTopRow();
|
||||||
return strcmp(status, "healthy") == 0;
|
return strcmp(status, "healthy") == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,14 @@
|
|||||||
#include "website.h"
|
#include "website.h"
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
API_INIT,
|
||||||
|
API_IDLE,
|
||||||
|
API_TRANSMITTING
|
||||||
|
} spoolmanApiStateType;
|
||||||
|
|
||||||
|
extern volatile spoolmanApiStateType spoolmanApiState;
|
||||||
extern bool spoolman_connected;
|
extern bool spoolman_connected;
|
||||||
extern String spoolmanUrl;
|
extern String spoolmanUrl;
|
||||||
extern bool octoEnabled;
|
extern bool octoEnabled;
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ const uint16_t SCALE_LEVEL_WEIGHT = 500;
|
|||||||
uint16_t defaultScaleCalibrationValue = 430;
|
uint16_t defaultScaleCalibrationValue = 430;
|
||||||
// ***** HX711
|
// ***** HX711
|
||||||
|
|
||||||
|
// ***** TTP223 (Touch Sensor)
|
||||||
|
// TTP223 circuit wiring
|
||||||
|
const uint8_t TTP223_PIN = 25;
|
||||||
|
// ***** TTP223
|
||||||
|
|
||||||
// ***** Display
|
// ***** Display
|
||||||
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
|
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
|
||||||
// On an ESP32: 21(SDA), 22(SCL)
|
// On an ESP32: 21(SDA), 22(SCL)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ extern const uint8_t LOADCELL_DOUT_PIN;
|
|||||||
extern const uint8_t LOADCELL_SCK_PIN;
|
extern const uint8_t LOADCELL_SCK_PIN;
|
||||||
extern const uint8_t calVal_eepromAdress;
|
extern const uint8_t calVal_eepromAdress;
|
||||||
extern const uint16_t SCALE_LEVEL_WEIGHT;
|
extern const uint16_t SCALE_LEVEL_WEIGHT;
|
||||||
|
extern const uint8_t TTP223_PIN;
|
||||||
|
|
||||||
extern const int8_t OLED_RESET;
|
extern const int8_t OLED_RESET;
|
||||||
extern const uint8_t SCREEN_ADDRESS;
|
extern const uint8_t SCREEN_ADDRESS;
|
||||||
|
|||||||
+6
-3
@@ -177,9 +177,12 @@ void oledShowTopRow() {
|
|||||||
display.drawBitmap(50, 0, bitmap_off , 16, 16, WHITE);
|
display.drawBitmap(50, 0, bitmap_off , 16, 16, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spoolman_connected == 1) {
|
if (spoolmanApiState != API_INIT)
|
||||||
display.drawBitmap(80, 0, bitmap_spoolman_on , 16, 16, WHITE);
|
{
|
||||||
} else {
|
display.drawBitmap(80, 0, bitmap_spoolman_on, 16, 16, WHITE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
display.drawBitmap(80, 0, bitmap_off , 16, 16, WHITE);
|
display.drawBitmap(80, 0, bitmap_off , 16, 16, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+28
-25
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
bool mainTaskWasPaused = 0;
|
bool mainTaskWasPaused = 0;
|
||||||
uint8_t scaleTareCounter = 0;
|
uint8_t scaleTareCounter = 0;
|
||||||
|
bool touchSensorConnected = false;
|
||||||
|
|
||||||
// ##### SETUP #####
|
// ##### SETUP #####
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -39,7 +40,6 @@ void setup() {
|
|||||||
setupWebserver(server);
|
setupWebserver(server);
|
||||||
|
|
||||||
// Spoolman API
|
// Spoolman API
|
||||||
// api.cpp
|
|
||||||
initSpoolman();
|
initSpoolman();
|
||||||
|
|
||||||
// Bambu MQTT
|
// Bambu MQTT
|
||||||
@@ -48,6 +48,7 @@ void setup() {
|
|||||||
// NFC Reader
|
// NFC Reader
|
||||||
startNfc();
|
startNfc();
|
||||||
|
|
||||||
|
// Scale
|
||||||
start_scale();
|
start_scale();
|
||||||
|
|
||||||
// WDT initialisieren mit 10 Sekunden Timeout
|
// WDT initialisieren mit 10 Sekunden Timeout
|
||||||
@@ -56,6 +57,13 @@ void setup() {
|
|||||||
|
|
||||||
// Aktuellen Task (loopTask) zum Watchdog hinzufügen
|
// Aktuellen Task (loopTask) zum Watchdog hinzufügen
|
||||||
esp_task_wdt_add(NULL);
|
esp_task_wdt_add(NULL);
|
||||||
|
// Touch Sensor
|
||||||
|
pinMode(TTP223_PIN, INPUT_PULLUP);
|
||||||
|
if (digitalRead(TTP223_PIN) == LOW)
|
||||||
|
{
|
||||||
|
Serial.println("Touch Sensor is connected");
|
||||||
|
touchSensorConnected = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,13 +92,25 @@ uint8_t autoAmsCounter = 0;
|
|||||||
uint8_t weightSend = 0;
|
uint8_t weightSend = 0;
|
||||||
int16_t lastWeight = 0;
|
int16_t lastWeight = 0;
|
||||||
|
|
||||||
|
// WIFI check variables
|
||||||
unsigned long lastWifiCheckTime = 0;
|
unsigned long lastWifiCheckTime = 0;
|
||||||
const unsigned long wifiCheckInterval = 60000; // Überprüfe alle 60 Sekunden (60000 ms)
|
const unsigned long wifiCheckInterval = 60000; // Überprüfe alle 60 Sekunden (60000 ms)
|
||||||
|
|
||||||
|
// Button debounce variables
|
||||||
|
unsigned long lastButtonPress = 0;
|
||||||
|
const unsigned long debounceDelay = 500; // 500 ms debounce delay
|
||||||
|
|
||||||
// ##### PROGRAM START #####
|
// ##### PROGRAM START #####
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
|
|
||||||
|
// Überprüfe den Status des Touch Sensors
|
||||||
|
if (touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay)
|
||||||
|
{
|
||||||
|
lastButtonPress = currentMillis;
|
||||||
|
scaleTareRequest = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Überprüfe regelmäßig die WLAN-Verbindung
|
// Überprüfe regelmäßig die WLAN-Verbindung
|
||||||
if (intervalElapsed(currentMillis, lastWifiCheckTime, wifiCheckInterval)) {
|
if (intervalElapsed(currentMillis, lastWifiCheckTime, wifiCheckInterval)) {
|
||||||
checkWiFiConnection();
|
checkWiFiConnection();
|
||||||
@@ -105,7 +125,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
if (intervalElapsed(currentMillis, lastAutoSetBambuAmsTime, autoSetBambuAmsInterval))
|
if (intervalElapsed(currentMillis, lastAutoSetBambuAmsTime, autoSetBambuAmsInterval))
|
||||||
{
|
{
|
||||||
if (hasReadRfidTag == 0)
|
if (nfcReaderState == NFC_IDLE)
|
||||||
{
|
{
|
||||||
lastAutoSetBambuAmsTime = currentMillis;
|
lastAutoSetBambuAmsTime = currentMillis;
|
||||||
oledShowMessage("Auto Set " + String(autoSetBambuAmsCounter - autoAmsCounter) + "s");
|
oledShowMessage("Auto Set " + String(autoSetBambuAmsCounter - autoAmsCounter) + "s");
|
||||||
@@ -139,7 +159,7 @@ void loop() {
|
|||||||
// Ausgabe der Waage auf Display
|
// Ausgabe der Waage auf Display
|
||||||
if(pauseMainTask == 0)
|
if(pauseMainTask == 0)
|
||||||
{
|
{
|
||||||
if (mainTaskWasPaused || (weight != lastWeight && hasReadRfidTag == 0 && (!autoSendToBambu || autoSetToBambuSpoolId == 0)))
|
if (mainTaskWasPaused || (weight != lastWeight && nfcReaderState == NFC_IDLE && (!autoSendToBambu || autoSetToBambuSpoolId == 0)))
|
||||||
{
|
{
|
||||||
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
|
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
|
||||||
}
|
}
|
||||||
@@ -152,29 +172,10 @@ void loop() {
|
|||||||
|
|
||||||
|
|
||||||
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
|
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
|
||||||
if (currentMillis - lastWeightReadTime >= weightReadInterval && hasReadRfidTag < 3)
|
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING)
|
||||||
{
|
{
|
||||||
lastWeightReadTime = currentMillis;
|
lastWeightReadTime = currentMillis;
|
||||||
|
|
||||||
// Prüfen ob die Waage korrekt genullt ist
|
|
||||||
if ((weight > 0 && weight < 5) || weight < -1)
|
|
||||||
{
|
|
||||||
if(scaleTareCounter < 5)
|
|
||||||
{
|
|
||||||
scaleTareCounter++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
scaleTareRequest = true;
|
|
||||||
scaleTareCounter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
scaleTareCounter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prüfen ob das Gewicht gleich bleibt und dann senden
|
// Prüfen ob das Gewicht gleich bleibt und dann senden
|
||||||
if (weight == lastWeight && weight > 5)
|
if (weight == lastWeight && weight > 5)
|
||||||
{
|
{
|
||||||
@@ -188,7 +189,8 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reset weight counter after writing tag
|
// reset weight counter after writing tag
|
||||||
if (currentMillis - lastWeightReadTime >= weightReadInterval && hasReadRfidTag > 1)
|
// TBD: what exactly is the logic behind this?
|
||||||
|
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState != NFC_IDLE && nfcReaderState != NFC_READ_SUCCESS)
|
||||||
{
|
{
|
||||||
weigthCouterToApi = 0;
|
weigthCouterToApi = 0;
|
||||||
}
|
}
|
||||||
@@ -196,7 +198,8 @@ void loop() {
|
|||||||
lastWeight = weight;
|
lastWeight = weight;
|
||||||
|
|
||||||
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
|
||||||
if (spoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && hasReadRfidTag == 1) {
|
if (spoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS)
|
||||||
|
{
|
||||||
oledShowIcon("loading");
|
oledShowIcon("loading");
|
||||||
if (updateSpoolWeight(spoolId, weight))
|
if (updateSpoolWeight(spoolId, weight))
|
||||||
{
|
{
|
||||||
|
|||||||
+54
-36
@@ -32,7 +32,7 @@ String spoolId = "";
|
|||||||
String nfcJsonData = "";
|
String nfcJsonData = "";
|
||||||
volatile bool pauseBambuMqttTask = false;
|
volatile bool pauseBambuMqttTask = false;
|
||||||
|
|
||||||
volatile uint8_t hasReadRfidTag = 0;
|
volatile nfcReaderStateType nfcReaderState = NFC_IDLE;
|
||||||
// 0 = nicht gelesen
|
// 0 = nicht gelesen
|
||||||
// 1 = erfolgreich gelesen
|
// 1 = erfolgreich gelesen
|
||||||
// 2 = fehler beim Lesen
|
// 2 = fehler beim Lesen
|
||||||
@@ -185,10 +185,10 @@ void processTag(uint8_t *uid, uint8_t uidLength, uint8_t readerNumber) {
|
|||||||
void processNfcData(uint8_t *data, String tagId) {
|
void processNfcData(uint8_t *data, String tagId) {
|
||||||
// Process the data and send it via WebSocket
|
// Process the data and send it via WebSocket
|
||||||
if (decodeNdefAndReturnJson(data)) {
|
if (decodeNdefAndReturnJson(data)) {
|
||||||
hasReadRfidTag = 1;
|
nfcReaderState = NFC_READ_SUCCESS;
|
||||||
sendNfcData(nullptr);
|
sendNfcData(nullptr);
|
||||||
} else {
|
} else {
|
||||||
hasReadRfidTag = 2;
|
nfcReaderState = NFC_READ_ERROR;
|
||||||
oledShowMessage("NFC-Tag unknown");
|
oledShowMessage("NFC-Tag unknown");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
@@ -407,7 +407,7 @@ void writeJsonToTag(void *parameter) {
|
|||||||
Serial.println("Erstelle NDEF-Message...");
|
Serial.println("Erstelle NDEF-Message...");
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
|
|
||||||
hasReadRfidTag = 3;
|
nfcReaderState = NFC_WRITING;
|
||||||
vTaskSuspend(RfidReaderTask);
|
vTaskSuspend(RfidReaderTask);
|
||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
@@ -462,7 +462,7 @@ void writeJsonToTag(void *parameter) {
|
|||||||
Serial.println("NDEF-Message erfolgreich auf den Tag geschrieben");
|
Serial.println("NDEF-Message erfolgreich auf den Tag geschrieben");
|
||||||
oledShowIcon("success");
|
oledShowIcon("success");
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
hasReadRfidTag = 5;
|
nfcReaderState = NFC_WRITE_SUCCESS;
|
||||||
sendNfcData(nullptr);
|
sendNfcData(nullptr);
|
||||||
pauseBambuMqttTask = false;
|
pauseBambuMqttTask = false;
|
||||||
|
|
||||||
@@ -481,13 +481,13 @@ void writeJsonToTag(void *parameter) {
|
|||||||
Serial.println("Fehler beim Schreiben der NDEF-Message auf den Tag");
|
Serial.println("Fehler beim Schreiben der NDEF-Message auf den Tag");
|
||||||
oledShowIcon("failed");
|
oledShowIcon("failed");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
hasReadRfidTag = 4;
|
nfcReaderState = NFC_WRITE_ERROR;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Fehler: Kein Tag zu schreiben gefunden.");
|
Serial.println("Fehler: Kein Tag zu schreiben gefunden.");
|
||||||
oledShowMessage("No NFC-Tag found");
|
oledShowMessage("No NFC-Tag found");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
hasReadRfidTag = 0;
|
nfcReaderState = NFC_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWriteResult(nullptr, success);
|
sendWriteResult(nullptr, success);
|
||||||
@@ -503,7 +503,7 @@ void startWriteJsonToTag(const char* payload) {
|
|||||||
char* payloadCopy = strdup(payload);
|
char* payloadCopy = strdup(payload);
|
||||||
|
|
||||||
// Task nicht mehrfach starten
|
// Task nicht mehrfach starten
|
||||||
if (hasReadRfidTag != 3) {
|
if (nfcReaderState != NFC_WRITING) {
|
||||||
// Erstelle die Task
|
// Erstelle die Task
|
||||||
xTaskCreate(
|
xTaskCreate(
|
||||||
writeJsonToTag, // Task-Funktion
|
writeJsonToTag, // Task-Funktion
|
||||||
@@ -518,51 +518,61 @@ void startWriteJsonToTag(const char* payload) {
|
|||||||
|
|
||||||
void scanRfidTask(void * parameter) {
|
void scanRfidTask(void * parameter) {
|
||||||
Serial.println("RFID Task gestartet");
|
Serial.println("RFID Task gestartet");
|
||||||
for(;;) {
|
if (nfcReaderState != NFC_WRITING)
|
||||||
if (hasReadRfidTag != 3) {
|
{
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
uint8_t success = 0;
|
uint8_t success = 0;
|
||||||
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
|
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
|
||||||
uint8_t uidLength;
|
uint8_t uidLength;
|
||||||
Adafruit_PN532* activeReader = nullptr;
|
Adafruit_PN532 *activeReader = nullptr;
|
||||||
|
|
||||||
// Try first reader with increased timeout
|
// Try first reader with increased timeout
|
||||||
success = nfc1.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
|
success = nfc1.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
|
||||||
if (success) {
|
if (success)
|
||||||
|
{
|
||||||
activeReader = &nfc1;
|
activeReader = &nfc1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
delay(50); // Small delay between readers
|
delay(50); // Small delay between readers
|
||||||
// Try second reader with increased timeout
|
// Try second reader with increased timeout
|
||||||
success = nfc2.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
|
success = nfc2.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
|
||||||
if (success) {
|
if (success)
|
||||||
|
{
|
||||||
activeReader = &nfc2;
|
activeReader = &nfc2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foundNfcTag(nullptr, success);
|
foundNfcTag(nullptr, success);
|
||||||
|
|
||||||
if (success && hasReadRfidTag != 1 && activeReader != nullptr) {
|
if (success && nfcReaderState != NFC_READ_SUCCESS && activeReader != nullptr)
|
||||||
|
{
|
||||||
Serial.println("Found an ISO14443A card");
|
Serial.println("Found an ISO14443A card");
|
||||||
|
|
||||||
hasReadRfidTag = 6;
|
nfcReaderState = NFC_READING;
|
||||||
oledShowIcon("transfer");
|
oledShowIcon("transfer");
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
if (uidLength == 7) {
|
if (uidLength == 7)
|
||||||
|
{
|
||||||
uint16_t tagSize = readTagSize(*activeReader);
|
uint16_t tagSize = readTagSize(*activeReader);
|
||||||
if(tagSize > 0) {
|
if (tagSize > 0)
|
||||||
uint8_t* data = (uint8_t*)malloc(tagSize);
|
{
|
||||||
|
uint8_t *data = (uint8_t *)malloc(tagSize);
|
||||||
memset(data, 0, tagSize);
|
memset(data, 0, tagSize);
|
||||||
|
|
||||||
Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");
|
Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");
|
||||||
|
|
||||||
uint8_t numPages = readTagSize(*activeReader)/4;
|
uint8_t numPages = readTagSize(*activeReader) / 4;
|
||||||
for (uint8_t i = 4; i < 4+numPages; i++) {
|
for (uint8_t i = 4; i < 4 + numPages; i++)
|
||||||
if (!activeReader->ntag2xx_ReadPage(i, data+(i-4) * 4)) {
|
{
|
||||||
|
if (!activeReader->ntag2xx_ReadPage(i, data + (i - 4) * 4))
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (data[(i - 4) * 4] == 0xFE) {
|
if (data[(i - 4) * 4] == 0xFE)
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -571,29 +581,38 @@ void scanRfidTask(void * parameter) {
|
|||||||
vTaskDelay(pdMS_TO_TICKS(5)); // Increased delay between page reads
|
vTaskDelay(pdMS_TO_TICKS(5)); // Increased delay between page reads
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!decodeNdefAndReturnJson(data)) {
|
if (!decodeNdefAndReturnJson(data))
|
||||||
|
{
|
||||||
oledShowMessage("NFC-Tag unknown");
|
oledShowMessage("NFC-Tag unknown");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
hasReadRfidTag = 2;
|
nfcReaderState = NFC_READ_ERROR;
|
||||||
} else {
|
}
|
||||||
hasReadRfidTag = 1;
|
else
|
||||||
|
{
|
||||||
|
nfcReaderState = NFC_READ_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(data);
|
free(data);
|
||||||
} else {
|
|
||||||
oledShowMessage("NFC-Tag read error");
|
|
||||||
hasReadRfidTag = 2;
|
|
||||||
}
|
}
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
|
oledShowMessage("NFC-Tag read error");
|
||||||
|
nfcReaderState = NFC_READ_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("This doesn't seem to be an NTAG2xx tag (UUID length != 7 bytes)!");
|
Serial.println("This doesn't seem to be an NTAG2xx tag (UUID length != 7 bytes)!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!success && hasReadRfidTag > 0) {
|
if (!success && nfcReaderState != NFC_IDLE)
|
||||||
hasReadRfidTag = 0;
|
{
|
||||||
|
nfcReaderState = NFC_IDLE;
|
||||||
nfcJsonData = "";
|
nfcJsonData = "";
|
||||||
Serial.println("Tag entfernt");
|
Serial.println("Tag entfernt");
|
||||||
if (!autoSendToBambu) oledShowWeight(weight);
|
if (!autoSendToBambu)
|
||||||
|
oledShowWeight(weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendNfcData(nullptr);
|
sendNfcData(nullptr);
|
||||||
@@ -601,7 +620,6 @@ void scanRfidTask(void * parameter) {
|
|||||||
}
|
}
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void startNfc() {
|
void startNfc() {
|
||||||
initNfc();
|
initNfc();
|
||||||
|
|||||||
@@ -2,6 +2,16 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Adafruit_PN532.h>
|
#include <Adafruit_PN532.h>
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
NFC_IDLE,
|
||||||
|
NFC_READING,
|
||||||
|
NFC_READ_SUCCESS,
|
||||||
|
NFC_READ_ERROR,
|
||||||
|
NFC_WRITING,
|
||||||
|
NFC_WRITE_SUCCESS,
|
||||||
|
NFC_WRITE_ERROR
|
||||||
|
} nfcReaderStateType;
|
||||||
|
|
||||||
void startNfc();
|
void startNfc();
|
||||||
void startWriteJsonToTag(const char* payload);
|
void startWriteJsonToTag(const char* payload);
|
||||||
@@ -14,7 +24,7 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage);
|
|||||||
extern TaskHandle_t RfidReaderTask;
|
extern TaskHandle_t RfidReaderTask;
|
||||||
extern String nfcJsonData;
|
extern String nfcJsonData;
|
||||||
extern String spoolId;
|
extern String spoolId;
|
||||||
extern volatile uint8_t hasReadRfidTag;
|
extern volatile nfcReaderStateType nfcReaderState;
|
||||||
extern volatile bool pauseBambuMqttTask;
|
extern volatile bool pauseBambuMqttTask;
|
||||||
|
|
||||||
// Function declarations
|
// Function declarations
|
||||||
|
|||||||
+5
-1
@@ -38,11 +38,15 @@ void scale_loop(void * parameter) {
|
|||||||
for(;;) {
|
for(;;) {
|
||||||
if (scale.is_ready())
|
if (scale.is_ready())
|
||||||
{
|
{
|
||||||
// Waage nochmal Taren, wenn zu lange Abweichung
|
// Waage manuell Taren
|
||||||
if (scaleTareRequest == true)
|
if (scaleTareRequest == true)
|
||||||
{
|
{
|
||||||
Serial.println("Re-Tare scale");
|
Serial.println("Re-Tare scale");
|
||||||
|
oledShowMessage("TARE Scale");
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
scale.tare();
|
scale.tare();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
|
oledShowWeight(0);
|
||||||
scaleTareRequest = false;
|
scaleTareRequest = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-24
@@ -22,8 +22,7 @@ AsyncWebServer server(webserverPort);
|
|||||||
AsyncWebSocket ws("/ws");
|
AsyncWebSocket ws("/ws");
|
||||||
|
|
||||||
uint8_t lastSuccess = 0;
|
uint8_t lastSuccess = 0;
|
||||||
uint8_t lastHasReadRfidTag = 0;
|
nfcReaderStateType lastnfcReaderState = NFC_IDLE;
|
||||||
|
|
||||||
|
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||||
if (type == WS_EVT_CONNECT) {
|
if (type == WS_EVT_CONNECT) {
|
||||||
@@ -43,6 +42,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
String message = String((char*)data);
|
String message = String((char*)data);
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
deserializeJson(doc, message);
|
deserializeJson(doc, message);
|
||||||
|
bool spoolmanConnected = (spoolmanApiState != API_INIT);
|
||||||
|
|
||||||
if (doc["type"] == "heartbeat") {
|
if (doc["type"] == "heartbeat") {
|
||||||
// Sende Heartbeat-Antwort
|
// Sende Heartbeat-Antwort
|
||||||
@@ -50,7 +50,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
"\"type\":\"heartbeat\","
|
"\"type\":\"heartbeat\","
|
||||||
"\"freeHeap\":" + String(ESP.getFreeHeap()/1024) + ","
|
"\"freeHeap\":" + String(ESP.getFreeHeap()/1024) + ","
|
||||||
"\"bambu_connected\":" + String(bambu_connected) + ","
|
"\"bambu_connected\":" + String(bambu_connected) + ","
|
||||||
"\"spoolman_connected\":" + String(spoolman_connected) + ""
|
"\"spoolman_connected\":" + String(spoolmanConnected) + ""
|
||||||
"}");
|
"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,34 +139,33 @@ void foundNfcTag(AsyncWebSocketClient *client, uint8_t success) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sendNfcData(AsyncWebSocketClient *client) {
|
void sendNfcData(AsyncWebSocketClient *client) {
|
||||||
if (lastHasReadRfidTag == hasReadRfidTag) return;
|
if (lastnfcReaderState == nfcReaderState)
|
||||||
if (hasReadRfidTag == 0) {
|
return;
|
||||||
|
// TBD: Why is there no status for reading the tag?
|
||||||
|
switch (nfcReaderState)
|
||||||
|
{
|
||||||
|
case NFC_IDLE:
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{}}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{}}");
|
||||||
}
|
break;
|
||||||
else if (hasReadRfidTag == 1) {
|
case NFC_READ_SUCCESS:
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":" + nfcJsonData + "}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":" + nfcJsonData + "}");
|
||||||
}
|
break;
|
||||||
else if (hasReadRfidTag == 2)
|
case NFC_READ_ERROR:
|
||||||
{
|
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Empty Tag or Data not readable\"}}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Empty Tag or Data not readable\"}}");
|
||||||
}
|
break;
|
||||||
else if (hasReadRfidTag == 3)
|
case NFC_WRITING:
|
||||||
{
|
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Schreibe Tag...\"}}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Schreibe Tag...\"}}");
|
||||||
}
|
break;
|
||||||
else if (hasReadRfidTag == 4)
|
case NFC_WRITE_SUCCESS:
|
||||||
{
|
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Error writing to Tag\"}}");
|
|
||||||
}
|
|
||||||
else if (hasReadRfidTag == 5)
|
|
||||||
{
|
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Tag erfolgreich geschrieben\"}}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Tag erfolgreich geschrieben\"}}");
|
||||||
}
|
break;
|
||||||
else
|
case NFC_WRITE_ERROR:
|
||||||
{
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Error writing to Tag\"}}");
|
||||||
|
break;
|
||||||
|
case DEFAULT:
|
||||||
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Something went wrong\"}}");
|
ws.textAll("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Something went wrong\"}}");
|
||||||
}
|
}
|
||||||
lastHasReadRfidTag = hasReadRfidTag;
|
lastnfcReaderState = nfcReaderState;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendAmsData(AsyncWebSocketClient *client) {
|
void sendAmsData(AsyncWebSocketClient *client) {
|
||||||
|
|||||||
Reference in New Issue
Block a user