SalesForce Connected Cajon Drum

thumb_IMG_0519_1024

At the End of last Year I started thinking about connecting a Cajon to the Internet, It was really fun and I wanted to Share the steps to reproduce it.

    • You will need:

Step 1

The fist thing you need to do is assemble the cajon (everything but the front piece where we will put the sensors), this is quite fun just follow the instructions provided. The only recommendation I have is that you don’t really need all the tools mentioned in the cajon instructions, especially for holding the cajon together while it dries, you can use books or whatever you have that is heavy.

thumb_IMG_0357_1024

Make sure you let the glue do its magic and then you can sand it down to give a nice look, or have your cat do it.
thumb_IMG_0367_1024

Cat in a box

While the cajon dries out, you can work on the piezo sensors, piezo sensors have 2 cables normally a red and a black one, when you pass power thru it (3 or 5 volts) you will get an analog read between 0-1024, you can read more in here :Piezo Sensors and Arduino, I used this link to learn more about these sensors and get an example of the Arduino code.

I wanted to have a long cable for the Cajon, so i could move it around easily. I went to Home Depot and bought like 9 feet of Cat 5 Cable, this turned to be a great and cheap idea, I pealed the cable and used each of the individual inner cables to attach to each sensor, see image bellow:

thumb_IMG_0366_1024

I also glued the sensors to the front piece of the Cajon, I am sure gluing it wasn’t the best idea, I will attempt later with tape, since glue seems to add some noise to the sensor deformation (you can work on this calibrating initial values on Arduino).

Screw the front piece of the Cajon as a final step, have the Cat 5 Cable come out of the back of it.
thumb_IMG_0520_1024

Connect each sensor to the BreadBoard with the Resistors and then to the Arduino as you can see in the image

thumb_IMG_0522_1024

You can get the schematics and Fritzing file in here: Fritzing File

Once you have this properly connected you can use the Arduino Serial Monitor to see the values you are getting, the Arduino Code I used is the next:

// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int knockSensor2 = A1; // the piezo is connected to analog pin 1
const int knockSensor3 = A2; // the piezo is connected to analog pin 2
const int knockSensor4 = A3; // the piezo is connected to analog pin 3
const int knockSensor5 = A4; // the piezo is connected to analog pin 4

const int threshold = 400;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int sensorReading2 = 0;      // variable to store the value read from the sensor pin
int sensorReading3 = 0;      // variable to store the value read from the sensor pin
int sensorReading4 = 0;      // variable to store the value read from the sensor pin
int sensorReading5 = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
 int avg = 0;
  int avg2 = 0;
  int avg3 = 0;
  int avg4 = 0;
  int avg5 = 0;
  int oldvalue = 0;
  int oldvalue2 = 0;
  int oldvalue3 = 0;
  int oldvalue4 = 0;
  int oldvalue5 = 0;
  
  int loopnumber=0;

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  Serial.begin(9600);       // use the serial port
 
}

void loop() {

   avg = ((sensorReading + oldvalue) /2);
   avg2 = ((sensorReading2 + oldvalue2) /2);
   avg3 = ((sensorReading3 + oldvalue3) /2);
   avg4 = ((sensorReading4 + oldvalue4) /2);
   avg5 = ((sensorReading5 + oldvalue5) /2);
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
  sensorReading2 = analogRead(knockSensor2);
  sensorReading3 = analogRead(knockSensor3);
  sensorReading4 = analogRead(knockSensor4);
  sensorReading5 = analogRead(knockSensor5);
  
  oldvalue = sensorReading;
  oldvalue2 = sensorReading2;
  oldvalue3 = sensorReading3;
  oldvalue4= sensorReading4;
  oldvalue5 = sensorReading5;
  
  loopnumber++;

 
  if (sensorReading >= threshold || sensorReading2 >= threshold  || sensorReading3 >= threshold  || sensorReading4 >= threshold  || sensorReading5 >= threshold) {
  Serial.print("{ \"A0\": "); Serial.print(sensorReading); Serial.print(",  ");
 Serial.print("\"A1\": "); Serial.print(sensorReading2); Serial.print(",  ");
 Serial.print("\"A2\": "); Serial.print(sensorReading3); Serial.print(",  ");
 Serial.print("\"A3\": "); Serial.print(sensorReading4); Serial.print(",  ");
  Serial.print("\"A4\": "); Serial.print(sensorReading5); Serial.println("  }");//Serial.println("uT”);
  delay(3000);  // delay to avoid overloading the serial port buffer
  }else{
     delay(3000);
  }
}


The Arduino Code, sends via Serial (USB) data to the raspberry in Json Format if the value of the sensor is above the threshold you defined.

On the Raspberry you want to have a running Python script running reading this data and sending it to your SalesForce Org.

import time
import json
from datetime import datetime
import serial
from simple_salesforce import Salesforce
sf = Salesforce(username='youruser@user.com', password='yourpassword', security_token='yourtoken') 
#print sf.rafael__Drum__c.describe()
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 3000)
ser.readline()
ser.readline()

while True:
    sensorMsgStr = ser.readline().strip('\r')
    print sensorMsgStr
    sensorMsg = json.loads(sensorMsgStr)
    print sensorMsg['A0']
    updateDrum = sf.rafael__Drum__c.update('a08o000000Lrua7AAB',{'rafael__sensorA0__c': sensorMsg['A0'], 'rafael__sensorA1__c': sensorMsg['A1'],"rafael__sensorA2__c":sensorMsg['A2'],"rafael__sensorA3__c":sensorMsg['A3'],"rafael__sensorA4__c":sensorMsg['A4']})
    print updateDrum

On your SalesForce Developer Edition you want to have a Custom object to receive each of the drum Data.
Screen Shot 2016-01-15 at 10.29.54 PM

You will have to create a subscription to the object to use streaming API on your page.
I acchieve mine by following this SalesForce article Streaming API

Make sure that on your PushTopic the SOQL query points to the Sensor Fields shown in the Previous Image.

Then you just need a VF page that will include Processing to display an animation when we play the Drum.

   

Well that is basically everything, I have been trying to create a video about it but seems like video production is not one of my strengths, send me an email or post a comment if you have any questions.

Thank you,
Rafael

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.