Update projects/espnow/rc car/receiver/receiver.ino
This commit is contained in:
parent
e2ed6c677a
commit
e411c4465a
@ -1,10 +1,18 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
|
#include <esp_wifi.h>
|
||||||
#include <ESP32Servo.h>
|
#include <ESP32Servo.h>
|
||||||
|
|
||||||
|
//------------------------------------------------
|
||||||
|
// This is The Car Unit
|
||||||
|
//------------------------------------------------
|
||||||
|
// RC Car Remote MAC Address: 64:E8:33:B5:D8:40
|
||||||
|
// RC Car MAC Address: 50:78:7D:43:14:00
|
||||||
|
//------------------------------------------------
|
||||||
|
|
||||||
Servo leftServoA, leftServoB, rightServoA, rightServoB;
|
Servo leftServoA, leftServoB, rightServoA, rightServoB;
|
||||||
int leftPins[2] = {12, 13};
|
int leftPins[2] = {0, 1};
|
||||||
int rightPins[2] = {14, 15};
|
int rightPins[2] = {2, 3};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int speed;
|
int speed;
|
||||||
@ -20,9 +28,12 @@ typedef struct {
|
|||||||
CommandPacket command;
|
CommandPacket command;
|
||||||
TelemetryPacket telemetry;
|
TelemetryPacket telemetry;
|
||||||
|
|
||||||
uint8_t remoteAddr[] = {0x24, 0x6F, 0x28, 0x33, 0xDE, 0xB4}; // MAC of remote
|
unsigned long lastTelemetrySent = 0;
|
||||||
|
|
||||||
void onCommandRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
// RC Car Remote MAC Address: 64:E8:33:B5:D8:40
|
||||||
|
uint8_t remoteAddr[] = {0x64, 0xE8, 0x33, 0xB5, 0xD8, 0x40}; // MAC of remote
|
||||||
|
|
||||||
|
void onCommandRecv(const esp_now_recv_info_t *recv_info, const uint8_t *incomingData, int len) {
|
||||||
memcpy(&command, incomingData, sizeof(command));
|
memcpy(&command, incomingData, sizeof(command));
|
||||||
|
|
||||||
int left = constrain(command.speed + command.turn, -255, 255);
|
int left = constrain(command.speed + command.turn, -255, 255);
|
||||||
@ -40,27 +51,58 @@ void onCommandRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
|||||||
telemetry.rightSpeed = right;
|
telemetry.rightSpeed = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to ensure peer is always registered
|
||||||
|
void ensurePeerConnected() {
|
||||||
|
if (!esp_now_is_peer_exist(remoteAddr)) {
|
||||||
|
esp_now_peer_info_t peerInfo = {};
|
||||||
|
memcpy(peerInfo.peer_addr, remoteAddr, 6);
|
||||||
|
peerInfo.channel = 0;
|
||||||
|
peerInfo.encrypt = false;
|
||||||
|
esp_err_t res = esp_now_add_peer(&peerInfo);
|
||||||
|
if (res == ESP_OK) {
|
||||||
|
Serial.println("Re-added remote peer");
|
||||||
|
} else {
|
||||||
|
Serial.printf("Failed to add peer: %d\n", res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
leftServoA.attach(leftPins[0]);
|
leftServoA.attach(leftPins[0]);
|
||||||
leftServoB.attach(leftPins[1]);
|
leftServoB.attach(leftPins[1]);
|
||||||
rightServoA.attach(rightPins[0]);
|
rightServoA.attach(rightPins[0]);
|
||||||
rightServoB.attach(rightPins[1]);
|
rightServoB.attach(rightPins[1]);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
PrintMacAddress();
|
||||||
|
|
||||||
esp_now_init();
|
esp_now_init();
|
||||||
esp_now_peer_info_t peerInfo;
|
esp_now_peer_info_t peerInfo = {};
|
||||||
memcpy(peerInfo.peer_addr, remoteAddr, 6);
|
memcpy(peerInfo.peer_addr, remoteAddr, 6);
|
||||||
peerInfo.channel = 0;
|
peerInfo.channel = 0;
|
||||||
peerInfo.encrypt = false;
|
peerInfo.encrypt = false;
|
||||||
esp_now_add_peer(&peerInfo);
|
esp_now_add_peer(&peerInfo);
|
||||||
esp_now_register_recv_cb(onCommandRecv);
|
esp_now_register_recv_cb(onCommandRecv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lastTelemetrySent = 0;
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
ensurePeerConnected();
|
||||||
|
|
||||||
if (millis() - lastTelemetrySent > 500) {
|
if (millis() - lastTelemetrySent > 500) {
|
||||||
telemetry.battery = analogRead(36); // Replace with proper battery voltage calculation
|
telemetry.battery = analogRead(36); // Adjust this as needed
|
||||||
esp_now_send(remoteAddr, (uint8_t*)&telemetry, sizeof(telemetry));
|
esp_now_send(remoteAddr, (uint8_t*)&telemetry, sizeof(telemetry));
|
||||||
lastTelemetrySent = millis();
|
lastTelemetrySent = millis();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintMacAddress() {
|
||||||
|
delay(100); // Let hardware settle
|
||||||
|
uint8_t mac[6];
|
||||||
|
esp_wifi_get_mac(WIFI_IF_STA, mac); // Get MAC address for STA
|
||||||
|
Serial.print("Car MAC Address: ");
|
||||||
|
Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user