Scripting an Ice Cream Delivery with the Android SDK

Today, a smartphone-connected taxi company called Uber is operating five ice cream trucks in Seattle. You click a button in the Uber app and select your location, and an ice cream truck comes to you. Or at least that’s what should happen. Even with today’s weather (read: periodic torrential rain), people are clamoring to get their ice cream due to the fact that it’s only available for one day… meaning almost nobody is actually able to get a spot in the queue. Uber’s Twitter feed is full of advice telling customers just keep hitting the button, but man, I have work to do. I can’t sit here and press the little ice cream icon all day. Something has got to give.

The Android SDK contains a tool called monkeyrunner. Monkeyrunner lets you simulate touch events, take screenshots, etc… and it does it through a very simple Python library. I decided to see if I could write a little script to keep pressing the ice cream button in Uber’s app, and it only took a few minutes.

from time import sleep
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Attach to Android device
d = MonkeyRunner.waitForConnection()

# Take a screenshot of the "sorry, keep trying" screen for reference
raw_input("Get to the error screen, then hit enter.\n")
oldpic = d.takeSnapshot()

while True:
    # Touch the ice cream button
    d.touch(540, 200, MonkeyDevice.DOWN_AND_UP)
    sleep(4)
    newpic = d.takeSnapshot()
	# If the screen looks like our old error
	# screen (95%), hit OK and just keep going (in 10 seconds)
    if newpic.sameAs(oldpic, .95):
        d.touch(460, 700, MonkeyDevice.DOWN_AND_UP)
        sleep(10)
	# But if the screen has changed, stop running the loop.
    else:
		break

I still don’t have ice cream, but the script is running and working.

Bi-directional Caller ID Spoofing with Asterisk

Caller ID spoofing has been around for a long time. Way back when (well, 2004), an article was published on rootsecure.net with an included Perl script, detailing how to use Asterisk to change your caller ID information, in order to pretend to be somebody else. I had some fun toying with that (heh...), and noticed that some commercial services began to pop up which offered the same capability for a price. SpoofCard is the one that I’ve seen around the most, but many exist. Bah. Setting up an Asterisk server is free if you have access to a Linux box, and outbound SIP trunking is dead cheap. With flowroute.com, I pay less than a cent per minute to place calls within the U.S. The perl script, obviously, is also free.

The other day I saw something as part of a commercial spoofing service that I hadn’t seen before: bidirectional caller ID spoofing. Basically, it will call friend A from friend B’s number, friend B from friend A’s number, and record the result. This leads to something along the lines of “You called me!” “No, you called me!” As fun as that is, websites like PrankDial are charging a lot of money to do something that is, in practice, very simple.

I haven’t seen a script to do this posted on the internet yet, so I created one and put it on github. You’ll have to set up the Asterisk server by yourself, but implementing the script is easy.

  1. Put dual_cid_spoof.py in the /usr/share/asterisk/agi-bin directory
  2. Make it executable to the asterisk user
  3. Add an extension to extensions.conf that calls the script, using pattern matching to give it arguments. For example:
[spoof]
exten => _37NXXNXXXXXXNXXNXXXXXX,1,Answer
exten => _37NXXNXXXXXXNXXNXXXXXX,2,AGI(dualcidspoof.py,${EXTEN:2:10},${EXTEN:12:10})

This uses the extension 37, but that part can be changed. This pattern matches any calls that have the number 37 followed by two 10-digit phone numbers, and then pulls those numbers out and uses them as arguments to the AGI script ${EXTEN:2:10} means take the extension, at index 2, and pull out 10 digits. To execute it with phone numbers 123-123-1111 and 222-202-2020, I would dial 3712312311112222022020.

MeetMe is required to use this script, since it works by placing both users into a conference room together. The default conference room number is 1234, but that can be changed in the script. Also necessary is changing the outgoing SIP trunk in the Python script from “flowroute” to whatever the name of your outgoing SIP trunk is. Both of these are variables at the top of the script. Execution flow works like this:

  • Create two .call files, each with opposite CID/destination info. These files are set to dump the callees into a conference room when they answer.
  • Place the files in /var/spool/outgoing/asterisk, to be automatically processed by the Asterisk spooler.
  • Connect you into the same conference room as a muted admin, giving you the ability to kick users and listen without being heard.

