Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <esp_now.h>
- #include <WiFi.h>
- // Define the pin for the pump
- #define pumpPin 33
- #define RelayPin1 4
- #define RelayPin2 2
- #define RelayPin3 15
- int pumpON1 = 0;
- int pumpON2 = 0;
- int pumpON3 = 0;
- // Structure example to receive data
- // Must match the sender structure
- typedef struct struct_message {
- int id;
- int x;
- int y;
- } struct_message;
- // Create a struct_message called myData
- struct_message myData;
- // Create a structure to hold the readings from each board
- struct_message board1;
- struct_message board2;
- struct_message board3;
- // Create an array with all the structures
- struct_message boardsStruct[3] = {board1, board2, board3};
- // callback function that will be executed when data is received
- void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
- char macStr[18];
- Serial.print("Packet received from: ");
- 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.println(macStr);
- memcpy(&myData, incomingData, sizeof(myData));
- Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
- // Update the structures with the new incoming data
- boardsStruct[myData.id - 1].x = myData.x;
- boardsStruct[myData.id - 1].y = myData.y;
- Serial.printf("x value: %d \n", boardsStruct[myData.id - 1].x);
- Serial.printf("y value: %d \n", boardsStruct[myData.id - 1].y);
- Serial.println();
- }
- void setup() {
- // Set the pump pin as an output
- pinMode(RelayPin1, OUTPUT);
- pinMode(RelayPin2, OUTPUT);
- pinMode(RelayPin3, OUTPUT);
- pinMode(pumpPin, OUTPUT);
- digitalWrite(RelayPin1, HIGH);
- digitalWrite(RelayPin2, HIGH);
- digitalWrite(RelayPin3, HIGH);
- digitalWrite(pumpPin, HIGH);
- //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() {
- void OnDataRecv();
- // Acess the variables for each board
- int board1X = boardsStruct[0].x;
- int board1Y = boardsStruct[0].y;
- int board2X = boardsStruct[1].x;
- int board2Y = boardsStruct[1].y;
- int board3X = boardsStruct[2].x;
- int board3Y = boardsStruct[2].y;
- if (pumpON1 == 1 || pumpON2 == 1 || pumpON3 == 1)
- {
- digitalWrite(pumpPin, LOW);//Pump ON
- }
- else if (pumpON1 == 0 && pumpON2 == 0 && pumpON3 == 0) {
- digitalWrite(pumpPin, HIGH);//Pump OFF
- }
- // Check if the value is greater than 0 and the Relay is not already on
- // read the analog value from senso
- if ( boardsStruct[0].y <= 8 ) {
- // Turn the Relay on
- digitalWrite(RelayPin1, LOW);
- pumpON1 = 1;
- }
- // Check if the value is 0 and the Relay is already on
- else if ( boardsStruct[0].y >= 20 ) {
- // Turn the Relay off
- digitalWrite(RelayPin1, HIGH);
- pumpON1 = 0
- }
- //=================================================================================
- if ( boardsStruct[1].y <= 8 ) {
- // Turn the Relay on
- digitalWrite(RelayPin2, LOW);
- pumpON2 = 1;
- }
- // Check if the value is 0 and the Relay is already on
- else if ( boardsStruct[1].y >= 20 ) {
- // Turn the Relay off
- digitalWrite(RelayPin2, HIGH);
- pumpON2 = 0;
- }
- //=================================================================================
- if ( boardsStruct[2].y <= 8 ) {
- // Turn the Relay on
- digitalWrite(RelayPin3, LOW);
- pumpON3 = 1;
- }
- // Check if the value is 0 and the Relay is already on
- else if ( boardsStruct[2].y >= 20 ) {
- // Turn the Relay off
- digitalWrite(RelayPin3, HIGH);
- pumpON3 = 0;
- }
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment