Water Tank Level Project

Hey Guys,

I have been working on a project where I have two MPX1050 hooked up to the imp. I’m using the relative pressure sensors to monitor the pressure in my water tanks. It works very well, but I have been running into issues with plotly. I can send data no problem to plotly and I can see my graph, no issues there. The issue comes after I have accumulated quite a bit of data, then out of nowhere plotly will just erase everything and start ploting the new data. I’m assuming this is a max data restriction on plotly’s end. I do have a paid profession version, so i don’t think that is it.

I’m running out of ideas. I have contacted plotly support but have not received a response as of yet.

For my end, I changed the sample rate to 5 seconds, going to try and accumulate a bunch of data really fast and see how much data plotly will take before it resets the graph.

I have attached my code for anyone that is interested. I cahnge my API code to “x” (I’m no expert when it comes to this stuff!)


Agent Code
`#require “Plotly.class.nut:1.0.1”

function getTime() {
local date = date(time()-25200, "u");
local sec = stringTime(date["sec"]);
local min = stringTime(date["min"]);
local hour = stringTime(date["hour"]);
local day = stringTime(date["day"]);
local month = date["month"] + 1;
local year = date["year"];
return year+"-"+month+"-"+day+" "+hour+":"+min+":"+sec;


// function to fix time string
function stringTime(num) {
if (num < 10)
return “0”+num;
return “”+num;

function postToPlotly(reading) {
local timestamp = getTime();
“name” : “Tank 1”,
“x” : [timestamp],
“y” : [reading[“tank1”]]
“name” : “Tank 2”,
“x” : [timestamp],
“y” : [reading[“tank2”]]
“name” : “Max Level”,
“x” : [timestamp],
“y” : [“750”]
“name” : “Min Level”,
“x” : [timestamp],
“y” : [“140”]

plot1 <- Plotly(“Jesstr”, “xxxxxxxxxx”, “Tank Level”, false, [“Tank 1”, “Tank 2”, “Max Level”, “Min Level”], function(error, response, decoded) {
device.on(“reading”, postToPlotly);
server.log("See plot at " + plot1.getUrl());

Device Code
`// Device Code

// Establish a global variable to hold environmental data

data <- {}
data.tank1 <- 0
data.tank2 <- 0

// Configure the LED (on pin 2) as digital out with 0 start state

local led = hardware.pin5
led.configure(DIGITAL_OUT, 0)
// Configure pins for sensors
local tank1 = hardware.pin1
local tank2 = hardware.pin2

// This function will be called regularly to take the temperature
// and log it to the device’s agent

function getReadings()
// Flash the LED


// Read Tank Levels
local supplyVoltage = hardware.voltage()
local voltage1 = supplyVoltage * tank1.read() / 65535.0
local voltage2 = supplyVoltage * tank2.read() / 65535.0
local pressure1 = ((voltage1/supplyVoltage)-0.04)/0.09
local pressure2 = ((voltage2/supplyVoltage)-0.04)/0.09
local level1 = (pressure1*101.971621298)
local level2 = (pressure2*101.971621298)

data.tank1 = level1-3.43-0.0215
data.tank2 = level2-45.7+1.67

//Send the data to the agent
agent.send("reading", data)

// Put the imp to sleep for five minutes BUT
// only do so when impOS has done all it needs to
// do and has gone into an idle state

imp.onidle(function() { server.sleepfor(5) } )


function flashLed()
// Turn the LED on (write a HIGH value)


// Pause for half a second


// Turn the LED off



// it runs its device code afresh - ie. it does a warm boot



So by changing the sample rate to 5 seconds i was able to update data from 12:26:47 to 12:59:15, which is 604500 seconds roughly, so that is 390 - 5 second samples.

Do the levels change that quickly that you need to sample that often?

I don’t have a Plotly answer for you, but wondering about a way to think around it.

Hi mlseim,

The levels don’t really change that quickly, the reason I was going for shorter intervals, was to try and narrow down on the house hold items that use the most water, or people, haha. I live out at a lake and we have to have water trucked in the sewage trucked out, just trying to be more aware!

I recently switched to a two minute interval, and this has helped, so far I have accumulated over 24h of data, without a reset. but I’m going to leave it at the 2 minuet mark and see how things go. Eventually if I have to, I’ll increase the interval till I find a happy medium.

Out of curiosity, how do you measure the tank level from pressure? Does the water go into the MPX1050 or is there an air column, in which case how do you maintain pressure in the air column? Thanks.

The imp knows the date and time. Whenever the level changes by 1 gallon, post the level and date/time. Most of the day, no water is being used, so nothing needs to be posted. You may end up only getting 60 posted values to Plotly each day.

Awesome mlseim,

That’s a good point. And I think that’s what I’ll do. Thanks for the help!

On a side note since I have posted this, I haven’t had a hiccup yet, plotly creates a new grid once the first one is full but keeps directing the plot to all the grids. So no loss of data.


You bet, I’m using a piece of 1/2" copper pipe, which has barbed fitting soldered to the top. Then I ran a vinyl hose to the mpx, also I screwed up the number it’s actually a MPX5010DP. So I basically put everything together, without submerging anything, calibrate the sensors to read 0, then once I place the copper pipe upright in the tank I have created a air pressure pocket through the pipe and vinyl hose. As the water column increases the pressure inside the system increases. I also place the opening of the copper pipe below the minimum level of the water tank, which maintains a slight pressure all the time. The reason I can do this is because the opening on the tank which feeds the house with water is about 4" off the bottom of the tank. The MPX5010, can not have any liquid inside of it, only air, they suggest dry air actually. Haha.


I was thinking about a pressure pocket the same as yours but with a small 3v air pump so the air column is “reset” for each measurement. Also the pressure vs water height would be an easier linear relationship. The air pocket could be all vinyl pipe with an end weight dropped in from the top of the tank.

The pumps are common nowdays courtesy of the “quantified self” phenomenon, which I’m all for.

A water tank monitor with air bubbler is on my to-do list for early 2016 so I’ll report back how it goes.

Thanks to Electric Imp I’m making things quicker and easier than ever which has only increased the scope of my to-do list :slight_smile:

Another idea if the tank is of reasonable size, set the tank on top of 3 metal blocks, where one of them is a load-cell. Calibrate 0 with empty tank. Fill with water and calibrate the full level.

Using weight, you don’t need to ever deal with a fouled pressure sensor or tube leaks.
All hoses going to the tank will need to be flexible poly tubing.
You can switch to a different tank if needed and not have to modify the tank.