Project Issue : powering water pump after http request

Hello everybody,

I wanna thanks you all for sharing helpful solutions on this forum.

A bit of description is needed before start my explanation :

We are a team of 5 people working on a project : a connected flower pot
so we bought a bit of componant :

  • Arduino YUN
  • Light sensor
  • Moisture sensor
  • Temperature sensor
  • Water Pump
  • Wired cables
  • Tiny Breadboard

Here is a schema of our prototype :

Please watch the attached picture.

And here is the arduino code we have build :

#include <Bridge.h>
#include <DHT.h>

// ################ Define Var ###################

#define DHTPIN 2            // The digital pin used for DHT data. 
#define DHTTYPE DHT22       // Define the type of DHT used (11, 22).  In this case, the DHT-22

DHT dht(DHTPIN, DHTTYPE);

// ############### Setup var for the code ###################

int ledpump = 3; // pin led for the pump
int ledlight = 8; // pin led for the sun light
int pinlight = 0; // pin for light sensor
int pinmois = 2; // pin moistuse sensor
int lederror = 7; // pin led for network error

char c; // WebClient

float photoReading; // var lumem in percent
float moistReading; // var moisture in percent calibrated
float tmpmoistReading; // var tmp moisture in percent
float tempReading; // var temperature in celcius

String macAddr = "";
String concatdata; // Concat bloc
String urltoget; // The url to get with HttpClient
String readingString; // string for data get anwser
String repwater = "{\"action\":\"water\"}"; // rep code for water pump action
String replight = "{\"action\":\"light\"}"; // rep code for sun light action
String repboth = "{\"action\":\"both\"}"; // // rep code for water pump and sun light action
String repok = "{\"action\":\"ok\"}"; // rep code for nothing to do
String urlstart  = "http://Our_Website.com/api/process/information.json?id_mac=";


// ############### Setup function #################

void setup() {
  Bridge.begin(); // start bridge
  dht.begin(); // Start DHT
  pinMode(ledpump, OUTPUT); // init ledpump as OUTPUT
  pinMode(ledlight, OUTPUT); // init ledlight as OUTPUT
  pinMode(lederror, OUTPUT); // init lederror as OUTPUT 
  digitalWrite(lederror, HIGH);
  while(macAddr.equals("")){
    macAddr = getMacAddr();
  }
}


// ############### Main function ##################

void loop() {

  // ############### Sensor Reading Data ####################
  
  tempReading = dht.readTemperature(); // Read temp with DHT Librairy
  // h = dht.readHumidity(); // Read humidity with DHT Librairy

  photoReading = (analogRead(pinlight) * 100.00 / 750.00); // Lumen value to send API

  tmpmoistReading = (analogRead(pinmois) * 100 / 500); // Moisture value to send API
  
  if(tmpmoistReading < 5){
    moistReading = tmpmoistReading;
  } else {
    moistReading = tmpmoistReading - 5.00;
  }   

  // ############### Http Get to API #####################

  // Initialize the client library
  readingString = "";
  urltoget = "";
  concatdata = "";

  concatdata = urlstart;
  concatdata.concat(macAddr);
  concatdata.concat("&temp=");
  concatdata.concat(tempReading);
  concatdata.concat("&moisture=");
  concatdata.concat(moistReading);
  concatdata.concat("&lumen=");
  concatdata.concat(photoReading);
  urltoget = concatdata;


  Process webp; // Create a process
  webp.begin("curl");  // Process that launch the "curl" command
  webp.addParameter(urltoget); // Add the URL parameter to "curl"
  webp.run(); // Run the process and wait for its termination

  while (webp.available()>0) {
    char c = webp.read();
    readingString += c;
  }  

  // ############### Process action ##################
  
  if(readingString.equals("")){
    disableAll();
  } else {
    digitalWrite(lederror, LOW); 
    action(readingString); // Use action function to make action over arduino pump & light
  }


}


void action(String returnget){

  if(returnget.equals(repwater)){

    // Activate ledpump
    digitalWrite(ledpump, HIGH);
    delay(10000);

    digitalWrite(ledpump, LOW);
    delay(2000);        

  } else if(returnget.equals(replight)){

    // Activate ledlight
    digitalWrite(ledlight, HIGH);
    delay(10000);

    digitalWrite(ledlight, LOW);
    delay(2000);            


  } else if(returnget.equals(repboth)){

    // Activate ledpump and ledlight
    digitalWrite(ledpump, HIGH);
    digitalWrite(ledlight, HIGH);
    delay(10000);

    digitalWrite(ledpump, LOW);
    digitalWrite(ledlight, LOW);
    delay(2000);      

  } else if(returnget.equals(repok)){

    // Nothing to do
    delay(12000);

  } else {

    // Arduino can't reach webserver
    digitalWrite(lederror, HIGH);
    delay(1000);
    digitalWrite(lederror, LOW);
    delay(1000);                  
    digitalWrite(lederror, HIGH);              

  }
  
} 

void disableAll(){
  
   // Shutdown ledpump and ledlight
  digitalWrite(ledpump, LOW);
  digitalWrite(ledlight, LOW);
  digitalWrite(lederror, HIGH);

  // reset network ethernet card  
  Process ethp;
  ethp.begin("ifup");
  ethp.addParameter("wan");
  ethp.run();
  
  delay(3000);
   
  // reset network wifi card
  Process wifp;
  wifp.begin("ifup");
  wifp.addParameter("lan");
  wifp.run();  
  
  delay(3000);
  

}


String getMacAddr(){
  // Function to get Mac addresse using bridge librairy

  String command = "ifconfig eth0 | grep HWaddr | tr -s ' '| cut -d' ' -f1,5 | cut -d' ' -f2";
  String macAddr = "";
  Process macp; 
  macp.runShellCommand(command);

  while (macp.available()>0) {
    char c = macp.read();
    if(c != '\n'){
      macAddr += c;
    }
  }

  return macAddr;

}

Let’s talk about the issue :

When the pump need to be powered for repwater or repboth, the pump is powered and it will be shut after 10 seconds
After the action method, the code restart the loop from the beginning.
BUT :

  • If the pump has been powered, the HTTP request part code doesn’t work on the next loop
  • If the pump hasn’t been powerd, the HTTP request part code WORKS !!

So we don’t understand why the HTTP request doesn’t work only when the pump has been powered.
And we don’t know what’s going wrong.
Is it :

  • Our circuit ?
  • Our code ?
  • One of our componant ?

If anyone get any idea about this issue, i will thank you so much.
Sorry for grammar I’m french.

Waiting for an answer …

It's the delays in your action function. While your action function is running you're not paying attention to any HTML requests.