From ac0aa5289ef10955fd0abf5c90e4b4ef326df6a2 Mon Sep 17 00:00:00 2001 From: GhassanYusuf Date: Sun, 23 Mar 2025 23:41:54 +0300 Subject: [PATCH] added remote code --- espnow-remote/espnow-remote.ino | 44 ++++++++++++++++ espnow-remote/functions.ino | 14 +++++ espnow-remote/header.h | 91 +++++++++++++++++++++++++++++++++ espnow-remote/joystic.h | 31 +++++++++++ 4 files changed, 180 insertions(+) create mode 100644 espnow-remote/espnow-remote.ino create mode 100644 espnow-remote/functions.ino create mode 100644 espnow-remote/header.h create mode 100644 espnow-remote/joystic.h diff --git a/espnow-remote/espnow-remote.ino b/espnow-remote/espnow-remote.ino new file mode 100644 index 0000000..9b87d7b --- /dev/null +++ b/espnow-remote/espnow-remote.ino @@ -0,0 +1,44 @@ +//========================================================== +// HEADER +//========================================================== + + // HEADER FILE + #include "header.h" + +//========================================================== +// SETUP +//========================================================== + + // SETUP + void setup() { + + Serial.begin(115200); + JY.setPin(X_AXIS_PIN, Y_AXIS_PIN, SWITCH_PIN); + JY.begin(); + // StartWireless(); // Initialize ESP-NOW + + } + +//========================================================== +// LOOP +//========================================================== + + // LOOP + void loop() { + + JD.X = map(average(JY.readX(), 10), 0, 4095, 0, 255); // Map to 0-255 + JD.Y = map(average(JY.readY(), 10), 0, 4095, 0, 255); // Map to 0-255 + JD.B = JY.readB(); + + Serial.print(JD.X); Serial.print(" "); + Serial.print(JD.Y); Serial.print(" "); + Serial.println(!JD.B); + + // SendData(JD); // Uncomment to send data + delay(20); + + } + +//========================================================== +// END +//========================================================== \ No newline at end of file diff --git a/espnow-remote/functions.ino b/espnow-remote/functions.ino new file mode 100644 index 0000000..a295b87 --- /dev/null +++ b/espnow-remote/functions.ino @@ -0,0 +1,14 @@ +//========================================================== +// JOYSTIC +//========================================================== + + // Helper function + float average(uint16_t pin, unsigned int times) { // Changed to uint16_t + unsigned long readings = 0; + for(int i=0; i + #include + + // THIS REMOTE MAC ADDRESS + // 88:13:BF:04:24:7C + // THE RECEIVER MAC ADDRESS + // 1C:69:20:E9:13:EC + + // REPLACE WITH YOUR RECEIVER MAC ADDRESS + uint8_t receiverMacAddress[] = {0x1C, 0x69, 0x20, 0xE9, 0x13, 0xEC}; // 1C:69:20:E9:13:EC + + // 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); + Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Message sent" : "Message failed"); + } + + // SEND DATA + void SendData(JoysticData &JD) { + esp_err_t result = esp_now_send(receiverMacAddress, (uint8_t *) &JD, sizeof(JD)); + if (result == ESP_OK) { + Serial.println("Sent with success"); + } else { + Serial.println("Error sending the data"); + } + } + + // Starting WiFi ESP NOW + void StartWireless() { + + // WiFi Station Mode + WiFi.mode(WIFI_MODE_STA); + + // Init ESP-NOW + if (esp_now_init() != ESP_OK) { + Serial.println("Error initializing ESP-NOW"); + return; + } else { + Serial.println("Succes: Initialized ESP-NOW"); + } + + esp_now_register_send_cb(OnDataSent); + + // Register peer + esp_now_peer_info_t peerInfo; + memcpy(peerInfo.peer_addr, receiverMacAddress, 6); + peerInfo.channel = 0; + peerInfo.encrypt = false; + + // Add peer + if (esp_now_add_peer(&peerInfo) != ESP_OK) { + Serial.println("Failed to add peer"); + return; + } else { + Serial.println("Succes: Added peer"); + } + + } + +//========================================================== \ No newline at end of file diff --git a/espnow-remote/joystic.h b/espnow-remote/joystic.h new file mode 100644 index 0000000..297ef1b --- /dev/null +++ b/espnow-remote/joystic.h @@ -0,0 +1,31 @@ +class Joystic { + + private: + + uint8_t x_pin, y_pin, b_pin; + + public: + + Joystic() { } + + Joystic(uint8_t x, uint8_t y, uint8_t b) { + setPin(x, y, b); + } + + void setPin(uint8_t x, uint8_t y, uint8_t b) { + x_pin = x; + y_pin = y; + b_pin = b; + } + + void begin() { + pinMode(x_pin, INPUT); + pinMode(y_pin, INPUT); + pinMode(b_pin, INPUT_PULLUP); + } + + unsigned int readX() { return analogRead(x_pin); } + unsigned int readY() { return analogRead(y_pin); } + bool readB() { return !digitalRead(b_pin); } + +};