-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannelPointBot.ino
More file actions
266 lines (231 loc) · 6.65 KB
/
Copy pathchannelPointBot.ino
File metadata and controls
266 lines (231 loc) · 6.65 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ArduinoWebsockets.h>
#include <ArduinoOTA.h>
#include "config.h"
/* arduino ESP twitch pubsub keyword keyword
*
* This code is intended originally for a fartbot but here we are.
* Twitch pubsub documentation: https://dev.twitch.tv/docs/pubsub
*
* ArduinoJson v6 (https://arduinojson.org/)
* ESP8266Wifi and ArduinoOTA 2.6.3 (URL for board manager)(http://arduino.esp8266.com/stable/package_esp8266com_index.json)
* Arduino Websockets by gilmaimon 0.4.15 (https://github.com/gilmaimon/ArduinoWebsockets)
*
*
* The code sets up all the libraries and then uses a state machine
* to handle the login/relogin procedure.
* The final two states are dictated by a ping timer.
* Most of the stuff in here is untested so if the software kills your cat
* don't blame me.
*
* Also rename the config file and put your stuff innit
*/
//pins
#define LED_PIN D1 //D1 gpio 5
#define PING_TIME 240000 //+- 10s using random later
#define REAPING 12000 //ms for no ping reconnect
#define RECON_TIME 1000 //initial time to reconnect
#define RECON_UPTO 120000 //max reconnect time
#define RESPONSETIME 5000 //time to wait for login response from server
//webSettings
const char* robotName = "PenisBot";
const char* ssid = "GloriousPCMR";
const char* password = "deadbeef";
String nonce = "";
const String websockets_connection_string = "wss://pubsub-edge.twitch.tv"; //Enter server adress
const String echo_org_ssl_fingerprint = "f6 44 ca 49 a6 ff 6c 0e 4c e6 f5 a9 05 dc 7b fb 76 9c 37 0d"; //pubsub edge twitch tv fingerprint (WILDCARD!)
//variables
int stater = 0;
int reconMult = 0; //multiplier for reconnection attemps
unsigned long pptimer = 0; //time for next ping
unsigned long timerpp = 0; //time since sent ping
unsigned long clientLast = 0; //last time we tried
unsigned long loginAt = 0; //time login was sent
//socket stuff
using namespace websockets;
WebsocketsClient client;
//json stuff
const size_t capacity = 2*JSON_OBJECT_SIZE(2) + 1866;
DynamicJsonDocument doc(capacity);
//=============SETUP================
void setup() {
setupWifi();
setupOTA();
setupPins();
setupSerial();
setupSocketStuff();
setupDONE();
}
//============LOOP==============
void loop() {
checkWifi();
ArduinoOTA.handle();
loginMachine();
}
//=========setup Stuff========
void setupOTA(){
ArduinoOTA.setHostname(robotName);
ArduinoOTA.begin();
}
void setupPins(){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//external led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void setupSocketStuff(){
client.onMessage(onMessageCallback);
client.onEvent(onEventsCallback);
client.setFingerprint(echo_org_ssl_fingerprint.c_str());
}
void setupWifi(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if(WiFi.waitForConnectResult() != WL_CONNECTED){
delay(5000);
ESP.restart();
}
}
void setupSerial(){
Serial.begin(115200);
Serial.println("starteding up");
}
void setupDONE(){
for(int i = 5; i > 0; i--){
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(10);
}
}
//==========loop functions========
void checkWifi(){
//esp automagically reconnects so wait if disconnected
if(WiFi.status() != WL_CONNECTED){
delay(5000);
ESP.restart();
}
}
//========= state machine ========
//uses stater to login to twitch api ESP handles wifi
void loginMachine(){
switch(stater){
case 0: //login to twitch pubsub
connectClient();
break;
case 1: //subscribe to the topic
login();
break;
case 2: //wait for response
client.poll();
responseTimer();
break;
case 3: //normal running mode
client.poll();
pponging();
break;
case 4:
client.poll();
waitForPong();
break;
}
}
//============state machine functions=============
void connectClient(){
if(millis() - clientLast > constrain(RECON_TIME*reconMult, 0, RECON_UPTO)){
reconMult++;
client.connect(websockets_connection_string);
clientLast = millis();
}
}
void login(){
//make nonce
nonce = "e4t5v345nz3sm";
//in case you friggen tryhards want random nonces
//nonce = string(RANDOM_REG32);
const String logmein = "{\"type\": \"LISTEN\",\"nonce\": \""+nonce+"\",\"data\": {\"topics\": [\"channel-points-channel-v1."+chanId+"\"],\"auth_token\": \""+OAuth+"\"}}";
client.send(logmein);
loginAt = millis();
stater = 2;
}
void responseTimer(){
if(millis() - loginAt > RESPONSETIME){
//too much time passed, try again
resetEverything();
}
}
//twitch requires a ping every 4 mins +- 10s, but it's its own format
void pponging(){
if((millis() - pptimer) > PING_TIME){
//send ping
client.send("{\"type\": \"PING\"}");
//fail timer
timerpp = millis();
//state up
stater = 4;
}
}
void waitForPong(){
if((millis() - timerpp) > REAPING){
//you fail
resetEverything();
}
}
//============ Callbacks =============
//do we even get this or is it a message?
void onEventsCallback(WebsocketsEvent event, String data) {
if(event == WebsocketsEvent::ConnectionOpened){
Serial.println("Connnection Opened");
reconMult = 0;
stater = 1;
}else if(event == WebsocketsEvent::ConnectionClosed){
Serial.println("Connnection Closed");
resetEverything();
}
}
void onMessageCallback(WebsocketsMessage message) {
//print it
Serial.print("Got Message: ");
Serial.println(message.data());
//parse main message, get message type
deserializeJson(doc, message.data());
const String initialType = doc["type"];
Serial.println(initialType);
if(initialType == "RECONNECT"){resetEverything();}
if(initialType == "PONG"){resetPing();}
if(initialType == "RESPONSE"){responseChecks();}
if(initialType == "MESSAGE"){parseMessage();}
}
void resetEverything(){
client.close();
stater = 0;
}
void resetPing(){
pptimer = millis() + random(-10000,10000);
stater = 3;
}
void responseChecks(){
if(doc["nonce"] != nonce){
Serial.println("NONCE FAIL");
resetEverything();
}
if(doc["error"] != ""){
Serial.println("error message");
resetEverything();
}
Serial.println("RESPONSEDED");
resetPing();
}
void parseMessage(){
blinkLED();
}
//do the stuff done stuff do
void blinkLED(){
if(digitalRead(LED_PIN) == LOW){
digitalWrite(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
}