-
Notifications
You must be signed in to change notification settings - Fork 0
/
heybox.ino
181 lines (152 loc) · 4.3 KB
/
heybox.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
/*****************************************
* Include Libraries
****************************************/
#include <ESP8266WiFi.h>
#include <ConfigManager.h>
#include <PubSubClient.h>
/****************************************
* Define Constants
****************************************/
namespace{
const char * AP_NAME = "Heybox Access Point"; // Assigns your Access Point name
const char * MQTT_SERVER = "io.adafruit.com";
const char * TOKEN = "b20aea6d2bdc41beba537705083e094b"; // Assigns your adafruit TOKEN
// const char * USERNAME = "gumakerhub";
// const char * FEEDNAME = "makerhubevents.backdoorbell";
const char * USERNAME = "donundeen";
const char * FEEDNAME_TO = "heybox-todon";
const char * FEEDNAME_FROM = "heybox-toliz";
// int LED = LED_BUILTIN;
// int BUTTONPIN = 13;
int LED = 16;
int BUTTONPIN = 2;
}
char topic_to[150];
char topic_from[150];
char payload[50];
String clientMac = "";
unsigned char mac[6];
struct Config {
char name[20];
bool enabled;
int8 hour;
} config;
/****************************************
* Initialize a global instance
****************************************/
WiFiClient espClient;
PubSubClient client(espClient);
ConfigManager configManager;
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length){
String input = String((char *)payload).substring(0, length);
/*
Serial.println("got message");
Serial.println(topic);
Serial.println(length);
Serial.println(input);
*/
if(input == "ON"){
digitalWrite(LED, LOW);
}else{
digitalWrite(LED, HIGH);
}
}
void reconnect() {
while (!client.connected()) {
// configManager.loop();
Serial.print("Attempting MQTT connection...");
// Attempt to connect
Serial.println(clientMac.c_str());
// if (client.connect(clientMac.c_str(), TOKEN, NULL)) {
if (client.connect(clientMac.c_str(), USERNAME, TOKEN)) {
Serial.println("connected");
client.subscribe(topic_from);
break;
} else {
// configManager.reset();
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 3 seconds");
for(uint8_t Blink=0; Blink<=3; Blink++){
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH);
delay(500);
}
}
}
}
String macToStr(const uint8_t* mac) {
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)result += ':';
}
Serial.println(result);
return result;
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
/* Declare PINs as input/outpu */
pinMode(BUTTONPIN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
/* Assign WiFi MAC address as MQTT client name */
WiFi.macAddress(mac);
clientMac += macToStr(mac);
/* Access Point configuration */
configManager.setAPName(AP_NAME);
configManager.addParameter("name", config.name, 20);
configManager.addParameter("enabled", &config.enabled);
configManager.addParameter("hour", &config.hour);
configManager.begin(config);
/* Set Sets the server details */
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);
/* Build the topic request */
sprintf(topic_to, "%s/f/%s", USERNAME, FEEDNAME_TO);
sprintf(topic_from, "%s/f/%s", USERNAME, FEEDNAME_FROM);
}
int prevVal = 0;
void loop() {
configManager.loop();
/* MQTT client reconnection */
if(WiFi.status() != WL_CONNECTED){
Serial.println("not on wifi");
Serial.println(WL_CONNECTED);
Serial.println(WiFi.status());
delay(500);
return;
}else{
}
if (!client.connected()) {
Serial.println("not connected, recon");
reconnect();
}
int button = digitalRead(BUTTONPIN);
if(button == 0){
button = 1;
}else{
button = 0;
}
Serial.println(button);
if(button != prevVal){
// new value;
Serial.println("sending");
Serial.println(button);
if(button == 0){
client.publish(topic_to, "OFF"); // turn it off for now...
}else{
client.publish(topic_to, "ON"); // turn it off for now...
}
}
prevVal = button;
client.loop();
delay(200);
}