Esp32 wireless project using joystick

 

Esp32 wireless project using joystick 





Complete Project Code: (TX.ino)
 
#include <esp_now.h>
#include <WiFi.h>

typedef struct struct_message {
  int xValue;
  int yValue;
} struct_message;

struct_message dataToSend;

uint8_t receiverAddress[] = {0xC0, 0x5D, 0x89, 0xB1, 0x1B, 0x4C}; // Replace with your receiver MAC

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW init failed!");
    return;
  }

  esp_now_peer_info_t peerInfo = {};
  memcpy(peerInfo.peer_addr, receiverAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

void loop() {
  dataToSend.xValue = analogRead(34); // Joystick X pin
  dataToSend.yValue = analogRead(35); // Joystick Y pin

  esp_now_send(receiverAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));
  Serial.print("Sent X: ");
  Serial.print(dataToSend.xValue);
  Serial.print(" | Y: ");
  Serial.println(dataToSend.yValue);
  delay(100);
}


Complete Project Code: (Mac Address.ino)
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  Serial.print("MAC Address: ");
  Serial.println(WiFi.macAddress());
}

void loop() {
  // Nothing to do here
}

Complete Project Code: (RX.ino)

#include <esp_now.h>
#include <WiFi.h>
#include <ESP32Servo.h>

Servo servo1;
Servo servo2;

int servo1Pin = 18;  // Adjust as needed
int servo2Pin = 19;

typedef struct struct_message {
  int xValue;
  int yValue;
} struct_message;

struct_message incomingData;

void onDataReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
  memcpy(&incomingData, data, sizeof(incomingData));

  Serial.print("X: ");
  Serial.print(incomingData.xValue);
  Serial.print(" | Y: ");
  Serial.println(incomingData.yValue);

  // Map joystick to servo angle
  int angleX = map(incomingData.xValue, 0, 4095, 0, 180);
  int angleY = map(incomingData.yValue, 0, 4095, 0, 180);

  servo1.write(angleX);
  servo2.write(angleY);
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);

  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW init failed!");
    return;
  }

  esp_now_register_recv_cb(onDataReceive);
}

void loop() {
  // Nothing to do here
}







Comments