Tag Archives: proximity

TinkurCrate – Where’s The Dog?!


One of my favorite things about the Internet of Things is “giving a voice to things”.  And who’s more deserving of a voice – and a Twitter account – than our roommate’s dog, Tyr.  Tyr, an English Springer Spaniel enjoys running, playing with his toys, and hanging out in his crate.  In fact, he’s got a fancy crate located in a prime location in our living room.  TinkurCrate is the first in a series of projects to learn more about Tyr’s activities and connect him to the IoT.  TinkurCrate uses an Arduino with Ethernet and a proximity sensor to determine when Tyr is in his crate.  The Arduino posts the data to Xively for storage, which sends Web Hook triggers to Xively when Tyr enters and exits his crate – triggering Tweets on TinkurLab’s IO Twitter account – @TinkurLabIO.





Wiring for the project is pretty simple, just connect the proximity sensor to the Arduino Ethernet shield as follows:

  • Red to 5v
  • Black to Ground
  • Yellow to Analog Pin 5

TinkurCrate Wiring



<br />/*<br /><br />Published:  2014<br />Author:     Adam - TINKURLAB<br />Web:        www.TinkurLab.com<br /><br />Copyright:  This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. http://creativecommons.org/licenses/by-nc/4.0/<br /><br />Contributors:<br />-Xively code based on http://arduino.cc/en/Tutorial/XivelyClient<br />-Median library http://playground.arduino.cc/Main/RunningMedian<br /><br /><%%KEEPWHITESPACE%%> */<br /><br />#include &lt;SPI.h&gt;<br />#include &lt;Ethernet.h&gt;<br />#include "RunningMedian.h"<br /><br />#define APIKEY         "xxxxxxxxxxx" // replace your Xively API Key here<br />#define FEEDID         xxxxxxxxxxx // replace your Xively Feed ID here<br />#define USERAGENT      "TinkurCrate" // user agent is the project name<br /><br />// assign a MAC address for the ethernet controller.<br />// Newer Ethernet shields have a MAC address printed on a sticker on the shield<br />// fill in your address here:<br />byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // replace your Mac Address here<br /><br />// fill in an available IP address on your network here,<br />// for manual configuration:<br />//IPAddress ip(10,0,1,20);<br />// initialize the library instance:<br />EthernetClient client;<br /><br />char server[] = "api.xively.com";   // name address for xively API<br /><br />unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds<br />boolean lastConnected = false;                 // state of the connection last time through the main loop<br />const unsigned long postingInterval = 10*1000; //delay between updates to Xively.com<br /><br />int modeSwitch = 1;<br /><br />int incrate = 0;<br /><br />RunningMedian proximityLast10 = RunningMedian(10);<br /><br />int timesincrate = 0;<br /><br />int sensorReading = 0;<br /><br />int sensorReadingMedian = 0;<br /><br />void setup() {<br /><%%KEEPWHITESPACE%%>  // Open serial communications and wait for port to open:<br /><%%KEEPWHITESPACE%%>  Serial.begin(9600);<br /><br /><%%KEEPWHITESPACE%%>  delay(2000);<br /><br /><%%KEEPWHITESPACE%%> // Connect to network amd obtain an IP address using DHCP<br /><%%KEEPWHITESPACE%%>  if (Ethernet.begin(mac) == 0)<br /><%%KEEPWHITESPACE%%>  {<br /><%%KEEPWHITESPACE%%>    Serial.println("DHCP Failed, reset Arduino to try again");<br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><%%KEEPWHITESPACE%%>  }<br /><%%KEEPWHITESPACE%%>  else<br /><%%KEEPWHITESPACE%%>  {<br /><%%KEEPWHITESPACE%%>    Serial.println("Arduino connected to network using DHCP");<br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><%%KEEPWHITESPACE%%>  }<br />}<br /><br />void loop() {<br /><br /><%%KEEPWHITESPACE%%>  if (modeSwitch == 1)<br /><%%KEEPWHITESPACE%%>  {<br /><%%KEEPWHITESPACE%%>    // read the analog sensor:<br /><%%KEEPWHITESPACE%%>    sensorReading = analogRead(A5);<br /><br /><%%KEEPWHITESPACE%%>    proximityLast10.add(sensorReading);<br /><br /><%%KEEPWHITESPACE%%>    sensorReadingMedian = proximityLast10.getMedian();<br /><br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><br /><%%KEEPWHITESPACE%%>    Serial.print("Proximity: ");<br /><%%KEEPWHITESPACE%%>    Serial.println(sensorReading);<br /><br /><%%KEEPWHITESPACE%%>    Serial.print("Median Proximity: ");<br /><%%KEEPWHITESPACE%%>    Serial.print(sensorReadingMedian);<br /><%%KEEPWHITESPACE%%>    Serial.print(" w/ ");<br /><%%KEEPWHITESPACE%%>    Serial.print(proximityLast10.getCount());<br /><%%KEEPWHITESPACE%%>    Serial.println(" samples");<br /><br /><%%KEEPWHITESPACE%%>    delay(1000);<br /><br /><%%KEEPWHITESPACE%%>    if (sensorReadingMedian &gt; 160)<br /><%%KEEPWHITESPACE%%>    {<br /><%%KEEPWHITESPACE%%>      incrate = 1;<br /><%%KEEPWHITESPACE%%>    }<br /><br /><%%KEEPWHITESPACE%%>    if (sensorReadingMedian &lt; 100)<br /><%%KEEPWHITESPACE%%>    {<br /><%%KEEPWHITESPACE%%>      incrate = 1;<br /><%%KEEPWHITESPACE%%>    }<br /><br /><%%KEEPWHITESPACE%%>    Serial.print("Is In Crate: ");<br /><%%KEEPWHITESPACE%%>    Serial.println(incrate);<br /><br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><br /><%%KEEPWHITESPACE%%>  }<br /><br /><%%KEEPWHITESPACE%%>  // convert the data to a String<br /><%%KEEPWHITESPACE%%>  String dataString = "proximity,";<br /><%%KEEPWHITESPACE%%>  dataString += String(sensorReadingMedian);<br /><br /><%%KEEPWHITESPACE%%>  // you can append multiple readings to this String to<br /><%%KEEPWHITESPACE%%>  // send the xively feed multiple values<br /><%%KEEPWHITESPACE%%>  dataString += "\nincrate,";<br /><%%KEEPWHITESPACE%%>  dataString += String(incrate);<br /><br /><%%KEEPWHITESPACE%%>  // if there's incoming data from the net connection.<br /><%%KEEPWHITESPACE%%>  // send it out the serial port.  This is for debugging<br /><%%KEEPWHITESPACE%%>  // purposes only:<br /><%%KEEPWHITESPACE%%>  if (client.available()) {<br /><%%KEEPWHITESPACE%%>    char c = client.read();<br /><%%KEEPWHITESPACE%%>    Serial.print(c);<br /><%%KEEPWHITESPACE%%>  }<br /><br /><%%KEEPWHITESPACE%%>  // if there's no net connection, but there was one last time<br /><%%KEEPWHITESPACE%%>  // through the loop, then stop the client:<br /><%%KEEPWHITESPACE%%>  if (!client.connected() &amp;&amp; lastConnected) {<br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><%%KEEPWHITESPACE%%>    Serial.println("disconnecting.");<br /><br /><%%KEEPWHITESPACE%%>    resetMode();<br /><br /><%%KEEPWHITESPACE%%>    client.stop();<br /><br /><%%KEEPWHITESPACE%%>  }<br /><br /><%%KEEPWHITESPACE%%>  // if you're not connected, and ten seconds have passed since<br /><%%KEEPWHITESPACE%%>  // your last connection, then connect again and send data:<br /><%%KEEPWHITESPACE%%>  if(!client.connected() &amp;&amp; (millis() - lastConnectionTime &gt; postingInterval)) {<br /><br /><%%KEEPWHITESPACE%%>    modeSwitch = 2;<br /><br /><%%KEEPWHITESPACE%%>    sendData(dataString);<br /><%%KEEPWHITESPACE%%>  }<br /><%%KEEPWHITESPACE%%>  // store the state of the connection for next time through<br /><%%KEEPWHITESPACE%%>  // the loop:<br /><%%KEEPWHITESPACE%%>  lastConnected = client.connected();<br /><br />}<br /><br />// this method makes a HTTP connection to the server:<br />void sendData(String thisData) {<br /><br /><%%KEEPWHITESPACE%%>  // if there's a successful connection:<br /><%%KEEPWHITESPACE%%>  if (client.connect(server, 80)) {<br /><%%KEEPWHITESPACE%%>    Serial.println("connecting...");<br /><%%KEEPWHITESPACE%%>    // send the HTTP PUT request:<br /><%%KEEPWHITESPACE%%>    client.print("PUT /v2/feeds/");<br /><%%KEEPWHITESPACE%%>    client.print(FEEDID);<br /><%%KEEPWHITESPACE%%>    client.println(".csv HTTP/1.0");<br /><%%KEEPWHITESPACE%%>    client.println("Host: api.xively.com");<br /><%%KEEPWHITESPACE%%>    client.print("X-ApiKey: ");<br /><%%KEEPWHITESPACE%%>    client.println(APIKEY);<br /><%%KEEPWHITESPACE%%>    client.print("Content-Length: ");<br /><%%KEEPWHITESPACE%%>    client.println(thisData.length());<br /><br /><%%KEEPWHITESPACE%%>    // last pieces of the HTTP PUT request:<br /><%%KEEPWHITESPACE%%>    client.println("Content-Type: text/csv");<br /><%%KEEPWHITESPACE%%>    client.println("Connection: close");<br /><%%KEEPWHITESPACE%%>    client.println();<br /><br /><%%KEEPWHITESPACE%%>    // here's the actual content of the PUT request:<br /><%%KEEPWHITESPACE%%>    client.println(thisData);<br /><%%KEEPWHITESPACE%%>    Serial.println(thisData);<br /><br /><%%KEEPWHITESPACE%%>    client.println();<br /><br /><%%KEEPWHITESPACE%%>  }<br /><%%KEEPWHITESPACE%%>  else {<br /><%%KEEPWHITESPACE%%>    // if you couldn't make a connection:<br /><%%KEEPWHITESPACE%%>    Serial.println("connection failed");<br /><%%KEEPWHITESPACE%%>    Serial.println();<br /><%%KEEPWHITESPACE%%>    Serial.println("disconnecting.");<br /><%%KEEPWHITESPACE%%>    client.stop();<br /><%%KEEPWHITESPACE%%>  }<br /><%%KEEPWHITESPACE%%>   // note the time that the connection was made or attempted:<br /><%%KEEPWHITESPACE%%>  lastConnectionTime = millis();<br />}<br /><br />void resetMode()<br />{<br /><%%KEEPWHITESPACE%%> modeSwitch = 1;<br /><%%KEEPWHITESPACE%%> incrate = 0;<br />}<br />

