I2c Soil Moisture Sensor (chirp) Particle Photon sends out 0 data after soft reset

Hello!

My first post, been looking for a solution to my problem for a while.

I have a non chirp version of the i2c soil moisture sensor (version 2.7.3) from Tindie. When its working, its a great sensor, really pleased with it.

1 problem I just cant get past though is, if the photon resets for whatever reason, power failure, upload of new code, sometimes if I power cycle the power supply, the sensor only ever gives out ‘0’ values for the temperature and light and ‘-1’ for the soil moisture reading.

The only way I have found to get the sensor to reset and give out correct readings is to hot plug the sensor, something the manufacturer does not recommmend.

I wonder what I am doing wrong?

Here is my dreadful code from start to finish, I started adding sensor.resetSensor(); instances all over the place out of desperation to try and kick the sensor back into life.

// This #include statement was automatically added by the Particle IDE.
#include <HC_SR04.h>
#include <I2CSoilMoistureSensor.h>
#include <Adafruit_DHT.h>

#define DHTPIN 7
#define DHTTYPE DHT11





//************************
char luminanceStr[30];
int SensRes;
int SensTemp;
int SensLight;
int averageTemp;
int temperature;
int humidity;
int waterPump = D3;
int fanHeater = D4;
int intakeFan = D6;
int exhaustFan = D5;
int moist = D7;
int airHumidity = A0;
int deHumidifier = A4;
int PH = 0;
int Hour = 0;
int Min = 0;
int Sec = 0;



I2CSoilMoistureSensor sensor;


DHT dht(DHTPIN, DHTTYPE); 

STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

void setup()
{
   Wire.begin();
   Particle.syncTime();
   sensor.begin();
   delay (2000);
   
   dht.begin();
   pinMode(fanHeater, OUTPUT);
   pinMode(intakeFan, OUTPUT);
   pinMode(exhaustFan, OUTPUT);
   pinMode(waterPump, OUTPUT);
   pinMode(A5, INPUT);
   pinMode(A0, OUTPUT);
   pinMode(deHumidifier, OUTPUT);  //dehumidifier
   
   sensor.resetSensor();
    
   delay (5000);

  
   Particle.publish("Version 4.0");
}
    
void loop()
{ 
    
    readTime();
    readPH();
    readHumidity();
    readTemp();
    readSoilMoisture();
    readLightLevel();
    highTemp();
    //lowTempBeforeMidnight();
    lowTempNight();
    highTempNight();
    lowTempDay();
     
    
}


void readTime()

{
    Particle.publish("1");
   Hour = Time.hour();
   Min = Time.minute();
   Sec = Time.second();
  // Particle.publish("Time", String(Hour), ":", String(Min), ":", String(Sec));
  
  
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    delay(2000);
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   return;
   
}

//*******************************************************************************
void readPH()
{
    Particle.publish("2");
   PH = analogRead(A5);
   Particle.publish ("PH", String(PH));
   //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   delay (2000);

    return;
}

void readHumidity()

{
   Particle.publish("3");
   humidity = dht.getHumidity();
   Particle.publish("Humidity", String(humidity));
   delay (2000);
   if (humidity < 40)
    {
        lowHumidity();
    }
   else if (humidity > 50)
    {
        highHumidity();
    }
    
    return;
    
}
//*************************************************************************
void readTemp ()
{
    Particle.publish("4");
   temperature = dht.getTempCelcius();
   Particle.publish("Canopy Temp", String(temperature));
   delay (2000);
   
   sensor.resetSensor();
    
   delay (5000);
   
   SensTemp = sensor.getTemperature()/10;
   Particle.publish("Ground Temp", String(SensTemp));
   delay (2000);
   averageTemp = (SensTemp + temperature)/2;
   Particle.publish("Average Temp", String(averageTemp));
   return;
   
}
//*********************************************************************** 



void lowHumidity()
{
    Particle.publish("5");
    if (humidity < 40)

    {
    digitalWrite (airHumidity, LOW);
    delay (2000);
    humidity = dht.getHumidity();
    Particle.publish("Humidifying!", String(humidity));
    //}
    }
else
    {
         
         digitalWrite(airHumidity, LOW);
    }
  delay(2000);
  
  return;
 
}
//**************************************************************** 
 
