Making things, writing code, wasting time...

Tag: Intel Galileo

Making a Twitter-Bot on your Galileo or Raspberry Pi

Twitter have developed an API (Application Programming Interface) for their website, which makes it really easy to send and receive Tweets from your Raspberry Pi or Galileo! The Twitter API takes all of the hard work out of writing a program to interface with Twitter, There are several ways to access the Twitter API, the easiest of which (in my opinion anyway 🙂 is to use the Twython package of the Python language.

Differences between Raspberry Pi and the Galileo:

It’s really easy to install Twython on the Raspberry Pi, a little harder to install on the Galileo – so for that reason, I’ll show the step-by-step instructions from the Galileo install. The only difference is the Galileo doesn’t require you to use the sudo command as you already have root permissions set. For example, when editing a file with the Galileo you would use:

# nano my_file.txt

When editing the file on a Raspberry Pi you would use:

# sudo nano my_file.txt

Changing the date on the Galileo:

In order to install some of these packages, you’ll need to update the date and time of your Galileo – this isn’t automatically done when you connect to the internet as you may expect. If you don’t update your date and time, you’ll get SSL certification errors when you try to download the Twython package.

To change the date and time – use the following command in the format year-month-day hour:minute:second:

# date --set="2015-01-20 10:00:00"

Installing Twython (and other Python packages):

In order to connect our computer to Twitter, we’ll need to download some Python packages:

  1. setuptools – this will allow you to “Easily download, build, install, upgrade, and uninstall Python packages.”
  2. pip – this is a  Python package installer.
  3. twython – this is the package which will actually interface with Twitter.

Here are the commands to install these packages – remember if you’re on a Raspberry Pi you’ll need to put “sudo” in front of each command.

# apt-get install python-setuptools

Here’s what that looks like on your computer, when asked “do you want to continue” as per the picture below – enter “y”. to continue.

Package 1 of 3.

Installing the setuptools Python package.

Next, install the “pip” package:

# easy_install pip

Finally, using pip – install the Twython package:

# pip install twython

 

Creating a Twitter App:

To create a Twitter App, you’ll need to sign up to Twitter and register the account as an application. This is important as you’ll need to verify this App with Twitter every time you use it. The verification method Twitter uses is called OAuth 2.0 to verify your App, this means you’ll never have to supply your password to 3rd party App developers but it does make it a little harder to verify your App – the good news is, Twython and other API’s handle all the OAuth pain, all you need to do is register your App and save the information. Register your app here: https://apps.twitter.com/ Click on “Create New App” and enter your information.

Let the fun begin!

Creating an App.

 

Changing App permissions:

As this is your App and you’ll want to be able to play around with it – you can chance the App permissions to allow you to read, write and access your direct messages. You can change these permissions at a later stage.

Change permissions.

Change permissions.

Authorize your account:

You need to create your access token and access token secret before you use your App. Click on the “Keys and access tokens” tab. Click on the “Create my access token” button.

Generate your secret token.

Generate your secret token.

You should now have all 4 pieces of required information:

  1. Consumer Key (API key).
  2. Consumer Secret (API Secret).
  3. Access Token
  4. Access Token Secret.

Now it’s time to write some Python!

Writing the Python:

Creating a Python file:

On your Raspberry Pi or Galileo, create a file called “Tweet.py” using the following command:

# nano Tweet.py

Now paste in the Python code:

Twitter Authorization:

In order to send  a Tweet, you’ll need to send Twitter your OAuth information. This process is handled by the Twython package. Here we are creating 4 string objects and a Twython object called “twitter”. When we create the Twython object we are passing the 4 strings to it an arguments. These are the access keys you generated in the previous section.

CONSUMER_KEY = '<YOUR CONSUMER_KEY>'
CONSUMER_SECRET = '<YOUR CONSUMER_SECRET>'
ACCESS_KEY = '<YOUR ACCESS_KEY>'
ACCESS_SECRET = '<YOUR ACCESS_SECRET>'
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

Reading in a command line argument:

To make the code a bit more flexible, we can pass the text we want to Tweet into the Python script as a command line argument. This is done by using the system “argv” parameter. In our case – we only want to take the first 140 characters of this text, as this is the character limit set by Twitter for each Tweet. We do that by using the command:

sys.argv[1][:140]

Executing the code:

You can execute the code from the command line like so:

# python Tweet.py "Hello world."

Here’s what that looks like on the Galileo:

First Tweet!

Hello World!

Then check your Twitter Bot!

Installing Debian on the Intel Galileo

Some of the guys in the Intel communities forum have done a great job of porting the Debian Linux distribution to the Intel Galileo. If you’re not familiar with the Debian, it is an open source operating system used on Linux systems. This is great news for the Galileo as it makes it very easy to port any Raspberry Pi based projects directly to the Galileo!

Debian was ported to the Galileo by StuartAnderson, who posted on the Intel Communities page.

You can download the Linux image from here!

What you’ll need:

  1. Galileo.
  2. SD card.
  3. Serial cable.

Downloading the Galileo Debian disk image:

You can find the Galileo Debian image here. Click on the “SD card image” file. This will take some time as the file is 900MB. While the disk image is downloading, we can prepare the SD card.

Formatting your SD Card:

If your SD card has been used to write a Linux image before (used for a Raspberry Pi or Galileo Little Linux image), Windows can have trouble formatting the SD card. This is because Windows can only see the FAT32 partition and won’t see the much bigger Linux partition.

For this reason I used the SDformatter program from the SD Association, you can find it here (Click on Windows or Mac on the left).

You can see from the picture, I’m using an 8GB SD card (7.4GB shown but it’s 8GB, it’s normal for SD cards to be about 10% smaller than the advertised disk size), in my case Windows format only saw 56MB of the disk and ignored the remaining 7.35GB! However with the SD Formater program the full 7.4GB can be seen and will be formatted. Click on the Format button to start the process.

...

Using Rawrite32 to write the Galileo Debian disk image.

Writing the Debian Image to an SD card:

To write the Linux Debian image to the SD card you’ll need to download a disk image tool, as recommended by the original creators, I used the Rawrite32 Disk Image Tool software, found here.

After you’ve downloaded the Debian image, open Rawrite32 and select the Debian image to write.

Whoop - Debian!!!

Writing the Galileo Debian image.

You’ll see a pretty scary warning like this, but as long as you’re sure there’s no important data on the SD card that you need, go ahead and click “Yes”!

Alt text for this picture.

Click OK to start writing the Disk Image.

Once the Disk Image tool has finished writing – you’ll see something like this:

...

Finished writing the image.

You can see from the image above, the image file takes up 900MB, less than 1GB of my 7.4GB SD card. This means that the remaining 6.5GB of space on the SD card will not be available to me when I try to the Linux image. In order to make the remaining 6.5GB of space available, we need to grow the size of our partition on the SD card.

Now that we have a Debian Linux system available – this can be done from the Galileo itself!

Expanding the filesystem:

To expand the filesystem, first we need to expand the partition, then expand the file system to fill that partition.

When formatting the SD card, we could see that the 8GB SD card had a size of 7.4GB, so I’m going to expand my partition to 7.4GB just to be on the safe side.

Disk size before partitioning:

Not enough room!

Before resizing the filesystem.

Expanding the partition:

To resize the partition, we are going to use a program called Parted. This can be used to create, resize, move and destroy disk partitions. The first command used is the resizepart command.

# parted /dev/mmcblk0 resizepart 2

Select “y” to continue and then enter the size of the partition you want to create, here’s where I entered the 7.4GB (which works out as 7400MB).

# parted /dev/mmcblk0 resizepart 2

# parted /dev/mmcblk0 resizepart 2

You’ll need to reboot here!

# reboot

Expanding the filesystem:

When the partition has been resized we create a journal inode.

# tune2fs -j /dev/mmcblk0p2
# tune2fs -j /dev/mmcblk0p2

# tune2fs -j /dev/mmcblk0p2

You’ll need to reboot here!

# reboot

When the reboot has finished, finish by expanding the filesystem to the partition size.

# resize2fs /dev/mmcblk0p2
# resize2fs /dev/mmcblk0p2

# resize2fs /dev/mmcblk0p2

The resize2fs will expand the filesystem to fill the partition, this will take about 10 minutes but when it’s finished you should see a message like this, above.

Disk size after expanding the filesystem:

Loads of room!

Disk size after expanding the partition.

From the above image you can see, we’ve grown the filesystem to fill the partition, so we now have a total disk size of 6.8GB, with 6GB available to play with!

Now that the filesystem is expanded, it’s a good idea to update the system.

apt-get update && apt-get dist-upgrade

The update and upgrade will take about 10 minutes, then you can have fun playing with your new Debian computer.

Making an IR remote control for a Nikon camera:

Introduction:

I started this project as a bit fun, when I first bought my Galileo board. As this was my first Arduino project, I decided to try downloading an already completed project and get it working on my own board. I soon realised this wouldn’t be as easy as I thought…
As the Intel Galileo is actually a Linux computer emulating the Arduino IDE, the GPIO speeds are a lot slower on the Galileo when compared to a dedicated microcontroller. This proved to be a big problem for me, as it meant I couldn’t just run any of the timing or frequency dependant Arduino projects directly on the Galileo.

With this in mind, I decided to build the Infra-Red remote control for my Nikon SLR camera. As the Arduino libraries would not work for the Galileo, I decided to build my own.

How the command signal works:

The command signal is transmitted on a 38.4KHz carrier wave, the command signal is sent in a binary format.
This means, when sending a binary “1”, the LED is turned on and off at a frequency of 38400 times a second for a given time period of 400uS. When transmitting a “0”, the LED remains off for 400uS.
Luckily for me the command signal for the Nikon camera has already been decoded and published, the details can be found here: http://www.sbprojects.com/projects/nikon/

So, from observing the diagram on the SB Projects page, we can convert each binary value of the command signal into a series of pulses generated on the carrier frequency. The pulses can be turned on or off to generate the binary values.

Cool stuff, eh?

Creating a binary “1” and piecing together the command signal.

 

Parts list:

  • Intel Galileo.
  • IR LED – I broke open an old remote control and took the LED from here.
  • Resistor – 220Ω.
  • Breadboard – for building a prototype design.
  • Nikon Camera – you know, to use the remote control on…

Schematic:

It's pretty basic, honestly...

Schematic diagram for the circuit.

Salvaging an IR LED from an old remote:

I had been looking to buy an IR LED but as they’re so cheap, I couldn’t find anywhere that would ship one to me unless I bought about €40 worth of items, so my dad donated his old TV remote, which I broke open and removed the IR LED.

Time to open it up and remove the LED:

inside out.

Work in progress.

I used a wire snips to cut off the IR LED:

D'oh!

Here’s a close up of the IR LED, just in case you’ve read this far and still don’t know what I’m taking about (highly possible).

Creating the 38.4KHz clock pulses:

In order to generate the command pulse, we need to be able to create a clock frequency of 38.4KHz and turn this off and on to create the binary values. I’ve already created a post on how to create a clock pulse using the Galileo, you can view it here.

For this project I am using the method shown in example 1, which is plenty fast for what we need.

The code from example 1 outputs a 444KHz frequency (I’ve measured this as 444KHz, it should be 477KHz, which is why some of the maths may not add up), which is way too fast, so we’ll need to add in a delay to slow that down, in this case the delay is created with an “for statement”. We use a delay value of 12 as this translates to 38.4KHz, so in order to generate the required frequency – we turn on the LED and count for 12 clock periods, then turn off the LED and count for another 12 clock periods.

void pulseLoop(int pulses, int SHINE)
{
  // Integers for loops:
  int i;
  int n;
  // delayValue is used to delay the clock from 460KHz to 38.4KHz:
  int delayValue = 12;

  register int x = 0;

  // Loop to control the number of pulses created
  // Number is multiplied by 2 to create both the low and high period of the pulse:
  for(n=0; n<(pulses*2); n++)
  {
    // Loop to delay the digitalWrite to 38.4KHz:
    for(i=0; i<delayValue; i++)
      {
        // SHINE only activated the IR LED when we are supposed to pulse, deactivates for the delay:
        digitalWrite(2, (x & SHINE));
      }
    x =!x;
 }
}

Here’s how that code looks on a Logic Analyser:

Quality picture, eh?

38.4KHz clock frequency created using the GPIO example 1.

Using the code above, we can call the function pulseLoop and pass the number of required pulses and the LED state as an argument, for example we could just type in:

  // First Pulse:
  pulseLoop(76, 1);
  pulseLoop(1058, 0);
  pulseLoop(15, 1);
  pulseLoop(61, 0);
  pulseLoop(15, 1);
  pulseLoop(137, 0);
  pulseLoop(15, 1);
  // Wait for 2nd pulse
  pulseLoop(1078, 0);
  // Second Pulse:
  pulseLoop(76, 1);
  pulseLoop(1058, 0);
  pulseLoop(15, 1);
  pulseLoop(61, 0);
  pulseLoop(15, 1);
  pulseLoop(137, 0);
  pulseLoop(15, 1);

Creating the full command:

The code above works – however it’s a bit too messy and just not cool really..
So lets put all of our pulse values into an array and create a new function which will use that array to build the command.

// Create an array for the pulse values:
int nikonCommand[16] = {76, 1058, 15, 61, 15, 137, 15, 1078, 76, 1058, 15, 61, 15, 137, 15, 1078};

void takePicNikon()
{
for(int x=0; x<sizeof(nikonCommand)/sizeof(int); x++)
  {
    int y = x + 1;
      pulseLoop(nikonCommand[x], (y%2));
  }
}

The line “pulseLoop(nikonCommand[x], (y%2));” uses the C language modulus operator to tells us if the index number is even, if it is we pass a “1” into the pulseLoop function, this will switch on the LED for each item in the array.

Full command:

Using the above code, it was possible to replicate the command required for the Nikon camera, here is a

Coooollll...

The full command pulse, with the Logic Analyser data.

 

It’s Working!!!

This is also Horstachio’s big screen debut…

Galileo – IR Remote for Nikon Camera.. from Allyn H on Vimeo.

Grab the code here:

Feel free to use my code, make something yourself. It should be very easy to port this to any other make camera.

Please let me know if you plan on using this in any projects 🙂

Link to my code on GitHub.

Intel Galileo – Getting to grips with GPIO speeds:

Using the fastGpioDigitalWriteDestructive command to write to the GPIO's.

Using the fastGpioDigitalWriteDestructive command to write to the GPIO’s.

As part of my latest project, I’ve been trying to come to terms with how the GPIO’s (General Purpose Input / Output) on the  Galileo work, there are several ways to program the Galileo I/O’s, mainly: GPO, SPI or ADC.

As the Galileo is running Linux and emulating the Arduino IDE, the GPIO’s run a lot slower than expected, as the chip copes with this overhead.

The GPIO’s on the Galileo are routed through the Cypress CY8C9540A chip, which drastically slows down the performance of these pins, however, not all of the GPIO pins are routed through the PWM.

While reading up on this, I found the below post by “deckard026354” on the Intel communities site, which states the Galileo has 2 dedicated IO’s  with “significant data rates”.

https://communities.intel.com/message/207904#207904

I managed to get my hands on a Saleae Logic Analyser, which I used to measure the speeds of the GPIO.

I created a basic sketch on the Arduino IDE and measured the frequency at the Galileo outputs.

Default speed – 226Hz:

int led = 2;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);
  digitalWrite(led, LOW);
}

