Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------Sender code---------------------------------------------
- #include <esp_now.h>
- #include <WiFi.h>
- /////////////////////////////////////////////////////////////Show_all
- unsigned long Current_time1;
- unsigned long Previoustime1 = 0;
- const long Show_all_start = 120000;
- const long Show_all_stop = 60000;
- /////////////////////////////////////////////////////////////Show_Group1
- unsigned long Current_time2;
- unsigned long Previoustime2 = 0;
- const long Show_Group1_start = 30000;
- const long Show_Group1_stop = 15000;
- /////////////////////////////////////////////////////////////Show_Group2
- unsigned long Current_time3;
- unsigned long Previoustime3 = 0;
- const long Show_Group2_start = 15000;
- const long Show_Group2_stop = 7500;
- // REPLACE WITH YOUR ESP RECEIVER’S MAC ADDRESS
- uint8_t broadcastAddress1[] = {0xE8, 0x31, 0xCD, 0xD6, 0xE9, 0x20};
- uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x84, 0x2F, 0x4C};
- uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04};
- typedef struct Show_struct {
- bool Show_StatusOn;
- bool Show_StatusOff;
- } Show_struct;
- Show_struct Show_all;
- Show_struct Show_Group1;
- Show_struct Show_Group2;
- esp_now_peer_info_t peerInfo;
- 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 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;
- memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- }
- void loop()
- {
- Current_time1 = millis();
- if(Current_time1 - Previoustime1 >= Show_all_start)
- {
- Show_all.Show_StatusOn = 1;
- esp_err_t result1 = esp_now_send(
- broadcastAddress1,
- (uint8_t *) &Show_all,
- sizeof(Show_struct));
- Previoustime1 = Current_time1;
- }
- else if (Current_time1 - Previoustime1 >= Show_all_stop)
- {
- Show_all.Show_StatusOff = 0;
- esp_err_t result1 = esp_now_send(
- broadcastAddress1,
- (uint8_t *) &Show_all,
- sizeof(Show_struct));
- Previoustime1 = Current_time1;
- if (result1 == ESP_OK) {
- Serial.println("Show_All sent");
- Serial.println();
- }
- else {
- Serial.println("Error sending the data");
- }
- delay(500);
- }
- /////////////////////////////////////////////////////////////////Show_Group1
- ----------- This code broadcastAddress2 got a problem its not sending-----------
- //////////////////////////////////////////////////////////////////////////////
- Current_time2 = millis();
- if(Current_time2 - Previoustime2 >= Show_Group1_start)
- {
- Show_Group1.Show_StatusOn = 1;
- esp_err_t result2 = esp_now_send(
- broadcastAddress2,
- (uint8_t *) &Show_Group1,
- sizeof(Show_struct));
- Previoustime2 = Current_time2;
- }
- else if (Current_time2 - Previoustime2 >= Show_Group1_stop)
- {
- Show_Group1.Show_StatusOff = 0;
- esp_err_t result2 = esp_now_send(
- broadcastAddress1,
- (uint8_t *) &Show_Group1,
- sizeof(Show_struct));
- Previoustime2 = Current_time2;
- if (result2 == ESP_OK) {
- Serial.println("Show_Group1 sent");
- Serial.println();
- }
- else {
- Serial.println("Error sending the data");
- }
- delay(500);
- }
- //////////////////////////////////////////////////////////////////Show_Group2
- Current_time3 = millis();
- if(Current_time3 - Previoustime3 >= Show_Group2_start)
- {
- Show_Group2.Show_StatusOn = 1;
- esp_err_t result3 = esp_now_send(
- broadcastAddress3,
- (uint8_t *) &Show_Group2,
- sizeof(Show_struct));
- Previoustime3 = Current_time3;
- }
- else if(Current_time3 - Previoustime3 >= Show_Group2_stop)
- {
- Show_Group2.Show_StatusOff = 0;
- esp_err_t result3 = esp_now_send(
- broadcastAddress3,
- (uint8_t *) &Show_Group2,
- sizeof(Show_struct));
- Previoustime3 = Current_time3;
- if (result3 == ESP_OK) {
- Serial.println("Show_Group2 sent");
- Serial.println();
- }
- else {
- Serial.println("Error sending the data");
- }
- delay(2000);
- }
- }
- -----------------------------------------------------------Receiver code----------------------------------------
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com
- *********/
- #include <esp_now.h>
- #include <WiFi.h>
- // Structure example to receive data
- // Must match the sender structure
- typedef struct Show_struct {
- bool Show_StatusOn;
- bool Show_StatusOff;
- } show_struct;
- // Create a struct_message called myData
- Show_struct incomingReadings;
- // callback function that will be executed when data is received
- void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- Serial.print("Byte received: ");
- Serial.println(len);
- Serial.print("Show_StatusOn: ");Serial.println(incomingReadings.Show_StatusOn);
- Serial.println();
- Serial.print("Show_StatusOff: ");Serial.println(incomingReadings.Show_StatusOff);
- Serial.println();
- }
- void setup() {
- // Initialize 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;
- }
- // Once ESPNow is successfully Init, we will register for recv CB to
- // get recv packer info
- esp_now_register_recv_cb(OnDataRecv);
- }
- void loop() {
- }
- ------And this code that i will put in the receiver once received data from the sender it well run is this achivable?-----------
- // LED 1
- int led1 = 32;
- unsigned long currentMillis;
- unsigned long previoustime = 0;
- long led1off = 1000;
- long led1on = 500;
- int ledState1 = 0;
- // LED 2
- int led2 = 33;
- unsigned long currentMillis2;
- unsigned long previoustime2 = 0;
- long led2off = 1500;
- long led2on = 500;
- int ledState2 = 0;
- // LED3
- int led3 = 27;
- unsigned long currentMillis3;
- unsigned long previoustime3 = 0;
- long led3off = 3000;
- long led3on = 3000;
- int ledState3 = 0;
- // LED 4
- int led4 = 14;
- unsigned long currentMillis4;
- unsigned long previoustime4 = 0;
- long led4off = 2000;
- long led4on = 2500;
- int ledState4 = 0;
- // LED 5
- int led5 = 26;
- unsigned long currentMillis5;
- unsigned long previoustime5 = 0;
- long led5off = 3000;
- long led5on = 3000;
- int ledState5 = 0;
- // LED 6
- int led6 = 34;
- unsigned long currentMillis6;
- unsigned long previoustime6 = 0;
- long led6off = 3000;
- long led6on = 3000;
- int ledState6 = 0;
- // MUSIC 7
- int led7 = 19;
- unsigned long currentMillis7;
- unsigned long previoustime7 = 0;
- long led7off = 200000;
- long led7on = 3000;
- int ledState7 = 0;
- // SOUND SENSOR
- int MIC = 25;
- int sensorMIC = 12;
- const int thershold = 50;
- void setup(){
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- pinMode(led3, OUTPUT);
- pinMode(led4, OUTPUT);
- pinMode(led5, OUTPUT);
- pinMode(led6, OUTPUT);
- pinMode(led7, OUTPUT);
- pinMode(MIC, OUTPUT);
- pinMode(sensorMIC, INPUT);
- Serial.begin (9600);
- }
- void loop(){
- currentMillis = millis();
- if (((currentMillis - previoustime) >= led1on) && (ledState1 ==1))
- {
- ledState1 = !ledState1;
- digitalWrite(led1, ledState1);
- previoustime = currentMillis;
- }
- else if (((currentMillis - previoustime) >= led1off) && (ledState1 == 0))
- {
- ledState1 = !ledState1;
- digitalWrite(led1, ledState1);
- previoustime = currentMillis;
- }
- // led2
- {
- currentMillis2 = millis();
- if (((currentMillis2 - previoustime2) >= led2on) && (ledState2 == 1))
- {
- ledState2 = !ledState2;
- digitalWrite(led2, ledState2);
- previoustime2 = currentMillis2;
- }
- else if (((currentMillis2 - previoustime2) >= led2off) && (ledState2 == 0))
- {
- ledState2 = !ledState2;
- digitalWrite(led2, ledState2);
- previoustime2 = currentMillis2;
- }
- }
- // LED 3
- {
- currentMillis3 = millis();
- if (((currentMillis3 - previoustime3) >= led3on) && (ledState3 == 1))
- {
- ledState3 = !ledState3;
- digitalWrite(led3, ledState3);
- previoustime3 = currentMillis3;
- }
- else if (((currentMillis3 - previoustime3) >= led3off) && (ledState3 == 0))
- {
- ledState3 = !ledState3;
- digitalWrite(led3, ledState3);
- previoustime3 = currentMillis3;
- }
- }
- // LED 4
- {
- currentMillis4 = millis()
- if (((currentMillis4 - previoustime4) >= led4on) && (ledState4 == 1))
- {
- ledState4 = !ledState4;
- digitalWrite(led4, ledState4);
- previoustime4 = currentMillis4;
- }
- else if (((currentMillis4 - previoustime4) >= led4off) && (ledState4 == 0))
- {
- ledState4 = !ledState4;
- digitalWrite(led4, ledState4);
- previoustime4 = currentMillis4;
- }
- }
- // LED 5
- {
- currentMillis5 = millis();
- if (((currentMillis5 - previoustime5) >= led5on) && (ledState5 == 1))
- {
- ledState5 = !ledState5;
- digitalWrite(led5, ledState5);
- previoustime5 = currentMillis5;
- }
- else if (((currentMillis5 - previoustime5) >= led5off) && (ledState5 == 0))
- {
- ledState5 = !ledState5;
- digitalWrite(led5, ledState5);
- previoustime5 = currentMillis5;
- }
- }
- // LED 6
- {
- currentMillis6 = millis();
- if (((currentMillis6 - previoustime6) >= led6on) && (ledState6 == 1))
- {
- ledState6 = !ledState6;
- digitalWrite(led6, ledState6);
- previoustime6 = currentMillis6;
- }
- else if (((currentMillis6 - previoustime6) >= led6off) && (ledState6 == 0))
- {
- ledState6 = !ledState6;
- digitalWrite(led6, ledState6);
- previoustime6 = currentMillis6;
- }
- }
- // MUSIC
- {
- currentMillis7 = millis();
- if (((currentMillis7 - previoustime7) >= led7on) && (ledState7 == 1))
- {
- ledState7 = !ledState7;
- digitalWrite(led7, ledState7);
- previoustime7 = currentMillis7;
- }
- else if (((currentMillis7 - previoustime7) >= led7off) && (ledState7 == 0))
- {
- ledState7 = !ledState7;
- digitalWrite(led7, ledState7);
- previoustime7 = currentMillis7;
- }
- }
- // SOUND SENSOR
- int Soundsens=analogRead(sensorMIC);
- if (Soundsens>=thershold) {
- digitalWrite(MIC, LOW);
- delay(100);
- }
- else{
- digitalWrite(MIC,HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement