pseudozach
Published © GPL3+

"Alexa, Feed Toto"

Feed Toto is an Arduino-controlled pet feeder. Meal size and schedule can be set via an Android app and/or Alexa.

IntermediateFull instructions provided20 hours7,758
"Alexa, Feed Toto"

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Adafruit Bluefruit LE UART Friend
×1
Adafruit Large push-pull solenoid
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
actually used tip120
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
1N4004 is fine.
×1
Resistor 1k ohm
Resistor 1k ohm
×1
9V battery (generic)
9V battery (generic)
×2

Software apps and online services

Alexa Voice Service
Amazon Alexa Alexa Voice Service
Alexa Skills Kit
Amazon Alexa Alexa Skills Kit
Arduino IDE
Arduino IDE
AWS Lambda
Amazon Web Services AWS Lambda

Story

Read more

Schematics

schematic

Code

arduino

Arduino
#include <Timer.h>
#include <Event.h>

/*********************************************************************
 This is an example for our nRF51822 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
  #include <SoftwareSerial.h>
#endif

#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include "BluefruitConfig.h"

/*=========================================================================
    APPLICATION SETTINGS

  FACTORYRESET_ENABLE     Perform a factory reset when running this sketch
 
                            Enabling this will put your Bluefruit LE module
                              in a 'known good' state and clear any config
                              data set in previous sketches or projects, so
                            running this at least once is a good idea.
 
                            When deploying your project, however, you will
                              want to disable factory reset by setting this
                              value to 0. If you are making changes to your
                            Bluefruit LE device via AT commands, and those
                              changes aren't persisting across resets, this
                              is the reason why. Factory reset will erase
                              the non-volatile memory where config data is
                              stored, setting it back to factory default
                              values.
   
                            Some sketches that require you to bond to a
                              central device (HID mouse, keyboard, etc.)
                              won't work at all with this feature enabled
                              since the factory reset will clear all of the
                              bonding data stored on the chip, meaning the
                              central device won't be able to reconnect.
    MINIMUM_FIRMWARE_VERSION  Minimum firmware version to have some new features
    MODE_LED_BEHAVIOUR        LED activity, valid options are
                              "DISABLE" or "MODE" or "BLEUART" or
                              "HWUART"  or "SPI"  or "MANUAL"
    -----------------------------------------------------------------------*/
    #define FACTORYRESET_ENABLE         1
    #define MINIMUM_FIRMWARE_VERSION    "0.6.6"
    #define MODE_LED_BEHAVIOUR          "MODE"
/*=========================================================================*/

// Create the bluefruit object, either software serial...uncomment these lines

SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
                      BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);


/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

Timer t;
int regularFeedEvent;
/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  while (!Serial);  // required for Flora & Micro
  delay(500);

  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit Command Mode Example"));
  Serial.println(F("---------------------------------------"));

  /* Initialise the module */
  Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin(VERBOSE_MODE) )
  {
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

  if ( FACTORYRESET_ENABLE )
  {
    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ){
      error(F("Couldn't factory reset"));
    }
  }

  /* Disable command echo from Bluefruit */
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
  Serial.println(F("Then Enter characters to send to Bluefruit"));
  Serial.println();

  ble.verbose(false);  // debug info is a little annoying after this point!

  /* Wait for connection */
  while (! ble.isConnected()) {
      delay(500);
  }

  // LED Activity command is only supported from 0.6.6
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    // Change Mode LED Activity
    Serial.println(F("******************************"));
    Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
    ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
    Serial.println(F("******************************"));
  }
}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
String releaseLatch, bleBuffer, feedInterval, releaseInterval;
long feedInt;
int releaseInt = 1000;
void loop(void)
{
  
  //timer lib
  t.update();
  
  // Check for user input
  char inputs[BUFSIZE+1];

  if ( getUserInput(inputs, BUFSIZE) )
  {
    // Send characters to Bluefruit
    Serial.print("[Send] ");
    Serial.println(inputs);

    ble.print("AT+BLEUARTTX=");
    ble.println(inputs);

    // check response stastus
    if (! ble.waitForOK() ) {
      Serial.println(F("Failed to send?"));
    }
  }

  // Check for incoming characters from Bluefruit
  ble.println("AT+BLEUARTRX");
  ble.readline();
  if (strcmp(ble.buffer, "OK") == 0) {
    // no data
    return;
  }
  releaseLatch = String("release_latch");
  // Some data was found, its in the buffer
  bleBuffer = ble.buffer;
  Serial.print(F("[Recv] ")); Serial.println(bleBuffer);
  if(bleBuffer == releaseLatch) {

    feedToto();

    //send ACK back to Android    
    // Check for user input
    char inputs[BUFSIZE+1];
    String ack = String("ac");
    
    ack.toCharArray(inputs, BUFSIZE);

    // Send characters to Bluefruit
    Serial.print("[Send] ");
    Serial.println(inputs);

    ble.print("AT+BLEUARTTX=");
    ble.println(inputs);

    // check response stastus
    if (! ble.waitForOK() ) {
      Serial.println(F("Failed to send?"));
    }

  } else if(bleBuffer.indexOf("feedinterval") > -1 ) {
      //expects -> feedinterval:seconds
      feedInterval = getValue(bleBuffer, ':', 1);
      feedInt = feedInterval.toInt();
      Serial.print("setting feedInterval to (seconds): ");
      Serial.println(feedInt);
      
      setRegularFeeding(feedInt);
//      t.stop(regularFeedEvent);
//      if(feedInt != 0) {
//        Serial.println("regular feeding set.");
////        int tickEvent = t.every(2000, feedTotoWrapper, NULL);
//        regularFeedEvent = t.every(feedInt * 1000, feedToto, NULL);
//      } else {
//          Serial.println("regular feeding canceled."); 
//          digitalWrite(13, LOW);
//          delay(1000); 
//      }
      
  } else if(bleBuffer.indexOf("releaseinterval") > -1 ) {
    //expects -> releaseinterval:milliseconds
    //expects -> String[] tickMarkLabels = {"1", "3", "5", "10", "20"};
      releaseInterval = getValue(bleBuffer, ':', 1);
      releaseInt = releaseInterval.toInt();
      if(releaseInt == 1) {
        releaseInt = 1 * 1000;
        } else if(releaseInt == 2) {
          releaseInt = 3 * 1000;     
        } else if(releaseInt == 3) {
          releaseInt = 5 * 1000; 
        } else if (releaseInt == 4) {
          releaseInt = 10 * 1000;  
        } else if(releaseInt == 5) {
          releaseInt = 20 * 1000;  
        } else {
          releaseInt = 1 * 1000;
        }
      Serial.print("RI:setting releaseInterval to (milliseconds): ");
      Serial.println(releaseInt);
      
  } else {
     //any other input put things back to LOW
     Serial.println("Other input: solenoid -> LOW");
     // releases solenoid - push position 
     t.stop(regularFeedEvent);
     digitalWrite(13, LOW);
     delay(1000);  
  }
  ble.waitForOK();


}

/**************************************************************************/
/*!
    @brief  Checks for user input (via the Serial Monitor)
*/
/**************************************************************************/
bool getUserInput(char buffer[], uint8_t maxSize)
{
  // timeout in 100 milliseconds
  TimeoutTimer timeout(100);

  memset(buffer, 0, maxSize);
  while( (!Serial.available()) && !timeout.expired() ) { delay(1); }

  if ( timeout.expired() ) return false;

  delay(2);
  uint8_t count=0;
  do
  {
    count += Serial.readBytes(buffer+count, maxSize);
    delay(2);
  } while( (count < maxSize) && (Serial.available()) );

  return true;
}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void setRegularFeeding(long feedInt_) {
  t.stop(regularFeedEvent);
  if(feedInt_ != 0) {
    Serial.println("regular feeding set.");
    regularFeedEvent = t.every(feedInt_ * 1000, feedToto, NULL);
  } else {
      Serial.println("regular feeding canceled."); 
      digitalWrite(13, LOW);
      delay(1000); 
  }
  
}  

void feedTotoLoop(int x) {
  for(int i = 0; i < x; i++) {
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
    }
  }