Output:

As expected, the wave form reads 226Hz, pretty slow though – so lets see what we can do about that.

Don't hover over me, it's creepy...

Default 226Hz

 

Example-1 – outputs 477kHz waveform on IO2:

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT_FAST);
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(2, HIGH);
  digitalWrite(2, LOW);  
}

Output:

The output measures 444KHz, not too shabby. Although I seem to me missing 33Hz…  I’ve measured this a few times and am pretty sure it’s accurate. So it could possibly be down to the overhead from the Galileo emulating the Arduino IDE.

Using the OUTPUT_FAST pinMode definition.

Using the OUTPUT_FAST pinMode definition.

Example-2 – outputs 683kHz waveform on IO3:

void setup(){
    pinMode(2, OUTPUT_FAST);
}

void loop()
{
    register int x = 0;
    while(1){
        fastGpioDigitalWrite(GPIO_FAST_IO2, x);
        x =!x;
    }

}
Using the fastGpioDigitalWrite command to write to the GPIO.

Using the fastGpioDigitalWrite command to write to the GPIO.

Output – 705KHz: 

The pulses generated seemed to alternate between 667KHz and 705KHz, I’m not sure if this is an inaccuracy with the Galileo or the Saleae, although this does average at 686KHz, so it looks to be an issue with the measurement.

