-
Notifications
You must be signed in to change notification settings - Fork 2
/
wifi.ino
161 lines (142 loc) · 4.53 KB
/
wifi.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
void setup_wifi() {
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
}
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to WPA SSID: "));
Serial.println(WIFI_SSID);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
}
// you're connected now, so print out the data
Serial.println(F("You're connected to the network"));
//connect to MQTT server
client.setServer(MQTT_IP, 1883);
client.setCallback(callback);
}
//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
if(last_command_ts + 1000 > millis()) {
Serial.println(F("MQTT debouncing. Dropping message."));
return;
}
last_command_ts = millis();
Serial.print(F("Message arrived ["));
Serial.print(topic);
Serial.print("] ");
char commandArray[10];
for (int i = 0; i < length; i++) {
commandArray[i] = (char)payload[i];
}
commandArray[length] = '\0';
String command = String(commandArray);
Serial.print(F("Command: "));
Serial.println(command);
if(command.startsWith("start_feed")) {
Serial.println(F("Feeding fish"));
feed_fish();
} else if(command.startsWith("toggle_auto")) {
Serial.println(F("Toggling auto"));
auto_enabled = !auto_enabled;
client.loop();
print_status(true);
send_status(true);
client.loop();
} else if(command.startsWith("toggle_audio")) {
Serial.println(F("Toggling audio"));
audio_enabled = !audio_enabled;
client.loop();
print_status(true);
send_status(true);
client.loop();
} else if(command.startsWith("reset_feeds")) {
Serial.println(F("Resetting feeds"));
client.loop();
set_num_feeds(0);
print_status(true);
send_status(true);
client.loop();
}
}
void send_status() {
send_status(false);
}
void send_status(boolean override) {
if((last_status_print + 60000 > millis()) && !override ) {
return;
}
if(client.connected()) {
Serial.println(F("Publishing status"));
send_availability();
delay(100);
send_attributes();
delay(200);
send_state();
delay(100);
send_last_feed();
last_status_print = millis();
Serial.println(F("Finished publishing status"));
}
}
void send_availability() {
char buffer[15];
String availability_payload = F("online");
availability_payload.toCharArray(buffer, availability_payload.length()+1);
client.publish("home/feeder_beta/availability", buffer);
}
void send_attributes() {
char buffer[110];
String attributes_payload = F(" { \"num_feeds\": ");
attributes_payload.concat(num_feeds);
attributes_payload.concat(F(", \"auto_mode\": "));
if(auto_enabled) attributes_payload.concat("\"on\"");
else attributes_payload.concat(F("\"off\""));
attributes_payload.concat(F(", \"sound_mode\": "));
if(audio_enabled) attributes_payload.concat("\"on\"");
else attributes_payload.concat(F("\"off\""));
attributes_payload.concat(F(", \"firmware\": \""));
attributes_payload.concat(VERSION_NUMBER);
attributes_payload.concat(F("\" }"));
attributes_payload.toCharArray(buffer, attributes_payload.length()+1);
client.publish("home/feeder_beta/attrs", buffer);
}
void send_last_feed() {
char buffer[30];
String last_feed_payload = "";
last_feed_payload.concat(last_feed/1000);
last_feed_payload.toCharArray(buffer, last_feed_payload.length()+1);
client.publish("home/feeder_beta/last_feed", buffer);
}
void send_state() {
char buffer[10];
String state_payload = "";
state_payload.concat(num_feeds);
state_payload.toCharArray(buffer, state_payload.length()+1);
client.publish("home/feeder_beta/state", buffer);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print(F("Attempting MQTT connection..."));
// Attempt to connect, just a name to identify the client
if (client.connect("NANO", MQTT_USER, MQTT_PASS)) {
Serial.println(F("connected"));
// Once connected, publish an announcement...
client.publish("home/feeder_beta/messages","Feeder Connected");
// ... and resubscribe
client.subscribe("home/feeder_beta/control", 0);
} else {
Serial.print(F("failed, rc="));
Serial.print(client.state());
Serial.println(F(" try again in 5 seconds"));
// Wait 5 seconds before retrying
delay(5000);
}
}
}