Enjoy, and feel free to ask questions. Again, you can get the script here.

Arduino, Circular Linked Lists, and Christmas Lights

EDIT: Yes, the linked list solution is not the most efficient one (and in fact could be dangerous if used with much more memory than this when malloc’d on a small chip like the Arduino. The “right” way is using a circular buffer. A sketch utilizing a circular buffer has been added to the github project.

Around two years ago, a fellow by the name of darco posted an article called “Hacking Christmas Lights” on his blog. He was nice enough to reverse engineer the LED data bus protocol of the GE Color Effects christmas lights, as well as release some code for an ATMel microcontroller. This was of course turned into an Arduino library soon after.

I had an idea for something to do with these lights: I wanted a network service I could connect to which would pick a random color from a pre-defined list, tell me which color it picked, and then “push” that color onto the string of lights. Each time the service was hit, a new color would enter the string of lights. Each other color would move to the next light until it was at the end of the string, where it would “fall off.”

I decided to do all the networking and color choosing in a Python script on my laptop, leaving the Arduino with just the task of controlling the lights and storing their colors. The GEColorEffects library would handle the control, so all I needed to do on that end was find a data structure to store the colors in. Usually I would just use an array, but this approach gets ugly when you want to “move” each color to the next light. It would involve copying every single color value in the array to the next location before adding the new color.

// colors are 12 bits, so can be stored in a 16-bit type easily
uint16_t lights[NUMLIGHTS]; // populated somewhere else in code
int i;

--snip--

for (i = NUMLIGHTS; i > 1; i--) {
    lights[i-1] = lights[i-2];
}

lights[0] = newcolor;

The previous code would have to be called every time I wanted to add a new color to the string of lights. It would probably work just fine (meaning sufficiently fast) considering that it’s only copying 72 bytes, but it feels clunky. I wanted an approach which felt elegant. The thing that came to mind after some shower-aided thinking (all good programming ideas happen in the shower) was a circular linked list. A linked list is a collection of structures, 0r collections of data, with the unique characteristic that each structure contains a pointer to the next one. In a doubly-linked list, each structure contains a pointer to the previous and the next one. In a circular linked list, the last structure points to the first, effectively making it act like a circle. If you were to follow the path forever, you’d never reach an end.

The nice part of using circular linked lists is that you can just choose where the “beginning” is dynamically.

struct RGB
{
    // each color is only 4 bits but stored as an 8-bit type
    uint8_t r;
    uint8_t g;
    uint8_t b;
    struct RGB *next; // pointer to the next RGB structure
};
typedef struct RGB rgb;

rgb *head;

void populate_linked_list()
{
  int i;
  rgb *curr;
  curr = head = (rgb*)malloc(sizeof(rgb)); // allocate first struct as head

  for (i = 0; i < lightCount-1; i++) { // allocate 35 more (36 lights total)
      curr->r = 15; // they all start out red
      curr->g = curr->b = 0;
      curr->next = (rgb*)malloc(sizeof(rgb));
      curr = curr->next;
  }
  // and finally, the last struct
  curr->r = 15;
  curr->g = curr->b = 0;
  curr->next = head; // the "next" after the last, is the first
  curr = head;
}

The lights are set by iterating through the list and using the GEColorEffects library to set each light to the color represented by each structure. Here comes the fun part. To “push” a color onto the set of lights (and move each other color to the next light), all you have to do is write the color values to the current head of the list, and then move the head to point to the next structure. Keep in mind that we’re pushing colors onto the end of the list, and each color at the very beginning of the list falls off when we do that.

void add_color(uint8_t r, uint8_t g, uint8_t b)
{
  head->r = r;
  head->g = g;
  head->b = b;
  head = head->next;
}

We’ve just moved the beginning/head of the list one light forward (meaning the 2nd color on the string of lights moves to the 1st, 3rd moves to 2nd etc.) and put the data for the new color in the previous location of the head, which is now the very end of the list. The colors of the lights appear to move accordingly.

I put the code on github here. It comes with the Arduino sketch which implements the linked list setup as well as a simple serial interface. Read the README for more info about that.

The Energy of Making

I returned from Maker Faire Bay Area yesterday. The wonderful mix of conference, expo, and fair always fills me with a lot of energy to make things. Just being around people like the members of Noisebridge, the inventors of sugru, Mitch Altman (inventor of the TV-B-Gone), etc., osmotically sparks my creativity and leaves me thinking: it feels so good to make things. There are people out there who haven’t made a thing in their life, and they need to be exposed to it.

Cory Doctorow captures what I’m saying in a quote from his book Little Brother:

If you’ve never programmed a computer, you should. There’s nothing like it in the whole world. When you program a computer, it does exactly what you tell it to do. It’s like designing a machine — any machine, like a car, like a faucet, like a gas-hinge for a door — using math and instructions. It’s awesome in the truest sense: it can fill you with awe.

A computer is the most complicated machine you’ll ever use. It’s made of billions of micro-miniaturized transistors that can be configured to run any program you can imagine. But when you sit down at the keyboard and write a line of code, those transistors do what you tell them to.

Most of us will never build a car. Pretty much none of us will ever create an aviation system. Design a building. Lay out a city.

While the quote discusses programming specifically, it explains the brilliance of using the tools available to you to invent new tools. Check out a hackerspace near you, or the MAKE homepage, or Hack-A-Day. Look at all the links in this single post: all people who make things and want to help others do the same.

Go make something.

Amusing Encounter with the Police

I went to a party last night (at my friend J’s house), and it ended up with about 10 boys in a stuffy basement. Two friends of mine (G and N for this story’s purposes) and I decided to take a long walk. It was about midnight and we just wanted some fresh air, so we left.

J’s house. is near two public schools, so we decided to go walk around near them. We weren’t doing anything wrong– just walking around and having a conversation. N decided to give a “thumbs up” sign to a passing car, but we really thought nothing of it. It’s a lot better than flipping them off– but that probably would have got us into less trouble in the end.

We started heading home after around 45 minutes, but about halfway there a cop car stops right behind us. The cop gets out of the car and says “stop right there! Why are you boys walking late at night?” Our response of “to get some fresh air” apparently was invalid to him. While in the middle of asking us some questions like “where are you headed?” and “what are your names?”, another car shows up. Officer R. Christopher of the BIPD (our first officer) begins to explain to us that somebody called in about kids walking along the road hitchhiking. He then explains that in the past week there have been five fires set, a school broken into, and a house vandalized. He adds that most of these are confirmed to be done by juveniles. He goes on to give us the scare treatment: “So you know, if we get a call about anything wrong here, or at the bus barn, or at the schools, you know who our first suspects will be?” The three of us stayed silent throughout most of the ordeal, only speaking to give information. The two officers treat us like children and say things like “you see the problem here?” and “now I’m a suspicious guy.”

For future reference, giving the thumbs-up sign to a car is frequently misinterpreted.

Officer 2 looks at the bottom of our shoes while officer 1 calls all of our parents and asks if we’re supposed to be where we are. G’s father answers and confirms that he’s free to continue. N’s father comes to get him and take him back to the house where we were headed back to anyways. My parents both didn’t answer their phones, so by protocol the officers had to escort me back to J’s house in the cop car. G comes along for the ride.

G and I stand laughing behind the officer while he knocks on the door. A few of the boys at the party come up the stairs expecting only us, but see the cop and freak out. “J! There’s a cop outside your door!!” J gets to the door and gives G and I the most disgusted look I’ve ever seen. “What the hell did you guys do?” The officer tells J to wake up his parents and then the officer explains the situation to them. G and I are free to go back to the house, but now N’s father is pissed off, J’s parents are woken up, and J won’t talk to us.

This subsides quickly though, as we all realize how silly the situation is.

That was my first ever encounter with the police, and I think I’ve learned something from it: in a town as small as mine, the cops don’t have too much to do. This would explain why they sent two cars. Hopefully I won’t have to deal with them too much more. I would hope that police could treat us more like adults in the future. And I hope next time I spend time with the police, I do something worth getting caught for. ;)

