-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot-t5-esp32.c
274 lines (254 loc) · 9.81 KB
/
robot-t5-esp32.c
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
267
268
269
270
271
272
273
274
/*
▪ * Team Id: 34
▪ * Author List: Abdul Khaliq Almel,Shashank K Holla,Amrathesh
▪ * Filename: robot-t5-esp32.c
▪ * Theme: Rapid Rescuer(RR)
▪ * Functions: onRecieve,wifi_init_softap,event_handler
▪ * Global Variables: rx,s_event_group,somethingToSend
*/
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
// UART Stuff
#include "driver/uart.h"
#include "driver/gpio.h"
#define ECHO_TEST_TXD (GPIO_NUM_32) // Connected to AVR Rx-0
#define ECHO_TEST_RXD (GPIO_NUM_33) // Connected to AVR Tx-0
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define BUF_SIZE (1024)
#define PORT 3333 // Port address for socket communication
#define RX_BUFFER_SIZE 128
static const char *TAG = "ESP32";
static EventGroupHandle_t s_wifi_event_group;
bool somethingToSend = false;
int onReceive(char *rx, int sock);
char rx[100];
/* Wi-Fi event handler */
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id)
{
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
default:
break;
}
return ESP_OK;
}
/* Function to initialize Wi-Fi at station */
void wifi_init_softap() //my_wifi_config my_wifi
{
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = "Team34",
.password = "teamleader34",
.ssid_len = 0,
.channel = 6,
.authmode = WIFI_AUTH_WPA_WPA2_PSK, //WIFI_AUTH_WPA_WPA2_PSK, //WIFI_AUTH_OPEN
.ssid_hidden = 0,
.max_connection = 4,
.beacon_interval = 100},
};
// printf(">>>>>>>> SSID: %s <<<<<<<<<\n", wifi_config.ap.ssid);
// printf(">>>>>>>> PASS: %s <<<<<<<<<\n", wifi_config.ap.password);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_softap();
char rx_buffer[RX_BUFFER_SIZE]; // buffer to store data from client
char ipv4_addr_str[128]; // buffer to store IPv4 addresses as string
char ipv4_addr_str_client[128]; // buffer to store IPv4 addresses as string
int addr_family;
int ip_protocol;
//char *some_addr;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
uart_param_config(UART_NUM_1, &uart_config);
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
char *sendBuf = (char *)malloc(RX_BUFFER_SIZE);
uint8_t *data_uart = (uint8_t *)malloc(BUF_SIZE);
while (1)
{
struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(PORT);
addr_family = AF_INET;
ip_protocol = IPPROTO_IP;
inet_ntop(AF_INET, &dest_addr.sin_addr, ipv4_addr_str, INET_ADDRSTRLEN);
int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
if (listen_sock < 0)
{
printf("[ERROR] Unable to create socket. ERROR %d\n", listen_sock);
break;
}
int flag = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err != 0)
{
printf("[ERROR] Socket unable to bind. ERROR %d\n", err);
break;
}
err = listen(listen_sock, 1);
if (err != 0)
{
printf("[ERROR] Error occurred during listen. ERROR %d\n", err);
break;
}
printf("[DEBUG] Socket listening\n");
struct sockaddr_in6 source_addr; // Can store both IPv4 or IPv6
uint addr_len = sizeof(source_addr);
int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
if (sock < 0)
{
printf("[ERROR] Error occurred during listen. ERROR %d\n", sock);
break;
}
printf("[DEBUG] Socket accepted\n");
int complete = 0;
while (1)
{
int len_uart = uart_read_bytes(UART_NUM_1, data_uart, BUF_SIZE, 20 / portTICK_RATE_MS);
if ((len_uart > 0 && (strstr((char *)data_uart, "@started@") != NULL)))
{
printf("[DEBUG] Got Start \n");
strcpy(sendBuf, " @started@");
int err = send(sock, sendBuf, strlen(sendBuf), 0);
int i = 0;
if (err != -1)
{
printf("[DEBUG] Sent @started@ successfully\n");
while (1)
{
printf("[DEBUG] Going to recv\n");
int len = recv(sock, rx, sizeof(rx) - 1, 0);
if (len < 0)
{
printf("[error] recieving from client failed \n");
}
else
{
printf("[DEBUG] recvd inside now %d %s\n", len, rx);
//printf("Path recieved : %s", rx);
uart_write_bytes(UART_NUM_1, (const char *)rx, strlen(rx));
while (1)
{
int len_uart = uart_read_bytes(UART_NUM_1, data_uart, BUF_SIZE, 20 / portTICK_RATE_MS);
data_uart[len_uart] = NULL;
if (len_uart > 0)
{
printf("[debug] recived from atmega %s \n", (char *)data_uart);
strcpy(sendBuf, (char *)data_uart);
int err = send(sock, sendBuf, strlen(sendBuf), 0);
if (err < 0)
{
printf("[error] sending to client failed \n");
}
else
{
if (strstr(sendBuf, " Task accomplished"))
{
complete = 1;
}
break;
}
}
else
{
printf("[error]could read from atmega \n");
}
}
if (complete == 1)
{
break;
}
}
}
}
else
{
printf("[error] could not send @Started@ to client \n");
}
}
else
{
printf("[error] the message was not start \n");
}
}
if (sock != -1)
{
printf("[DEBUG] Shutting down socket and restarting...\n");
shutdown(sock, 0);
close(sock);
shutdown(listen_sock, 0);
close(listen_sock);
vTaskDelay(5); // Required for FreeRTOS on ESP32
}
}
return;
}
/*
if(i == 0){
//printf("[DEBUG] i == 0 \n");
sprintf(sendBuf, "@$|2|(6,9)@");
//sprintf(sendBuf, "@start@");
int err = send(sock,sendBuf, strlen(sendBuf), 0);
i++;
break;
}
if(i==1){
sprintf(sendBuf, "@$|4|(8,6)@");
int err = send(sock,sendBuf, strlen(sendBuf), 0);
i++;
break;
}
if(i==2){
sprintf(sendBuf, "@HA reached, Task accomplished!@");
int err = send(sock,sendBuf, strlen(sendBuf), 0);
i++;
break;
}
*/