PrawnBot-ESP/PrawnBot-ESP.ino

124 lines
3.4 KiB
Arduino
Raw Permalink Normal View History

2015-08-31 22:47:14 +10:00
#include <Bounce2.h>
2015-08-30 23:31:38 +10:00
#include <SPI.h>
#include <ESP8266WiFi.h>
2015-08-30 23:31:38 +10:00
#include <PubSubClient.h>
#include "secrets.h"
const int feedButtonPin = 0;
const int motorPin = 14;
2015-08-30 23:31:38 +10:00
const int motorSwitchPin = 12;
const int dispenseTimeout = 2000;
2015-08-30 23:31:38 +10:00
const int debounce = 100;
boolean motorJammed = false;
2015-08-30 23:31:38 +10:00
char clientName[] = "PrawnBot-ESP";
2015-08-31 22:47:14 +10:00
boolean somethingToSay = false;
String messageToReport;
2015-08-30 23:31:38 +10:00
long modeStartTime = 0;
2015-08-31 22:47:14 +10:00
unsigned int lastAnnounced = 5;
int botMode = 0; // 0: Listening; 1: Dispensing; 2: Jam
2015-08-30 23:31:38 +10:00
void callback(char* topic, byte* incoming, unsigned int length);
2015-08-31 22:47:14 +10:00
Bounce motorSwitchDebounced = Bounce();
Bounce feedButtonDebounced = Bounce();
WiFiClient wifiClient;
2015-08-31 22:47:14 +10:00
PubSubClient mqttclient(server, 1883, callback, wifiClient);
2015-08-30 23:31:38 +10:00
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 = 1;
2015-08-31 22:47:14 +10:00
else if (incomingMessage == "ping") mqttclient.publish(topic, (char*)("pong"));
}
void setup() {
2015-08-30 23:31:38 +10:00
pinMode(motorPin, OUTPUT);
2015-08-31 22:47:14 +10:00
pinMode(feedButtonPin, INPUT_PULLUP);
2015-08-30 23:31:38 +10:00
pinMode(motorSwitchPin, INPUT_PULLUP);
2015-08-31 22:47:14 +10:00
motorSwitchDebounced.attach(motorSwitchPin);
feedButtonDebounced.attach(feedButtonPin);
motorSwitchDebounced.interval(50);
feedButtonDebounced.interval(50);
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
2015-08-31 22:06:38 +10:00
}
void getOnline(){
while (WiFi.status() != WL_CONNECTED) {
delay(500);
2015-08-30 23:31:38 +10:00
Serial.print(":) ");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
2015-08-31 22:47:14 +10:00
if (mqttclient.connect(clientName)) {
mqttclient.publish(topic, (char*)("WiFi GO"));
mqttclient.subscribe(topic);
}
}
void loop() {
2015-08-31 22:06:38 +10:00
if(WiFi.status() != WL_CONNECTED) getOnline();
if (!mqttclient.connected()) mqttclient.connect(clientName);
2015-08-31 22:47:14 +10:00
feedButtonDebounced.update();
motorSwitchDebounced.update();
mqttclient.loop(); //
2015-08-30 23:31:38 +10:00
if (motorJammed == true) botMode = 3;
switch (botMode) {
case 0: // Listening
if(lastAnnounced != 0) {
Serial.println("Listening");
lastAnnounced = 0;
}
2015-08-31 22:47:14 +10:00
if (feedButtonDebounced.fell()){
Serial.println("Button pressed");
2015-08-30 23:31:38 +10:00
botMode = 1;
modeStartTime = 0;
}
break;
case 1: // Dispensing
2015-08-31 22:47:14 +10:00
modeStartTime=millis();
2015-08-30 23:31:38 +10:00
if(lastAnnounced != 1) {
Serial.println("Dispensing");
lastAnnounced = 1;
}
2015-08-31 22:47:14 +10:00
digitalWrite(motorPin, HIGH);
if (motorSwitchDebounced.fell()){
2015-08-30 23:31:38 +10:00
digitalWrite(motorPin, LOW);
2015-08-31 22:47:14 +10:00
botMode = 0;
2015-08-30 23:31:38 +10:00
modeStartTime = 0;
2015-08-31 22:47:14 +10:00
somethingToSay = true;
2015-08-30 23:31:38 +10:00
messageToReport="fed";
}
2015-08-31 22:47:14 +10:00
else if (millis() - modeStartTime > dispenseTimeout) {
2015-08-30 23:31:38 +10:00
botMode = 3;
modeStartTime = 0;
}
break;
2015-08-31 22:47:14 +10:00
case 2: // Jammed
2015-08-30 23:31:38 +10:00
if(lastAnnounced != 2) {
2015-08-31 22:47:14 +10:00
Serial.println("Jammed");
2015-08-30 23:31:38 +10:00
lastAnnounced = 2;
}
if (messageToReport != "jam") somethingToSay = true;
motorJammed = true;
messageToReport="jam";
2015-08-31 22:47:14 +10:00
break;
2015-08-30 23:31:38 +10:00
}
if (somethingToSay) {
Serial.println("Send MQTT");
int messageLength = messageToReport.length() + 1;
char messageChar[messageLength];
messageToReport.toCharArray(messageChar, messageLength);
2015-08-31 22:47:14 +10:00
mqttclient.publish(topic, messageChar);
2015-08-30 23:31:38 +10:00
somethingToSay = false;
}
}