-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcatFeeder.ino
178 lines (158 loc) · 5.03 KB
/
catFeeder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Look for all "REPLACEME" before uploading the code.
#include <Stepper.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoOTA.h>
// wifi
ESP8266WiFiMulti wifiMulti;
WiFiClientSecure client;
// stepper
const int stepsPerDose = 100;
Stepper myStepper(stepsPerDose, D1, D2, D3, D4);
int enA = D5;
int enB = D6;
int motorPower = 990;
// ultrasonic
long t;
int trigger = D8;
int echo = D7;
float distance;
float percentageFood;
float max_food = 27.00;
// telegram
#define BOTtoken "REPLACEME"
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000;
long Bot_lasttime;
bool Start = false;
void setup() {
// Serial setup
Serial.begin(115200);
// Wifi connection setup
wifiMulti.addAP("REPLACEME", "REPLACEME");
wifiMulti.addAP("REPLACEME", "REPLACEME");
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(1000);
Serial.print('.');
}
Serial.print(WiFi.localIP());
// pins setup
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
// stepper speed
myStepper.setSpeed(55);
// OTA setup
ArduinoOTA.setHostname("catFeeder");
ArduinoOTA.begin();
}
// calc remaining food in %
void calcRemainingFood() {
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
t = (pulseIn(echo, HIGH) / 2);
if (t == 0.00) {
Serial.println("Failed to read from SR02");
delay(1000);
return;
}
distance = float(t * 0.0343);
Serial.println(distance);
Serial.println(t);
percentageFood = (100 - ((100 / max_food) * distance));
if (percentageFood < 0.00) {
percentageFood = 0.00;
}
Serial.print("Remaining food:\t");
Serial.print(percentageFood);
Serial.println(" %");
delay(500);
}
// feeds cats
void feedCats() {
analogWrite(enA, motorPower);
analogWrite(enB, motorPower);
myStepper.step(stepsPerDose);
analogWrite(enA, 0);
analogWrite(enB, 0);
delay(2000);
}
// clean feeder
void cleanFeeder() {
analogWrite(enA, motorPower);
analogWrite(enB, motorPower);
myStepper.step(400);
analogWrite(enA, 0);
analogWrite(enB, 0);
delay(1000);
}
// telegram message handler
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if ( chat_id != "REPLACEME") {
bot.sendMessage(chat_id, "Hey, you are not allowed to play with my cats!!! Contact @JorgeRance on Twitter in case of any doubts or questions.", "");
}
else if ( chat_id == "REPLACEME") {
if (text == "/feed") {
if (percentageFood == 0.00) {
bot.sendMessage(chat_id, "There's no food! (Ultrasonic measured distance: " + String(distance) + " cm).", "");
}
else {
feedCats();
bot.sendMessage(chat_id, "Cats feeded! Remaining food: " + String(percentageFood) + " %. Ultrasonic measured distance: " + String(distance) + " cm.", "");
}
}
if (text == "/status") {
calcRemainingFood();
char buffer[5];
bot.sendMessage(chat_id, "Remaining food: " + String(percentageFood) + " % (Ultrasonic measured distance: " + String(distance) + " cm).", "");
}
if (text == "/clean") {
feedCats();
char buffer[5];
bot.sendMessage(chat_id, "Feader cleaned. Remaining food: " + String(percentageFood) + " % (Distance to food: " + String(distance) + " cm).", "");
}
if (text == "/ip") {
String catFeederIP = WiFi.localIP().toString();
bot.sendMessage(chat_id, "catFeeder local IP address: " + (catFeederIP), "");
}
if (text == "/help" || text == "/start") {
//String welcome = "Welcome to the most awesome ESP8266 catFeeder, " + from_name + "!\n";
String welcome = "Welcome to the most awesome ESP8266 catFeeder!\n";
welcome += "/clean : Cleans the feeder regardless of whether or not there is food.\n";
welcome += "/feed : Delivers one dose of feed.\n";
welcome += "/help : Outputs this help message.\n";
welcome += "/ip : Prints catFeeder local IP.\n";
welcome += "/status : Returns remaining feed quantity.\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
}
void loop() {
ArduinoOTA.handle();
calcRemainingFood();
Serial.println(WiFi.localIP());
if (millis() > Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Bot_lasttime = millis();
}
delay(1000);
}