-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_controller_access_final.ino
More file actions
109 lines (92 loc) · 2.24 KB
/
wifi_controller_access_final.ino
File metadata and controls
109 lines (92 loc) · 2.24 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
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define rx_pin 4
#define tx_pin 5
#define LEDPIN 2
unsigned long time_out = 1000;
SoftwareSerial controller(rx_pin, tx_pin);
uint8_t ps_data[6];
const char *ssid = "controller";
const char *pass = "csdrobocon";
unsigned int localPort = 2000; // local port to listen for UDP packets
IPAddress ServerIP(192, 168, 4, 1);
IPAddress ClientIP(192, 168, 4, 2);
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
char packetBuffer[9];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
controller.begin(9600);
// Serial.println();
WiFi.softAP(ssid, pass); //Create Access point
//
// Start UDP
Serial.println("PROGRAM: starting UDP");
if (udp.begin(localPort) == 1)
{
Serial.println("PROGRAM: UDP started");
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
else
{
Serial.println("PROGRAM: UDP not started");
}
}
void loop()
{
send_command(31);
bool k = readAllData();
//print_data();
int cb = udp.parsePacket();
if (!cb)
{
//if there is no data to read
udp.beginPacket(ClientIP, 2000);
//Send UDP requests to port 2000
udp.write(ps_data, 6); //Send six bytes to ESP8266
udp.endPacket();
}
else {
//if any UDP packet is received then display that on serial monitor,not really needed for the controller task
udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte
Serial.print(packetBuffer);
}
delay(100);
}
//======================================================================
void print_data()
{
for(int i = 0; i < 6; i++)
{
Serial.print(ps_data[i]);
Serial.write('\t');
}
Serial.println(" ");
}
void send_command(uint8_t cmd)
{
while (controller.available() > 0)
{
controller.read();
}
controller.write(cmd);
}
bool readAllData(void)
{
unsigned long prev_ms = millis();
while (controller.available() < 6)
{
if((millis() - prev_ms) > time_out)
{
return false;
}
}
for (int i = 0; i < 6; i++)
{
ps_data[i] = controller.read();
}
return true;
}