I have a code for Automatic Water Pump controller using esp8266, OLED display and nrf24l01.
Working:
- The motor starts when water level is 25% and can be switched to manual and automatic mode
- Buzzer beeps when tank is full and it can be turned off
- Motor can be controlled through both physical switches and blynk app
I was able to successfully upload the code to board but when i check serial monitor the it says successful Wifi connection along with some un usuall charcters & symbol and it works like loop saying wifi connected again and again.
So please tell whats wrong with this code.
Transmitter Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(2, 16); // CE, CSN pins
const byte address[6] = "00001"; // Address for communication
// Define float switch pin connections
const int floatSwitch1Pin = 3;
const int floatSwitch2Pin = 4;
const int floatSwitch3Pin = 5;
const int floatSwitch4Pin = 6;
void setup() {
Serial.begin(9600);
// Initialize float switch pins as inputs
pinMode(floatSwitch1Pin, INPUT);
pinMode(floatSwitch2Pin, INPUT);
pinMode(floatSwitch3Pin, INPUT);
pinMode(floatSwitch4Pin, INPUT);
// Initialize the radio module
radio.begin();
radio.openWritingPipe(address);
radio.setChannel(76);
radio.setPayloadSize(32);
radio.setPALevel(RF24_PA_LOW); // Set power level to low
}
void loop() {
// Read the actual states of the float switches
bool floatSwitch1 = digitalRead(floatSwitch1Pin);
bool floatSwitch2 = digitalRead(floatSwitch2Pin);
bool floatSwitch3 = digitalRead(floatSwitch3Pin);
bool floatSwitch4 = digitalRead(floatSwitch4Pin);
// Prepare the data to send
int dataToSend[4] = {(int)floatSwitch1, (int)floatSwitch2, (int)floatSwitch3, (int)floatSwitch4};
// Send data and check if it was successfully sent
if (radio.write(&dataToSend, sizeof(dataToSend))) {
Serial.println("Data sent successfully!");
} else {
Serial.println("Data sending failed!");
}
delay(2000); // Send data every 2 seconds
}
Receiver Code:
#define BLYNK_TEMPLATE_ID "TMPL3byZ4b1QG"
#define BLYNK_TEMPLATE_NAME "Automatic Motor Controller"
#define BLYNK_AUTH_TOKEN "-c20kbugQqouqjlAYmn9mvuvs128MkO7"
// WiFi credentials
char ssid[] = "testcheck";
char pass[] = "123erefnv";
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AceButton.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
using namespace ace_button;
WiFiClient client;
RF24 radio(2, 16); // CE, CSN pins
const byte address[6] = "00001"; // Address for communication
// Define connections
#define wifiLed 7
#define BuzzerPin 6
#define RelayPin 10
#define ButtonPin1 9 // Mode switch
#define ButtonPin2 8 // Relay control
#define ButtonPin3 11 // Buzzer reset
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_WATER_LEVEL V4 // Virtual pin for water level
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// State variables
bool toggleRelay = false; // Define the toggle state for relay
bool modeFlag = true; // true for AUTO mode, false for MANUAL mode
int waterLevel = 0; // Water level percentage
char auth[] = BLYNK_AUTH_TOKEN;
ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
pinMode(wifiLed, OUTPUT);
pinMode(RelayPin, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
pinMode(ButtonPin1, INPUT_PULLUP);
pinMode(ButtonPin2, INPUT_PULLUP);
pinMode(ButtonPin3, INPUT_PULLUP);
digitalWrite(wifiLed, HIGH);
digitalWrite(RelayPin, LOW);
digitalWrite(BuzzerPin, LOW);
button1.init(ButtonPin1);
button2.init(ButtonPin2);
button3.init(ButtonPin3);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
// Setup NRF24L01
radio.begin();
radio.openReadingPipe(1, address);
radio.setChannel(76);
radio.setPayloadSize(32);
radio.startListening();
// Initialize Blynk
Blynk.config(auth);
}
void loop() {
Blynk.run();
button1.check(); // Mode change
button3.check(); // Buzzer reset
// Check for incoming data from NRF24L01
if (radio.available()) {
int receivedData[4]; // Array to hold float switch states
radio.read(&receivedData, sizeof(receivedData)); // Read the incoming data
// Update water level based on received data
waterLevel = (receivedData[0] * 25); // Assuming floatSwitch1, multiply by 25 for percentage
if (receivedData[1]) waterLevel += 25; // Level 2
if (receivedData[2]) waterLevel += 25; // Level 3
if (receivedData[3]) waterLevel += 25; // Level 4
Blynk.virtualWrite(VPIN_WATER_LEVEL, waterLevel); // Send water level to Blynk
// Control the pump based on water level and mode
if (modeFlag) { // AUTO mode
if (waterLevel < 25) { // If water level is below 25%
digitalWrite(RelayPin, HIGH); // Turn on pump
toggleRelay = true;
} else {
digitalWrite(RelayPin, LOW); // Turn off pump
toggleRelay = false;
if (waterLevel >= 100) { // If tank is full
digitalWrite(BuzzerPin, HIGH); // Turn on buzzer
}
}
}
}
// Update OLED display
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Mode: ");
display.print(modeFlag ? "AUTO" : "MANUAL");
display.setCursor(0, 10);
display.print("Water Level: ");
display.print(waterLevel);
display.print("%");
display.setCursor(0, 20);
display.print("Pump: ");
display.print(toggleRelay ? "ON" : "OFF");
display.display();
}
BLYNK_WRITE(VPIN_BUTTON_1) {
modeFlag = param.asInt(); // Get mode from Blynk
Blynk.virtualWrite(VPIN_BUTTON_1, modeFlag);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
if (!modeFlag) { // If in manual mode
toggleRelay = param.asInt(); // Get relay state from Blynk
digitalWrite(RelayPin, toggleRelay ? HIGH : LOW);
}
}
BLYNK_WRITE(VPIN_BUTTON_3) {
digitalWrite(BuzzerPin, LOW); // Turn off buzzer
}
void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
if (eventType == AceButton::kEventReleased) {
modeFlag = !modeFlag; // Toggle mode
}
}
void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
if (eventType == AceButton::kEventReleased) {
toggleRelay = !toggleRelay; // Toggle relay
digitalWrite(RelayPin, toggleRelay ? HIGH : LOW);
}
}
void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
if (eventType == AceButton::kEventReleased) {
digitalWrite(BuzzerPin, LOW); // Turn off buzzer
}
}