Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Tuesday, 10 December 2013

Android to Arduino: the code

In a follow up to the Android to Arduino post, here's the code used. The code currently takes a command from an Android app (a button press), and sends a value to the Arduino over serial. In the mean time the arduino is sending a "nop" string back to the app every second. Not much, but enough to prove input & output over serial.
Video of it working here
Arduino code:

 #include  String inData;
Servo myservo;
int pos=0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  myservo.attach(14);
}

// the loop routine runs over and over again forever:
void loop() {
      while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 

        // Process message when new line character is recieved
        if (recieved == '\n')
        {
            Serial.print("Arduino Received: ");
            Serial.print(inData);
           
            Serial.print("\n");
            
            inData.trim();
            int dataAsInt=inData.toInt();
            if(dataAsInt>0 && dataAsInt<360 buffer="" clear="" dataasint="" delay="" indata="" myservo.write="" nop="" pre="" recieved="" serial.println="">
The Java app is based on the example from https://github.com/mik3y/usb-serial-for-android/tree/master/UsbSerialExamples but has a button with an onClick function with mSerialIoManager.writeAsync("10\n".getBytes());

Monday, 2 December 2013

Linking Android to Arduino

I've started our latest project (to be unveiled soon). It requires Arduino to receive commands from an Android device. It needs to be as light as possible, so ideally needs to be powered from the USB connection.

I've seen a number of solutions using Android ADK and special Arduinos, but this seems rather unnecessary. The problem seems to be the USB communication on either side, requiring USB sheilds (http://www.freetronics.com/products/usbdroid#.Upz8jx8hG1E). Alternatively there's Bluetooth (http://blog.arduino.cc/2013/07/18/how-to-control-arduino-board-using-an-android-phone/), but that would mean an external power source.

So instead, just use a Serial interface. There are Java libraries that can be used on Android. Everything including examples can be found here: https://github.com/mik3y/usb-serial-for-android

 I've hooked up my arduino Uno to an SIII using an A to B USB cable and OTG cable. It all works nicely. Stay tuned for what we're going to use it for ;-)

Saturday, 20 April 2013

Beer fridge on the go




It's been a little while since my last post about the beer fridge so let's get you up to date. Since my last post:
1. My company has sponsored the fridge (so they'll pay for all the parts :-) )
2. We spent a Saturday getting to work on it.
3. It's not finished yet
4. I'm having issues with the MOSFEST/solenoid part


The basic design is as follows:
 A fridge will be locked from the inside by a set of solenoids acting as bar locks. The solenoids and the corresponding LEDs are controlled by an Arduino. The Arduino takes commands from a Raspberry Pi over serial and the Pi will have network connectivity.

 If you want to open the fridge, complete the fiendish hacking challenges on the Pi. Each challenge will control 1 or more solenoid. Unlocking the solenoid will only keep it unlocked for 30 seconds.

On the front of the fridge is a little black box. This has a set of red and green LEDs that will change whenever a lock is opened. There will also be (very importantly), a key lock that will bypass all challenges and power all the solenoids. This is because if (when) the pi or Adruino crashes, I still have a way to open the fridge. Otherwise it would be a matter of crowbar and damage.

fridge with the override lock
The next steps will be getting it in a working state. Currently there is only 1 solenoid (they are an absolute pain to mount correctly given the fridge's build and the small range of motion of the solenoid).

 I was thinking of having a Python script that sent the command over serial. The problems are that the exact serial port can change and also that writing to the serial port requires root permissions. I need to create a user to do this then Suid the python file (possible?).

 Finally it will be a matter of mounting it on the door. I'm a little worried that the condensation inside the fridge will screw with the electronics. Time will tell

Wednesday, 31 October 2012

On the road with Arduino

Well the Arduino UNO kit has arrived and I'm very impressed. Lots of things to try and I'm already well beyond the flashing LED tutorial that is the electronics version of HelloWorld.

Some quick experimenting has shown that the servo is easy to position using this example. Similarly I have built up a quick serial communication app that replies to a specific string (based on this). Interestingly you need to tweak PUTTY to talk to it correctly


// Command line variables
String command; // String input from command prompt
String temp1,temp2; // temporary strings
char inByte; // Byte input from command prompt
char carray[6]; // character array for string to int // manipulation
int a,b,c; // temporary numbers

void setup(){
    Serial.begin(9600);
 }


void loop(){

    // Input serial information:
    if (Serial.available() > 0){
       inByte = Serial.read();
        // only input if a letter, number, =,?,+ are typed!
        if ((inByte >= 65 && inByte <= 90) || (inByte >=97 && inByte <=122) || (inByte >= 48 &&     inByte <=57) || inByte == 43 || inByte == 61 || inByte == 63) {
          command.concat(inByte);

        }
       
     }// end serial.available
     // Process command when NL/CR are entered:
     if (inByte == 10 || inByte == 13){
       if (command.equalsIgnoreCase("hey")){
         Serial.println("hello there!");
       }       command="";      }
}

Thursday, 25 October 2012

R00t beer fridge

So as an update to the previous post, I've been doing some research over the last couple of days and found that:
a) servos are weird things that require crazy Pulse Width Modulation (PWM) to control them. This is not something Raspberry Pis do out of the box. I've seen a few cases where they use an in between piece of hardware to work around this.
b) arduinos can be made to do some crazy stuff. Like run a web server

For these two reasons I have (sadly) moved over to using Arduino. I would love to use a Pi but it's horses for courses and although I think it's possible to use a Pi for a project like this, it's just too much effort.

The Arduino will be delivered tomorrow so I can get to grips with the basics over the weekend and have the first photos next week.

It looks like the locking mechanism will be best done with Meccano. Using servos to move the locks into place will mean that they can be used without a second power source and won't "fail open" if someone was to pull the plug. It will either be a sliding bar, or a latch.