Esp8266... Blynk Water level sensor

Hello all, this is my first project ever and I have a esp8266 with wifi and I copied the terracontrol v3 project on instructibles and everything works lovely other then my water level sensor, it’s a conductive sensor… a flat pcb with 10 metal lines on it, no matter what I do I cant seem to get the sensor to work, it will not output any readings. The sensor is connected to pin A0 and v+/ground respectively, any help would be greatly appreciated

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

/*------------------ LIBRARIES ------------------*/
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>

/*------------------ YOUR WIFI AND BLYNK SETTINGS ------------------*/
const char ssid[] = "";
const char pass[] = "";
char auth[] = "";
/*----------------- DEFINE WATER LEVEL SENSOR ----------------*/
#define SENSOR_IN A0
/*------------------ DEFINE DHT SENSOR PIN ------------------*/
#define SENSOR_IN D6
#define SENSOR_IN_Type DHT22
/*------------------ DEFINE RELAY PINS ------------------*/
#define relayLight D1
#define relayHeat D2
#define relayFan D9 //RX pin on NodeMCU 
#define relayHeat2 D5

/*---------- OTHER ----------*/
float humIN, tempIN;
DHT dht_IN(SENSOR_IN, SENSOR_IN_Type);
SimpleTimer timer;

/*---------- SYNC ALL SETTINGS ON BOOT UP ----------*/
bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}
/*---------- LIGHT CONTROL ----------*/
BLYNK_WRITE(V10)
{
  if (param.asInt()) {
    digitalWrite(D1, HIGH);
    Blynk.virtualWrite(V0, 255);
  } else {
    digitalWrite(D1, LOW);
    Blynk.virtualWrite(V0, 0);
  }
}
/*--------------WATER LEVEL--------*/
BLYNK_WRITE(V20) 
 {
 analogRead(A0); // read input value
  
int sensorValue = analogRead (A0);
  Blynk.virtualWrite(V20, 0); //sending to Blynk
  Serial.print("Sensor = A0 ");
  }

/*---------- HEAT CONTROL ----------*/
BLYNK_WRITE(V11)
{
  if (param.asInt()) {
    digitalWrite(D2, HIGH);
    Blynk.virtualWrite(V1, 255);
  } else {
    digitalWrite(D2, LOW);
    Blynk.virtualWrite(V1, 0);
  }
}
/*---------- HEAT2 CONTROL ----------*/
BLYNK_WRITE(V12)
{
  if (param.asInt()) {
    digitalWrite(D5, HIGH);
    Blynk.virtualWrite(V2, 255);
  } else {
    digitalWrite(D5, LOW);
    Blynk.virtualWrite(V2, 0);
  }
}
/*---------- FAN CONTROL ----------*/
BLYNK_WRITE(V13)
{
  if (param.asInt()) {
    digitalWrite(D9, HIGH);
    Blynk.virtualWrite(V3, 255);
  } else {
    digitalWrite(D9, LOW);
    Blynk.virtualWrite(V3, 0);
  }
}
/*----------READ DHT SENSOR ----------*/
void sensorRead() {
  tempIN = dht_IN.readTemperature();
  humIN = dht_IN.readHumidity();
  Blynk.virtualWrite(V8, tempIN);
  Blynk.virtualWrite(V9, humIN);
}

void setup()
{
  Blynk.begin(auth, ssid, pass);
  Blynk.notify("Terrarium: Successfully connected to: " + String(ssid));
  pinMode(relayLight, OUTPUT);
  pinMode(relayHeat, OUTPUT);
  pinMode(relayFan, OUTPUT);
  pinMode(relayHeat2, OUTPUT);
  timer.setInterval(1000L, sensorRead);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void loop()

I don’t understand why you’re doing your analogRead in the BLYNK_WRITE(V20) callback rather than in your sensorRead function.

Pete.

Hello Peter, me either, this is the first time I have done anything like this, the project as a whole was a copy of a diy, but I am trying to the water level sensor into the project and I was trying to copy/paste the existing code and change the perimeters and I realize that is why the sensor does not seem to have any output, I have tried adjusting the code since last night with no avail… I will continue to look for a similar project and adjust/copy/paste till I get it to function

I guess that the V20 widget is meant to be configured with a Refresh Interval, which is meant to trigger the readings - which may be why it’s not working for you. But that’s a very poor approach to triggering the readings, especially when your code already includes a timed function to take readings.

Pete.

Thanks Pete, so how would I go about assigning the level indicator to pin A0 and reading on blink, I don’t expect you to write the code for me but where would I look to start with?, This project is for a pet lizard and this is the last piece of the puzzle and as I stated, this is my first time programming anything or assembling anything like this.

Thank you
Nelson

Not sure what you mean by this. You’ve said that the sensor is already connected to A0

Put this code in your sensorRead function…

int sensorValue = analogRead (A0); 
Blynk.virtualWrite(V20, sensorValue); //sending to Blynk 
Serial.print("Sensor A0 = ");
Serial.println(sensorValue);

Pete.

Thank you Pete, I now get a ‘blynk’ does name a tye message when I try to compile the code with arduino ide. Below is the error copy/paste, I am assuming I now have a library error?

Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

TerraControl_v3.0_INSTRUCTABLES:47:1: error: 'Blynk' does not name a type

 Blynk.virtualWrite(V20, 255); //sending to Blynk

 ^

TerraControl_v3.0_INSTRUCTABLES:48:1: error: 'Serial' does not name a type

 Serial.print("Sensor A0 = ");

 ^

TerraControl_v3.0_INSTRUCTABLES:49:1: error: 'Serial' does not name a type

 Serial.println(sensorValue);

 ^

TerraControl_v3.0_INSTRUCTABLES:50:1: error: expected declaration before '}' token

 }

 ^

