Advertisement
Evenmerk

ESPnow send boolean data structure

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