Clint Wolf
Published

FreshSwamp

Activates the purge pump via a relay that evacuates the stale water in a swamp cooler, and simultaneously shuts down the pad pump.

BeginnerFull instructions provided12 hours6
FreshSwamp

Things used in this project

Hardware components

Argon
Particle Argon
×1
Relay (generic)
×1
SparkFun Atmospheric Sensor Breakout - BME280
SparkFun Atmospheric Sensor Breakout - BME280
×1
Otdorpatio Junction Box, Hinged Cover Stainless Steel Latch IP67 Waterproof ABS Project Box
×1

Software apps and online services

Adafruit Dashboard

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

FreshSwamp

Purges stale water from a swamp cooler.

Code

FreshSwamp

C/C++
Purges stale water from a swamp cooler.
/*
 * Project FreshSwamp
 * Description: 
 * Version 1: June 2022 - Activates a water pump via relay that evacuates the stale water in a swamp cooler
 * Version 2: September 2022 - Simultaneously shuts down pad pump when purge pump is activated, and converts the manual
 * shut off toggle to a button that works in conjunction with a timer.
 * Author: Clint Wolf
 * 
 */

#include <Particle.h>
#include <Wire.h>
#include <JsonParserGeneratorRK.h>

// Adafruit.io Set Up BEGIN
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT/Adafruit_MQTT.h"
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h"

#include <Credentials.h>

/************ Global State (you don't need to change this) ************/
TCPClient TheClient;

// Setup the MQTT client class by passing in the WiFi client, MQTT server, and login details
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY);

// For the Adafruit publishing and subscribing - Feeds 
// Refer to the feed name used on the dashboard
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>

// example: Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_On");
Adafruit_MQTT_Subscribe theWaterPumpObject = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/FreshSwampWaterPump");
Adafruit_MQTT_Publish thetemperatureObject = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/FreshSwampTemperature");

// Adafruit.io Set Up END

//===================================

#include <math.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// define bme as the I2C interface to the BME Sensor
Adafruit_BME280 theBMEObject;
bool BMEstatus;
float var_AirTemperature;

int var_ObjectValueFromSubscription;

// the below is used to publish temp data per a specific period as defined in the function call
int var_TemperatureStartTime = 0;
int var_TemperatureElapsedTime = 0;

// the below is used to operate the purge pump per a specific period as defined in the function call
int var_PurgeStartTime = 0;
int var_PurgeElapsedTime = 0;


//=============================================================================

SYSTEM_THREAD(ENABLED);

void setup() 
{
  // Put initialization like pinMode and begin functions here.
  pinMode(6,OUTPUT); // Relay

  Serial.begin(9600);
  while(!Serial);
  
  Wire.begin();

  // BME280 SECTION
    Serial.println(F("BME280 test"));   // ask the instructor about the F
    BMEstatus = theBMEObject.begin(0x76);  // we specified a hex 
    if (!BMEstatus) 
      {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(theBMEObject.sensorID(),HEX);
        Serial.println("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085");
        Serial.println("   ID of 0x56-0x58 represents a BMP 280,");
        Serial.println("        ID of 0x60 represents a BME 280.");
        Serial.println("        ID of 0x61 represents a BME 680.");
        while (1);
      }
    else 
      {
      Serial.println("BME280 Up and Running");
      }


  Serial.println("The FreshSwamp program has activated the Serial port.");

  mqtt.subscribe(&theWaterPumpObject);

  var_TemperatureStartTime = millis(); // establish snapshot of current time for temp data publishing
}

//=============================================================================

void loop() 
{
  MQTT_connect();

  Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(1000))) // wait 1 sec for new feed data
    { 
      //if (subscription == &theWaterPumpObject)
      //  { // assign new data to appropriate variable
          var_ObjectValueFromSubscription = atof((char *)theWaterPumpObject.lastread);
          Serial.print("The object value is: ");
          Serial.println(var_ObjectValueFromSubscription);
          //Serial.printf ("Received %0.2f from Adafruit .io feed FeedNameB \n",var_ObjectOneValueFromSubscription);
      //  }
    }

  if (var_ObjectValueFromSubscription == 1)
    {
      digitalWrite(D6,HIGH); // turning the PURGE pump ON, and the PAD pump off
      delay(10000); // this is the length of time the switch stays active
    }
  else // for safety, any other value other than 1 will turn pump OFF
    {
      digitalWrite(D6,LOW); // turning the PURGE pump OFF, and the PAD pump ON
    }

  var_TemperatureElapsedTime = millis() - var_TemperatureStartTime;
  
  if (var_TemperatureElapsedTime > 60000) // calling the TEMPERATURE PUBLISH function every 60 seconds
    {
      var_AirTemperature = (theBMEObject.readTemperature() * 9/5) + 32; // conversion to degrees Fahrenheit
      Serial.print("var_AirTemperature: ");
      Serial.println(var_AirTemperature);
      Serial.println("-------------------");
      PublishToDashboard(var_AirTemperature);
      var_TemperatureStartTime = millis();
    }
}

//=============================================================================

void PublishToDashboard(float theAirTemp)
{
  if(mqtt.Update())
    {
      thetemperatureObject.publish(theAirTemp);
    }
}

//=============================================================================

// this function connects and reconnects as needed to the MQTT server
void MQTT_connect()
{
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) 
  {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  while ((ret = mqtt.connect()) != 0) 
  { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT Connected!");
}

Credits

Clint Wolf

Clint Wolf

3 projects • 2 followers

Comments

Add projectSign up / Login