Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Example Astro Pi code

Tue Apr 21, 2015 9:47 pm

Here is some example code you can run on an Astro Pi.

It will scroll temperature, humidity and pressure on the LED matrix.

It's a good way to test your sensors. There are some parts that can be tweaked easily to see the changes, like scroll_speed and text_colour.

I'm not an expert, so there might be a better way, but it works on my Astro Pi!

If you are interested in Astro Pi, look me up on Twitter @JohnChinner

Good luck

Code: Select all

#!/usr/bin/python
from astro_pi import AstroPi
import time

ap = AstroPi()
temp = ap.get_temperature()
humidity = ap.get_humidity()
pressure = ap.get_pressure()


print("Temperature: %s C" % temp)               # Show temp on console
print("Humidity: %s %%rH" % humidity)        # Show humidity on console
print("Pressure: %s Millibars" % pressure)    # Show pressure on console

ap.set_rotation(180)        # Set LED matrix to scroll from right to left
              
ap.show_message("Temperature: %.2f C" % temp, scroll_speed=0.05, text_colour=[0, 255, 0])

time.sleep(1)           # Wait 1 second

ap.show_message("Humidity: %.2f %%rH" % humidity, scroll_speed=0.05, text_colour=[255, 0, 0]) 

time.sleep(1)		# Wait 1 second

ap.show_message("Pressure: %.2f Millibars" % humidity, scroll_speed=0.05, text_colour=[0, 0, 255])

ap.clear()		# Clear LED matrix
Last edited by Johnny5C on Sat Apr 25, 2015 7:53 pm, edited 1 time in total.

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Sat Apr 25, 2015 7:53 pm

