From 12b1fb9d00168b6d8460f125836ad390b942c7b7 Mon Sep 17 00:00:00 2001 From: zzzz Date: Mon, 20 May 2024 16:47:51 +0200 Subject: [PATCH] Added basic failure recovery --- src/SmartServo.cpp | 2 +- src/TeaNetworking.cpp | 2 ++ src/TeaTimer.cpp | 6 ++-- src/TeaTimer.hpp | 9 ++--- src/main.cpp | 79 +++++++++++++++++++++++++++++++------------ 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/SmartServo.cpp b/src/SmartServo.cpp index 44e3da6..4c27a10 100644 --- a/src/SmartServo.cpp +++ b/src/SmartServo.cpp @@ -23,7 +23,7 @@ void SmartServo::moveServoTo(int pos, int stepSize) void SmartServo::moveServoToZero() { - moveServoTo(0, 10); + moveServoTo(0, 5); } void SmartServo::moveServoTo180() diff --git a/src/TeaNetworking.cpp b/src/TeaNetworking.cpp index ea79631..2466622 100644 --- a/src/TeaNetworking.cpp +++ b/src/TeaNetworking.cpp @@ -10,12 +10,14 @@ void TeaNetworking::init(const char *ssid, const char *password) int attempts = 0; while (WiFi.status() != WL_CONNECTED) { + /* if (attempts++ > 30) { Serial.println(""); Serial.println("Failed to connect to network."); return; } + */ delay(500); Serial.print("."); } diff --git a/src/TeaTimer.cpp b/src/TeaTimer.cpp index e57e1cc..72a0ea6 100644 --- a/src/TeaTimer.cpp +++ b/src/TeaTimer.cpp @@ -1,6 +1,6 @@ #include "TeaTimer.hpp" -TeaTimer::TeaTimer(ContinuousServo *t_servo) +TeaTimer::TeaTimer(SmartServo *t_servo) { m_servo = t_servo; } @@ -10,7 +10,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration) { if (m_isSteeping) return; - m_servo->moveServoForMillisClockwise(m_servoTurningTime); + m_servo->moveServoTo180(); //moveServoForMillisClockwise(m_servoTurningTime); m_endSteepingTime = millis() + t_steepingDuration; m_isSteeping = true; } @@ -18,7 +18,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration) // Move servo up, stop timer immediately. void TeaTimer::end_steeping() { - m_servo->moveServoForMillisCounterClockwise(m_servoTurningTime); + m_servo->moveServoToZero(); m_isSteeping = false; } diff --git a/src/TeaTimer.hpp b/src/TeaTimer.hpp index d6f2307..eb570e6 100644 --- a/src/TeaTimer.hpp +++ b/src/TeaTimer.hpp @@ -1,12 +1,13 @@ #pragma once #include #include "SmartServo.hpp" -#include "ContinuousServo.hpp" +//#include "ContinuousServo.hpp" +#include "SmartServo.hpp" class TeaTimer { public: - TeaTimer(ContinuousServo *t_servo); + TeaTimer(SmartServo *t_servo); // Move servo down, start timer. void beginSteeping(unsigned long t_steepingDuration); // Move servo up, stop timer immediately. @@ -20,6 +21,6 @@ private: bool m_isSteeping = false; // The time in millis when steeping is done. unsigned long m_endSteepingTime; - unsigned long m_servoTurningTime = 1000; - ContinuousServo *m_servo; + unsigned long m_servoTurningTime = 250; + SmartServo *m_servo; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ec6a270..7d12720 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,8 +15,9 @@ #define TOUCH_BTN_PIN 16 // D0 // RFID -#define PN532_IRQ 13 // D7 -#define PN532_RESET 12 // D6 (not connected) +#define PN532_IRQ 13 // D7 (not connected) +#define PN532_RESET 12 // D6 (not connected) +// Or use hardware Serial: Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C // OLED display @@ -24,12 +25,13 @@ Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C auto display = Adafruit_SSD1306(128, 32, &WIRE); // FastLED -#define PIXELCOUNT 20 +#define PIXELCOUNT 15 CRGB leds[PIXELCOUNT]; // Define the array of leds // util classes -ContinuousServo contServo(SERVO_PIN); -TeaTimer teaTimer(&contServo); +// ContinuousServo contServo(SERVO_PIN); +SmartServo smartServo(SERVO_PIN); +TeaTimer teaTimer(&smartServo); SmartRFID smartRFID(&nfc); SmartDial smartDial(POTENTIOMETER_PIN); SmartDisplay smartDisplay(&display); @@ -80,18 +82,22 @@ void setup() FastLED.show(); delay(500); - leds[19].setRGB(0, 0, 2); + leds[14].setRGB(0, 0, 2); FastLED.show(); delay(500); - teaNet.init(ssid, password); - smartRFID.init(); Serial.println("Initializing OLED..."); display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32 smartDisplay.printMsg("Tea is Great!"); + delay(500); + smartDisplay.printMsg("waiting for internet connection"); + + teaNet.init(ssid, password); + + smartDisplay.printMsg("Success!"); delay(500); pinMode(LED_BUILTIN, OUTPUT); @@ -99,11 +105,13 @@ void setup() buttonLeft.init(); buttonRight.init(); + + smartServo.moveServoToZero(); } void loop() { - contServo.tick(); + // contServo.tick(); switch (state) { @@ -131,7 +139,7 @@ void loop() // this blocks execution until a tag has been detected (5s timeout) String uid = smartRFID.readTag(5000); - if (uid != "no_tag_detected") + if (uid != "no_tag_detected" && uid != "") { currentTea.m_rfidCode = uid; state = PROCESS_RFID_SCAN; @@ -145,7 +153,7 @@ void loop() display.display(); delay(500); - display.println("Success!"); + display.println("connecting..."); display.display(); delay(250); @@ -153,11 +161,26 @@ void loop() Serial.print("Result:"); Serial.println(result); + // catch possible failure + if (result == JSON.parse("{}")) + { + smartDisplay.printMsg("GET FAILED"); + delay(1000); + break; + } + if (JSONVar::stringify(result) == "null") { Serial.print("TEA DOES NOT EXIST YET!"); String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, 90, 180); result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); + // catch possible failure + if (result == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } state = State::DISPLAY_ADD_NEW_TEA_CONFIG; } else @@ -172,7 +195,7 @@ void loop() { String uid = smartRFID.readTag(50); // just enough time to detect tag - if (uid != "no_tag_detected") + if (uid != "no_tag_detected" && uid != "") { currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); @@ -204,7 +227,14 @@ void loop() String payload = teaNet.createTeaData(currentTea); Serial.println(payload); smartDisplay.printMsg("sync..."); - teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } teaTimer.beginSteeping(5000); state = State::STEEPING_IN_PROGRESS; } @@ -221,11 +251,8 @@ void loop() else { smartDisplay.printMsg("DONE"); - if (!contServo.isTurning()) - { - delay(1000); - state = State::REQUEST_FEEDBACK; - } + delay(1000); + state = State::REQUEST_FEEDBACK; } break; } @@ -234,7 +261,7 @@ void loop() smartDisplay.printRequestFeedbackScreen(currentTea); String uid = smartRFID.readTag(50); // just enough time to detect tag - if (uid != "no_tag_detected") + if (uid != "no_tag_detected" && uid != "") { currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); @@ -242,7 +269,8 @@ void loop() break; } - if (isBtnPressed()) { + if (isBtnPressed()) + { // keep steeping time for next brew smartDisplay.printMsg("Feedback accepted (perfect)"); delay(1000); @@ -269,7 +297,7 @@ void loop() smartDisplay.printAddNewTeaConfigScreen(currentTea); String uid = smartRFID.readTag(50); // just enough time to detect tag - if (uid != "no_tag_detected") + if (uid != "no_tag_detected" && uid != "") { currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); @@ -291,7 +319,14 @@ void loop() smartDisplay.printMsg("Accepted"); delay(500); smartDisplay.printMsg("sync..."); - teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", teaNet.createTeaData(currentTea)); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", teaNet.createTeaData(currentTea)); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } state = State::DISPLAY_SCANNED_TEA_CONFIG; }