Laser turret controllable with mouse through websocket

In this blog I will explain how to build a laser turret which you can control with your mouse remotely. A great device to play with your pet.

Stap 1: Benodigdheden

The components used in this laserturret:

  • ESP32 WEMOS d1 mini (link)
  • 3V laser (link)
  • 2x servo motor sg90 (link)
  • Some jumper wires

Stap 2: Software

To control the laserturret with your mouse via a socket connection you need to set up a server and a client. The server will be run by the laserturret itself whereas the client will be a html page which communicates with the turret through a php script.

The code can be found in the download at the bottom of this page

Stap 2.1: Arduino firmware

The laserturret will be controlled by a (small) microcontroller with Wi-Fi. There can be chosen for a different microcontroller, for example a ESP8266 however the ESP32 is a bit faster and has Bluetooth capabilities.

The IDE that I've used to develop this firmware is PlatformIO, you can also use other IDE's.

To allow the turret to be accessible from the internet through Wi-Fi you need to edit the following lines in main.cpp:

const char* ssid     = "SSID";
const char* password = "Password";

The values "SSID" and "Password" should be replaced by your Wi-Fi credentials. Once you've filled these in the turret will automatically connect to your Wi-Fi.

Once you've flashed the firmware to the ESP32, the microcontroller will connect to the Wi-Fi and show your local IP address to the Serial output. The ESP32 will listen to port 80 to incoming socket connections.

When a client connects to the server, the ESP32 will wait until a command has been received. The terminator character of a command is '@'. The format of a command is the following:

xCoords/yCoords/Speed/Laser@

xCoords: The X coordinates of the servo motor (0 to 180)

yCoords: The Y coordinates of the servo motor (0 to 180)

Speed: The speed of the servo motors (0 to 100)

Laser: Laser on = 1 Laser off= 0

@ = terminating character. The server will wait until this character is received to indicate the end of a command message.

Stap 2.2: Socket client

In this example a html/javascript page is used to function as socket client. Through ajax requests the coordinates of the mouse within a black square will be communicated to the socket server. There are multiple ways to communicate the position of your mouse to the server for example a python script on a Raspberry Pi.

Index.php contains the socket client. The image below shows what it looks like

The php script to communicate with the ESP32 is found within turretComm.php

To use the HTML page you're going to need a running webserver. You can run this locally on your Windows machine by downloading for example WAMP or you can place the files on a webserver. If you plan on using a remote server you will have to forward the ports of the ESP32.

On line 32 in index.php you can find the following line:

var turretCommURL 	= "URL_TO_TURRET_COMM";

The line above contains the URL to turretComm.php (located on your webserver) which communicates with the ESP32.

In turretComm.php you can find the following lines which contain the IP and port of the ESP32:

$port = 80;
$ip = 'IP_ADDRESS_TURRET';

To allow the socket client to communicate with the server it is necessary to replace the value of the line above with the IP address of the server. Don't forget to keep the quotes in place

When these steps have been followed you will be able to control your laser turret when you go to index.php on your webserver. The position within the black square will communicate the coordinates to move the laserturret to every 25 ms.

Stap 3: Case

The case in this example is printed in PLA (Black and white), however other materials can be used to print it as well. The STL files can be found in the download at the bottom of this page. To keep the ESP32 and laser in place you can use glue (I've used hotglue). The servo motors can be screwed in place by using the screw-holes. However the servo motors will be held in place by the plastic itself.

Stap 4: Electronics

The wiring of the ESP32 and Servo motors should be as following:

Servo motor X:

  • Red-> VCC (5V)
  • Black-> GND
  • Orange-> GPIO4

Servo motor Y:

  • Red-> VCC (5V)
  • Black-> GND
  • Orange-> GPIO2

Laser:

  • Red-> GPIO27
  • Black-> GND



© Prompt. All rights reserved. Crafted by Kevin Assen