The code below will display the IP address of network port eth0 on the LED matrix. I find this useful, as I work my Astro Pi headless over SSH, and often move between networks. (It's another example of how to display variables on the LED matrix)

It's also useful to have it run on boot, so..........

This python script's is named ip.py and is saved in the /astro-pi-hat/examples folder. Paste the code onto a nano editor (sudo nano) and save with the same file name. (ip.py)

Then, you can edit rc.local with

Code: Select all

sudo nano /etc/rc.local
and add this line above exit 0:

Code: Select all

python /home/pi/astro-pi-hat/examples/ip.py &
don't forget the &

It will scroll eth0 and the IP address on boot, so you can SSH right in.

Simple job to change to show wlan0 too if you need it.

As always, I'm not an expert, just a tinkerer, so don't judge me on my code!

Code: Select all

from astro_pi import AstroPi
import socket
import fcntl
import struct
import time

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

print get_ip_address('eth0')      # Use when ethernet port is used
#print get_ip_adress('wlan0')     # Use weh wireless used

time.sleep(0)

ap = AstroPi()
addr  = get_ip_address('eth0')
ap.set_rotation(180)
ap.show_message("eth0 %s " % addr, scroll_speed=0.05,  text_colour=[0, 255 ,0])
time.sleep(3)
ap.show_message("eth0 %s " % addr, scroll_speed=0.05,  text_colour=[0, 255 ,0])
ap.clear()

W. H. Heydt
Posts: 10762
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Example Astro Pi code

Sun Apr 26, 2015 4:50 am

This kind of implies that AstroPi boards are available... Where?

DirkS
Posts: 9902
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Example Astro Pi code

Sun Apr 26, 2015 12:53 pm

W. H. Heydt wrote:This kind of implies that AstroPi boards are available... Where?
Not yet. See viewtopic.php?f=104&t=108520

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Sun Apr 26, 2015 1:40 pm

I'm lucky enough to have some hardware, because I'm part of the Astro Pi team.

I'm putting these example here to record my tinkering, and when hardware gets into the hands of people, schools or other, they can get going quickly.

These two just scratch the surface of the capability of the Astro Pi HAT's suite of sensors.

John

W. H. Heydt
Posts: 10762
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Example Astro Pi code

Sun Apr 26, 2015 4:50 pm

Okay... Thanks for putting the code out there. Pity no one can actually try out the code yet.

ThomasS
Posts: 118
Joined: Thu Jan 23, 2014 7:05 pm
Location: Leeds
Contact: Website

Re: Example Astro Pi code

Tue Apr 28, 2015 8:32 pm

I know the board isn't yet available but how would I go about installing the Astro Pi library? Thanks!
ThomasS

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5874
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Example Astro Pi code

Tue Apr 28, 2015 8:35 pm


ThomasS
Posts: 118
Joined: Thu Jan 23, 2014 7:05 pm
Location: Leeds
Contact: Website

Re: Example Astro Pi code

Wed Apr 29, 2015 7:00 pm

Thanks. I'll give it ago.
ThomasS

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Mon May 11, 2015 9:15 pm

Got a camera connected to your Astro Pi?

This little script will count down from 5 on the LED display and then take an image with the camera.

Code: Select all

import time
import picamera
from astro_pi import AstroPi

ap = AstroPi()
ap.clear()

for i in reversed(range(1,6)):
    ap.show_letter(str(i), text_colour=[128, 0, 0])
    time.sleep(1)
    ap.clear()

with picamera.PiCamera() as camera:
    camera.capture('/home/pi/astro-pi-hat/examples/image.jpg')

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Sat May 16, 2015 1:20 pm

This code will read figures of temperature, humidity and pressure and write them to a .csv file.

There are a few things you can easily change like maxcount, to adjust the length of time for the experiment, and the sleep time to adjust the time between samples in seconds.

It also displays the count number on the LED matrix, so you know its still working if you are not connected to a terminal.

Mine is logging right now. I hope to produce a graph showing how all three measurements change overnight.

On edit, I'm using SSH to control a remotely located Astro Pi. When you close the SSH session, it stops the code running. Not good for a 24 hour data logging exercise! So now I am using screen to stop this happening. If you have a long duration test, type screen, and then run your script. You can then close the SSH session. Simply come back later and start another SSH, log in and type screen -r to get back where you were! screen was already installed on my Pi

Code: Select all

from astro_pi import AstroPi
from time import sleep, asctime

count = 0
maxcount = 60  #how many samples to take
file = open('./weather-log.csv', 'w')
file.write("\"Time\",\"Temperature\",\"Humidity\",\"Pressure\",\n")
ap = AstroPi()

while count < maxcount:
   temp = ap.get_temperature()
   humidity = ap.get_humidity()
   pressure = ap.get_pressure()

   ap.show_message("Logged %s " % count, scroll_speed=0.05,  text_colour=[0, 255, 0])

   print("Logged {}".format(count))  #keeps you informed of progress on the terminal
   file.write("\"{}\",\"{:0.2f}\",\"{:0.2f}\",\"{:0.2f}\"\n".format(asctime(),temp,humidity,pressure))

   sleep(10)   #number of seconds between samples
   count+=1

file.close()
As always, i'm not an expert, there might be better or different ways to do this!

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Mon May 25, 2015 9:31 pm

Some people have asked for the code I have been using to take-time-lapse videos.

Time-lapse photography could be an exciting use for an Astro Pi when on the ISS, looking out of the Cupola?

My simple code:

Code: Select all

import time
import picamera
from astro_pi import AstroPi

ap = AstroPi()
with picamera.PiCamera() as camera:

    time.sleep(2)
    for filename in camera.capture_continuous('img{counter:04d}.jpg'):
        ap.show_message('%s' % filename, scroll_speed=0.05, text_colour=[0, 100$
        print('Captured %s' % filename)
        time.sleep(5) # wait 5 seconds
#       time.sleep(120) # wait 2 minutes
#       time.sleep(60) # wait 1 minute
You can remove the # to adjust the time between shots.

The useful thing for having an Astro Pi HAT attached, is that it displays the file name of the photo it has just taken on the LED matrix, so you can see it's working. I got the 18 hour beach video below when running on a 12000mAh USB backup battery.

I end up with a folder full of .jpg files. There are ways for the pi to compile them into a video (google it). But I use Windows Movie Maker.

Here is my latest:

https://youtu.be/x-hXeUrVQNw

theirry
Posts: 42
Joined: Sat Jul 20, 2013 6:08 pm

Re: Example Astro Pi code

Mon May 25, 2015 9:41 pm

Thanks! I've been wanting to use this code!

Johnny5C
Posts: 82
Joined: Thu Apr 18, 2013 3:32 pm

Re: Example Astro Pi code

Thu Jun 25, 2015 1:09 pm

Bump - for you know who you are.

Return to “Astro Pi”