Some problems about my moisture sensor and gauge

I’m working on a project to control various valve with a moisture sensor,but the gauge didn’t work,and my board even can’t connect to wifi,what’s the problem?Is there any problems if I use the timer to get the data from my sensor?

P.S. my board is NodeMcu

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "e8d9a44454964958b3dd083cd40df412";

char ssid[] = "Helix";
char pass[] = "021400531";

BlynkTimer timer;

int automa = 0;
int cancel = 0;
int Valve = 0;
int a = 0;
int b = 0;
int c = 0;
int initial = 0;
int displayM = 0;
int timeID;
int interval = 0;
WidgetLED led1(V2);//this led would be turned on in auto mode
WidgetLED led2(V3);//this led would be turned on in manul mode

BLYNK_WRITE(V0)// auto mode
{
 automa = param.asInt();
 if(automa == 1)
 { timeID=timer.setInterval(interval,sensor);//get datas from the sensor//set a timer to read datas
   led1.setValue(255);
   led2.setValue(0);
  }
 }

BLYNK_WRITE(V1)// manual mode
{
  cancel = param.asInt();
  if(cancel==1)
  {led2.setValue(255);
   timer.deleteTimer(timeID);
   led1.setValue(0);
  }
  }

BLYNK_WRITE(V4)// change the interval by a slider
{
  b = param.asInt();
  interval = b*200L;
  }

BLYNK_WRITE(V5)// in manual mode,we can use a botton to send a signal to water the plant
{
 c = param.asInt();
 if(c==1&&cancel==1)
 {digitalWrite(Valve,HIGH);
 delay(1000);
 digitalWrite(Valve,LOW); 
 }
  }


BLYNK_WRITE(V9)// set a menu to select the specific vavle
{
  a=param.asInt();
  if(a==1)
  Valve=16;
  if(a==2)
  Valve=5;
  if(a==3)
  Valve=4;
  if(a==4)
  Valve=0;
  if(a==5)
  Valve=2;
  if(a==6)
  Valve=14;
  if(a==7)
  Valve=12;
  if(a==8)
  Valve=13;
  if(a==9)
  Valve=15;
  
  }
 
void sensor()
{
  initial = analogRead(A0);//get datas
  displayM = (-0.25)*initial+200;//change the data to value ranged from 0 to 100(according to my sensor)
  Blynk.virtualWrite(V6,displayM);// send the value to gauge
  if(displayM<30)
  {
   digitalWrite(Valve,HIGH);//open the valve
   Blynk.setProperty(V6,"color","#D3435C");//change gauge color to red
   }
  else
   {
    digitalWrite(Valve,LOW);//close the valve
    Blynk.setProperty(V6,"color","#04C0F8");//change gauge color to blue
   }
  }

void setup()
{
  // Debug console
  pinMode(16,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(0,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(14,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(15,OUTPUT);   

  interval = 3000L;
  Valve = 1;
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

That’s not a valid Auth Token. Have you changed it before posting?

What does your serial monitor output look like?

Pete.

Thanks for replying my question.Actually I’ve changed the token this morning because I thought a new auth may solve this problem,but the former one was quite similar to this.
Emm,I saw nothing in my serial monitor maybe because I didn’t use :

serial.println()

It looks like the Auth Token in your code above is valid, I think that I’m getting “Invalid Token” when I type this in a browser because I’m hitting a different Blynk regional server:
http://blynk-cloud.com/e8d9a44454964958b3dd083cd40df41/project

When I pop your code onto a NodeMCU, with my own Wi-Fi credentials, I get this in the serial monitor when I reset the board:

⸮txG⸮X6d⸮$⸮⸮>Y⸮[55] Connecting to [REDACTED]
[558] Connected to WiFi
[558] IP: 192.168.1.163
[558] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on NodeMCU

[625] Connecting to blynk-cloud.com:8442
[1363] Redirecting to 188.166.206.43:8080
[1365] Connecting to 188.166.206.43:8080
[2019] Ready (ping: 217ms).

Note to self - must upgrade to the latest version of the Blynk library!!

Pete.

V[quote=“Somnus, post:1, topic:24098”]
pinMode(16,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
pinMode(14,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(15,OUTPUT);
[/quote]

Just looked at your code in a bit more detail and if you have something connected to GPIO2 (Pin D4) that’s pulling it LOW at boot-up then that’s probably what’s stopping the Wi-Fi from working.

Looks like you’re using lots of other “special” pins as well, which will cause you problems.
You should read this post:

Pete.

1 Like

Thanks for replying my question.

Yeah,I think you are right.The board worked just now as I removed some parts of my sketch like

pinMode(xx,OUTPUT);

But here is another problem.When I switched to the auto mode the gauge acted strangely.

Most of the time the value showed in gauge was 200(sometimes changed to 8 and returned to 200 immediately).

Is there anything wrong with my formula :

displayM = (-0.25)*initial+200;

Or some other errors which I didn’t realise.

I suppose it depends on what you want it to do. At the moment, 200 will be added to the result of (-0.25)*initial. If you wanted it to do something else then you should add more brackets.
Are you using a breadboard? Slightly dodgy contacts on the analogue pin contacts of a breadboard could make big differences to the values that you’re seeing.

Adding a few serial.prints into the code will help you diagnose what’s happening a little better.

Pete.

Emm,you are right.There must be some interference when a breadboard connected to the vpin.

I tried to connect the sensor with my board instead of using a breadboard,and the gauge finally acted well.

Thank you very much for solving my problem.

1 Like

It’s very difficult to manage without breadboards, but I’ve lost count of how many hours of time I’ve wasted because of poor contacts producing erratic results.
It’s not as bad with digital signals, because a bit of resistance on one of the connections will often still give enough voltage top turn a digital pin on (most of the time), but with Analogue signals you’re measuring quite low voltages and you need them to be accurate. A tiny bit of resistance can cause a big difference in the readings.

I’ll mark the topic as “Solved”.

Happy Blynking

Pete.

That’s alright.Thanks.