Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com
- *********/
- #include <esp_now.h>
- #include <WiFi.h>
- unsigned long Currentmillis;
- //////////////////////////////////////////////// SHOW-all
- unsigned long previoustime_all = 0;
- const long Start_showall = 900000;
- const long Stop_showall= 50000;
- //////////////////////////////////////////////// Show-Group1
- unsigned long previoustime_Group1 = 0;
- const long Start_show_Group1 = 300000;
- const long Stop_show_Group1 = 50000;
- //////////////////////////////////////////////// Show-Group2
- unsigned long previoustime_Group2 = 0;
- const long Start_show_Group2 = 600000;
- const long Stop_show_Group2 = 50000;
- // REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
- uint8_t broadcastAddress1[] = {0xE8, 0x31, 0xCD, 0xD6, 0xE9, 0x20};
- uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x78};
- uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04};
- /////////////////////////////////////////////// DATA to be sent
- typedef struct struct_message
- {
- bool Start_show1;
- bool Start_show2;
- bool Start_show_all;
- }
- struct_message;
- ///Struct_message holds the time(millis) start show
- struct_message Show_all;
- struct_message Show_Group1;
- struct_message Show_Group2;
- 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
- esp_now_peer_info_t peerInfo;
- 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() {
- ////////////////////////////////////////////////////////Show_All_On
- Currentmillis = millis();
- if(Currentmillis - previoustime_all >= Start_showall)
- {
- Show_all.Start_show_all = 1; /////////////////////RECIEVER ON
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show_all = 1;/////////////////////RECIEVER ON
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show_all = 1;/////////////////////RECIEVER ON
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_all = Currentmillis;
- }
- }
- }
- ////////////////////////////////////////////////////////Show_ALL_OFF
- else if (Currentmillis - previoustime_all >= Stop_showall)
- {
- Show_all.Start_show_all = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show_all = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show_all = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_all = Currentmillis;
- }
- }
- }
- ////////////////////////////////////////////////////////Show_Group1_ON
- Currentmillis = millis();
- if(Currentmillis - previoustime_Group1 >= Start_show_Group1)
- {
- Show_all.Start_show1 = 0;/////////////////////RECIEVER 0ff
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show1 = 1;/////////////////////RECIEVER ON
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show1 = 0;/////////////////////RECIEVER off
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_Group1 = Currentmillis;
- }
- }
- }
- ////////////////////////////////////////////////////////Show_Group1_OFF
- else if (Currentmillis - previoustime_Group1 >= Stop_show_Group1)
- {
- Show_all.Start_show1 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show1 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show1 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_Group1 = Currentmillis;
- }
- }
- }
- ///////////////////////////////////////////////////////Show_Group2_ON
- Currentmillis = millis();
- if ( Currentmillis - previoustime_Group2 >= Start_show_Group2)
- {
- Show_all.Start_show2 = 0;/////////////////////RECIEVER off
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show2 = 0;/////////////////////RECIEVER off
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show2 = 1;/////////////////////RECIEVER ON
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_Group2 = Currentmillis;
- }
- }
- }
- ////////////////////////////////////////////////////////Show_Group2_OFF
- else if (Currentmillis - previoustime_all >= Stop_show_Group2)
- {
- Show_all.Start_show2 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress1, (uint8_t *) &Show_all, sizeof(struct_message));
- {
- Show_Group1.Start_show2 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &Show_Group1, sizeof(struct_message));
- {
- Show_Group2.Start_show2 = 0;/////////////////////RECIEVER Off
- esp_err_t result = esp_now_send(broadcastAddress3, (uint8_t *) &Show_Group2, sizeof(struct_message));
- previoustime_Group2 = Currentmillis;
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ESP32 divkit
- /*********
- 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 struct_message {
- bool Start_show1;
- bool Start_show2;
- bool Start_show_all;
- } struct_message;
- struct_message incomingReadings;
- String success;
- // 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("Bytes received: ");
- Serial.println(len);
- Serial.print("Show_all: ");Serial.println(incomingReadings.Start_show_all);
- Serial.print("Show_Group1: ");Serial.println(incomingReadings.Start_show1);
- Serial.print("Show_Group2: ");Serial.println(incomingReadings.Start_show2);
- }
- 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);
- // 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() {
- //////////////////Will run once recieve data from sender.
- 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