Controlling the GPIO's with fastGpioDigitalWrite.

Controlling the GPIO’s with fastGpioDigitalWrite.

Example-3 – outputs 2.93MHz waveform on IO3:

uint32_t latchValue;

void setup(){
    pinMode(3, OUTPUT_FAST);
    latchValue = fastGpioDigitalLatch();
}

void loop()
{
    while(1){
        fastGpioDigitalWriteDestructive(latchValue);
       latchValue ^= GPIO_FAST_IO3;
    }

}

Output:

Nice! 3MHz from a GPIO,

Although I’m a bit worried about the function name “fastGpioDigitalWriteDestructive“, so I’d like to know a bit more about this before using it in a dedicated sketch.

Using the fastGpioDigitalWriteDestructive command to write to the GPIO's.

Using the fastGpioDigitalWriteDestructive command to write to the GPIO’s.

 

Example-4 – outputs 2.93MHz waveform on both IO2 and IO3:

uint32_t latchValue;

void setup(){
    pinMode(2, OUTPUT_FAST);
    pinMode(3, OUTPUT_FAST);
    latchValue = fastGpioDigitalLatch(); // latch initial state
}

void loop()
{
    while(1){
        fastGpioDigitalWriteDestructive(latchValue);
        if(latchValue & GPIO_FAST_IO3){
            latchValue |= GPIO_FAST_IO2;
            latchValue &= ~ GPIO_FAST_IO3;
        }else{
            latchValue |= GPIO_FAST_IO3;
            latchValue &= GPIO_FAST_IO2;
        }
    }
}

Output:

I didn’t get this to work -_-

If I get a chance, I’ll go through the code again at a later stage and debug…

 

So there you go, depending on how you program IO2 or IO3, you can get:

  • 226Hz by using the default setup.
  • 477KHz by using pinMode(2, OUTPUT_FAST).
  • 683KHz by using fastGpioDigitalWrite(GPIO_FAST_IO3, x);
  • 2.93MHz by using fastGpioDigitalWriteDestructive – although I don’t like the sound of this command…

© 2024 Allyn H

Theme by Anders NorenUp ↑