4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

Added basic failure recovery

This commit is contained in:
zzzz 2024-05-20 16:47:51 +02:00
parent 421d00b3fb
commit 12b1fb9d00
5 changed files with 68 additions and 30 deletions

View file

@ -23,7 +23,7 @@ void SmartServo::moveServoTo(int pos, int stepSize)
void SmartServo::moveServoToZero() void SmartServo::moveServoToZero()
{ {
moveServoTo(0, 10); moveServoTo(0, 5);
} }
void SmartServo::moveServoTo180() void SmartServo::moveServoTo180()

View file

@ -10,12 +10,14 @@ void TeaNetworking::init(const char *ssid, const char *password)
int attempts = 0; int attempts = 0;
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
/*
if (attempts++ > 30) if (attempts++ > 30)
{ {
Serial.println(""); Serial.println("");
Serial.println("Failed to connect to network."); Serial.println("Failed to connect to network.");
return; return;
} }
*/
delay(500); delay(500);
Serial.print("."); Serial.print(".");
} }

View file

@ -1,6 +1,6 @@
#include "TeaTimer.hpp" #include "TeaTimer.hpp"
TeaTimer::TeaTimer(ContinuousServo *t_servo) TeaTimer::TeaTimer(SmartServo *t_servo)
{ {
m_servo = t_servo; m_servo = t_servo;
} }
@ -10,7 +10,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration)
{ {
if (m_isSteeping) if (m_isSteeping)
return; return;
m_servo->moveServoForMillisClockwise(m_servoTurningTime); m_servo->moveServoTo180(); //moveServoForMillisClockwise(m_servoTurningTime);
m_endSteepingTime = millis() + t_steepingDuration; m_endSteepingTime = millis() + t_steepingDuration;
m_isSteeping = true; m_isSteeping = true;
} }
@ -18,7 +18,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration)
// Move servo up, stop timer immediately. // Move servo up, stop timer immediately.
void TeaTimer::end_steeping() void TeaTimer::end_steeping()
{ {
m_servo->moveServoForMillisCounterClockwise(m_servoTurningTime); m_servo->moveServoToZero();
m_isSteeping = false; m_isSteeping = false;
} }

View file

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include "SmartServo.hpp" #include "SmartServo.hpp"
#include "ContinuousServo.hpp" //#include "ContinuousServo.hpp"
#include "SmartServo.hpp"
class TeaTimer class TeaTimer
{ {
public: public:
TeaTimer(ContinuousServo *t_servo); TeaTimer(SmartServo *t_servo);
// Move servo down, start timer. // Move servo down, start timer.
void beginSteeping(unsigned long t_steepingDuration); void beginSteeping(unsigned long t_steepingDuration);
// Move servo up, stop timer immediately. // Move servo up, stop timer immediately.
@ -20,6 +21,6 @@ private:
bool m_isSteeping = false; bool m_isSteeping = false;
// The time in millis when steeping is done. // The time in millis when steeping is done.
unsigned long m_endSteepingTime; unsigned long m_endSteepingTime;
unsigned long m_servoTurningTime = 1000; unsigned long m_servoTurningTime = 250;
ContinuousServo *m_servo; SmartServo *m_servo;
}; };

View file