Source code also available on GitHub at https://github.com/TinkurLab/TinkurCrate/.


Xively Setup

Xively is a service for powering the Internet of Things, providing API access for data storage, data retrieval, triggers, and data charting.  After registering for a free Xively account, perform the following steps:

1) Add Device.

2) Add Channels for “incrate” and “proximity”.


3) Setup Triggers for “incrate” status change.  One Trigger calls the Zappier “Entering Crate” Zap, and the other Trigger calls the Zappier “Exiting Crate” Zap.



Zapier Setup

Zapier is a service for orchestrating and automating the Internet of Things and popular online services.  After registering for a free Zapier account, perform the following steps:

1) Create a new Zap, using a Web Hook – Catch Hook event, triggering a Twitter – Crate Tweet event.


2) Authorize your Twitter account if needed, and add a Filter to only trigger when the Xively trigger is “1” (when the dog enters the crate).


3) Setup a Message to post to Twitter and save the Zap.


You can view Tyr’s live status at https://xively.com/feeds/1765156749 and his Tweets at @TinkurLabIO.

What’s Next

While TinkurCrate is a good beginning, I hope to add a few more sensors to Tyr’s world.  Possibilities include:

  • Pressure sensors to determine when Tyr is on the sofa
  • Continuity sensor to determine when Tyr’s water bowl is empty
  • Processing visualization of Tyr’s in crate / out of crate activity by time of day and day of week

Have any other ideas or suggestions.  Tweet @TinkurLab.