//one time feed
void feedToto() {
  //shake things
  feedTotoLoop(releaseInt/1000);
  
  //open up
//  // pulls solenoid
//  digitalWrite(13, HIGH); 
//  
//  // releases solenoid - push position
//  delay(20000);  
//  digitalWrite(13, LOW);

  //OR
//  Serial.println("feedtoto1");
//  t.pulse(13, 1000, HIGH);
//  Serial.println("feedtoto2");
  
  }

static void feedTotoWrapper(void* context) {
 feedToto();
}

alexa lambda

JavaScript
/* eslint-disable  func-names */
/* eslint quote-props: ["error", "consistent"]*/
/* global AWS*/
/* global putObjectToS3*/
/* global fs*/
/* global deasync*/
/* global cp*/
/* global async*/

// var AWS = require('aws-sdk');
// var fs = require('fs');
// var deasync = require('deasync');
//var cp = require('child_process');
// var async = require('async');


/**
 * This sample demonstrates a simple skill built with the Amazon Alexa Skills
 * nodejs skill development kit.
 * This sample supports multiple lauguages. (en-US, en-GB, de-DE).
 * The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
 * as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact
 **/

'use strict';
const Alexa = require('alexa-sdk');
var moment = require('moment');
var now = moment();


//=========================================================================================================================================
//TODO: The items below this comment need your attention.
//=========================================================================================================================================

//Replace with your app ID (OPTIONAL).  You can find this value at the top of your skill's page on http://developer.amazon.com.
//Make sure to enclose your value in quotes, like this: const APP_ID = 'amzn1.ask.skill.-';
const APP_ID = '-';

const SKILL_NAME = 'Feed Toto';
const GET_FEED_MESSAGE = "OK. Toto will be fed ";
const HELP_MESSAGE = 'You can say feed toto now, or, every 3 hours...Let me know.';
const HELP_REPROMPT = 'Should I feed Toto now?';
const STOP_MESSAGE = 'Goodbye!';

//=========================================================================================================================================
//TODO: Replace this data with your own.  You can find translations of this data at http://github.com/alexa/skill-sample-node-js-fact/lambda/data
//=========================================================================================================================================
const data = [
    'it is done.'
];

// 1. Text strings =====================================================================================================
//    Modify these strings and messages to change the behavior of your Lambda function
var myBucket = '-'; //'alexabucket12';      // replace with your own bucket name!
var myObject = '-'; //'hello.txt';          // replace with your own file name!

// var putObjectToS3 = function (bucket, key, data){
//
// };
//=========================================================================================================================================
//Editing anything below this line might break your skill.
//=========================================================================================================================================

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.appId = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

