added remote code
This commit is contained in:
parent
0c0669d069
commit
ac0aa5289e
44
espnow-remote/espnow-remote.ino
Normal file
44
espnow-remote/espnow-remote.ino
Normal file
@ -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
|
||||||
|
//==========================================================
|
14
espnow-remote/functions.ino
Normal file
14
espnow-remote/functions.ino
Normal file
@ -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<times; i++) readings += analogRead(pin);
|
||||||
|
return readings / times;
|
||||||
|
}
|
||||||
|
|
||||||
|
//==========================================================
|
||||||
|
// JOYSTIC
|
||||||
|
//==========================================================
|
91
espnow-remote/header.h
Normal file
91
espnow-remote/header.h
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
//==========================================================
|
||||||
|
// JOYSTIC
|
||||||
|
//==========================================================
|
||||||
|
|
||||||
|
// LIBRARY
|
||||||
|
#include "joystic.h"
|
||||||
|
|
||||||
|
// JOYSTIC PIN DEFINITION
|
||||||
|
#define X_AXIS_PIN 32 // ADC1_CH4 (Valid)
|
||||||
|
#define Y_AXIS_PIN 33 // ADC1_CH5 (Valid)
|
||||||
|
#define SWITCH_PIN 34 // Changed from 25 (now ADC1_CH6)
|
||||||
|
|
||||||
|
// DATA STRUCTURE
|
||||||
|
struct JoysticData {
|
||||||
|
uint8_t X;
|
||||||
|
uint8_t Y;
|
||||||
|
bool B;
|
||||||
|
};
|
||||||
|
|
||||||
|
// JOYSTIC OBJECT
|
||||||
|
Joystic JY;
|
||||||
|
|
||||||
|
// JOYSTIC DATA
|
||||||
|
JoysticData JD;
|
||||||
|
|
||||||
|
//==========================================================
|
||||||
|
// ESPNOW
|
||||||
|
//==========================================================
|
||||||
|
|
||||||
|
// LIBRARY
|
||||||
|
#include <esp_now.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//==========================================================
|
31
espnow-remote/joystic.h
Normal file
31
espnow-remote/joystic.h
Normal file
@ -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); }
|
||||||
|
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user