@PeteKnight @Blynk_Coeur
I want to receive a notification when my LED is on, which FLOATSENSOR2 triggered. I code Blynk.notify(“First water level detected”); after the digitalWrite(LED, HIGH); then when I upload the code and run then the blynk will disconnect and connect itself in a short time and no output result for my DHT11. Below is my coding that looks fine when there without Blynk.notify(“First water level detected”);
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
char auth[] = "xxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
int statusLed = 13; // byte stores an 8-bit unsigned number, from 0 to 255.
int sensorInterrupt = 0; // 0 = digital pin 2
int sensorPin = 2;
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
#define FLOAT_SENSOR 12 // the number of the pushbutton pin
#define FLOAT_SENSOR2 13
#define LED 5 // the number of the LED pin
#define BUZZER 6
#define DHTPIN 4 // What digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
//Insterrupt Service Routine
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
Serial.print("\n");
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
{
// initialize the WFS pin as an input:
pinMode(sensorPin, INPUT);
// initialize the LED pin as an output:
pinMode(LED, OUTPUT);
// initialize the BUZZER pin as an output:
pinMode(BUZZER, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(FLOAT_SENSOR, INPUT_PULLUP);
// initialize the pushbutton pin as an input:
pinMode(FLOAT_SENSOR2, INPUT_PULLUP);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
}
void loop()
{
if(digitalRead(FLOAT_SENSOR2) == HIGH)
{
// turn LED on:
digitalWrite(LED, HIGH);
}
else
{
// turn LED off:
digitalWrite(LED, LOW);
}
if(digitalRead(FLOAT_SENSOR) == HIGH)
{
// turn BUZZER on:
digitalWrite(BUZZER, HIGH);
}
else
{
// turn BUZZER off:
digitalWrite(BUZZER, LOW);
}
if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
// Note the time this processing pass was executed. Note that because we've
// disabled interrupts the millis() function won't actually be incrementing right
// at this point, but it will still return the value it was set to just before
// interrupts went away.
oldTime = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
unsigned int frac;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.println("mL");
Serial.print("\t"); // Print tab space
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// range value:
switch (range) {
case 0: // Sensor getting more wet
Serial.println("Flood");
break;
case 1: // Sensor getting wet
Serial.println("Rain Warning");
break;
case 2: // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
Serial.println("Not Raining");
break;
}
delay(1000); // delay between reads
Blynk.run();
timer.run();
}