0% found this document useful (0 votes)
5 views7 pages

RELAY PANEL CODE SIMPLE (ESP32 Board MAC Address: 88:13:bf:02:11:f4)

The document contains code for an ESP32-based relay control system using WiFi and ESP-NOW protocols. It includes a MAC address retrieval function, a relay panel code for toggling relays based on serial commands, a receiver code for handling incoming relay commands, and a control panel code for sending commands to the relay panel. The system allows for controlling up to 8 relays with feedback on their states and sender MAC address.

Uploaded by

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

RELAY PANEL CODE SIMPLE (ESP32 Board MAC Address: 88:13:bf:02:11:f4)

The document contains code for an ESP32-based relay control system using WiFi and ESP-NOW protocols. It includes a MAC address retrieval function, a relay panel code for toggling relays based on serial commands, a receiver code for handling incoming relay commands, and a control panel code for sending commands to the relay panel. The system allows for controlling up to 8 relays with feedback on their states and sender MAC address.

Uploaded by

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

MAC ADDRESS GETTING CODE

#include <WiFi.h>
#include <esp_wifi.h>

void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
[Link]("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
[Link]("Failed to read MAC address");
}
}

void setup(){
[Link](115200);

[Link](WIFI_STA);
[Link]();

[Link]("[DEFAULT] ESP32 Board MAC Address: ");


readMacAddress();
}

void loop(){

RELAY PANEL CODE SIMPLE (ESP32 Board MAC Address: 88:13:bf:02:11:f4 )

// Define GPIOs for 8 relays


const int relayPins[8] = {23, 22, 19, 5, 2, 33, 32, 25};

// Store relay states (start all OFF)


bool relayStates[8] = {false, false, false, false, false, false, false, false};

// Define LED pins


const int ledStatus = 21; // Always ON at start (inverted logic)
const int ledBlink = 4; // Blinks on relay toggle (inverted logic)

void setup() {
[Link](115200);

// Initialize relay pins


for (int i = 0; i < 8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Set all relays OFF (LOW)
}

// Initialize LED pins


pinMode(ledStatus, OUTPUT);
pinMode(ledBlink, OUTPUT);
digitalWrite(ledStatus, LOW); // ON (inverted logic)
digitalWrite(ledBlink, LOW); // ON at start (inverted logic)

[Link]("All relays are OFF. Type relay1 to relay8 to toggle.");


}

void loop() {
if ([Link]()) {
String command = [Link]('\n');
[Link]();

if ([Link]("relay")) {
int relayNum = [Link](5).toInt(); // Extract number from command

if (relayNum >= 1 && relayNum <= 8) {


int index = relayNum - 1;
relayStates[index] = !relayStates[index]; // Toggle state
digitalWrite(relayPins[index], relayStates[index] ? HIGH : LOW);

// Blink ledBlink for 200ms (inverted logic)


digitalWrite(ledStatus, HIGH); // OFF
delay(200);
digitalWrite(ledStatus, LOW); // ON

[Link]("Relay ");
[Link](relayNum);
[Link](" is now ");
[Link](relayStates[index] ? "ON" : "OFF");
} else {
[Link]("Invalid relay number. Use relay1 to relay8.");
}
} else {
[Link]("Unknown command. Use relay1 to relay8.");
}
}
}

RECEIVER CODE FOR RELAY PANEL


#include <esp_now.h>
#include <WiFi.h>

// Define GPIOs for 8 relays


const int relayPins[8] = {23, 22, 19, 5, 2, 33, 32, 25};
bool relayStates[8] = {false, false, false, false, false, false, false, false};

// Define LED pins


const int ledStatus = 21; // Always ON at start (inverted logic)
const int ledBlink = 4; // Blinks on relay toggle (inverted logic)

// Structure to receive commands


typedef struct relay_command {
uint8_t relay_num; // 1-8
bool state; // true = ON, false = OFF
} relay_command;

// Updated callback function signature


void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
relay_command cmd;
memcpy(&cmd, incomingData, sizeof(cmd));

if (cmd.relay_num >= 1 && cmd.relay_num <= 8) {


int index = cmd.relay_num - 1;
relayStates[index] = [Link];
digitalWrite(relayPins[index], relayStates[index] ? HIGH : LOW);

// Blink confirmation LED


digitalWrite(ledBlink, HIGH); // OFF (inverted)
delay(100);
digitalWrite(ledBlink, LOW); // ON

[Link]("Relay ");
[Link](cmd.relay_num);
[Link](" set to ");
[Link]([Link] ? "ON" : "OFF");

// Print MAC address of sender


[Link]("From MAC: ");
for (int i = 0; i < 6; i++) {
[Link](info->src_addr[i], HEX);
if (i < 5) [Link](":");
}
[Link]();
}
}

