diff --git a/PrawnBot-ESP.ino b/PrawnBot-ESP.ino index 66787de..83791d1 100644 --- a/PrawnBot-ESP.ino +++ b/PrawnBot-ESP.ino @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,16 +10,18 @@ const int dispenseTimeout = 2000; const int debounce = 100; boolean motorJammed = false; char clientName[] = "PrawnBot-ESP"; -boolean somethingToSay = true; -String messageToReport = "Online"; +boolean somethingToSay = false; +String messageToReport; long modeStartTime = 0; -int lastAnnounced; -int botMode = 2; // 0: Listening; 1: Dispensing; 2: Resetting; 3: Jam - +unsigned int lastAnnounced = 5; +int botMode = 0; // 0: Listening; 1: Dispensing; 2: Jam void callback(char* topic, byte* incoming, unsigned int length); +Bounce motorSwitchDebounced = Bounce(); +Bounce feedButtonDebounced = Bounce(); + WiFiClient wifiClient; -PubSubClient client(server, 1883, callback, wifiClient); +PubSubClient mqttclient(server, 1883, callback, wifiClient); void callback(char* theTopic, byte* incoming, unsigned int length) { String incomingMessage = String((char *)incoming); @@ -27,17 +30,20 @@ void callback(char* theTopic, byte* incoming, unsigned int length) { Serial.print(": "); Serial.println(incomingMessage); if (incomingMessage == "feed") botMode = 1; - else if (incomingMessage == "ping") client.publish(topic, (char*)("pong")); + else if (incomingMessage == "ping") mqttclient.publish(topic, (char*)("pong")); } void setup() { - pinMode(feedButtonPin, INPUT_PULLUP); pinMode(motorPin, OUTPUT); + pinMode(feedButtonPin, INPUT_PULLUP); pinMode(motorSwitchPin, INPUT_PULLUP); + motorSwitchDebounced.attach(motorSwitchPin); + feedButtonDebounced.attach(feedButtonPin); + motorSwitchDebounced.interval(50); + feedButtonDebounced.interval(50); Serial.begin(115200); delay(10); WiFi.begin(ssid, password); - getOnline(); } void getOnline(){ @@ -49,15 +55,17 @@ void getOnline(){ Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); - if (client.connect(clientName)) { - client.publish(topic, (char*)("online")); - client.subscribe(topic); + if (mqttclient.connect(clientName)) { + mqttclient.publish(topic, (char*)("WiFi GO")); + mqttclient.subscribe(topic); } } void loop() { if(WiFi.status() != WL_CONNECTED) getOnline(); - client.loop(); + feedButtonDebounced.update(); + motorSwitchDebounced.update(); + mqttclient.loop(); // if (motorJammed == true) botMode = 3; switch (botMode) { case 0: // Listening @@ -65,9 +73,7 @@ void loop() { Serial.println("Listening"); lastAnnounced = 0; } - if (digitalRead(feedButtonPin) == HIGH) modeStartTime = 0; - else if (digitalRead(feedButtonPin) == LOW && modeStartTime == 0) modeStartTime = millis(); - else if (digitalRead(feedButtonPin) == LOW && (millis() - modeStartTime > debounce)) { + if (feedButtonDebounced.fell()){ Serial.println("Button pressed"); botMode = 1; modeStartTime = 0; @@ -75,56 +81,34 @@ void loop() { break; case 1: // Dispensing + modeStartTime=millis(); if(lastAnnounced != 1) { Serial.println("Dispensing"); lastAnnounced = 1; } - if (modeStartTime == 0) modeStartTime = millis(); - if ((digitalRead(motorSwitchPin) == LOW) && (millis() - modeStartTime > debounce)) { - digitalWrite(motorPin, HIGH); - } else if ((digitalRead(motorSwitchPin) == HIGH) && (millis() - modeStartTime > debounce)) { - digitalWrite(motorPin, LOW); - botMode = 2; - modeStartTime = 0; - if (messageToReport != "fed") somethingToSay = true; - messageToReport="fed"; - } - if ((modeStartTime > 0) && (millis() - modeStartTime > dispenseTimeout)) { - botMode = 3; - modeStartTime = 0; - } - break; - - case 2: // Resetting - if(lastAnnounced != 2) { - Serial.println("Resetting Motor"); - lastAnnounced = 2; - } - if (modeStartTime == 0) modeStartTime = millis(); - if ((digitalRead(motorSwitchPin) == HIGH)) { // && (millis() - modeStartTime > debounce)) { - digitalWrite(motorPin, HIGH); - } - else if ((digitalRead(motorSwitchPin) == LOW) && (millis() - modeStartTime > debounce)) { + digitalWrite(motorPin, HIGH); + if (feedButtonDebounced.fell()){ digitalWrite(motorPin, LOW); botMode = 0; modeStartTime = 0; - if (messageToReport != "ready") somethingToSay = true; - messageToReport="ready"; + somethingToSay = true; + messageToReport="fed"; } - if ((modeStartTime > 0) && (millis() - modeStartTime > dispenseTimeout)) { + else if (millis() - modeStartTime > dispenseTimeout) { botMode = 3; modeStartTime = 0; } break; - case 3: // Jammed - if(lastAnnounced != 3) { - Serial.println("Resetting Motor"); - lastAnnounced = 3; + case 2: // Jammed + if(lastAnnounced != 2) { + Serial.println("Jammed"); + lastAnnounced = 2; } if (messageToReport != "jam") somethingToSay = true; motorJammed = true; messageToReport="jam"; + break; } if (somethingToSay) { @@ -132,7 +116,7 @@ void loop() { int messageLength = messageToReport.length() + 1; char messageChar[messageLength]; messageToReport.toCharArray(messageChar, messageLength); - client.publish(topic, messageChar); + mqttclient.publish(topic, messageChar); somethingToSay = false; } }