Thursday, August 24, 2017

ToF Laser to Monitor Cat Food Levels

-----
We live in great times. There was once a day when the only way to determine if the cat feeder needed more kibble was to actually look at it with your own eyes like some Neanderthal. Thankfully technology and the magical world of IoT has changed all of that.
----
For this project we use a WiFi enable ESP8266 and a STMicro VL53L0X ToF Sensor mounted to a breakout board. If you don't find a need to measure feline food consumption the project still provides Arduino code that can be extremely useful for your other IoT projects:

All of the above services are free but; of course, you will need to establish an account if you do not have on.
----What's happening?
The ESP8266 runs in Arduino mode (source code below) in an endless loop. Every hour it polls the VL53L0X ToF Sensor mounted on the lid of the cat food feeder.

-----
Since we know how many centimeters the food is from the ToF sensor at full and at empty we are able to scale those values and report/log "percent full" status. Those values are posted to a Google Drive Spreadsheet and to AT&T's M2X machine to machine servers for logging. If the food level is considered CRITICALLY LOW a SMS message goes out to our mobile device. Just to increase the geek factor, CRITICALLY LOW alerts are also displayed on our Pebble watch.
----
The Google Drive Spreadsheet looks like this:

-----
Here is the bad ass AT&T M2X machine to machine server graph (note the increase after we filled the feeder):

-----
The IFTTT SMS alerts are sent if the rig determines food levels critically low:

// Define and set up some variables
float Range_inches; // How far the sensor is from the food at time of reading. Sensor is on roof of feeder.
float Min_level = 5.0; // Distance in inches before low food alarm level. Food is far from sensor on feeder roof
float Max_level = 0.5; // Distance in inches from sensor for full feeder level. Food is close to sensor on feeder roof
float Percent_full; // How full in xx.x% is the food based on the Min/Max_levels defined above
float Caution_alarm = 35.0; // at xx.x% food level is considered low. String Status YELLOW
float Critical_alarm = 25.0; // at xx.x% food level is considered critically low. String Status RED
String Status = "***_Starting_with_Caution_at:_" + String(Caution_alarm) + "%_and_CRITICAL_at:_" + String(Critical_alarm) + "%"; // Update to Out of Range, NORMAL, LOW, CRITICAL, etc. "spaces" will error IFTTT Webhook; use "_"
int Run_number; // how many times the sensor has been read

// Output pins
const int led = 2; // Blue on board LED is on PIN2 for this NoderMCU 1.0 ESP8266. Blink it between reads

// Program control variables
int Seconds_between_posts = 60 * 60; // how often to post the results of the sensor read. NOT EXACT due to post delays, Sensor reads, LED flashing, etc.
int logging = 1; // If 1 then log to cloud. Any other value (0) turns it off. ESP8266 "Start/Restart" message is always logged.

void setup(void){ // This is run once.
pinMode(led, OUTPUT); // set up the onbaord LED pin as an output.
Serial.begin(115200); // turn on the serial monitor for debug