Automating the Nespresso Coffee Machine part 1

wp-1482139836728.jpg

I like drinking coffee first thing in the morning, however preparing coffee in the early morning is not something I’m a big fan of, so I decided to use my particle photon to automate the coffee making process. Thus turning my normal Nespresso machine to an IoT enabled machine.

The steps for this build are quite simple, it requires the following components :

  • Particle Photon
  • Servo
  • Power Bank
  • Paper Clip
  • Rubber Band
  • Velcro Tape

To build it connect the Servo to the photon, noting that the data pin must be connected to one of the PWM enabled pins, in my build I used Pin D0.

Use the paper clip to twist and attach on the servo keeping it in place using the rubber band, this serves as the push rod that’ll press on the coffee maker’s button.

wp-1482139836728.jpg

Attach the servo to the Nespresso machine using velcro and tape, you’ll need to adjust the servo angles on the code to work with your machine and how the servo is attached to its body.

Connect it to IFTTT service and enjoy your coffee, Personally I then created a DO button on my phone to press the first thing in the morning.

Continue reading

Downloading Torrents Remotely

I’ve set up a home server and wanting a simple straight forward way to download torrents remotely I relied on an old hack I’ve heard about but never attempted. You can configure transmission the torrents client to pick up torrents files from a certain directory, sharing this directory on google drive means that you can drop torrent files for transmission to download for you at home.

Screen Shot 2016-03-15 at 4.21.56 PM.png

The problem is you can’t really monitor the progress of the torrents and some of these torrent files may not even start. So I decided to write a small shell script that monitors two events and updates me through push bullet, the first event being the torrent download start (creation of a *.part file) the second event is the completion of the download (new file in the downloads directory).

The script works as follows:

Screen Shot 2016-03-15 at 4.15.30 PM.png

 
#!/bin/bash
partsLocation=<DOWNLOADS LOCATION HERE>
completeLocation=<COMPLETE LOCATION HERE>
logLocation=./TorrentsMonitor.log
pushBulletAPI=<PBAPI KEY HERE>

for i in $(find $partsLocation -name "*.part" -maxdepth 1|sed -e "s/ /_/g");
do 
echo "Handling File"
echo $i;
echo "----------------------"

#apply check here if file exists
countOfParts=$(cat parts.log|grep $i|wc -l)
echo $countOfParts
if [ $countOfParts -gt 0 ]
then 
echo "already listed"
else 
echo "new file, adding to parts.log"
echo $i >> parts.log
curl -u $pushBulletAPI: https://api.pushbullet.com/v2/pushes -d type=note -d title="Tor Started" -d body="Download started for file $i"
fi
done

#-------------------------
#scan for complete files
#-----------------------------

for i in $(find $completeLocation ! -name '*.part' ! -name '*.log' ! -name "*.sh*" ! -name "." -maxdepth 1|sed -e "s/ /_/g");
do 
echo "Handling File"
echo $i;
echo "----------------------"
#apply check here if file exists
countOfComplete=$(cat complete.log|grep $i|wc -l)
echo $countOfComplete
if [ $countOfComplete -gt 0 ]
then 
echo "already listed"
else 
echo "new file, adding to complete.log"
echo $i >> complete.log

curl -u $pushBulletAPI: https://api.pushbullet.com/v2/pushes -d type=note -d title="Tor Completed" -d body="Download Completed for file $i"
fi
done

Deploying A Mobile APP in an Enterprise Environment

The use of mobile application on consumer grade devices is increasing in popularity as more and more companies are using customised apps on mobile devices for field purposes instead of using a purpose built device. Example of such an implementation can range from Biometrics Scanning, Merchandise Delivery or even Taxi Dispatching.

BlogImage_E-ioT.2

Certain risks are associated with this approach since unlike web applications each user is responsible for managing and updating his version, just like the pre web-apps days back when people used desktop applications, These risks include:

  1. Using an obsolete version of the app that is no longer compatible with the backend.
  2. Using a version of the app that include a critical security issue.
  3. Incorrect business process due to the use of an older version of the app.
  4. Using a none official version of the app using the same backend, thus bypassing any front end validations.

There are certain guidelines that can be followed to control the inherent risk, I’m going to list some of them here as a guideline

I. Upgrade Enforcement 

For critical upgrades that renders the previous versions obsolete, for instance changing the business process or introducing a critical security enhancement. The best practice is to break the backends backward compatibility.

Breaking the backend backward compatibility can be done by adding an app version check with every request, including the app version in every request is easy and has a negligible cost on both data and processing yet is very useful when needed. The server response should include an error code that would trigger a “You Must Upgrade Now” message.

II. Upgrade Notification

For less critical updates push notifications can be used to suggest an upgrade to the customer, a more aggressive approach  (for android) would be to handle the push to fire the play store on the application’s URI. The frequency of these notifications can reflect how important this update is.

III. Application Verification

To restrict against none official apps the api should include a verification token, there are many ways to implement this, one of the easiest way would be encrypting one of the fields (timestamp for instance) and sending both the encrypted as well as the none encrypted versions. The backend would then verify the version of the app by comparing the decrypted field against the plain text one, if they do not match the response should indicate that.

There are many ways to implement this, the encrypted field approach happens to be the easiest way to do it.

IV. Root Check/Emulator Check

Rooted phones can offer a malicious user the means to manipulate the backend calls, while keeping the verification field. A root check can be conducted on the device every time an activity is started. Emulators are easy to uncover as well.

V. Malicious Usage checks

Just in case all of your checks fail, backend should conduct even a rudimentary malicious behaviour check, blocking devices that exhibit none expected behaviour.

VI. Connectivity Issues

