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()
{
moveServoTo(0, 10);
moveServoTo(0, 5);
}
void SmartServo::moveServoTo180()

View file

@ -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(".");
}

View file

@ -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;
}

View file

@ -1,12 +1,13 @@
#pragma once
#include <Arduino.h>
#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;
};

View file

@ -15,8 +15,9 @@
#define TOUCH_BTN_PIN 16 // D0
// RFID
#define PN532_IRQ 13 // D7
#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,12 +251,9 @@ void loop()
else
{
smartDisplay.printMsg("DONE");
if (!contServo.isTurning())
{
delay(1000);
state = State::REQUEST_FEEDBACK;
}
}
break;
}
case REQUEST_FEEDBACK:
@ -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;
}