How do I connect relay module to wifi control by esp32
Posted: Sun Apr 30, 2023 8:41 pm
I'm using esp_now(transmitter & receiver) to switch relays on/off ( relays on at LOW signal) the problem is when power on receiver circuit ,esp32 and relay module at the same time,(relay module connected to +ve & -ve pins of esp32), there's no signal at receiver and showing error (invalid header) in serial monitor , but when power on esp32 first then connect relay module terminal to +ve & -ve pins ,it is working right .I need to solve this problem(power on all circuit at the same time. . the code of receiver
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
#define LED_Pin1 4
#define LED_Pin2 12
typedef struct struct_message {
int led1;
int led2;
} struct_message ;
struct_message receive_Data; // Create a struct_message to receive data.
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&receive_Data, incomingData, sizeof(receive_Data));
Serial.println();
Serial.println("<<<<< Receive Data:");
Serial.print("Bytes received: ");
Serial.println(len);
digitalWrite(LED_Pin1, receive_Data.led1);
digitalWrite(LED_Pin2, receive_Data.led2);
Serial.println("<<<<<");
}
void setup(){
Serial.begin(115200);
digitalWrite(LED_Pin1, HIGH);
digitalWrite(LED_Pin2, HIGH);
pinMode(LED_Pin1, OUTPUT);
pinMode(LED_Pin2, OUTPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop(){}