Hi
I am using Arduino UNO+ESP8266-01+sensors
Using Blynk app on Android MotoG5
Code compiled, however
- arduino not reading Virtual input from app for slider(V28) and output slider reading on V29.
- Also reading value from Analog A1-A3 but not from attached virtual pin.
- Tank level responded once since started device after code upload
- Wifi still disconnecting, using Blynk connect function also
Need help, i have spent 2months with different codes. this was final field working code.
//Irrigation Controller documentation
//#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <WidgetLED.h>
int isFirstConnect = true;
int humLowTrigger;
//Defining Ultrasonic Sensor PINs and variables
const int ROTRIGPIN = 5;
const int ROECHOPIN = 6;
const int NUTRIGPIN = 8;
const int NUECHOPIN = 7;
long duration, distance;
long duration1, distance1;
long volume, volume1;
float percent, percent1;
//*Defining Relay PINs
const int IRRIGATIONPIN = 9;
const int FOGGERPIN = 10;
const int ROPUMPPIN = 11; //RO, Nutrient A & Nutrient B submersible pumps are connected to PIN 12
const int RELAY4 = 12;
//Defining LED PIN outs
WidgetLED ROLED(20);
WidgetLED IRRIGATIONLED(21);
WidgetLED FOGGERLED(22);
WidgetLED RAINLED(24);
WidgetLED RELAY4LED(23);
//defining RTC
WidgetRTC rtc;
void updateHum(int param);
BLYNK_WRITE(V28) {
updateHum(param.asInt()); //Reading slider value
}
char auth[] = "de60d990f6184f8191e4a56d4250d02a";
char ssid[] = "D-Link";
char pass[] = "hathway123";
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
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 15 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V29, humLowTrigger);
if(h < humLowTrigger) {
FOGGERLED.off();
digitalWrite(FOGGERPIN, LOW);
Blynk.virtualWrite(V22, 0);
} else {
FOGGERLED.on();
digitalWrite(FOGGERPIN, HIGH);
Blynk.virtualWrite(V22, 255);
}
//Serial.println(h);
//Serial.println(t);
}
void updateHum(int param) {
humLowTrigger = param;
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
// Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
// Setup a function to be called every second
pinMode(ROTRIGPIN, OUTPUT);
pinMode(ROECHOPIN, INPUT);
pinMode(NUTRIGPIN, OUTPUT);
pinMode(NUECHOPIN, INPUT);
pinMode(ROPUMPPIN, OUTPUT);
pinMode(FOGGERPIN, OUTPUT);
pinMode(IRRIGATIONPIN, OUTPUT);
pinMode(RELAY4, OUTPUT);
digitalWrite(FOGGERPIN, LOW);
int humLowTrigger = 40;
// Display digital clock every 10 seconds
timer.setInterval(1000L, clockDisplay);
timer.setInterval(1000L, rolevelSENSOR);
timer.setInterval(1030L, moistureSENSOR);
timer.setInterval(1070L, sendSensor);
rtc.begin();
dht.begin();
}
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
}
void rolevelSENSOR()
{
digitalWrite(ROTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(ROTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(ROTRIGPIN, LOW);
duration = pulseIn(ROECHOPIN, HIGH);
distance = (duration/58.2);
volume = (212823.300 - (distance*(3.14*(28.75*28.75))))/1000;
percent = (volume/200)*100;
digitalWrite(NUTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(NUTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(NUTRIGPIN, LOW);
duration1 = pulseIn(NUECHOPIN, HIGH);
distance1 = (duration1/58.2);
volume1 = (425646.600 - (2*(distance1*(3.14*(28.75*28.75)))))/1000;
percent1 = (volume1/400)*100;
if(distance1 <15 || distance >65)
{
ROLED.off();
digitalWrite(ROPUMPPIN, LOW);
}
else
{
ROLED.on();
digitalWrite(ROPUMPPIN, HIGH);
}
}
int rainSENSOR = A0;
int bedmoisturePIN = A1;
int growbagmoisture1PIN = A2;
int growbagmoisture2PIN = A3;
int waterSENSOR = A4;
void moistureSENSOR()
{
if(bedmoisturePIN < 400 || bedmoisturePIN > 1023)
{
IRRIGATIONLED.off();
digitalWrite(IRRIGATIONPIN, LOW);
}
else
{
IRRIGATIONLED.on();
digitalWrite(IRRIGATIONPIN, HIGH);
}
}
// Digital clock display of the time
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
Serial.print("Current time: ");
Serial.print(currentTime);
Serial.print(" ");
Serial.print(currentDate);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V1, currentTime);
// Send date to the App
Blynk.virtualWrite(V2, currentDate);
}
BLYNK_READ(V7)
{
Blynk.virtualWrite(7,distance);// virtualpin 7 distance
}
BLYNK_READ(V8)
{
Blynk.virtualWrite(8, percent); //virtualpin 8 percent
}
BLYNK_READ(V9)
{
Blynk.virtualWrite(9, distance1);
}
BLYNK_READ(V10)
{
Blynk.virtualWrite(10, percent1);
}
BLYNK_READ(V26)
{
Blynk.virtualWrite(26, volume);
}
BLYNK_READ(V27)
{
Blynk.virtualWrite(27, volume1);
}
//BLYNK_READ(V11)
//{
//Blynk.virtualWrite(11, ROPUMPPIN);
//}
//BLYNK_READ(V12)
//{
//Blynk.virtualWrite(12, IRRIGATIONPIN);
//}
//BLYNK_READ(V13)
//{
//Blynk.virtualWrite(13, FOGGERPIN);
//}
//BLYNK_READ(V14)
//{
//Blynk.virtualWrite(14, RELAY4);
//}
BLYNK_READ(V15)
{
Blynk.virtualWrite(15, rainSENSOR);
}
BLYNK_READ(V16)
{
Blynk.virtualWrite(16, bedmoisturePIN);
}
BLYNK_READ(V17)
{
Blynk.virtualWrite(17, growbagmoisture1PIN);
}
BLYNK_READ(V18)
{
Blynk.virtualWrite(18, growbagmoisture2PIN);
}
BLYNK_READ(V19)
{
Blynk.virtualWrite(19, waterSENSOR);
}
//BLYNK_READ(V22)
//{
//Blynk.virtualWrite(22, FOGGERPIN);
//}
//BLYNK_READ(V23)
//{
//Blynk.virtualWrite(23, RELAY4PIN);
//}
//BLYNK_READ(V24)
//{
//Blynk.virtualWrite(24, rainSENSOR);
//}
void loop()
{
Blynk.run();
timer.run();
}
Mapping table:
Pin Mapping Table
Analog/Digital Pin Virtual Pin Device Name
0
1 Water Flow sensor
2 ESP RX
3 ESP TX
4 DHT DHT11
5 1 HR-S04 Trig ROTRIGPIN
6 1 HR-S04 Echo ROECHOPIN
7 2 HR-S04 Trig NUTRIGPIN
8 2 HR-S04 Echo NUECHOPIN
9 V11 Relay1 ROPUMPPIN
10 V12 Relay2 IRRIGATIONPIN
11 V13 Relay3 FOGGERPIN
12 V14 Relay4 RELAY4
13
A0 V15 Rain Sensor rainSENSOR
A1 V16 Soil Moisture Sensor bedmoisturePIN
A2 V17 Soil Moisture Sensor growbagmoisture1PIN
A3 V18 Soil Moisture Sensor growbagmoisture2PIN
A4 V19 Water level sensor waterSENSOR
A5
Humidity V5 DHT H h
Temperature V6 DHT T t
Ultrasonic 1 Distance V7 1 Distance distance
Ultrasonic 1 Percentage V8 1 Percentage perent
Ultrasonic 2 Distance V9 2 Distance distance1
Ultrasonic 2 Percentage V10 2 Percentage percent1
RO LED V20 ROLED
Irrigation LED V21 IRRIGATIONLED
Fogger LED V22 FOGGERLED
Relay4 LED V23 RELAY4LED
Rain LED V24 RAINLED
RTC V25 rtc
Ultrasonic 1 Volume V26 RO Tank volume volume
Ultrasonic 2 Volume V27 Nutrition Tank Volume volume1
Slider Value to control Humidity V28 Humidy control variable Param
Slider value display V29 Humidy control value humLOWTrigger
Please help .