@ -15,8 +15,9 @@
#define TOUCH_BTN_PIN 16 // D0 #define TOUCH_BTN_PIN 16 // D0
// RFID // RFID
#define PN532_IRQ 13 // D7 #define PN532_IRQ 13 // D7 (not connected)
#define PN532_RESET 12 // D6 (not connected) #define PN532_RESET 12 // D6 (not connected)
// Or use hardware Serial:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C
// OLED display // OLED display
@ -24,12 +25,13 @@ Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C
auto display = Adafruit_SSD1306(128, 32, &WIRE); auto display = Adafruit_SSD1306(128, 32, &WIRE);
// FastLED // FastLED
#define PIXELCOUNT 20 #define PIXELCOUNT 15
CRGB leds[PIXELCOUNT]; // Define the array of leds CRGB leds[PIXELCOUNT]; // Define the array of leds
// util classes // util classes
ContinuousServo contServo(SERVO_PIN); // ContinuousServo contServo(SERVO_PIN);
TeaTimer teaTimer(&contServo); SmartServo smartServo(SERVO_PIN);
TeaTimer teaTimer(&smartServo);
SmartRFID smartRFID(&nfc); SmartRFID smartRFID(&nfc);
SmartDial smartDial(POTENTIOMETER_PIN); SmartDial smartDial(POTENTIOMETER_PIN);
SmartDisplay smartDisplay(&display); SmartDisplay smartDisplay(&display);
@ -80,18 +82,22 @@ void setup()
FastLED.show(); FastLED.show();
delay(500); delay(500);
leds[19].setRGB(0, 0, 2); leds[14].setRGB(0, 0, 2);
FastLED.show(); FastLED.show();
delay(500); delay(500);
teaNet.init(ssid, password);
smartRFID.init(); smartRFID.init();
Serial.println("Initializing OLED..."); Serial.println("Initializing OLED...");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32 display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32
smartDisplay.printMsg("Tea is Great!"); smartDisplay.printMsg("Tea is Great!");
delay(500);
smartDisplay.printMsg("waiting for internet connection");
teaNet.init(ssid, password);
smartDisplay.printMsg("Success!");
delay(500); delay(500);
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
@ -99,11 +105,13 @@ void setup()
buttonLeft.init(); buttonLeft.init();
buttonRight.init(); buttonRight.init();
smartServo.moveServoToZero();
} }
void loop() void loop()
{ {
contServo.tick(); // contServo.tick();
switch (state) switch (state)
{ {
@ -131,7 +139,7 @@ void loop()
// this blocks execution until a tag has been detected (5s timeout) // this blocks execution until a tag has been detected (5s timeout)
String uid = smartRFID.readTag(5000); String uid = smartRFID.readTag(5000);
if (uid != "no_tag_detected") if (uid != "no_tag_detected" && uid != "")
{ {
currentTea.m_rfidCode = uid; currentTea.m_rfidCode = uid;
state = PROCESS_RFID_SCAN; state = PROCESS_RFID_SCAN;
@ -145,7 +153,7 @@ void loop()
display.display(); display.display();
delay(500); delay(500);
display.println("Success!"); display.println("connecting...");
display.display(); display.display();
delay(250); delay(250);
@ -153,11 +161,26 @@ void loop()
Serial.print("Result:"); Serial.print("Result:");
Serial.println(result); Serial.println(result);
// catch possible failure
if (result == JSON.parse("{}"))
{
smartDisplay.printMsg("GET FAILED");
delay(1000);
break;
}
if (JSONVar::stringify(result) == "null") if (JSONVar::stringify(result) == "null")
{ {
Serial.print("TEA DOES NOT EXIST YET!"); Serial.print("TEA DOES NOT EXIST YET!");
String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, 90, 180); String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, 90, 180);
result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); 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; state = State::DISPLAY_ADD_NEW_TEA_CONFIG;
} }
else else
@ -172,7 +195,7 @@ void loop()
{ {
String uid = smartRFID.readTag(50); // just enough time to detect tag 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; currentTea.m_rfidCode = uid;
smartDisplay.resetDisplay(); smartDisplay.resetDisplay();
@ -204,7 +227,14 @@ void loop()
String payload = teaNet.createTeaData(currentTea); String payload = teaNet.createTeaData(currentTea);
Serial.println(payload); Serial.println(payload);
smartDisplay.printMsg("sync..."); 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); teaTimer.beginSteeping(5000);
state = State::STEEPING_IN_PROGRESS; state = State::STEEPING_IN_PROGRESS;
} }
@ -221,12 +251,9 @@ void loop()
else else
{ {
smartDisplay.printMsg("DONE"); smartDisplay.printMsg("DONE");
if (!contServo.isTurning())
{
delay(1000); delay(1000);
state = State::REQUEST_FEEDBACK; state = State::REQUEST_FEEDBACK;
} }
}
break; break;
} }
case REQUEST_FEEDBACK: case REQUEST_FEEDBACK:
@ -234,7 +261,7 @@ void loop()
smartDisplay.printRequestFeedbackScreen(currentTea); smartDisplay.printRequestFeedbackScreen(currentTea);
String uid = smartRFID.readTag(50); // just enough time to detect tag 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; currentTea.m_rfidCode = uid;
smartDisplay.resetDisplay(); smartDisplay.resetDisplay();
@ -242,7 +269,8 @@ void loop()
break; break;
} }
if (isBtnPressed()) { if (isBtnPressed())
{
// keep steeping time for next brew // keep steeping time for next brew
smartDisplay.printMsg("Feedback accepted (perfect)"); smartDisplay.printMsg("Feedback accepted (perfect)");
delay(1000); delay(1000);
@ -269,7 +297,7 @@ void loop()
smartDisplay.printAddNewTeaConfigScreen(currentTea); smartDisplay.printAddNewTeaConfigScreen(currentTea);
String uid = smartRFID.readTag(50); // just enough time to detect tag 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; currentTea.m_rfidCode = uid;
smartDisplay.resetDisplay(); smartDisplay.resetDisplay();
@ -291,7 +319,14 @@ void loop()
smartDisplay.printMsg("Accepted"); smartDisplay.printMsg("Accepted");
delay(500); delay(500);
smartDisplay.printMsg("sync..."); 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; state = State::DISPLAY_SCANNED_TEA_CONFIG;
} }