Even with the advances in cellular service coverage 3G/4G service remains spotty especially in rural areas. There are few ways to mitigate this depending on the nature of the requirements. If no online/sync operations are required you can implement a simple request cashing service, in which server side requests are cashed to be retried when connectivity is available.

VII. Usage Patterns Analytics (for android)

Creating usage heat maps is important when it comes to determining how the people are actually using the applications and whether certain features should be augmented or removed due to lack of usage. Luckily google analytics can be integrated to track usage or activity launches, it can even be used to track individual controls actions.

 

I hope this post was helpful I’m planning to write another post soon on how to conduct unit, QA and scale tests on enterprise apps.

Particle Photon Powered Physical Notifications Device

wpid-20151023_021501.jpg

Having just received my photon maker kit I wanted to build a simple project to show case the photon’s capabilities and so I decided to build a photon powered RGB lantern which I’d light up using IFTTT recipes.

image

The Internet Powered Lamp Lighting Up

The components :

  • Particle Photon
  • 3 x 8mm RGB LEDs (common anode)
  • 330ohm resistor
  • Prototyping board
  • Battery Pack
  • Ikea Paper Lantern

Wiring:
image

  • Connect the 3 RGB in parallel with the  the resistor connected to the common anode.
  • Red to A4
  • Green to A3
  • Red to A2
  • Common anode resistor to D0
  • Battery pack positive to photon Vin
  • Battery pack -Ve to photon GND

Code:

I didn’t want the lantern to light up all of a sudden instead I wanted it to gradually light up.

// -----------------------------------
// Controlling RGB LEDs over the Internet
// 23 October 2015
// -----------------------------------
int led_red = A5;
int led_blue = A4;
int led_green = A3;
int led_on = D0;

int led=D7;

int r_val=255;
int g_val=255;
int b_val=255;
int p_val=0;
int t_val=1;
bool is_on=false;
// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Spark function

void setup()
{
    Serial.begin(9600); 
  
  
   // Pin configuration, 3 PINS, plus anode
   pinMode(led_on, OUTPUT);
   pinMode(led_red, OUTPUT);
   pinMode(led_blue, OUTPUT);
   pinMode(led_green, OUTPUT);

   digitalWrite(led,HIGH);
    
    
    //we declare a photon function to use through IFTTT
   Spark.function("led",ledToggle);
  
    //turn the LEDs off 
   digitalWrite(led_on,LOW);
   analogWrite(led_green,255);
   analogWrite(led_red,255);
   analogWrite(led_blue,255);

}

void loop()
{
   // Nothing to do here
}

/*This is the function IFTTT will be talking to, it accepts a string command
*/

int ledToggle(String command) {
    /* Spark function to receive colour and duration RRRGGGBBBTTT example 000255255001 for RED turn on for 1 second, 
    values are reversed so 000 is the strongest illumination and 255 is off
    */

    is_on=true;
    /*debug bit*/
    Serial.println("c : ");
    Serial.println("command recieved "+command);
    
    /*tokenize string and extract colors*/
    String red=command.substring(0,3);
    String green=command.substring(3,6);
    String blue=command.substring(6,9);
    String backoff_t=command.substring(9,12);
    
    /*debug bit*/
    Serial.println("R"+red);
    Serial.println("G"+green);
    Serial.println("B"+blue);
    Serial.println("t"+backoff_t);

    /*cast string colors*/
    r_val=red.toInt();
    g_val=green.toInt();
    b_val=blue.toInt();
    t_val=backoff_t.toInt();

/*cycle colours on*/
    set_lights();
    
    /*keep lights on for the required duration*/
    delay(1000*t_val);
    
    //reset vars and turn off 
    is_on=false;
    r_val=255;
    g_val=255;
    b_val=255;
    t_val=1;
    
    set_lights();
}

/*
Function used to set lights
*/
int set_lights()
{
    //colour cycle while lighting up
    
    //set colour
    analogWrite(led_red,r_val);
    analogWrite(led_green,g_val);
    analogWrite(led_blue,b_val);
    
    if(is_on)
    {
        // gradual power increase to common anode
        for(int i =0; i<=255; i++)
        {
             delay(5);
             analogWrite(led_on,i);
        }
    }
    else 
    {
        analogWrite(led_on,0);
    }
   
}

IFTTT integration:

Once the code was flashed on my photon, I was able to invoke the photon function over the internet, with the desired RGB colour value and duration sent as a parameter.

IFTTT Action

IFTTT Action, to light up purple for 20 seconds

 

 
**Update: I cleaned up the implementation by using a neopixel stick which greatly reduced the wiring and increased the functionality**
 

USSD API on Android

I’m currently trying to build a small widget that shows you several statistics about your cell phone usage, this would require using USSD codes and catching their responses. I scoured the internet for a solution however wasn’t able to reach one.

The only available solution which is mainly a hack relied on catching the logcat entry and displaying it, unfortunately this was fixed in subsequent versions of android and now USSD response doesn’t appear in the logcat.

There is even a Facebook page created just to request this feature from google…anyway if anybody manages to find a way to handle USSD responses please do share.

Sharing an Image from Bitmap in Android

I had a project that required downloading an image from the internet then calling the sharing intent to share that bitmap, I went through countless entries until I reached the conclusion that it is impossible to share an image directly from a bitmap object, the bitmap must be saved first and then you can share it and if you dont want to keep a copy on disk you have to explicitly delete it.

Here are how I ended up doing it.

1. Save BitMap

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ProjectName";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, name);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
abmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}

2. Share URI from file

Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");

intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");

intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Cover Image"));

 

If you can come up with a hack to share directly from BitMap please do share, but I dont believe it is possible.