0% found this document useful (0 votes)
27 views3 pages

Car Esp 32

This document is an ESP32 program that utilizes ESP-NOW for wireless communication to control a servo and a beeper based on incoming data. It defines a structure for the data received, which includes direction, angle, speed, and beep status. The program initializes the ESP32, sets up the servo and beeper, and processes incoming data to control the motor and sound output accordingly.

Uploaded by

fozol.hanum27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Car Esp 32

This document is an ESP32 program that utilizes ESP-NOW for wireless communication to control a servo and a beeper based on incoming data. It defines a structure for the data received, which includes direction, angle, speed, and beep status. The program initializes the ESP32, sets up the servo and beeper, and processes incoming data to control the motor and sound output accordingly.

Uploaded by

fozol.hanum27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <esp_now.

h>
#include <ESP32Servo.h>
#include <WiFi.h>

// remote mac: a0:b7:65:4a:7c:dc


// car mac: a0:b7:65:69:c9:78
#define beeper 13
#define servo 15
#define en 26
#define in1 27
#define in2 14
bool isIncomingData = false;
Servo steer; // create servo object to control a servo

#define NOTE_B4 494

// Define variables to store BME280 readings to be sent


bool incomingDir;
int incomingAngle;
int incomingSpeed;
bool incomingBeep;

//Structure example to send data


//Must match the receiver structure
typedef struct struct_message {
bool direct;
int angleOfAttack;
int mehirut;
bool spkr;
} struct_message;

// Create a struct_message to hold incoming sensor readings


struct_message spaceTime;

esp_now_peer_info_t peerInfo;

// Callback when data is received


void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&spaceTime, incomingData, sizeof(spaceTime));
Serial.print("Bytes received: ");
Serial.println(len);
incomingDir = spaceTime.direct;
incomingAngle = spaceTime.angleOfAttack;
incomingSpeed = spaceTime.mehirut;
incomingBeep = spaceTime.spkr;
Serial.println(spaceTime.direct);
Serial.println(spaceTime.angleOfAttack);
Serial.println(spaceTime.mehirut);
Serial.println(spaceTime.spkr);
isIncomingData = true;
}

void setup() {
ledcAttach(beeper, 0, 8);

ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
steer.setPeriodHertz(50); // standard 50 hz servo
steer.attach(servo, 500, 2400); // attaches the servo on pin 18 to the servo
object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
steer.write(0);
pinMode(beeper, OUTPUT);
pinMode(servo, OUTPUT);
pinMode(en, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Init Serial Monitor
Serial.begin(115200);

// 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;
}

// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));

void loop() {
if (isIncomingData){
incomingDir = spaceTime.direct;
incomingAngle = spaceTime.angleOfAttack;
incomingSpeed = spaceTime.mehirut;
incomingBeep = spaceTime.spkr;

if (incomingDir == true){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
} else {
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
}

steer.write(incomingAngle);

if (incomingSpeed >= 100){


analogWrite(en, incomingSpeed);
} else {
analogWrite(en, 0);
}

if (incomingBeep){
Tone(0, NOTE_B4);
} else {
noTone(0, 0);
}

isIncomingData = false;
}
}

You might also like