const handlers = {
    'LaunchRequest': function () {
        this.emit('feedtoto');
    },
    'PersonIntent': function () {

    },
    'putObjectToS3': function () {
        console.log("putObjectToS3 handler");
    },
    'feedtoto': function () {
        var feedResponseSpeak = "";
        var retText = "";


        var intent = this.event.request.intent.name;
        var durationValue = isSlotValidDuration(this.event.request, "feedduration");
        console.log("feedduration: " + durationValue);
        var duration = moment.duration(durationValue);
        console.log("parsed duration: " + duration); // correct!
        var hourDuration = moment.duration(durationValue).hours();
        var minuteDuration = moment.duration(durationValue).minutes();
        var secondDuration = moment.duration(durationValue).seconds();

        var hourSlotValue = hourDuration;
        var minuteSlotValue = minuteDuration;

        console.log("hourSlotValue, minuteSlotValue, secondDuration: " + hourSlotValue + ", " + minuteSlotValue + ", "+ secondDuration);
        if  (hourSlotValue) {
            //slot has a valid value
            if(hourSlotValue.toString() == "?") {
              console.log("hourSlotValue ? hit!");
              feedResponseSpeak = "Sorry, could not get the number of hours.";
              retText = "NaN";
            } else {
              //not ?
              var hours = parseInt(hourSlotValue);
              console.log("parseinthour: " + hours);
              var seconds = hours * 60 * 60;
              retText = "feedinterval:" + seconds.toString();
              feedResponseSpeak += GET_FEED_MESSAGE + "every " + hours.toString() + " hours";
              console.log("hour feedResponseSpeak: " + feedResponseSpeak);
              // console.log("intent.slots.hour: " + this.event.request.intent.slots.hour.value);
          }
        } else if (minuteSlotValue){
          //possibly ?
          if(minuteSlotValue.toString() == "?") {
            console.log("minute ? hit!");
            feedResponseSpeak = "Sorry, could not get the number of minutes.";
            retText = "NaN";
          } else {
            var minutes = parseInt(minuteSlotValue);
            // var minutes = this.event.request.intent.slots.minute.value;
            var seconds = minutes * 60;
            console.log("parseintminutes: " + seconds);
            retText = "feedinterval:" + seconds.toString();
            feedResponseSpeak += GET_FEED_MESSAGE + "every " + minutes.toString() + " minutes";
            // console.log("minutes.toString(): " + minutes.toString());
            console.log("minute feedResponseSpeak: " + feedResponseSpeak);
            // speechOutput="Intent " + intent + ", did not get a value for " + slotName;
          }

        } else if (secondDuration){
          //possibly ?
          if(secondDuration.toString() == "?") {
            console.log("seconds ? hit!");
            feedResponseSpeak = "Sorry, could not get the number of seconds.";
            retText = "NaN";
          } else {
            // var minutes = parseInt(minuteSlotValue);
            // var minutes = this.event.request.intent.slots.minute.value;
            // var seconds = minutes * 60;
            var seconds = secondDuration;
            console.log("parseintsecs: " + seconds);
            retText = "feedinterval:" + seconds.toString();
            feedResponseSpeak += GET_FEED_MESSAGE + "every " + seconds.toString() + " seconds";
            // console.log("minutes.toString(): " + minutes.toString());
            console.log("seconds feedResponseSpeak: " + feedResponseSpeak);
            // speechOutput="Intent " + intent + ", did not get a value for " + slotName;
          }

        } else {
          //now
          feedResponseSpeak += GET_FEED_MESSAGE + "now";
          retText = "now";
          console.log("now.toString(): ");
        }
        
        console.log("feedResponseSpeak: " + feedResponseSpeak);
        const speechOutput = feedResponseSpeak;

        console.log("before all");

        var myParams = {
            Bucket : myBucket,
            Key : myObject,
            Body : retText
        };
        console.log("myParams.Body: " + myParams.Body.toString());

        S3write(myParams,  myResult => {
          console.log("sent     : " + JSON.stringify(myParams));
          console.log("received : " + JSON.stringify(myResult));

          this.response.cardRenderer(SKILL_NAME, retText);
          this.response.speak(speechOutput);
          this.emit(':responseReady');
        });

        console.log('done');
        console.log("after all");

    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput).listen(reprompt);
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
};

// 3. Helper Function  =================================================================================================

function S3write(params, callback) {
    // call AWS S3
    var AWS = require('aws-sdk');
    var s3 = new AWS.S3();

    s3.putObject(params, function(err, data) {
        if(err) { console.log(err, err.stack); }
        else {
            callback(data["ETag"]);

        }
    });
}

function isSlotValid(request, slotName){
    if(request.intent && request.intent.slots) {
      var slot = request.intent.slots[slotName];
      console.log("request = "+JSON.stringify(request)); //uncomment if you want to see the request
      var slotValue;

      //if we have a slot, get the text and store it into speechOutput
      if (slot && slot.value) {
          //we have a value in the slot
          slotValue = slot.value.toLowerCase();
          return slotValue;
      } else {
          //we didn't get a value in the slot.
          return false;
      }
    } else {
      return false;
    }

}
function isSlotValidDuration(request, slotName){
    if(request.intent && request.intent.slots) {
      var slot = request.intent.slots[slotName];
      console.log("duration request = "+JSON.stringify(request)); //uncomment if you want to see the request
      var slotValue;

      //if we have a slot, get the text and store it into speechOutput
      if (slot && slot.value) {
          //we have a value in the slot
          slotValue = slot.value;
          return slotValue;
      } else {
          //we didn't get a value in the slot.
          return false;
      }
    } else {
      return false;
    }

}

Credits

pseudozach

pseudozach

1 project • 2 followers
too many things to do, not enough time.

Comments