forked from jorgerance/catFeeder
-
Notifications
You must be signed in to change notification settings - Fork 19
/
catFeeder.ino
226 lines (197 loc) · 6.73 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <NTPClient.h>
// Look for all "REPLACEME" before uploading the code.
#include <Stepper.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <NTPClient.h>
// wifi
const char* ssid = "REPLACEME"; //type your WIFI information inside the quotes
const char* password = "REPLACEME";
WiFiClient espClient;
// wifi UDP for NTP, we dont have real time and we dont trust http headers :)
WiFiUDP ntpUDP;
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "europe.pool.ntp.org"
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
// OTA
#define SENSORNAME "CatFeeder" //change this to whatever you want to call your device
#define OTApassword "REPLACEME" //the password you will need to enter to upload remotely via the ArduinoIDE yourOTApassword
int OTAport = 8266;
// MQTT
const char* mqtt_server = "REPLACEME"; // IP address or dns of the mqtt
const char* mqtt_username = "REPLACEME"; //
const char* mqtt_password = "REPLACEME";
const int mqtt_port = 1883; //REPLACEME, usually not?
PubSubClient client(espClient);
// MQTT TOPICS (change these topics as you wish)
const char* lastfed_topic = "home/catfeeder/lastfed"; // UTF date
const char* remaining_topic = "home/catfeeder/remaining"; //Remain % fix distance above
const char* feed_topic = "home/catfeeder/feed"; // command topic
const char* willTopic = "home/catfeeder/LWT"; // Last Will and Testiment topic
// stepper
const int steps = 200; //REPLACEME this is the number of steps of the motor for a 360° rotation.
const int stepsPerDose = 50; //REPLACEME as you wish, mine was perfect at about 45-50 steps
Stepper myStepper(steps, D1, D3, D2, D4); // you may want to REPLACEME this based on how you cabled the motor.
int enA = D5;
int enB = D6;
//int motorPower = 990; // legacy.. for using pwm
// ultrasonic
long t;
int trigger = D8;
int echo = D7;
float distance;
float percentageFood;
float max_food = 23.50; // REPLACEME in cm? seems to be "about" right
// Button
const int buttonPin = 3; // number of the pushbutton pin (RX, cause no other IO was available)
void setup() {
// Serial setup
Serial.begin(115200);
// pins setup
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LEDs pin as an output
pinMode(2, OUTPUT); // ^ other led
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
// Wifi connection setup
setup_wifi();
timeClient.begin();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
// Turn OFF builtin leds
digitalWrite(BUILTIN_LED, HIGH);
digitalWrite(2, HIGH);
// stepper speed
myStepper.setSpeed(55);
// OTA setup
ArduinoOTA.setHostname(SENSORNAME);
ArduinoOTA.setPort(OTAport);
ArduinoOTA.setPassword((const char *)OTApassword);
ArduinoOTA.begin();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.setSleepMode(WIFI_NONE_SLEEP); // bit more power hungry, but seems stable.
WiFi.hostname("CatFeeder"); // This will (probably) happear on your router somewhere.
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/********************************** START CALLBACK*****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char message[length];
for (int i = 0; i < length; i++) {
message[i] = ((char)payload[i]);
}
message[length] = '\0';
Serial.print("[");
Serial.print(message);
Serial.print("]");
Serial.println();
if (strcmp(message,"feed") == 0) {
Serial.print("Feeding cats...");
Serial.println();
feedCats();
} else {
Serial.print("Unknown Message");
Serial.println();
}
}
// feeds cats
void feedCats() {
digitalWrite(2, LOW); // Turn on onboard LED
digitalWrite(enA, HIGH); // Enable motors, i dont see the point in pwm with a stepper?
digitalWrite(enB, HIGH);
myStepper.step(stepsPerDose);
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
delay(2000); // you may wanna change this based on how many times you press te button continously
timeClient.update(); // could this fail?
String formattedTime = timeClient.getFormattedDate();
char charBuf[20];
formattedTime.toCharArray(charBuf, 20);
client.publish(lastfed_topic, charBuf, true); // Publishing time of feeding to MQTT Sensor retain true
Serial.print("Fed at: ");
Serial.print(charBuf);
Serial.println();
digitalWrite(2, HIGH); // Turn off onboard LED
calcRemainingFood();
}
// 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(" %");
char charBuf[6];
int ret = snprintf(charBuf, sizeof charBuf, "%f", percentageFood); // Translate float to char before publishing...
client.publish(remaining_topic, charBuf, true); // Publishing remaining food to MQTT Sensor retain true
delay(500);
}
void reconnect() {
// Loop until we're reconnected, i may wanna check for pushbutton here somewhere in case of wifi disaster?
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(SENSORNAME, mqtt_username, mqtt_password, willTopic, 1, true, "Offline")) {
client.publish(willTopic,"Online", true);
Serial.println("connected");
// ... and resubscribe
client.subscribe(feed_topic);;
} else {
Serial.print("MQTT failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
ArduinoOTA.handle();
// Check for buttonpin push
if (digitalRead(buttonPin) == LOW) {
Serial.println("Button pushed, feeding cats...");
feedCats();
}
if (!client.connected()) {
reconnect();
}
client.loop();
delay(100);
}