Photon water tank sensor

You might have read my earlier tutorial on how to measure your rain water tank with an Arduino. If you haven’t, this tutorial is based on that one and will assume some ground knowledge from that post. I’ll try to link back as much as needed to let this make sense. The Arduino worked perfect for a while, but it became a hassle to check the level under the stairs, a sensor connected to the internet made much more sense. That way, you can receive warnings when the level gets low or very high, and you can respond accordingly. I used a Particle Photon, a $19 internet-connected development board to achieve this, connected to IFTTT to quickly switch between outputs.

First things first: you’ll need to start with installing a sensor in your tank. I used the HC-SR04, a well-known and documented ultrasonic range sensor. You can order one from DealExtreme on the cheap, even with a mount to hold it. From there, you’ll need to wire it. I installed the Particle Photon inside the house and connected it with a standard Ethernet cable, but feel free to handle this differently. You might have noticed how badly I installed it in the previous tutorial, and yes, the sensor failed after that due to water damage. A new and improved setup can be seen here:

Then, indoors, connect the 4 cables you used to a small breadboard (also to be ordered cheap from DealExtreme). Insert the Particle Photon too, but not on the same line as the sensor cables, you’ll need to connect those manually with small jumper wires (again, can be ordered from DealExtreme).  For details on the wiring:

Photon_HCSR04

Just in case the above isn’t clear: VCC (of the sensor) is connected to the VIN of the Photon, GND connected to GND, Trig connected to D4 and Echo connected to D5 and GND, 2 times through a 470 ohm resistor. You’ll see the sensor on the board in the graph above, obviously I connected the ethernet cable instead there. This is what my setup under the stairs looks like:

2015-10-05 20.32.19

Now, hook up the Particle Photon to power and watch the LED flash. Just in case you haven’t connected your Particle Photon yet, a quick guide to connect can be found here: Getting started with the Particle Photon. After that, you can head to https://build.particle.io and upload new firmware to your device. (This is actually pretty cool, you can have a fleet of Photons throughout your home and upload new firmware on the fly without touching any of them.)

This is the code I used, gathered from various sources:

// This #include statement was automatically added by the Particle IDE.
#include "HC_SR04/HC_SR04.h"

int distance = 0;
int percentage,heightTank, deviation;

int trigPin = D4;
int echoPin = D5;

/*
Connect an HC-SR04 Range finder as follows:
Spark   HC-SR04
GND     GND
5V      VCC
D4      Trig
D5      Voltage divider output - see below
Echo --|
       >
       < 470 ohm resistor
       >
       ------ D5 on Spark
       >
       < 470 ohm resistor
       >
GND ---|
Test it using curl like this:
curl https://api.spark.io/v1/devices/<deviceid>/cm?access_token=<accesstoken>
The default usable rangefinder is 10cm to 250cm. Outside of that range -1 is returned as the distance.
You can change this range by supplying two extra parameters to the constructor of minCM and maxCM, like this:
HC_SR04 rangefinder = HC_SR04(trigPin, echoPin, 5.0, 300.0);
*/

HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

void setup() 
{
    heightTank=151;
    deviation=25;
}

void loop() 
{
    distance = rangefinder.getDistanceCM();
    percentage=100-(((distance-deviation)*100)/heightTank);
    Particle.publish("Tanklevel", String(percentage), 60, PRIVATE);
    if (percentage < 25)
        Particle.publish("TanklevelDangerlow", String(percentage), 60, PRIVATE);
    delay(3600000);
}

You will need to incorporate the HC-SR04 library to interact with the distance sensor, as you can see from the first 2 lines of code in mine. You can find it by clicking ‘Libraries’ in the bottom left of the Build platform (the icon looks like a bookmark) and search for ‘HC_SR04’. Then just add it to your project.

Final step is the follow-up you want to add to this. In the code I connected it to IFTTT, to send an event ‘Tanklevel’, with the percentage it measures (don’t forget to edit the code to make sure it’s correct for you, especially the variables on heights). So head to IFTTT and create a new recipe. As the ‘This’, connect to the Particle channel and as ‘If (Event Name)’, fill out ‘Tanklevel’ and select your device. As the ‘That’, you can select Google Drive, to add an hourly measurement to a spreadsheet and generate graphs of the differences. So you choose that channel, name the file and add this as the formatted row: “{{CreatedAt}}|||{{EventContents}}”. That way, you’ll get a nice row, with the log date and percentage.

A savy programmer can also connect it to an own API through the Maker channel and generate their own graphs. Or to Pushbullet to send notifications when it gets below a certain level.

Anything that didn’t work out for you? Let me know, then I’ll make the changes!

Also, if you liked this tutorial, you’re always welcome to buy me a beer! Not that you owe me or anything, but I can only try, right? https://www.paypal.me/dennisjanssen