- Joined
- Aug 29, 2021
- Messages
- 399
- Reputation
- 451
- Reaction score
- 1,997
- Points
- 0
- Currently Smoking
- Bread
As min and max values I have calibrated 610 > inside water, 1024 > dry
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Raw Value: ");
Serial.println(analogRead(A0));
delay(500);
}
Ah, that sensor...
I've written a function for it.
Try this in your code
C++:void moisture(int pin, const int AirValue, const int WaterValue, int virPin ) { int i; int value = 0; int numReadings = 5; int soilMoistureValue = 0; int soilmoisturepercent = 0; for (i = 0; i < numReadings; i++){ value = value + analogRead(pin); delay(1); } soilMoistureValue = value / numReadings; soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); Blynk.virtualWrite(virPin, soilmoisturepercent); }
Call it by adding this line in void setup()
C++:moisture(A0, 787, 446, 1); //Arduino Pin, Raw Air Value, Raw Water Value, Blynk Virtual Pin
Best to use the map function to do the calibration. The output of the sensor is reasonable linear so a map would do the trick.As min and max values I have calibrated 610 > inside water, 1024 > dry
#define BLYNK_TEMPLATE_ID "TMPLxxxx"
#define BLYNK_DEVICE_NAME "Sensor 1"
#define BLYNK_AUTH_TOKEN "xxxxx-PmFc"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ssid ";
char pass[] = "pw";
BlynkTimer timer;
void moisture(int pin, const int AirValue, const int WaterValue, int virPin ) {
int i;
int value = 0;
int numReadings = 5;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
for (i = 0; i < numReadings; i++){
value = value + analogRead(pin);
delay(1);
}
soilMoistureValue = value / numReadings;
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
Blynk.virtualWrite(virPin, soilmoisturepercent);
Serial.print(soilmoisturepercent);
delay(5000);
//Serial.print("%");
}
void runMe(){
moisture(A0, 1024, 560, V1); //Arduino Pin, Raw Air Value, Raw Water Value, Blynk Virtual Pin
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
//timer.setInterval(5000L, runMe);
runMe();
}
Sorry brother, but your code is faulty... Never ever use the loop function for anything other then Blynk.run(); and timer.run();In case som1 will need it I post the woriking code. Thanks for the help, very cool from you! ... pass the boof ... Deserved...
C++:#define BLYNK_TEMPLATE_ID "TMPLxxxx" #define BLYNK_DEVICE_NAME "Sensor 1" #define BLYNK_AUTH_TOKEN "xxxxx-PmFc" #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> char auth[] = BLYNK_AUTH_TOKEN; char ssid[] = "ssid "; char pass[] = "pw"; BlynkTimer timer; void moisture(int pin, const int AirValue, const int WaterValue, int virPin ) { int i; int value = 0; int numReadings = 5; int soilMoistureValue = 0; int soilmoisturepercent = 0; for (i = 0; i < numReadings; i++){ value = value + analogRead(pin); delay(1); } soilMoistureValue = value / numReadings; soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); Blynk.virtualWrite(virPin, soilmoisturepercent); Serial.print(soilmoisturepercent); delay(5000); //Serial.print("%"); } void runMe(){ moisture(A0, 1024, 560, V1); //Arduino Pin, Raw Air Value, Raw Water Value, Blynk Virtual Pin } void setup() { Serial.begin(9600); Blynk.begin(auth, ssid, pass); } void loop() { Blynk.run(); //timer.setInterval(5000L, runMe); runMe(); }
#define BLYNK_TEMPLATE_ID "TMPLxxxx"
#define BLYNK_DEVICE_NAME "Sensor 1"
#define BLYNK_AUTH_TOKEN "xxxxx-PmFc"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ssid ";
char pass[] = "pw";
BlynkTimer timer;
void moisture(int pin, const int AirValue, const int WaterValue, int virPin ) {
int i;
int value = 0;
int numReadings = 5;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
for (i = 0; i < numReadings; i++){
value = value + analogRead(pin);
delay(1);
}
soilMoistureValue = value / numReadings;
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
Blynk.virtualWrite(virPin, soilmoisturepercent);
Serial.print(soilmoisturepercent);
Serial.println("%");
}
void runMe(){
moisture(A0, 1024, 560, V1); //Arduino Pin, Raw Air Value, Raw Water Value, Blynk Virtual Pin
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(5000L, runMe); //This line runs 'runMe' every 5 seconds, but it has to be put in the setup funtion.
}
void loop()
{
Blynk.run();
timer.run(); //The above timer will only run if you activate the timer function in the void loop...confusing isn't it...
}
You're welcome brother.Thanks for the review, much appreciated, learn writing good code is even better
#define BLYNK_TEMPLATE_ID "xxx-xxx"
#define BLYNK_DEVICE_NAME "ESP2rele"
#define BLYNK_AUTH_TOKEN "xxx"
#define BLYNK_PRINT Serial
#define PUMP_PIN D4 // FAN RELAY
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Tony";
char pass[] = "marm1te1sterr1ble";
BlynkTimer timer;
int humidity = 0; //humidity value for actuating the relay pump
void pump(int pin, int virPin, int humidity){ //function to check and set relay on/off depending on humidity %
if(humidity>=60)
Blynk.virtualWrite(virPin, LOW);
else
Blynk.virtualWrite(virPin, HIGH);
}
void moisture(int pin, const int AirValue, const int WaterValue, int virPin ) {
int i;
int value = 0;
int numReadings = 5;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
for (i = 0; i < numReadings; i++){
value = value + analogRead(pin);
delay(1);
}
soilMoistureValue = value / numReadings;
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
Blynk.virtualWrite(virPin, soilmoisturepercent);
Serial.print(soilmoisturepercent);
Serial.println("%");
humidity = soilmoisturepercent;
}
void runMe(){
moisture(A0, 1024, 600, V1); //Arduino Pin, Raw Air Value, Raw Water Value, Blynk Virtual Pin
pump(D4, V2, humidity); //passing digital pin of esp8266, virtual pin linked + humidity % top check needed for trigger
//Serial.print(analogRead(A0)); //use for calibration
//Serial.println("<--");
}
void setup()
{
Serial.begin(9600);
//pinMode(PUMP_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(5000L, runMe); //This line runs 'runMe' every 5 seconds, but it has to be put in the setup funtion.
}
void loop()
{
Blynk.run();
timer.run(); //The above timer will only run if you activate the timer function in the void loop...confusing isn't it...
}
[ATTACH type="full" align="left"]1543651[/ATTACH]