Noisebridge

image

I’d like to extend a thank you to San Francisco’s Noisebridge hackerspace for being so hospitable and friendly towards me during my time here. On Friday when I arrived in SFO, I took the BART straight to Noisebridge and hung out there till that night. It was nice to be in an environment where I could work, learn, and talk, despite being a stranger in this city.

I’ve met a lot of interesting people and was even invited out to dinner that night. San Franciscans really love their lights, I’ve found. Our crepe restaraunt had fancy fading rainbow lights.

I’ve included a picture of Noisebridge at a fairly dormant time.

Making Fulgurites

We recently acquired a pole pig at the lab. We bought it off a guy who didn’t have time for a high voltage hobby any more, and so far we’ve made a Jacob’s ladder with it, and used it to make fulgurites.

Fulgurites are the figures made by lightning hitting sand (or other melty grainy materials). We don’t have lightning but we can definitely simulate it with the pole pig. We start by putting sand (100lb for $7 at Home Depot) in a terracotta pot. We stick electrodes from the transformer straight into the pot and turn it on. The result comes out like this.

They're beautiful and fractally.

We then epoxy them into nice solid pieces of art. I made a video about the process for my science class.

Fulgurite Production at Hackerbot Labs from Nick Mooney on Vimeo.

Credits to Pip aka @yoyojedi for helping me out with this. He’s the fulgurite master and was nice enough to teach me how.

