From 38f94011b3691f5313335f4590637606b73cef0f Mon Sep 17 00:00:00 2001 From: Muhammad Hazimi Bin Yusri <mhby1g21@soton.ac.uk> Date: Wed, 19 Feb 2025 15:46:12 +0000 Subject: [PATCH] added some ui to new file --- wifi-test/wifi-with-ui.ino | 220 +++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 wifi-test/wifi-with-ui.ino diff --git a/wifi-test/wifi-with-ui.ino b/wifi-test/wifi-with-ui.ino new file mode 100644 index 0000000..cd4dd88 --- /dev/null +++ b/wifi-test/wifi-with-ui.ino @@ -0,0 +1,220 @@ +#include <WiFiNINA.h> +#include <Servo.h> + +// WiFi credentials +const char ssid[] = "SOTON-IoT"; +const char pass[] = "pUguZXWtBDzl"; +int status = WL_IDLE_STATUS; + +// Server settings +WiFiServer server(80); + +// Create servo objects +Servo index_f; +Servo middle; +Servo ring; +Servo pinky; + +// Servo pins +const int indexPin = 5; +const int middlePin = 4; +const int ringPin = 3; +const int pinkyPin = 2; + +// Current positions +int indexPos = 90; +int middlePos = 90; +int ringPos = 90; +int pinkyPos = 90; + +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(indexPos); + middle.write(middlePos); + ring.write(ringPos); + pinky.write(pinkyPos); + + // Check for WiFi module + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + while (true); + } + + // Attempt to connect to WiFi network + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network: "); + Serial.println(ssid); + status = WiFi.begin(ssid, pass); + delay(10000); + } + + Serial.println("Connected to wifi"); + printWiFiStatus(); + + server.begin(); +} + +void loop() { + WiFiClient client = server.available(); + + if (client) { + Serial.println("New client connected"); + String request = ""; + boolean currentLineIsBlank = true; + + while (client.connected()) { + if (client.available()) { + char c = client.read(); + request += c; + + if (c == '\n' && currentLineIsBlank) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println("Connection: close"); + client.println(); + + // Send HTML page + client.println("<!DOCTYPE html>"); + client.println("<html>"); + client.println("<head>"); + client.println("<title>Robotic Hand Control</title>"); + client.println("<meta name='viewport' content='width=device-width, initial-scale=1'>"); + client.println("<style>"); + client.println("body { font-family: Arial; margin: 20px; }"); + client.println(".control-group { margin-bottom: 20px; background: #f5f5f5; padding: 15px; border-radius: 8px; }"); + client.println(".slider { width: 200px; margin: 10px 0; }"); + client.println(".button { background: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; margin: 5px; }"); + client.println(".button:hover { background: #45a049; }"); + client.println(".preset { background: #2196F3; }"); + client.println(".reset { background: #f44336; }"); + client.println("</style>"); + client.println("</head>"); + client.println("<body>"); + + client.println("<h1>Robotic Hand Control Panel</h1>"); + + // Individual finger controls + String fingers[] = {"Index", "Middle", "Ring", "Pinky"}; + int positions[] = {indexPos, middlePos, ringPos, pinkyPos}; + String endpoints[] = {"index", "middle", "ring", "pinky"}; + + for(int i = 0; i < 4; i++) { + client.println("<div class='control-group'>"); + client.println("<h3>" + fingers[i] + " Finger</h3>"); + client.println("<input type='range' class='slider' min='0' max='180' value='" + String(positions[i]) + "' "); + client.println("oninput='updateFinger(\"" + endpoints[i] + "\", this.value)'>"); + client.println("<span>Position: " + String(positions[i]) + "°</span>"); + client.println("<br>"); + client.println("<button class='button' onclick='updateFinger(\"" + endpoints[i] + "\", 0)'>Close</button>"); + client.println("<button class='button' onclick='updateFinger(\"" + endpoints[i] + "\", 180)'>Open</button>"); + client.println("</div>"); + } + + // Preset controls + client.println("<div class='control-group'>"); + client.println("<h3>Preset Controls</h3>"); + client.println("<button class='button preset' onclick='allFingers(0)'>Close All</button>"); + client.println("<button class='button preset' onclick='allFingers(180)'>Open All</button>"); + client.println("<button class='button reset' onclick='resetPositions()'>Reset to 90°</button>"); + client.println("</div>"); + + // JavaScript + client.println("<script>"); + client.println("function updateFinger(finger, angle) {"); + client.println(" fetch('/' + finger + '/' + angle);"); + client.println("}"); + + client.println("function allFingers(angle) {"); + client.println(" ['index', 'middle', 'ring', 'pinky'].forEach(finger => {"); + client.println(" updateFinger(finger, angle);"); + client.println(" });"); + client.println("}"); + + client.println("function resetPositions() {"); + client.println(" allFingers(90);"); + client.println("}"); + client.println("</script>"); + + client.println("</body></html>"); + break; + } + if (c == '\n') { + currentLineIsBlank = true; + } else if (c != '\r') { + currentLineIsBlank = false; + } + } + } + + // Process the request + if (request.indexOf("GET /") != -1) { + processRequest(request); + } + + delay(10); + client.stop(); + Serial.println("Client disconnected"); + } +} + +void processRequest(String request) { + int start = request.indexOf("GET ") + 4; + int end = request.indexOf(" HTTP"); + + if (start != -1 && end != -1) { + String command = request.substring(start, end); + Serial.println("Command: " + command); + + // Parse command and move servos + if (command.startsWith("/index/")) { + int angle = command.substring(7).toInt(); + angle = constrain(angle, 0, 180); + index_f.write(angle); + indexPos = angle; + Serial.println("Moving index to " + String(angle)); + } + else if (command.startsWith("/middle/")) { + int angle = command.substring(8).toInt(); + angle = constrain(angle, 0, 180); + middle.write(angle); + middlePos = angle; + Serial.println("Moving middle to " + String(angle)); + } + else if (command.startsWith("/ring/")) { + int angle = command.substring(6).toInt(); + angle = constrain(angle, 0, 180); + ring.write(angle); + ringPos = angle; + Serial.println("Moving ring to " + String(angle)); + } + else if (command.startsWith("/pinky/")) { + int angle = command.substring(7).toInt(); + angle = constrain(angle, 0, 180); + pinky.write(angle); + pinkyPos = angle; + Serial.println("Moving pinky to " + String(angle)); + } + } +} + +void printWiFiStatus() { + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + long rssi = WiFi.RSSI(); + Serial.print("Signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} \ No newline at end of file -- GitLab