-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi.ino
132 lines (107 loc) · 4.02 KB
/
wifi.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
void WiFiTaskFunction(void * pvParameters) {
while (true) { // infinite loop
// wifi recieve code:
int packetSize = Udp.parsePacket();
if (packetSize) {
if (xSemaphoreTake(mutexReceive, 1) == pdTRUE) {
receivedNewData = true;
lastMessageTimeMillis = millis();
Udp.read(packetBuffer, maxWifiRecvBufSize);
for (int i = 0; i < maxWifiRecvBufSize; i++) {
recvdData[i] = (byte)((int)(256 + packetBuffer[i]) % 256);
}
Udp.beginPacket();
for (byte i = 0; i < numBytesToSend; i++) { // send response, maybe change to go less frequently
Udp.write(dataToSend[i]);
}
Udp.endPacket();
xSemaphoreGive(mutexReceive);
}
}
}
}
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_AP_START:
Serial.println("configuring wifi");
break;
default: break;
}
}
void setupWifi() {
WiFi.softAP(robotSSID, robotPass, 4, 0, 1); // start wifi network, on channel 4, not hiding, and only allowing one client
delay(1000);
WiFi.softAPConfig(IPAddress(10, 25, 21, 1), IPAddress(10, 25, 21, 1), IPAddress(255, 255, 255, 0));
Udp.begin(2521); // port 2521 on 10.25.21.1
xTaskCreatePinnedToCore( // create task to run WiFi recieving
WiFiTaskFunction, /* Function to implement the task */
"WiFiTask", /* Name of the task */
15000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
NULL, /* Task handle. */
0 /* Core on which task should run */
);
}
boolean readBoolFromBuffer(byte &pos) { // return boolean at pos position in recvdData
byte msg = recvdData[pos];
pos++; // increment the counter for the next value
return (msg == 1);
}
byte readByteFromBuffer(byte &pos) { // return byte at pos position in recvdData
byte msg = recvdData[pos];
pos++; // increment the counter for the next value
return msg;
}
int readIntFromBuffer(byte &pos) { // return int from four bytes starting at pos position in recvdData
union { // this lets us translate between two variable types (equal size, but one's four bytes in an array, and one's a four byte int) Reference for unions: https:// www.mcgurrin.info/robots/127/
byte b[4];
int v;
} d; // d is the union, d.b acceses the byte array, d.v acceses the int
for (int i = 0; i < 4; i++) {
d.b[i] = recvdData[pos];
pos++;
}
return d.v; // return the int form of union d
}
float readFloatFromBuffer(byte &pos) { // return float from 4 bytes starting at pos position in recvdData
union { // this lets us translate between two variable types (equal size, but one's 4 bytes in an array, and one's a 4 byte float) Reference for unions: https:// www.mcgurrin.info/robots/127/
byte b[4];
float v;
} d; // d is the union, d.b acceses the byte array, d.v acceses the float
for (int i = 0; i < 4; i++) {
d.b[i] = recvdData[pos];
pos++;
}
return d.v;
}
void addBoolToBuffer(boolean msg, byte &pos) { // add a boolean to the tosendData array
dataToSend[pos] = msg;
pos++;
}
void addByteToBuffer(byte msg, byte &pos) { // add a byte to the tosendData array
dataToSend[pos] = msg;
pos++;
}
void addIntToBuffer(int msg, byte &pos) { // add an int to the tosendData array (four bytes)
union {
byte b[4];
int v;
} d; // d is the union, d.b acceses the byte array, d.v acceses the int
d.v = msg; // put the value into the union as an int
for (int i = 0; i < 4; i++) {
dataToSend[pos] = d.b[i];
pos++;
}
}
void addFloatToBuffer(float msg, byte &pos) { // add a float to the tosendData array (four bytes)
union { // this lets us translate between two variables (equal size, but one's 4 bytes in an array, and one's a 4 byte float Reference for unions: https:// www.mcgurrin.info/robots/127/
byte b[4];
float v;
} d; // d is the union, d.b acceses the byte array, d.v acceses the float
d.v = msg;
for (int i = 0; i < 4; i++) {
dataToSend[pos] = d.b[i];
pos++;
}
}