void highHumidity()
{
    Particle.publish("6");
  if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("Dehumidifying!", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    delay(2000);

    return;
}

//************************************************************

void highTemp()
{
    Particle.publish("7");
    readTemp2(); 
 
   if (temperature > 28)
    {
     digitalWrite (intakeFan, HIGH);
     digitalWrite (exhaustFan, HIGH);
     Particle.publish ("Cooling", "On");
     readTemp2();
    }
  else
    {
     digitalWrite(intakeFan, LOW);
     digitalWrite(exhaustFan, LOW);
    }

     //delay (2000);
     
     
     //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     return;
}

//*************************************************************************

 void lowTempBeforeMidnight()
{
    Particle.publish("8");
    readTemp2();     
 
    if(Hour > 8)
    {
        if(Hour > 20)
            {
                if (averageTemp < 16)
                {
                    digitalWrite (fanHeater, HIGH);
                    Particle.publish ("Heating!");
                   readTemp2();
                }
            }
    }
    
    
     else
    {
        digitalWrite(fanHeater, LOW);
        Particle.publish ("Heating Off!");
    }
 
 
    delay (2000);
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    return;
}
 
//**************************************************************************** 
 
void lowTempNight()
    {
        Particle.publish("9");

 readTemp2();


 if(Hour > 8)
    {
        if(Hour > 20)
            {
                if (averageTemp < 16)
                {
                    digitalWrite (fanHeater, HIGH);
                    Particle.publish ("Heating!");
                    readTemp2();
                }
            }
    }
     
 else if(Hour < 8)
{
     
            if(Hour > 20)
        {
            if (averageTemp < 16)
                {
                    digitalWrite (fanHeater, HIGH);
                    Particle.publish ("Heating!");
                   readTemp2();
                }   
        }
}
    else
{
    digitalWrite(fanHeater, LOW);
    Particle.publish ("Heating Off!");
         
}
    delay (2000);
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    return;

}

void highTempNight()
    {
        Particle.publish("9a");

 readTemp2();


 if(Hour > 8)
    {
        if(Hour > 20)
            {
                if (averageTemp > 17)
                {
                    digitalWrite (fanHeater, LOW);
                    Particle.publish ("Heating Off");
                    readTemp2();
                }
            }
    }
     
 else if(Hour < 8)
{
     
            if(Hour > 20)
        {
            if (averageTemp > 17)
                {
                    digitalWrite (fanHeater, HIGH);
                    Particle.publish ("Heating!");
                    readTemp2();
                }   
        }
}
    else
{
    digitalWrite(fanHeater, LOW);
    Particle.publish ("Heating Off!");
         
}
    delay (2000);
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    return;

}

//*****************************************************************************

void lowTempDay()

{
    Particle.publish("10");
    if(Hour > 8)
        {
            if(Hour < 20)
            {
                if (averageTemp < 25)
                    {
                        digitalWrite (fanHeater, HIGH);
                        Particle.publish ("Heating!");
                        readTemp();
                    }
            }
        }
    else
        {
            digitalWrite(fanHeater, LOW);
            Particle.publish ("Heating Off!");
         
        }
    delay (2000);
    
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    return;
  
}
//***************************************************************************
 
void readSoilMoisture()
{

Particle.publish("11");

    sensor.resetSensor();
    
    delay (5000);
    
    while (sensor.isBusy())
            {   
                 delay (50); 
            }
    SensRes = sensor.getCapacitance();
    sprintf(luminanceStr,"%d", SensRes);
    Particle.publish("Soil Moisture", luminanceStr); 
    
            if (SensRes <= -50)
                {     
                    while (SensRes < 700)
                        {       digitalWrite (waterPump, HIGH);
                                Particle.publish("Watering!");
                        }
     
                                delay (2000);
                                
                    sensor.resetSensor();
    
                    delay (5000);
                                
                    SensRes = sensor.getCapacitance();
                    sprintf(luminanceStr,"%d", SensRes);
                    Particle.publish("Watering!", luminanceStr);
                }
            else
                {
                    digitalWrite (waterPump,LOW);
                }
            delay(2000);
            
            
            //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    return;
            
}

//******************************************************************


  // while (sensor.isBusy()) delay(50); 
   //SensTemp = sensor.getTemperature()/10;
  // Particle.publish ("Temperature", (String) SensTemp); 
  // delay (2000);

//******************************************************************

void readLightLevel()
{
   Particle.publish("12");
   
   sensor.resetSensor();
   
   delay (5000);
   
    while (sensor.isBusy()) 
        {
            delay(50);
        }
        
   SensLight = sensor.getLight(true);
   delay (9000);
   
    if (SensLight < 10000)
        {
            Particle.publish ("Lights On");
        }
    else
        {
            Particle.publish ("Lights Off");
        }
   
   delay (2000);
   sensor.resetSensor();
   delay (2000);
   
   //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if (humidity > 50)
    {
    digitalWrite (deHumidifier, LOW);
    digitalWrite (exhaustFan, HIGH);
    digitalWrite (airHumidity, LOW);
    delay (5000);
    humidity = dht.getHumidity();
    Particle.publish("**Dehumidifying! ***", String(humidity));
    }
  else
    {
     digitalWrite(deHumidifier, LOW);
     digitalWrite(exhaustFan, LOW);
       
    }
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
   return;
   
  
}

//***************************************************************************

void readTemp2 () //Does not publish temps.
{
    Particle.publish("4");
   temperature = dht.getTempCelcius();
   //Particle.publish("Canopy Temp", String(temperature));
   delay (2000);
   
   sensor.resetSensor();
    
   delay (5000);
   
   SensTemp = sensor.getTemperature()/10;
   //Particle.publish("Ground Temp", String(SensTemp));
   delay (2000);
   averageTemp = (SensTemp + temperature)/2;
  // Particle.publish("Average Temp", String(averageTemp));
   return;
   
}

Apologies if I have uploaded too much code, or have missed some vital info, I would be most grateful for your help and if any more info is required, please let me know.

Thanks.

How have you powered the sensor?
What other devices are you running off the Photon’s power?
What does the RGB LED do prior, during and after the unexpected reset?
Got a video (>30sec)?

Some hints:
You should use PRIVATE scope for your Particle.publish() calls and avoid String where possible.
You still got a hardcoded analogRead(A5) - consider using a pin variable as you’ve done with most/all your other pins (these chould (should?) be const int).
And there are quite some reoccuring code blocks that could be broken out into dedicated functions.
On the other hand some of your dedicated functions may be unified into a generalised function which can possibly act on a struct that may hold a set of pin states and time frames.

@tabletmctablet,

I also use the Chirp soil moisture sensor. I have documented the project here which you may find useful:

Also, if you want to see the code, the Github Repo is here:

Of note, I am using a different library for the Chirp which I found worked quite well. You may want to give it a try along with @ScruffR’s suggestions.

Thanks,

Chip

1 Like

Hi Scruff,

Thank you for the speedy reply. I think you are correct on the power, Ive got my photon mounted in an old shield shield, and have about 6/7 5v relays, 1 soil moisture sensor & a dht11 all hanging off the particle rather than the 12v incoming power supply, Ive been watching the sensor tonight and it has been failing without the particle resetting. Thank you for the PRIVATE tip, I havent read about it yet, but will implement, the reoccuring humidity code was there because all the functions seem to run until the condition is met except the humidity one for some reason. I will read about unifying my functions, because I suspect that is the solution that you are pointing me in the direction of.

Thank you!

1 Like

Hi Chip!

Yes, I am aware of your amazing watering system, it kind of started me off. Im trying to control the entire indoor climate at the mo I think my poor at best coding knowledge is holding me back. I will take a fresh look at your code and see if that library helps me out, I think @ScruffR’s thought about my power requirements are the path im going down right now… Thanks for your help, really appreaciate any help my poor brain can get when it comes to coding!

:slight_smile:

@tabletmctablet,

Glad you found this project useful. Happy to take another look when you think the time is right.

Good luck!

Chip

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.