kathleen kuypers
Published

Pet Feeder

Have you even forgotten to feed yout pet? Things can happen... But not anymore! From now on PetFeeder will buzz, blink and message you!

IntermediateShowcase (no instructions)4 hours10,122
Pet Feeder

Things used in this project

Hardware components

IOTOPIA Rapid Development kit
AllThingsTalk IOTOPIA Rapid Development kit
×1

Software apps and online services

maker.smartliving.io

Hand tools and fabrication machines

Box

Story

Read more

Schematics

PetFeeder Schematics

Code

petfeeder.ino

Arduino
The pet can be left without food for 36 hours, if you want to change that, adjust maxHours. Make sure you connect sensors and actuators to the pin as defined in the code.
/*
Author: students JOMA secundair Merksem and K. Kuypers
Contact: kathleen.kuypers@gmail.com or kkuypers@jomasecundair.be

Arduino UART Demo Sketch. This Sketch is made for an Genuino 101 IoT board with a Grove
UART WiFi module based on the popular ESP8266 IoT SoC to communicate to the AllThingsTalk
IoT developer cloud.
The Grove UART WiFi module has firmware installed which includes the ATT_IOT library. It
communicates through Serial1 of the Genuino 101 board.

Version 1.0 dd 16/03/2016

### Instructions
1. Setup the Arduino hardware
  - Use an Arduino Genuino 101 IoT board
  - Connect the Arduino Grove shield
  - Connect USB cable to your computer
  - Connect a Grove Light sensor to pin A0 of the Arduino shield
  - Connect a Grove Buzzer actuator to pin D2 of the Arduino shield
  - Connect a Grove Led actuator sensor to pin D4 of the Arduino shield
  - Grove UART wifi to pin UART (D0,D1)
2. Add 'ATT_IOT_UART' library to your Arduino Environment
     More info can be found at http://arduino.cc/en/Guide/Libraries
3. Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file
4. Optionally, change sensor names, labels as appropiate
5. Upload the sketch

Note: for use of extra actuators, extend the callback function at the end of the sketch

*/

#include "ATT_IOT_UART.h"      // AllThingsTalk Arduino UART IoT library
#include <SPI.h>               // Required to have support for signed/unsigned long type
#include "keys.h"              // Keep all your personal account information in a seperate file

// Fixed constants for minimum and maximum light sensor value
#define sensorMin 0
#define sensorMax 1023


ATTDevice Device(&Serial1);
char httpServer[] = "api.smartliving.io";          // HTTP API Server host
char mqttServer[] = "broker.smartliving.io";       // MQTT Server Address

//****************************************************************
//** PIN numbers 
//****************************************************************
#define lightId 0                // Analog sensor is connected to pin A0 on grove shield
#define ledId 4                  // Digital actuator led is connected to pin D4 on grove shield
#define buzzerId 2               // Digital actuator buzzer is connected to pin D2 on grove shield

// Required for the device
void callback(int pin, String& value);

//****************************************************************
//** setup
//****************************************************************

void setup()
{
  Serial.begin(57600);                                 // Init serial link for debugging
  while(!Serial && millis() < 1000);                   // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc
  Serial.println("Starting sketch");
  Serial1.begin(115200);                               // Init serial link for WiFi module
  while(!Serial1);

  while(!Device.StartWifi(true))
    Serial.println("Retrying...");
  while(!Device.Init(DEVICEID, CLIENTID, CLIENTKEY))   // If we can't succeed to initialize and set the device credentials, there is no point to continue
    Serial.println("Retrying...");
  while(!Device.Connect(httpServer))                   // Connect the device with the AllThingsTalk IOT developer cloud. No point to continue if we can't succeed at this
    Serial.println("Retrying");
    
  //*** Add assets to the smartliving IOT cloud
  Device.AddAsset(lightId, "STATUS", "Status", false, "string");        // Create the sensor asset for your device
  Device.AddAsset(ledId, "FEED ME PLEASE!", "LED", true, "boolean");    // Create the actuator asset for your device
  //***

  delay(1000);                                         // Give the WiFi some time to finish everything
  while(!Device.Subscribe(mqttServer, callback))       // Make sure that we can receive message from the AllThingsTalk IOT developer cloud (MQTT). This stops the http connection
    Serial.println("Retrying");

  pinMode(lightId, INPUT);                             // Initialize the digital pin as an input.
  Device.Send("OK", ledId);
  Serial.println("Light sensor is ready for use!");

  pinMode(buzzerId, OUTPUT);                           // Initialize the digital pin as an input.
  Serial.println("Buzzer is ready for use!");

  pinMode(ledId, OUTPUT);                              // Initialize the digital pin as an input.
  Device.Send("false", ledId);
  Serial.println("LED is ready for use!");
}

//****************************************************************
//** variables
//****************************************************************
int maxHours = 36; 						// the max time between 2 feedings in hours
//int maxSeconds = maxHours * 3600; 	// the max time between 2 feedings in seconds
int maxSeconds = 3; 					// for testing issues so that we don't have to wait maxHours
int sec = 0; 							// seconds since last feeding

int Prev_range = 5;						// assume we start with a lighty situation


void loop()
{
  sec++; // start timer
  int range = map(analogRead(lightId), sensorMin, sensorMax, 0, 5);  // meassure lightness and convert to a number between 0 and 5
  if (range != Prev_range)              // Verify if value has changed
  {
    if (range!=0 && range !=1)  		// it's not dark so it's "open" and we are feeding
    {
      Device.Send("feeding...", lightId);
      sec = 0; 							// pet has been fed so restart timer
    }
    if (sec >= maxSeconds) 				// it's dark and last feeding was to long ago
    {
      Device.Send("PLEASE FEED YOUR PET!", lightId);
      while (range==0 || range==1) 	// as long as it's dark (closed): buzz and blink led
      {
        digitalWrite(ledId, HIGH);
        Device.Send("true", ledId);  
        digitalWrite(buzzerId, HIGH);
        Device.Send("true", buzzerId);  
        digitalWrite(ledId, LOW);
        Device.Send("false", ledId);  
        digitalWrite(buzzerId, LOW);
        Device.Send("false", buzzerId);  
        delay(1000);  //wacht 1 sec
        range = map(analogRead(lightId), sensorMin, sensorMax, 0, 5);
      }
      Device.Send("OK feeding...", lightId);
      sec = 0; 							// pet has been fed so restart timer
    }
    Prev_range = range;
  }
  
  Device.Process(); 
  delay(1000); 							// meassure every second
}


// Callback function: handles messages that were sent from the iot platform to this device.
void callback(int pin, String& value) 
{ 
  Serial.print("Incoming data for: ");      // Display the value that arrived from the AllThingsTalk IOT developer cloud.
  Serial.print(pin);
  Serial.print(", value: ");
  Serial.println(value);

}

keys.h

Arduino
Register at maker.smartliving.io and create a ground and a device.
Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file you get for the device
#ifndef SETTINGS
#define SETTINGS

#define DEVICEID "xxx"           // Your device id comes here
#define CLIENTID "xxx"           // Your client id comes here;
#define CLIENTKEY "xxx"          // Your client key comes here;

#endif

Credits

kathleen kuypers

kathleen kuypers

4 projects • 11 followers

Comments