TechGuru
Published

Amazing DIY: Robotic Gripper

Let's make a RC mobile WiFi-based - WebSocket robotic gripper that can be used in many DIY projects.

BeginnerProtip2.5 hours3,977
Amazing DIY: Robotic Gripper

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Gripper
https://www.aliexpress.com/item/1set-Acrylic-Robot-Arm-Clamp-Claw-Mount-Kit-With-9G-Servo-For-Arduino-DIY-Robot-Kit/32783986597.html?ws_ab_test=searchweb0_0,searchweb201602_1_10152_10151_10065_10068_10344_10342_10343_10340_10341_10698_10697_10696_10084_10083_10618_10307_10059_308_100031_10103_441_10624_10623_10622_10621_10620,searchweb201603_25,ppcSwitch_5&algo_expid=c5297009-e816-440f-8bf0-b8c35a3715a0-0&algo_pvid=c5297009-e816-440f-8bf0-b8c35a3715a0&transAbTest=ae803_2&priceBeautifyAB=0
×1
Servos (Tower Pro MG996R)
SG90 Servo
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Setup snapshot

If interested how we assembled the gripper...follow the link below

https://www.aliexpress.com/item/1set-Acrylic-Robot-Arm-Clamp-Claw-Mount-Kit-With-9G-Servo-For-Arduino-DIY-Robot-Kit/32783986597.html?ws_ab_test=searchweb0_0,searchweb201602_1_10152_10151_10065_10068_10344_10342_10343_10340_10341_10698_10697_10696_10084_10083_10618_10307_10059_308_100031_10103_441_10624_10623_10622_10621_10620,searchweb201603_25,ppcSwitch_5&algo_expid=c5297009-e816-440f-8bf0-b8c35a3715a0-0&algo_pvid=c5297009-e816-440f-8bf0-b8c35a3715a0&transAbTest=ae803_2&priceBeautifyAB=0

Code

Wemos_WebServer_AP_StaticIP_WebSocket_RoboticGripper.ino

Arduino
Main code.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <ESP8266mDNS.h>

#define RED_PIN 4  //(D1)
#define GREEN_PIN 14 //(D2) 
#define BLUE_PIN 12  //(D3)

const char WiFiAPPSK[] = "12345678";

ESP8266WebServer server = ESP8266WebServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);

int val;    // variable to read the value from the analog pin

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {

  switch (type) {
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", num);
      break;
    case WStype_CONNECTED: {
        IPAddress ip = webSocket.remoteIP(num);
        Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

        // send message to client
        webSocket.sendTXT(num, "Connected");
      }
      break;
    case WStype_TEXT:
      Serial.printf("[%u] get Text: %s\n", num, payload);

      //val = analogRead((int) payload);            // reads the value of the potentiometer (value between 0 and 1023)
      Serial.println(atoi((const char *)payload));
      
      val = map(atoi((const char *)payload), 40, 255, 30, 75);     // scale it to use it with the servo (value between 0 and 180)
      //Serial.print("Val Sent: ");
      //Serial.println(val);
      myservo.write(val);                  // sets the servo position according to the scaled value
      delay(150);                           // waits for the servo to get there

      break;
  }
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);
  WiFi.softAP("espHotSpot", WiFiAPPSK);
}

void setup()
{
  Serial.begin(115200);
  Serial.print("Setting up... ");

  //Engine channels
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  analogWrite(RED_PIN, 1023);
  analogWrite(GREEN_PIN, 0);
  analogWrite(BLUE_PIN, 0);

  delay(1000);//wait for a second

  setupWiFi();

  // start webSocket server
  webSocket.begin();
  webSocket.onEvent(webSocketEvent);

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  // handle index
  server.on("/", []() {
    // send index.html
    server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () {  connection.send('Connect ' + new Date()); }; connection.onerror = function (error) {    console.log('WebSocket Error ', error);};connection.onmessage = function (e) {  console.log('Server: ', e.data);};function sendRGB() {  var r = parseInt(document.getElementById('r').value);  var rgb = r;    console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>Robotic Gripper:<br/><br/>Gripper Arm Controller: <input id=\"r\" type=\"range\" min=\"30\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/></body></html>");
  });

  server.begin();

  // Add service to MDNS
  MDNS.addService("http", "tcp", 80);
  MDNS.addService("ws", "tcp", 81);

  analogWrite(RED_PIN, 0);
  analogWrite(GREEN_PIN, 1023);
  analogWrite(BLUE_PIN, 0);

  myservo.attach(D7);  // attaches the servo on GIO2 to the servo object
  myservo.write(50);
  delay(100); 
}

void loop()
{
  webSocket.loop();
  server.handleClient();
}

Library

Arduino
Place the extracted folder in Arduino installed library folder.
No preview (download only).

Credits

TechGuru

TechGuru

8 projects • 13 followers
We provide Science,School and Technology projects(Working Real Time Models) for your requirements. YOU HAVE A BUDGET, WE HAVE A PROJECT !

Comments