-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-botathon.ino
188 lines (158 loc) · 5.17 KB
/
esp32-botathon.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
#include <WiFi.h>
#include "esp_timer.h"
#include "Arduino.h"
#include "soc/soc.h" // disable brownout problems
#include "soc/rtc_cntl_reg.h" // disable brownout problems
#include <WebSocketsClient.h>
WebSocketsClient webSocket;
//const char* ssid = "UNT";
const char* ssid = "2WIRE123";
const char* password = "QGKWMVVJ";
#define ONBOARD_LED 2
#define FLASH_LED 4
int TEAM_NO = 100;
TaskHandle_t Task1;
TaskHandle_t Task2;
void webSocketEvent(WStype_t type, uint8_t * payload, size_t len) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("WEBSOCKET_DISCONNECTED\n");
break;
case WStype_CONNECTED:
{
Serial.printf("WEBSOCKET_CONNECTED: %s\n", payload);
String teamNumber = "TEAM_";
// send message to server when Connected
char s[8];
sprintf(s, "ESP32_TEAM_%d", TEAM_NO);
//const char * teamNumber = "TEAM_".concat(TEAM_NO);
webSocket.sendTXT(s);
break;
}
case WStype_TEXT:
{
//Serial.printf("WEBSOCKET_RECEIVED_S: %s\n", payload);
String command = String((char *)payload);
//Serial.print("COMMAND: ");
//Serial.println(command);
Serial.println(command);
if (command == "FLASH_ON") {
digitalWrite(FLASH_LED, HIGH);
} else if (command == "FLASH_OFF") {
digitalWrite(FLASH_LED, LOW);
}
// send message to server
// webSocket.sendTXT("message here");
break;
}
case WStype_BIN:
//Serial.printf("WEBSOCKET_RECEIVED_B: %u\n", len);
//hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
case WStype_ERROR:
Serial.printf("WEBSOCKET_ERROR\n");
break;
case WStype_FRAGMENT_TEXT_START:
Serial.printf("WEBSOCKET_FRAGMENT_TEXT_START\n");
break;
case WStype_FRAGMENT_BIN_START:
Serial.printf("WEBSOCKET_FRAGMENT_BIN_START\n");
break;
case WStype_FRAGMENT:
Serial.printf("WEBSOCKET_FRAGMENT\n");
break;
case WStype_FRAGMENT_FIN:
Serial.printf("WEBSOCKET_FRAGMENT_FIN\n");
break;
}
}
int total_time = 0;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
pinMode(ONBOARD_LED, OUTPUT);
pinMode(FLASH_LED, OUTPUT);
Serial.begin(115200);
Serial.println("ESP32_INIT");
Serial.setDebugOutput(false);
if(!psramFound()){
Serial.println("ERR_PSRAM_NOT_FOUND");
}
// Wi-Fi connection
/*
WiFi.disconnect(true);
delay(10);
WiFi.mode(WIFI_STA);
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY));
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
esp_wpa2_config_t wifiConfig = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&wifiConfig);
*/
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
digitalWrite(ONBOARD_LED, HIGH);
delay(250);
digitalWrite(ONBOARD_LED, LOW);
}
//Serial.printf("IP_%s\n", WiFi.localIP().toString());
Serial.println(WiFi.localIP());
webSocket.begin("untrobotics.com", 81, "/", "team");
webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000);
// xTaskCreatePinnedToCore(
// camera, /* Task function. */
// "CAM", /* name of task. */
// 10000, /* Stack size of task */
// NULL, /* parameter of the task */
// 1, /* priority of the task */
// &Task1, /* Task handle to keep track of created task */
// 0); /* pin task to core 0 */
xTaskCreatePinnedToCore(
control, /* Task function. */
"CONTROL", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
}
//void camera(void * pvParameters) {
// //Serial.print("Task1 running on core ");
// //Serial.println(xPortGetCoreID());
//
// while(1) {
// //delay(1000);
// total_time = millis();
// capture();
// total_time = millis() - total_time;
//
// //Serial.print(F("Frame time (in miliseconds):"));
// //Serial.println(total_time, DEC);
// }
//}
void control(void * pvParameters) {
//Serial.print("Task2 running on core ");
//Serial.println(xPortGetCoreID());
while (1) {
digitalWrite(ONBOARD_LED, HIGH);
if (WiFi.status() != WL_CONNECTED) {
delay(100);
} else {
delay(2000);
}
digitalWrite(ONBOARD_LED, LOW);
if (WiFi.status() != WL_CONNECTED) {
delay(100);
} else {
delay(2000);
}
}
}
void loop() {
// nowt
webSocket.loop();
}