From 2c2d327de823a13d6325360640ac2110fca39cb0 Mon Sep 17 00:00:00 2001 From: jameswood Date: Wed, 2 Sep 2015 22:31:19 +1000 Subject: [PATCH] re-subscribe and refactor Vastly simpler. Better code. Now resubscribes on reconnect, and wifi and mutt subscriptions are discrete --- PrawnBot-ESP.ino | 172 ++++++++++++++++++++--------------------------- 1 file changed, 74 insertions(+), 98 deletions(-) diff --git a/PrawnBot-ESP.ino b/PrawnBot-ESP.ino index 0c88ed8..a2924a5 100644 --- a/PrawnBot-ESP.ino +++ b/PrawnBot-ESP.ino @@ -1,122 +1,98 @@ #include -#include #include #include #include "secrets.h" -const int feedButtonPin = 0; -const int motorPin = 14; -const int motorSwitchPin = 12; -const int dispenseTimeout = 2000; -const int debounce = 100; -boolean motorJammed = false; -char clientName[] = "PrawnBot-ESP"; -boolean somethingToSay = false; -String messageToReport; -long modeStartTime = 0; -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(); +const unsigned int motorPin = 14; +const unsigned int feedButtonPin = 0; +const unsigned int motorSwitchPin = 12; Bounce feedButtonDebounced = Bounce(); - +Bounce motorSwitchDebounced = Bounce(); +const unsigned int jamTimeout = 2000; +const unsigned int networkTimeout = 5000; +const unsigned int debounce = 50; +const char myName[] = "PrawnBot-ESP"; +long modeStartTime = 0; +long lastWifiConnectAttempt = 0; +long lastMQTTConnectAttempt = 0; +int botMode = 0; // 0: Listening; 1: Dispensing; 2: Jam +//void callback(char* topic, byte* incoming, unsigned int length); WiFiClient wifiClient; -PubSubClient mqttclient(server, 1883, callback, wifiClient); +PubSubClient mqttclient(server, 1883, MQTTReceived, wifiClient); -void callback(char* theTopic, byte* incoming, unsigned int length) { - String incomingMessage = String((char *)incoming); - incomingMessage = incomingMessage.substring(0, length); - Serial.print(String(theTopic)); - Serial.print(": "); - Serial.println(incomingMessage); - if (incomingMessage == "feed" && botMode != 2) botMode = 1; //if not jammed - else if (incomingMessage == "ping") mqttclient.publish(topic, (char*)("pong")); +void MQTTReceived(char* incomingTopic, byte* incomingPayload, unsigned int payloadLength){ + String incomingString = String((char *)incomingPayload); + incomingString = incomingString.substring(0, payloadLength); + Serial.println(String(incomingTopic) + ": " + incomingString); + if(incomingString == "ping") mqttclient.publish(incomingTopic, (char*)("pong")); + if(incomingString == "feed" && botMode != 2) botMode = 1; //if not jammed } -void setup() { +void setup(){ pinMode(motorPin, OUTPUT); pinMode(feedButtonPin, INPUT_PULLUP); pinMode(motorSwitchPin, INPUT_PULLUP); motorSwitchDebounced.attach(motorSwitchPin); feedButtonDebounced.attach(feedButtonPin); - motorSwitchDebounced.interval(50); - feedButtonDebounced.interval(50); + motorSwitchDebounced.interval(debounce); + feedButtonDebounced.interval(debounce); Serial.begin(115200); - delay(10); + delay(10); //wait for serial +} + +void connectWifi(){ WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED){ + delay(250); + Serial.print("-"); + } + Serial.println("WiFi OK. IP: " + WiFi.localIP()); } -void getOnline(){ - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print(":) "); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - if (mqttclient.connect(clientName)) { - mqttclient.publish(topic, (char*)("WiFi GO")); - mqttclient.subscribe(topic); +boolean connectMQTT(){ + if(mqttclient.connect(myName, statusTopic, 2, true, (char*)("offline"))){ + mqttclient.publish(statusTopic, (char*)("online")); + mqttclient.subscribe(controlTopic); } } -void loop() { - if(WiFi.status() != WL_CONNECTED) getOnline(); - if (!mqttclient.connected()) mqttclient.connect(clientName); - feedButtonDebounced.update(); - motorSwitchDebounced.update(); - mqttclient.loop(); - switch (botMode) { - case 0: // Listening - if(lastAnnounced != 0) { - Serial.println("Listening"); - lastAnnounced = 0; - } - if (feedButtonDebounced.fell()){ - Serial.println("Button pressed"); - botMode = 1; - modeStartTime = 0; - } - break; - - case 1: // Dispensing - modeStartTime=millis(); - if(lastAnnounced != 1) { - Serial.println("Dispensing"); - lastAnnounced = 1; - } - digitalWrite(motorPin, HIGH); - if (motorSwitchDebounced.fell()){ - digitalWrite(motorPin, LOW); - botMode = 0; - modeStartTime = 0; - somethingToSay = true; - messageToReport="fed"; - } - else if (millis() - modeStartTime > dispenseTimeout) { - botMode = 2; //jammed - modeStartTime = 0; - } - break; - - case 2: // Jammed - if(lastAnnounced != 2) { - Serial.println("Jammed"); - lastAnnounced = 2; - } - if (messageToReport != "jam") somethingToSay = true; - motorJammed = true; - messageToReport="jam"; - break; - } - - if (somethingToSay) { - Serial.println("Send MQTT"); - int messageLength = messageToReport.length() + 1; - char messageChar[messageLength]; - messageToReport.toCharArray(messageChar, messageLength); - mqttclient.publish(topic, messageChar); - somethingToSay = false; +void loop(){ + if(WiFi.status() != WL_CONNECTED){ + if(millis() - lastWifiConnectAttempt > networkTimeout){ + lastWifiConnectAttempt=millis(); + connectWifi(); + } + } else if(!mqttclient.loop()){ + if(millis() - lastMQTTConnectAttempt > networkTimeout){ + lastMQTTConnectAttempt=millis(); + connectMQTT(); + } + } else { + feedButtonDebounced.update(); + motorSwitchDebounced.update(); + switch (botMode){ + case 0: // Listening + if(feedButtonDebounced.fell()){ + Serial.println("Button pressed"); + botMode = 1; + } + break; + case 1: // Dispensing + modeStartTime=millis(); + digitalWrite(motorPin, HIGH); + if(motorSwitchDebounced.fell()){ + digitalWrite(motorPin, LOW); + botMode = 0; + modeStartTime = 0; + Serial.println("Fed"); + mqttclient.publish(controlTopic, (byte*)("fed"), 3, true); //3 is length + } + else if(millis() - modeStartTime > jamTimeout){ + botMode = 2; //jammed + Serial.println("Jammed"); + mqttclient.publish(statusTopic, (byte*)("jam"), 3, true); //3 is length + } + break; + } } }