Verizon Sent Me the Wrong Phone

On Wednesday, April 28, my Motorola Droid broke (the second time). A piece inside the microUSB port fell out while unplugging my phone, leaving the phone un-chargeable unless put in an odd position at an angle: not fun. No problem though, right? I’m still under warranty. Sure enough, Verizon was nice enough to send me a replacement phone, overnight, free of charge. Awesome! It shipped out April 29 and I got it on the 30th.

When the phone arrived I went through the fairly standard procedure: wipe data from old phone, keep battery/card/back-cover, install them in new phone and activate by dialing *228. In a few minutes after waiting on crappy activation music, it was done. My phone instantly picked up a signal and I went on my merry way packaging up the old phone to send back.

I started to notice some weird things that night though. The first was that I was told I had to dial the area code while ‘roaming’. This was weird, but I figured dialing *22899 to refresh the cell tower data would fix it. No luck though. Oh well– it’ll sort itself out. Then I started getting messages from my friends asking why I wasn’t answering my phone. I decided to run an experiment by calling my home phone from my cell. The caller ID showed up as a 410 area code (Maryland). It was bidirectional too, meaning my phone rang when I called the Maryland number. My old local number went straight to voicemail.

I called up Verizon and told them about the issue, and the rep I had was very confused by the situation. I was put on hold a couple times and told that he would have to converse with a higher-up. Verizon claims that they can’t program the phone remotely to my correct number and decide to ship me yet another new phone, same as last time. Meanwhile, I have no access to my old number and some poor soul in Maryland probably wants their number back. I can make and receive calls on this Maryland number, and Verizon recognizes me as the person on that account. Luckily though, it was secure enough such that I couldn’t make any account changes without the social security number.

The new (Droid #4) phone is set to arrive tomorrow, and hopefully it’ll work just fine. I’ll have 3 Droids in the house then… but I have to send 2 of them back via prepaid FedEx.

Robot Arm Project — New Life

If you haven’t gathered by now, I’m back from New Zealand. I’m just too lazy to write about it.

The Robot Arm project has been gathering some dust for a while. Code is a little slow to develop since I can only access the arm itself on Saturdays. That said, however, I’ve fixed  a problem! To connect to the Gamoto motor control board, I need a USB-Serial adapter and then a janky-as-shit serial-to-Gamoto adapter which connects to some protoboard, which connects finally to the Gamoto.  I learned Saturday that I rushed my first job in creating the janky-as-shit serial-to-Gamoto adapter and barely connected the wire to the correct pin. It was a cold solder joint, so I made a new adapter, and I wanted to share a trick that some people might not know.

When soldering wires to headers (sets of pins), you can put the bottom of the header in a protoboard, hook the wire around the pin, and solder like that. It works really well and is much easier than soldering without the hook.

Anyways, code will be posted soon. It’s currently being edited constantly in an effort to improve/optimize/extend. This is my first real Python project so I’m learning all the time.

Skeeter Eater

Tonight, I ate mosquito larvae. I was given $50 for doing so and it was totally worth it. Before you freak out: these are laboratory grade, pathogen-free mosquitoes which are stored in perfectly clean water. They’re full of protein and pose no threat. Since they were all in water, I didn’t notice at all. Just washed it down with some Mountain Dew. Despite the fact that it led to the nickname “Skeeter Eater” and internet infamy, I got paid! A++ would ingest again. Have fun with the video below.