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();
}
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 :
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:
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.
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.