safwan092

Untitled

May 20th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3. // Define the pin for the pump
  4. #define pumpPin 33
  5. #define RelayPin1 4
  6. #define RelayPin2 2
  7. #define RelayPin3 15
  8.  
  9. int pumpON1 = 0;
  10. int pumpON2 = 0;
  11. int pumpON3 = 0;
  12.  
  13. // Structure example to receive data
  14. // Must match the sender structure
  15. typedef struct struct_message {
  16. int id;
  17. int x;
  18. int y;
  19. } struct_message;
  20.  
  21. // Create a struct_message called myData
  22. struct_message myData;
  23.  
  24. // Create a structure to hold the readings from each board
  25. struct_message board1;
  26. struct_message board2;
  27. struct_message board3;
  28.  
  29. // Create an array with all the structures
  30. struct_message boardsStruct[3] = {board1, board2, board3};
  31.  
  32. // callback function that will be executed when data is received
  33. void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
  34. char macStr[18];
  35. Serial.print("Packet received from: ");
  36. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  37. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  38. Serial.println(macStr);
  39. memcpy(&myData, incomingData, sizeof(myData));
  40. Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
  41. // Update the structures with the new incoming data
  42. boardsStruct[myData.id - 1].x = myData.x;
  43. boardsStruct[myData.id - 1].y = myData.y;
  44. Serial.printf("x value: %d \n", boardsStruct[myData.id - 1].x);
  45. Serial.printf("y value: %d \n", boardsStruct[myData.id - 1].y);
  46. Serial.println();
  47.  
  48. }
  49.  
  50. void setup() {
  51. // Set the pump pin as an output
  52. pinMode(RelayPin1, OUTPUT);
  53. pinMode(RelayPin2, OUTPUT);
  54. pinMode(RelayPin3, OUTPUT);
  55. pinMode(pumpPin, OUTPUT);
  56. digitalWrite(RelayPin1, HIGH);
  57. digitalWrite(RelayPin2, HIGH);
  58. digitalWrite(RelayPin3, HIGH);
  59. digitalWrite(pumpPin, HIGH);
  60.  
  61.  
  62. //Initialize Serial Monitor
  63. Serial.begin(115200);
  64.  
  65. //Set device as a Wi-Fi Station
  66. WiFi.mode(WIFI_STA);
  67. //Init ESP-NOW
  68. if (esp_now_init() != ESP_OK) {
  69. Serial.println("Error initializing ESP-NOW");
  70. return;
  71. }
  72.  
  73. // Once ESPNow is successfully Init, we will register for recv CB to
  74. // get recv packer info
  75. esp_now_register_recv_cb(OnDataRecv);
  76. }
  77.  
  78. void loop() {
  79.  
  80. void OnDataRecv();
  81.  
  82. // Acess the variables for each board
  83. int board1X = boardsStruct[0].x;
  84. int board1Y = boardsStruct[0].y;
  85. int board2X = boardsStruct[1].x;
  86. int board2Y = boardsStruct[1].y;
  87. int board3X = boardsStruct[2].x;
  88. int board3Y = boardsStruct[2].y;
  89.  
  90. if (pumpON1 == 1 || pumpON2 == 1 || pumpON3 == 1)
  91. {
  92. digitalWrite(pumpPin, LOW);//Pump ON
  93. }
  94. else if (pumpON1 == 0 && pumpON2 == 0 && pumpON3 == 0) {
  95. digitalWrite(pumpPin, HIGH);//Pump OFF
  96. }
  97. // Check if the value is greater than 0 and the Relay is not already on
  98. // read the analog value from senso
  99.  
  100. if ( boardsStruct[0].y <= 8 ) {
  101. // Turn the Relay on
  102. digitalWrite(RelayPin1, LOW);
  103. pumpON1 = 1;
  104.  
  105. }
  106. // Check if the value is 0 and the Relay is already on
  107. else if ( boardsStruct[0].y >= 20 ) {
  108. // Turn the Relay off
  109. digitalWrite(RelayPin1, HIGH);
  110. pumpON1 = 0
  111. }
  112.  
  113. //=================================================================================
  114.  
  115.  
  116. if ( boardsStruct[1].y <= 8 ) {
  117. // Turn the Relay on
  118. digitalWrite(RelayPin2, LOW);
  119. pumpON2 = 1;
  120. }
  121.  
  122. // Check if the value is 0 and the Relay is already on
  123. else if ( boardsStruct[1].y >= 20 ) {
  124. // Turn the Relay off
  125. digitalWrite(RelayPin2, HIGH);
  126. pumpON2 = 0;
  127. }
  128. //=================================================================================
  129.  
  130. if ( boardsStruct[2].y <= 8 ) {
  131. // Turn the Relay on
  132. digitalWrite(RelayPin3, LOW);
  133. pumpON3 = 1;
  134. }
  135. // Check if the value is 0 and the Relay is already on
  136. else if ( boardsStruct[2].y >= 20 ) {
  137. // Turn the Relay off
  138. digitalWrite(RelayPin3, HIGH);
  139. pumpON3 = 0;
  140. }
  141.  
  142. delay(5000);
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment