aryounker
Published © GPL3+

Nano 33 IoT now monitors my DFC2 Pump Controller

Put back yard sump pump on the internet.

IntermediateWork in progress255
Nano 33 IoT now monitors my DFC2 Pump Controller

Things used in this project

Hardware components

DFC2 Pump Controller
×1
Arduino Nano 33 IoT
Arduino Nano 33 IoT
×1
Opto-Isolator
Opto-Isolator
×1
Resistor 4000 ohm
×1
similar Arduino Uno enclosure
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Schematics

Simple Connection

Power and relay coil voltage to Nano 33 IoT

Code

Edgent code for Sump Pump Monitor

Arduino
Note - Blynk Edgent on Arduino consists of 1 file of code along with 9 header files. What you see here is only the code file. The header files are unmodified except as needed to suit your specific board.
/*
 * based on Edgent_MKR1010 example
 * 
 * Required libraries:
 *  - WiFiNINA
 *  - ArduinoOTA
 *  - ArduinoHttpClient
 *  - Timer5
 *  
 * Please also update the WiFi module firmware, if needed
 */

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID           "TMPLH6IlbWIu"
#define BLYNK_DEVICE_NAME           "Sump Pump Monitor"

#define BLYNK_FIRMWARE_VERSION        "0.1.1"

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#define APP_DEBUG

#include "BlynkEdgent.h"

// my declarations
FlashStorage ( pumpRunHours, int );
// note - following uses a prototype version of the LSM6DS9
//  library available from here only
//  https://github.com/iLikePieTM/Arduino_LSM6DS3/archive/master.zip
//  (the standard Arduino_LMS6DS3 library does NOT include temperature)
#include <Arduino_LSM6DS3.h>

BlynkTimer timer;

#define pumpPin  8
// measured pumping rate for my pump is...
#define galPerSec 0.68
int upTimeHours = 0;
bool pumpRunning = false;
float pumpedGalsFloat = 0.0;
int pumpedGallons = 0;
int gallonsStart = 0;
int gallonsNow = 0;
bool pumpOnEventSent=false;
bool pumpOffEventSent=true;
float onBoardTemperature = 0.0;

// setup flash storage of gallons as a real number
FlashStorage ( pumpFlashStore, float);

// This function gets run at every 1 second timer event
void OneSecTimerFunction()
{
  // update what pump is doing every second
  /*
   * The DFC2 relay that powers the pump has a 12vdc coil.  I tap into
   * coil voltage and bring it to my add-on board where the 12 volts 
   * goes into LED side of an opto-isolator through an about 4K ohm resistor
   * to limit LED current.  Relay PULLIN voltage thus causes the opto-
   * isolator output to turn ON pulling down the voltage at Arduino input
   * pin D8.  Otherwise, D8 us pulled up using builtin pullup resistor.
   * Thus   pump ON   =  D8 pulled LOW   =  0
   *        pump OFF  =  D8 pulled HIGH  =  1
   */
  if (digitalRead(pumpPin) == HIGH) {
    pumpRunning = false;
  }
  else
  {
    pumpRunning = true;
  }
  if (pumpRunning == true) {
    // add one more second worth of pumping
    pumpedGalsFloat += galPerSec;
    // and round it off to an integer
    pumpedGallons = pumpedGalsFloat + 0.5;
  }  
  // now check if we need to cause any "Events"
  // if "pumpRunning" is true, then cause event
  if ((pumpRunning==true) && (pumpOnEventSent==false)) {
    gallonsStart = pumpedGallons;
    Blynk.logEvent ("pump_on");
    pumpOnEventSent=true;
    pumpOffEventSent=false;
    Serial.println ("pump Started");
  }
  if ((pumpRunning==false) && (pumpOffEventSent==false)) {
    // pump finished, lets compute gallons pumped this cycle
    gallonsNow = pumpedGallons - gallonsStart;
    Blynk.logEvent ("pump_off", String("Completed, pumped ")+gallonsNow+" gallons");
    pumpOffEventSent=true;
    pumpOnEventSent=false;
    // this is the only time gallons is updated so
    //  save it to flash storage
    pumpFlashStore.write(pumpedGalsFloat);
    Blynk.virtualWrite (V1, pumpedGallons);
    Serial.println ("pump Stopped");
  }
}

// This function gets run once per hour (no need for more frequently)
void HourlyTimerFunction()
{
  // display uptime in hours
  upTimeHours ++ ;
  Blynk.virtualWrite(V0, upTimeHours);
  Serial.print ("uptime=");
  Serial.print (upTimeHours);

  // display number of gallons
  Blynk.virtualWrite (V1, pumpedGallons);
  Serial.print ("   gallons=");
  Serial.print (pumpedGallons);

  // update on-board temperature
  if (IMU.temperatureAvailable()) {
    IMU.readTemperature(onBoardTemperature);
  } 
  else 
  {
    onBoardTemperature = 0.0;
  }
  // and pass it out to blynk data stream
  // as an integer rounded from a float
  Blynk.virtualWrite (V2, (onBoardTemperature + 0.5));
  Serial.print ("  temp=");
  Serial.println (onBoardTemperature);
}

void setup()
{
  Serial.begin(115200);
  delay(2000);

  // Setup the digital input
  pinMode (pumpPin, INPUT_PULLUP);

  BlynkEdgent.begin();

  // Set timer to be occur every second, ie 1000ms
  //  use "L" suffix to insure it is a "long" integer
  //  function timerFunctionA will be run
  timer.setInterval(1000L, OneSecTimerFunction);
  // Also an hourly timer for datastreams V0 V1 V2
  timer.setInterval ((1000L*60*60), HourlyTimerFunction);

  // get LSM9DS3 library setup so we can access on-board 
  //  temperature condition courtesy of the LSM6DS9
  IMU.begin();

  // retrieve gallons pumped from flash so we have some
  //  continuity over restart
  pumpedGalsFloat = pumpFlashStore.read();
  pumpedGallons = pumpedGalsFloat + 0.5;
  
  // initialize virtual data items
  //  otherwise they will continue to hold their previous
  //  value until 1st hourly update
  Blynk.virtualWrite (V0,upTimeHours);
  Blynk.virtualWrite (V1,pumpedGallons);
  // we can get on-board temperature now
  if (IMU.temperatureAvailable()) {
    IMU.readTemperature(onBoardTemperature);
  } 
  else 
  {
    onBoardTemperature = 0.0;
  }
  // and pass it out to blynk data stream
  // as an integer rounded from a float
  Blynk.virtualWrite (V2, (onBoardTemperature + 0.5));

  Serial.print ("installed firmware version ");
  Serial.println (BLYNK_FIRMWARE_VERSION);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}

Credits

aryounker

aryounker

0 projects • 0 followers

Comments

Add projectSign up / Login