Skip to content
Snippets Groups Projects
Commit fc53ad0a authored by mhby1g21's avatar mhby1g21
Browse files

a bit more robust http handler

parent 17b159c8
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@
#include <Servo.h>
// WiFi credentials
// For robotic Arm 2, mac address is 30:C6:F7:03:60:80
// the password is pUguZXWtBDzl
const char ssid[] = "SOTON-IoT"; // Your network SSID
......@@ -19,7 +18,6 @@ Servo ring;
Servo pinky;
// Servo pins
const int indexPin = 5;
const int middlePin = 4;
const int ringPin = 3;
......@@ -29,14 +27,12 @@ void setup() {
Serial.begin(9600);
// Initialize servos
index_f.attach(indexPin);
middle.attach(middlePin);
ring.attach(ringPin);
pinky.attach(pinkyPin);
// Set initial positions
index_f.write(90);
middle.write(90);
ring.write(90);
......@@ -64,56 +60,89 @@ void setup() {
void loop() {
WiFiClient client = server.available();
if (client) {
String currentLine = "";
while (client.connected()) {
Serial.println("New client connected");
String request = "";
boolean currentLineIsBlank = true;
unsigned long timeout = millis();
while (client.connected() && millis() - timeout < 1000) {
if (client.available()) {
char c = client.read();
request += c;
// If you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// Send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// Send a simple confirmation page
client.println("<html><body>");
client.println("<h1>Command received</h1>");
client.println("</body></html>");
break;
}
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println();
break;
} else {
currentLine = "";
}
currentLineIsBlank = true;
} else if (c != '\r') {
currentLine += c;
}
// Process commands
// Format: /finger/angle (e.g., /thumb/180)
if (currentLine.startsWith("GET /")) {
processCommand(currentLine);
currentLineIsBlank = false;
}
}
}
// Process the request
if (request.indexOf("GET /") != -1) {
Serial.println("Received request: " + request);
processRequest(request);
}
// Give the web browser time to receive the data
delay(10);
client.stop();
Serial.println("Client disconnected");
}
}
void processCommand(String command) {
Serial.println(command);
// Extract finger and angle from command
int firstSlash = command.indexOf('/');
int secondSlash = command.indexOf('/', firstSlash + 1);
int space = command.indexOf(' ', secondSlash);
void processRequest(String request) {
// Extract the command path
int start = request.indexOf("GET ") + 4;
int end = request.indexOf(" HTTP");
if (firstSlash != -1 && secondSlash != -1 && space != -1) {
String finger = command.substring(firstSlash + 1, secondSlash);
int angle = command.substring(secondSlash + 1, space).toInt();
// Constrain angle between 0 and 180
angle = constrain(angle, 0, 90);
if (start != -1 && end != -1) {
String command = request.substring(start, end);
Serial.println("Command: " + command);
// Move appropriate servo
if (finger == "index") index_f.write(angle);
else if (finger == "middle") middle.write(angle);
else if (finger == "ring") ring.write(angle);
else if (finger == "pinky") pinky.write(angle);
// Parse command and move servos
if (command.startsWith("/index/")) {
int angle = command.substring(7).toInt();
angle = constrain(angle, 0, 90);
index_f.write(angle);
Serial.println("Moving index to " + String(angle));
}
else if (command.startsWith("/middle/")) {
int angle = command.substring(8).toInt();
angle = constrain(angle, 0, 90);
middle.write(angle);
Serial.println("Moving middle to " + String(angle));
}
else if (command.startsWith("/ring/")) {
int angle = command.substring(6).toInt();
angle = constrain(angle, 0, 90);
ring.write(angle);
Serial.println("Moving ring to " + String(angle));
}
else if (command.startsWith("/pinky/")) {
int angle = command.substring(7).toInt();
angle = constrain(angle, 0, 90);
pinky.write(angle);
Serial.println("Moving pinky to " + String(angle));
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment