Smart Tank - using Photon

I’ve decided to make smart tank to avoid controlling process of filling tank - 'cause it make flood around if to forget to close water =)
hardware:

  • particle photon
  • hc-sr04
  • relay
  • power adaptors
  • leds
  • electric valve



code:

STARTUP(WiFi.selectAntenna(ANT_AUTO));
#include <OneWire.h>
#include <spark-dallas-temperature.h>
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
float t;

#include <ThingSpeak.h>
TCPClient client;                               
unsigned long myChannelNumber = XXXXXXX;          
const char * myWriteAPIKey = "mykey";
const char* server = "api.thingspeak.com";   

// PINs
 int led_r=0;                        // LED on D0 is Red
 int led_y=1;                        // LED on D1 is Yellow
 int led_g=2;                        // LED on D2 is Green
 const uint8_t RELAY_1_PIN=3;        // PIN on D3 if for Relay
// #define ONE_WIRE_BUS D4           // PIN on D4 is sensor DS18B20 - mentioned previosly - here for information only 
 int echoPin = 5;                    // sonic -  echo on D5 
 int trigPin = 6;                    // sonic -  trig on D6
 int led =7;                         // LED on D7 is Blue - build-in

// for keeping data
 char st; 
 char* status_current; char* status_old; // for keeping status of Tank - empty, fillin, full, fillout, error
 unsigned long lastmillis = 0;           // for tracking sending temperature and moistute data
 int duration, distance;                 // for sonic

void setup() {
 Serial.begin(9600);
 ThingSpeak.begin(client);           // start ThingSpeak
 sensor.begin();                     // start DS18B20 sensor
 pinMode(RELAY_1_PIN, OUTPUT);       // D3 output - relay
 pinMode(echoPin, INPUT);            // D5 - sonic - input  - echo
 pinMode(trigPin, OUTPUT);           // D6 - sonic - output - trigger  
 pinMode(led_r, OUTPUT);             // LEDs output
 pinMode(led_y, OUTPUT);
 pinMode(led_g, OUTPUT);
 pinMode(led, OUTPUT);               // LED output - onboard
}

void loop() {
 sensors_read();
 if ((millis() - lastmillis) > 10000) {  lastmillis = millis(); sensors_broadcast(); } // publish temperature & moisture - every minute
 
if         ( (distance <= 10 ) && (distance !=0) )  { tank_full();    }                  // status of tank filling with water
   else if (  distance >= 50 )                      { tank_empty();   }
   else if ( (distance < 50) && (distance > 10))    { tank_fillout(); } 
//   else if ( (distance > 14) && (distance < 85) ) 
//      {
//       if      (digitalRead(RELAY_1_PIN) == HIGH)  { tank_fillin(); }
//       else if (digitalRead(RELAY_1_PIN) == LOW)   { tank_fillout();} 
//      }
   else if (distance ==0)                          { tank_error();  }
   else                                            { tank_error();  }
 
// if        ( status_current != status_old)         { 
//    Particle.publish("Tank status:", status_current);
//    status_old = status_current;
//    }
 }
 
 void sensors_read() {
// read data from Sensors
 sensor.requestTemperatures(); 
 t=sensor.getTempCByIndex( 0 );                  // temperature

 digitalWrite(trigPin, LOW); delayMicroseconds(2);  // для большей точности установим значение LOW на пине Trig
 digitalWrite(trigPin, HIGH);delayMicroseconds(10); // Теперь установим высокий уровень на пине Trig             // Подождем 10 μs 
 digitalWrite(trigPin, LOW);          // getting duration of high ssignal on Echo pin 
 duration = pulseIn(echoPin, HIGH);   // calculating distance 
 distance = duration / 58;           

 delay(1000);
 }
 
 void sensors_broadcast() {
 String st(t, 1);        Particle.publish("Temperature:",st);
 String sdist(distance); Particle.publish("Distance :", sdist);
 digitalWrite(led_y, HIGH); delay(500); digitalWrite(led_y, LOW);    // blink LED Yellow    
 ThingSpeak.setField(4, t); 
 ThingSpeak.setField(5, distance);
 ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  
 delay(1000);
 }
 
 void tank_empty() { 
  digitalWrite(led_r, HIGH);   digitalWrite(led_y, LOW);  digitalWrite(led_g, LOW); 
  digitalWrite(RELAY_1_PIN, HIGH);
  char* status_current = "EMPTY";
  Particle.publish(" Tank is:", status_current);  
  delay(60000);
 } 
  
 void tank_fillin() {
  digitalWrite(led_r, LOW);   digitalWrite(led_y, HIGH);  digitalWrite(led_g, HIGH);
  char* status_current = "Fillin";
  Particle.publish(" Tank is:", status_current); 
  delay(60000);
 }

 void tank_fillout() {
  digitalWrite(led_r, HIGH);   digitalWrite(led_y, HIGH);  digitalWrite(led_g, LOW);
  char* status_current = "FillOut";
  Particle.publish(" Tank is:", status_current);
  delay(60000);
 }

 void tank_full() { 
  digitalWrite(led_r, LOW);    digitalWrite(led_y, LOW);  digitalWrite(led_g, HIGH);
  digitalWrite(RELAY_1_PIN, LOW);
  char* status_current = "FULL";
  Particle.publish(" Tank is:", status_current);
  delay(60000);
 } 
  
 void tank_error() {
  digitalWrite(led_r, HIGH);    digitalWrite(led_y, HIGH);  digitalWrite(led_g, HIGH);
  digitalWrite(RELAY_1_PIN, LOW);
  char* status_current = "ERROR";
  Particle.publish(" Tank is:", status_current);
  delay(60000);
 }

I am curious what type of HC-S04 sensor you used. Was it water resistant?

didn’t found water resistant , so i’ve placed it in cup so water can’t reach it - its like small satellite dish looking into water )

I had problems with a project I did but I put the sensor inside the IBC tote. I found this sensor will work should you need it. https://www.amazon.com/gp/product/B01J5KZU8M/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1