Hi
So I need some guidance on a group project. Just to clarify I'm more of the hardware guy, I set up the circuitry and did all of the soldering for the plumbing and the brazing for the enclosure. Our programmer dropped out with a week to go with some semi-working code.
So we have a soil moisture sensor connected up to a nodemcu esp2866 to its analogue input (pin 17 in the IDE) which then outputs to a relay via digital output 4 (pin 2 in the IDE) which then is connected to a solenoid valve. The valve is naturally closed so we need the solenoid valve to turn on when moisture is detected for a period of time, pause to allow water to flow around the plant pot, then if still dry water if wet stop (at any point during the cycle hence using timers in the code apposed to delays). Then simultaneously we need the nodemcu to send information to thingspeak with analytics. I have attached the code and some help would be greatly appreciated.
Thank you.
As of now, the program stops when moisture is detected which we want. But when the valve opens when the system is dry it just sends out a random pulse rather than just opening for a set period of time.
#include <elapsedMillis.h>
elapsedMillis timeElapsedOne;
elapsedMillis timeElapsedTwo;
elapsedMillis timeElapsedThree;
elapsedMillis timeElapsedFour;
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey;
String tsfield1Name;
String request_string;
char ssid[] = "VM1633133"; // Change WiFi Network name
char pass[] = "mg7Jtqrvdvty"; // Change Password
HTTPClient http;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(17,INPUT);
pinMode(2,OUTPUT);
digitalWrite(2,LOW);
#define timeElapsedFour 0
WiFi.disconnect();
WiFi.begin(ssid, pass);
while (WiFi.status() == WL_CONNECTED){
while (timeElapsedFour > 300){
}
}
timeElapsedFour == 0;
}
void loop() {
#define a 17
double CurrentLevel = WaterLevel()*100;
Serial.print("Current Moisture:" );
Serial.print(CurrentLevel);
if (CurrentLevel > 0.6){
Watering();
}
#define timeElapsedThree 0
if (timeElapsedThree == 0){
Upload();
}
else if (timeElapsedThree < 500){
timeElapsedThree == 0;
}
}
void Watering(){
double x = WaterLevel();
if(x > 0.7){
digitalWrite(2,HIGH);
x = WaterLevel();
}
else{
digitalWrite(2,LOW);
if(timeElapsedOne < 3000){
timeElapsedTwo = 0;
}
else{
digitalWrite(2,HIGH);
if(timeElapsedTwo < 3000){
}
else{
timeElapsedOne = 0;
timeElapsedTwo = 0;
}
}
}
}
void Upload(){
if (client.connect("api.thingspeak.com",80)) {
request_string = thingSpeakAddress;
request_string += "key=";
request_string += "8VWZHPCFAO9W96X7";
request_string += "&";
request_string += "field1";
request_string += "=";
request_string += (int(((analogRead(17))/764)*100));
http.begin(request_string);
http.GET();
http.end();
}
}
double WaterLevel(){
return 1 - (analogRead(17)/764);
}