#include <esp_now.h>
#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0x24,0x0A,0xC4,0x00,0x01,0x10};
// Define the button pin
const int buttonPin = 2;
// // Define the joystick pins
// const int joystickXP = 34;
// const int joystickXN = 35;
// const int joystickYP = 32;
// const int joystickYN = 33;
// Define the button pin
// const int buttonPin = 2;
// Initialize the button state
bool buttonState = false;
// Define the joystick pins
const int joystickYP = 32;
const int joystickYN = 33;
typedef struct button_struct {
bool buttonPressed;
} button_struct;
typedef struct joystick_struct {
int joystickX;
int joystickY;
} joystick_struct;
button_struct buttonData;
joystick_struct joystickData;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void sendButtonData() {
// Read the button state
buttonData.buttonPressed = digitalRead(buttonPin);
// Send the button data to the receiver ESP32 device
esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &buttonData, sizeof(button_struct));
if (result == ESP_OK) {
Serial.println("Button data sent successfully");
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
Serial.println("ESP-NOW not initialized");
} else if (result == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid argument");
} else if (result == ESP_ERR_ESPNOW_INTERNAL) {
Serial.println("Internal error");
} else if (result == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("Out of memory");
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println("Peer not found");
}
}
void sendJoystickData() {
// Read the joystick position
joystickData.joystickY = analogRead(joystickYP) - analogRead(joystickYN);
// Send the joystick data to the receiver ESP32 device
esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &joystickData, sizeof(joystick_struct));
if (result == ESP_OK) {
Serial.println("Joystick data sent successfully");
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
Serial.println("ESP-NOW not initialized");
} else if (result == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid argument");
} else if (result == ESP_ERR_ESPNOW_INTERNAL) {
Serial.println("Internal error");
} else if (result == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("Out of memory");
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println("Peer not found");
}
}
// Define the SSD1306 OLED display object
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// register peer
peerInfo.channel =0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1,6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Set up the button pin
pinMode(buttonPin, INPUT_PULLUP);
// Set up the joystick pins
pinMode(joystickYP, INPUT);
pinMode(joystickYN, INPUT);
// Configure the SSD1306 OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Set up the light pin
pinMode(13, OUTPUT);
}
void loop() {
sendButtonData();
sendJoystickData();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(10, 5);
display.print("Nothing here!!");
display.display();
// Clear the display
display.clearDisplay();
// Print the button state to the display
display.setCursor(0,0);
display.print("Button Pressed: ");
if (buttonData.buttonPressed) {
display.print("Yes");
display.display();
} else {
display.print("No");
display.display();
}
// Print the joystick position to the display
display.setCursor(0,10);
display.print("Joystick Y: ");
display.print(joystickData.joystickY);
// Display the changes
display.display();
delay(2000);
}