exit status 1
'Blynk' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I think you need to post your full code.

Pete.

Not sure what happened but it did compile and upload but still no activity on the sensor, Pete I am sure there are tons of “dumb” questions from newbies but I truly do appreciate the help you are providing

Thank you
Nelson

/*------------------ LIBRARIES ------------------*/
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_Sensor.h>
/*------------------ YOUR WIFI AND BLYNK SETTINGS ------------------*/
const char ssid[] = "";
const char pass[] = "";
char auth[] = "";
/*----------------- DEFINE WATER LEVEL SENSOR ----------------*/
#define SENSOR_IN A0
/*------------------ DEFINE DHT SENSOR PIN ------------------*/
#define SENSOR_IN D6
#define SENSOR_IN_Type DHT22
/*------------------ DEFINE RELAY PINS ------------------*/
#define relayLight D1
#define relayHeat D2
#define relayFan D9 //RX pin on NodeMCU 
#define relayHeat2 D5

/*---------- OTHER ----------*/
float humIN, tempIN;
DHT dht_IN(SENSOR_IN, SENSOR_IN_Type);
SimpleTimer timer;

/*---------- SYNC ALL SETTINGS ON BOOT UP ----------*/
bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}
/*---------- LIGHT CONTROL ----------*/
BLYNK_WRITE(V10)
{
  if (param.asInt()) {
    digitalWrite(D1, HIGH);
    Blynk.virtualWrite(V0, 255);
  } else {
    digitalWrite(D1, LOW);
    Blynk.virtualWrite(V0, 0);
  }
}
/*----------------- WATER LEVEL SENSOR ----------------*/
BLYNK_WRITE(V20)
{
  int sensorValue = analogRead (A0);
  Blynk.virtualWrite(V20, sensorValue); //sending to Blynk
  Serial.print("Sensor A0 = ");
  Serial.println(sensorValue);
}


/*---------- HEAT CONTROL ----------*/
BLYNK_WRITE(V11)
{
  if (param.asInt()) {
    digitalWrite(D2, HIGH);
    Blynk.virtualWrite(V1, 255);
  } else {
    digitalWrite(D2, LOW);
    Blynk.virtualWrite(V1, 0);
  }
}
/*---------- HEAT2 CONTROL ----------*/
BLYNK_WRITE(V12)
{
  if (param.asInt()) {
    digitalWrite(D5, HIGH);
    Blynk.virtualWrite(V2, 255);
  } else {
    digitalWrite(D5, LOW);
    Blynk.virtualWrite(V2, 0);
  }
}
/*---------- FAN CONTROL ----------*/
BLYNK_WRITE(V13)
{
  if (param.asInt()) {
    digitalWrite(D9, HIGH);
    Blynk.virtualWrite(V3, 255);
  } else {
    digitalWrite(D9, LOW);
    Blynk.virtualWrite(V3, 0);
  }
}

/*----------READ DHT SENSOR ----------*/
void sensorRead() {
  tempIN = dht_IN.readTemperature();
  humIN = dht_IN.readHumidity();
  Blynk.virtualWrite(V8, tempIN);
  Blynk.virtualWrite(V9, humIN);
}

void setup()
{
  Blynk.begin(auth, ssid, pass);
  Blynk.notify("Terrarium: Successfully connected to: " + String(ssid));
  pinMode(relayLight, OUTPUT);
  pinMode(relayHeat, OUTPUT);
  pinMode(relayFan, OUTPUT);
  pinMode(relayHeat2, OUTPUT);
  timer.setInterval(1000L, sensorRead);
}

void loop()
{
  Blynk.run();
  timer.run();
}

You don’t appear to have added my code in here as I suggested.

Pete.

I have put the code you suggested in, it’s directly under “/----------------- WATER LEVEL SENSOR ----------------/” , clearly I have no idea what I am doing, I am guessing I placed the code in the wrong place?

I’ve already said that I do t understand why the code to read the analog sensor is in the BLYNK_WRITE(V20) callback function, and that you should add the code I provided in to your sensorRead function.

Paste the code I provided into your sensorRead function, after this line of code Blynk.virtualWrite(V9, humIN); but before the closing curly bracket }

Pete.

Thank you so much Pete, that resolved it, should I remove the whole “/ ----------------- WATER LEVEL SENSOR ---------------- /” code above? and I can’t thank you enough for this, now I have to see if I can get an lcd to provide live feeds at the terrarium itself lol

1 Like

Yes.

Just be careful that when you add a physical LCD you don’t dump all the LCD code in the void loop. Read this…

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

You’re welcome! :smiley:

Pete.