Supplier photo of an assembled radio kit with a purple circuit board, telescopic antenna and clear case.
Supplier photo
Temu affiliate offer · Project idea

Build a radio

Soldering required. Check the supplied parts and board revision.

View kit on Temu

Tricky Circuit may earn a commission through these links. Product ideas have not been bench-tested by Tricky Circuit and are not verified lesson parts lists.

Here’s the WebSockets version for smooth, real-time motor control via Wi-Fi. 🚀


🔹 Why Use WebSockets?

  • Faster than HTTP requests (no need to reload the page).
  • No freezes (Arduino doesn’t get overloaded).
  • Instant response when clicking buttons.

📌 1️⃣ Arduino R4 WiFi Code

This sets up a WebSocket server on port 81 to control:
✅ SG90 servo motor
✅ 28BYJ-48 stepper motor

#include <WiFiS3.h>
#include <WebSocketsServer.h>
#include <Servo.h>
#include <Stepper.h>

// Wi-Fi Settings
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASS";

// Create WebSocket server
WiFiServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);

// Motors
Servo myServo;
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

// WebSocket event handler
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
    String message = (char*)payload;
    Serial.println("Command: " + message);

    if (message == "servo_up") myServo.write(180);
    if (message == "servo_down") myServo.write(0);
    if (message == "stepper_left") myStepper.step(-50);
    if (message == "stepper_right") myStepper.step(50);
}

void setup() {
    Serial.begin(115200);
    myServo.attach(6);
    myStepper.setSpeed(10);

    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi!");
    Serial.println(WiFi.localIP());

    // Start WebSocket server
    server.begin();
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
}

void loop() {
    webSocket.loop();  // Keep WebSocket connection active
}

🔹 2️⃣ HTML + JavaScript for Web Control

This creates a simple web page with buttons to control the motors via WebSockets.

<!DOCTYPE html>
<html>
<head>
    <script>
        var ws = new WebSocket("ws://YOUR_IP_ADDRESS:81/");

        function sendCommand(command) {
            ws.send(command);
        }
    </script>
</head>
<body>
    <h2>WiFi Motor Control</h2>
    <button onclick="sendCommand('servo_up')">Servo Up</button>
    <button onclick="sendCommand('servo_down')">Servo Down</button><br><br>
    <button onclick="sendCommand('stepper_left')">Stepper Left</button>
    <button onclick="sendCommand('stepper_right')">Stepper Right</button>
</body>
</html>

🔹 Replace YOUR_IP_ADDRESS with the IP address that Arduino prints in Serial Monitor after connecting to Wi-Fi.


🔹 Why WebSockets?

✅ Instant motor control – No delays.
✅ No freezes – Arduino doesn’t get overloaded with HTTP requests.
✅ Smooth operation – No need to reload the page.


📡Broadcast the signal — amplify the connection.

Leave a Reply

Temu affiliate offers

Find your next electronics project

Explore these do-it-yourself project kits. Check the selected option, included parts and instructions on each offer.

Tricky Circuit may earn a commission if you buy through these Temu affiliate links. These are product ideas; they have not been bench-tested by Tricky Circuit and are not verified lesson parts lists.

Supplier photo of an assembled infinity mirror light cube illuminated on a dark background.

Supplier product photo

Build an infinity light cube

Listing specifies 5-volt direct current. Soldering required; battery and glue not included.

View kit on Temu

Supplier photo of a breadboard, jumper wires and assorted electronic components in a plastic storage case.

Supplier product photo

Try breadboard experiments

Breadboard, wires and assorted parts. Arduino controller board not included.

View kit on Temu

Supplier photo of an assembled blue surface-mount soldering practice board with a circular ring of small lights.

Supplier product photo

Solder a chasing-light board

Surface-mount soldering practice. Battery and battery holder not included.

View kit on Temu