void setup() {
[Link](115200);

// Initialize relay pins


for (int i = 0; i < 8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Set all relays OFF (LOW)
}

// Initialize LED pins


pinMode(ledStatus, OUTPUT);
pinMode(ledBlink, OUTPUT);
digitalWrite(ledStatus, LOW); // ON (inverted logic)
digitalWrite(ledBlink, LOW); // ON at start (inverted logic)

// Set device as WiFi Station


[Link](WIFI_STA);

// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
[Link]("Error initializing ESP-NOW");
return;
}

// Register callback with correct signature


esp_now_register_recv_cb(OnDataRecv);

// Print MAC address (for pairing with sender)


[Link]("Receiver MAC: ");
[Link]([Link]());
[Link]("Ready to receive commands...");
}

void loop() {
// Nothing to do here - everything happens in the callback
delay(1000);
}

CODE FOR Control PANEL


#include <esp_now.h>
#include <WiFi.h>

typedef struct relay_command {


uint8_t relay_num; // 1-8
bool state; // true = ON, false = OFF
} relay_command;

uint8_t receiverMac[] = {0x88, 0x13, 0xBF, 0x02, 0x11, 0xF4};

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {


[Link]("Last Packet Send Status: ");
[Link](status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}

void setup() {
[Link](115200);
[Link](WIFI_STA);

if (esp_now_init() != ESP_OK) {
[Link]("ESP-NOW Init Failed");
return;
}

esp_now_register_send_cb(OnDataSent);

esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverMac, 6);
[Link] = 0;
[Link] = false;

if (esp_now_add_peer(&peerInfo) != ESP_OK) {
[Link]("Failed to add peer");
return;
}

[Link]("Control Panel Ready");


[Link]("Enter commands like: R1ON, R2OFF, etc. (R1-R8)");
}

void loop() {
if ([Link]()) {
String command = [Link]('\n');
[Link]();
[Link]();

// Check minimum length (R + number + ON/OFF)


if ([Link]() >= 4 && [Link]("R")) {
// Find where the number ends
int num_end = 1;
while (num_end < [Link]() && isDigit([Link](num_end))) {
num_end++;
}

if (num_end > 1) { // At least one digit found


uint8_t relay_num = [Link](1, num_end).toInt();
String state_str = [Link](num_end);

if (relay_num >= 1 && relay_num <= 8) {


relay_command cmd;
cmd.relay_num = relay_num;

if (state_str == "ON") {
[Link] = true;
} else if (state_str == "OFF") {
[Link] = false;
} else {
[Link]("Invalid state. Use ON or OFF");
return;
}

esp_err_t result = esp_now_send(receiverMac, (uint8_t *)&cmd, sizeof(cmd));

if (result == ESP_OK) {
[Link]("Sent: Relay ");
[Link](relay_num);
[Link](" ");
[Link]([Link] ? "ON" : "OFF");
} else {
[Link]("Send Error");
}
} else {
[Link]("Invalid relay number. Use R1-R8");
}
} else {
[Link]("Missing relay number");
}
} else {
[Link]("Invalid format. Use R1ON, R2OFF, etc.");
}
}
delay(10);
}

You might also like