Maker 101
Published © LGPL

ESP32 Joystick Hand Controller ESP-NOW

I designed an ESP32 joystick hand controller to wirelessly control the ESP32 Mecanum Wheels robot without using an app.

IntermediateProtip2,626
ESP32 Joystick Hand Controller ESP-NOW

Story

Read more

Code

esp32_mac_address.ino

Arduino
#include "WiFi.h"
 
void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_MODE_STA);
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

Sender-ESP32-Button.ino

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

#define buttonPin     32

int buttonState;

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x0C, 0xB8, 0x15, 0xF2, 0xC1, 0x78};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  int buttonValue;
} struct_message;

// Create a struct_message called myData
struct_message senderData;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

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

void loop() {
  // Set values to send
  buttonState = digitalRead(buttonPin);
  senderData.buttonValue = buttonState;

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &senderData, sizeof(senderData));

  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(50);
}

Receiver-ESP32-Motor.ino

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

#define motorPin   32

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
  int buttonValue;
} struct_message;

int motorState;

// Create a struct_message called myData
struct_message readingData;

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&readingData, incomingData, sizeof(readingData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  
  motorState = readingData.buttonValue;
  Serial.print("buttonValue: ");
  Serial.println(readingData.buttonValue);
  Serial.println();
  digitalWrite(motorPin, motorState);
}
 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  pinMode(motorPin, OUTPUT);
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

Credits

Maker 101

Maker 101

47 projects • 175 followers
Maker 101; Beginner and intermediate level Maker projects!

Comments