Advertisement
Evenmerk

Show

Nov 10th, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. ----------------------------------------Sender code---------------------------------------------
  2. #include <esp_now.h>
  3. #include <WiFi.h>
  4.  
  5.  
  6. /////////////////////////////////////////////////////////////Show_all
  7. unsigned long Current_time1;
  8. unsigned long Previoustime1 = 0;
  9. const long Show_all_start = 120000;
  10. const long Show_all_stop = 60000;
  11. /////////////////////////////////////////////////////////////Show_Group1
  12. unsigned long Current_time2;
  13. unsigned long Previoustime2 = 0;
  14. const long Show_Group1_start = 30000;
  15. const long Show_Group1_stop = 15000;
  16. /////////////////////////////////////////////////////////////Show_Group2
  17. unsigned long Current_time3;
  18. unsigned long Previoustime3 = 0;
  19. const long Show_Group2_start = 15000;
  20. const long Show_Group2_stop = 7500;
  21. // REPLACE WITH YOUR ESP RECEIVER’S MAC ADDRESS
  22. uint8_t broadcastAddress1[] = {0xE8, 0x31, 0xCD, 0xD6, 0xE9, 0x20};
  23. uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x84, 0x2F, 0x4C};
  24. uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04};
  25.  
  26. typedef struct Show_struct {
  27.  
  28. bool Show_StatusOn;
  29. bool Show_StatusOff;
  30.  
  31. } Show_struct;
  32.  
  33. Show_struct Show_all;
  34. Show_struct Show_Group1;
  35. Show_struct Show_Group2;
  36.  
  37. esp_now_peer_info_t peerInfo;
  38.  
  39. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  40. char macStr[18];
  41. Serial.print("Packet to: ");
  42. // Copies the sender mac address to a string
  43. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  44. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  45. Serial.print(macStr);
  46. Serial.print(" send status:\t");
  47. Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  48. }
  49.  
  50. void setup() {
  51.  
  52. Serial.begin(115200);
  53.  
  54. WiFi.mode(WIFI_STA);
  55.  
  56. if (esp_now_init() != ESP_OK) {
  57. Serial.println("Error initializing ESP-NOW");
  58. return;
  59. }
  60.  
  61. esp_now_register_send_cb(OnDataSent);
  62.  
  63. // register peer
  64. peerInfo.channel = 0;
  65. peerInfo.encrypt = false;
  66.  
  67. memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  68. if (esp_now_add_peer(&peerInfo) != ESP_OK){
  69. Serial.println("Failed to add peer");
  70. return;
  71. }
  72.  
  73. memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  74. if (esp_now_add_peer(&peerInfo) != ESP_OK){
  75. Serial.println("Failed to add peer");
  76. return;
  77. }
  78. memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  79. if (esp_now_add_peer(&peerInfo) != ESP_OK){
  80. Serial.println("Failed to add peer");
  81. return;
  82. }
  83. }
  84.  
  85. void loop()
  86. {
  87. Current_time1 = millis();
  88. if(Current_time1 - Previoustime1 >= Show_all_start)
  89. {
  90. Show_all.Show_StatusOn = 1;
  91. esp_err_t result1 = esp_now_send(
  92. broadcastAddress1,
  93. (uint8_t *) &Show_all,
  94. sizeof(Show_struct));
  95. Previoustime1 = Current_time1;
  96. }
  97. else if (Current_time1 - Previoustime1 >= Show_all_stop)
  98. {
  99. Show_all.Show_StatusOff = 0;
  100. esp_err_t result1 = esp_now_send(
  101. broadcastAddress1,
  102. (uint8_t *) &Show_all,
  103. sizeof(Show_struct));
  104. Previoustime1 = Current_time1;
  105.  
  106. if (result1 == ESP_OK) {
  107. Serial.println("Show_All sent");
  108. Serial.println();
  109. }
  110. else {
  111. Serial.println("Error sending the data");
  112. }
  113. delay(500);
  114. }
  115. /////////////////////////////////////////////////////////////////Show_Group1
  116. ----------- This code broadcastAddress2 got a problem its not sending-----------
  117. //////////////////////////////////////////////////////////////////////////////
  118. Current_time2 = millis();
  119. if(Current_time2 - Previoustime2 >= Show_Group1_start)
  120. {
  121. Show_Group1.Show_StatusOn = 1;
  122. esp_err_t result2 = esp_now_send(
  123. broadcastAddress2,
  124. (uint8_t *) &Show_Group1,
  125. sizeof(Show_struct));
  126. Previoustime2 = Current_time2;
  127. }
  128. else if (Current_time2 - Previoustime2 >= Show_Group1_stop)
  129. {
  130. Show_Group1.Show_StatusOff = 0;
  131. esp_err_t result2 = esp_now_send(
  132. broadcastAddress1,
  133. (uint8_t *) &Show_Group1,
  134. sizeof(Show_struct));
  135. Previoustime2 = Current_time2;
  136.  
  137. if (result2 == ESP_OK) {
  138. Serial.println("Show_Group1 sent");
  139. Serial.println();
  140. }
  141. else {
  142. Serial.println("Error sending the data");
  143. }
  144. delay(500);
  145. }
  146. //////////////////////////////////////////////////////////////////Show_Group2
  147. Current_time3 = millis();
  148. if(Current_time3 - Previoustime3 >= Show_Group2_start)
  149. {
  150. Show_Group2.Show_StatusOn = 1;
  151. esp_err_t result3 = esp_now_send(
  152. broadcastAddress3,
  153. (uint8_t *) &Show_Group2,
  154. sizeof(Show_struct));
  155. Previoustime3 = Current_time3;
  156. }
  157. else if(Current_time3 - Previoustime3 >= Show_Group2_stop)
  158. {
  159. Show_Group2.Show_StatusOff = 0;
  160. esp_err_t result3 = esp_now_send(
  161. broadcastAddress3,
  162. (uint8_t *) &Show_Group2,
  163. sizeof(Show_struct));
  164. Previoustime3 = Current_time3;
  165.  
  166. if (result3 == ESP_OK) {
  167. Serial.println("Show_Group2 sent");
  168. Serial.println();
  169. }
  170. else {
  171. Serial.println("Error sending the data");
  172. }
  173. delay(2000);
  174. }
  175. }
  176.  
  177. -----------------------------------------------------------Receiver code----------------------------------------
  178.  
  179. /*********
  180. Rui Santos
  181. Complete project details at https://RandomNerdTutorials.com
  182. *********/
  183.  
  184. #include <esp_now.h>
  185. #include <WiFi.h>
  186.  
  187. // Structure example to receive data
  188. // Must match the sender structure
  189. typedef struct Show_struct {
  190. bool Show_StatusOn;
  191. bool Show_StatusOff;
  192. } show_struct;
  193.  
  194. // Create a struct_message called myData
  195. Show_struct incomingReadings;
  196.  
  197.  
  198. // callback function that will be executed when data is received
  199. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  200. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  201. Serial.print("Byte received: ");
  202. Serial.println(len);
  203. Serial.print("Show_StatusOn: ");Serial.println(incomingReadings.Show_StatusOn);
  204. Serial.println();
  205. Serial.print("Show_StatusOff: ");Serial.println(incomingReadings.Show_StatusOff);
  206. Serial.println();
  207. }
  208.  
  209. void setup() {
  210. // Initialize Serial Monitor
  211. Serial.begin(115200);
  212.  
  213. // Set device as a Wi-Fi Station
  214. WiFi.mode(WIFI_STA);
  215.  
  216. // Init ESP-NOW
  217. if (esp_now_init() != ESP_OK) {
  218. Serial.println("Error initializing ESP-NOW");
  219. return;
  220. }
  221.  
  222. // Once ESPNow is successfully Init, we will register for recv CB to
  223. // get recv packer info
  224. esp_now_register_recv_cb(OnDataRecv);
  225. }
  226.  
  227. void loop() {
  228.  
  229. }
  230.  
  231. ------And this code that i will put in the receiver once received data from the sender it well run is this achivable?-----------
  232.  
  233. // LED 1
  234. int led1 = 32;
  235. unsigned long currentMillis;
  236. unsigned long previoustime = 0;
  237. long led1off = 1000;
  238. long led1on = 500;
  239. int ledState1 = 0;
  240. // LED 2
  241. int led2 = 33;
  242. unsigned long currentMillis2;
  243. unsigned long previoustime2 = 0;
  244. long led2off = 1500;
  245. long led2on = 500;
  246. int ledState2 = 0;
  247. // LED3
  248. int led3 = 27;
  249. unsigned long currentMillis3;
  250. unsigned long previoustime3 = 0;
  251. long led3off = 3000;
  252. long led3on = 3000;
  253. int ledState3 = 0;
  254. // LED 4
  255. int led4 = 14;
  256. unsigned long currentMillis4;
  257. unsigned long previoustime4 = 0;
  258. long led4off = 2000;
  259. long led4on = 2500;
  260. int ledState4 = 0;
  261. // LED 5
  262. int led5 = 26;
  263. unsigned long currentMillis5;
  264. unsigned long previoustime5 = 0;
  265. long led5off = 3000;
  266. long led5on = 3000;
  267. int ledState5 = 0;
  268. // LED 6
  269. int led6 = 34;
  270. unsigned long currentMillis6;
  271. unsigned long previoustime6 = 0;
  272. long led6off = 3000;
  273. long led6on = 3000;
  274. int ledState6 = 0;
  275. // MUSIC 7
  276. int led7 = 19;
  277. unsigned long currentMillis7;
  278. unsigned long previoustime7 = 0;
  279. long led7off = 200000;
  280. long led7on = 3000;
  281. int ledState7 = 0;
  282. // SOUND SENSOR
  283. int MIC = 25;
  284. int sensorMIC = 12;
  285. const int thershold = 50;
  286.  
  287.  
  288. void setup(){
  289. pinMode(led1, OUTPUT);
  290. pinMode(led2, OUTPUT);
  291. pinMode(led3, OUTPUT);
  292. pinMode(led4, OUTPUT);
  293. pinMode(led5, OUTPUT);
  294. pinMode(led6, OUTPUT);
  295. pinMode(led7, OUTPUT);
  296. pinMode(MIC, OUTPUT);
  297. pinMode(sensorMIC, INPUT);
  298. Serial.begin (9600);
  299. }
  300.  
  301. void loop(){
  302. currentMillis = millis();
  303. if (((currentMillis - previoustime) >= led1on) && (ledState1 ==1))
  304. {
  305. ledState1 = !ledState1;
  306. digitalWrite(led1, ledState1);
  307. previoustime = currentMillis;
  308. }
  309. else if (((currentMillis - previoustime) >= led1off) && (ledState1 == 0))
  310. {
  311. ledState1 = !ledState1;
  312. digitalWrite(led1, ledState1);
  313. previoustime = currentMillis;
  314. }
  315. // led2
  316. {
  317. currentMillis2 = millis();
  318. if (((currentMillis2 - previoustime2) >= led2on) && (ledState2 == 1))
  319. {
  320. ledState2 = !ledState2;
  321. digitalWrite(led2, ledState2);
  322. previoustime2 = currentMillis2;
  323. }
  324. else if (((currentMillis2 - previoustime2) >= led2off) && (ledState2 == 0))
  325. {
  326. ledState2 = !ledState2;
  327. digitalWrite(led2, ledState2);
  328. previoustime2 = currentMillis2;
  329. }
  330. }
  331. // LED 3
  332. {
  333. currentMillis3 = millis();
  334. if (((currentMillis3 - previoustime3) >= led3on) && (ledState3 == 1))
  335. {
  336. ledState3 = !ledState3;
  337. digitalWrite(led3, ledState3);
  338. previoustime3 = currentMillis3;
  339. }
  340. else if (((currentMillis3 - previoustime3) >= led3off) && (ledState3 == 0))
  341. {
  342. ledState3 = !ledState3;
  343. digitalWrite(led3, ledState3);
  344. previoustime3 = currentMillis3;
  345. }
  346. }
  347. // LED 4
  348. {
  349. currentMillis4 = millis()
  350. if (((currentMillis4 - previoustime4) >= led4on) && (ledState4 == 1))
  351. {
  352. ledState4 = !ledState4;
  353. digitalWrite(led4, ledState4);
  354. previoustime4 = currentMillis4;
  355. }
  356. else if (((currentMillis4 - previoustime4) >= led4off) && (ledState4 == 0))
  357. {
  358. ledState4 = !ledState4;
  359. digitalWrite(led4, ledState4);
  360. previoustime4 = currentMillis4;
  361. }
  362. }
  363. // LED 5
  364. {
  365. currentMillis5 = millis();
  366. if (((currentMillis5 - previoustime5) >= led5on) && (ledState5 == 1))
  367. {
  368. ledState5 = !ledState5;
  369. digitalWrite(led5, ledState5);
  370. previoustime5 = currentMillis5;
  371. }
  372. else if (((currentMillis5 - previoustime5) >= led5off) && (ledState5 == 0))
  373. {
  374. ledState5 = !ledState5;
  375. digitalWrite(led5, ledState5);
  376. previoustime5 = currentMillis5;
  377. }
  378. }
  379. // LED 6
  380. {
  381. currentMillis6 = millis();
  382. if (((currentMillis6 - previoustime6) >= led6on) && (ledState6 == 1))
  383. {
  384. ledState6 = !ledState6;
  385. digitalWrite(led6, ledState6);
  386. previoustime6 = currentMillis6;
  387. }
  388. else if (((currentMillis6 - previoustime6) >= led6off) && (ledState6 == 0))
  389. {
  390. ledState6 = !ledState6;
  391. digitalWrite(led6, ledState6);
  392. previoustime6 = currentMillis6;
  393. }
  394. }
  395. // MUSIC
  396. {
  397. currentMillis7 = millis();
  398. if (((currentMillis7 - previoustime7) >= led7on) && (ledState7 == 1))
  399. {
  400. ledState7 = !ledState7;
  401. digitalWrite(led7, ledState7);
  402. previoustime7 = currentMillis7;
  403. }
  404. else if (((currentMillis7 - previoustime7) >= led7off) && (ledState7 == 0))
  405. {
  406. ledState7 = !ledState7;
  407. digitalWrite(led7, ledState7);
  408. previoustime7 = currentMillis7;
  409. }
  410. }
  411. // SOUND SENSOR
  412. int Soundsens=analogRead(sensorMIC);
  413. if (Soundsens>=thershold) {
  414. digitalWrite(MIC, LOW);
  415. delay(100);
  416. }
  417. else{
  418. digitalWrite(MIC,HIGH);
  419. }
  420. }
  421.  
  422.  
  423.  
  424.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement