-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-talkback-buzzer.ino
165 lines (132 loc) · 3.7 KB
/
esp32-talkback-buzzer.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
/**
* ESP32 for listening to ThingSpeak TalkBack queue.
*
* Include config.h file with your WLAN credentials and the TalkBack URL
*
* Toni Alho 2022
*/
// Confiq file for keeping WLAN credentials and Talkback URL out from the code and public repositories
#include "config.h"
// Libraries
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
// WLAN login credentials
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
// Talkback URL
String talkbackURL = TALKBACK_URL;
String checker;
bool completion = false;
int buzzerPin = 13;
void setup()
{
// Start serial
Serial.begin(115200);
delay(10);
// Connect to the WLAN
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
// Set the ESP32 on-board led pin to output mode
pinMode(2, OUTPUT);
// Buzzer not working atm
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
Serial.println("Starting loop");
Serial.println("Checking for previous completion cooldown");
// Cooldown from previous completion
// The delay should be set according to the time TalkBack value "true" is reseted back to "false"
if(completion == true) {
Serial.println("On cooldown");
delay (150000);
completion = false;
}
HTTPClient http;
http.begin(talkbackURL);
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
Serial.println();
// JSON tricks
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(payload) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
Serial.println();
// Look for the value of JSON file's second key, which should be "true" or "false"
JSONVar keys = myObject.keys();
Serial.print("JSON object at [1] = ");
Serial.println(myObject[keys[1]]);
checker = myObject[keys[1]];
// The magic happens here
if(checker == "true") {
Serial.println("CONGRATSZZZZZZZZZ!!!!");
// For debuggind purposes, just blink the built-in led
blink();
completion = true;
}
if(checker == "false") {
Serial.println("NO REWARD!!!!");
Serial.println();
}
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// End the connection
http.end();
// Delay for the loop, affects on how quick the response to the change of Talkback value is
delay(2000);
}
void blink() {
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
// Buzzer not working atm
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
}