IoT Moisture sensor, thingspeak

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);
}

Pah-Dee:
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.

This seems to be a repetitive theme. I think we hear this a few times each semestor.

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);
}

I have a couple questions here

  #define timeElapsedThree  0
    if (timeElapsedThree == 0){
     Upload();
    }
    else if (timeElapsedThree < 500){
     timeElapsedThree == 0;
    }

you define the timeElapsedThree to be 0
that will have the effect of wiping out any values and make it 0

then you check to see if it is 0
if it is, you jump to upload()

then you seem to want to count to 500 . is that 500ms or 1/2 second ?

but by the time you do anything, you have spent more than 1 ms so are above 0

I am only guessing here, but

#define should be in the declaration section before you run setup()

// then

if (timeElapsedThree > 500)
timeElapsedThree = 0

would go next

// then

if timeElapsedthree <= 100
Upload()

this way, the time of your loop could occur and if you got to 1 or 13 or 63, it would be less than one loop so would then run to Upload()

// I think I would count to 5000, then upload, then re-set to 0.
// that way, every 5 minutes it uploads.

========

have you done any loop times ? I think that uploading might take more than 500ms ?
if that is the case, then this would try to upload on every loop.

and unless you have some special way of evaporating water, your soil will drift slowly over hours, not seconds.

I am more than willing to be wrong here, just asking.

===============

on naming conventions
elapsedMillis timeElapsedOne;
elapsedMillis timeElapsedTwo;
elapsedMillis timeElapsedThree;
elapsedMillis timeElapsedFour;

you get to pick any names you want. we use this to make it easier to understand long programs

elapsedMillis timeCheckSoil;
elapsedMillis timeOpenValve;
elapsedMillis timeUpload;
elapsedMillis timeOtherthing;

Hi

Thanks for your reply!

I have amended some of your changes including a few of my own have managed to get the code to respond correctly to the moisture input. However i'm having a few issues getting any data to thingspeak.

Some further help would be much appreciated!

Thanks again.

here is the code:

#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[] = "NOWTVA2575"; // Change WiFi Network name

char pass[] = "CNXYLVTCVL"; // 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);