Page 1 of 1

Example Astro Pi code

Posted: Tue Apr 21, 2015 9:47 pm
by Johnny5C
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

Re: Example Astro Pi code

Posted: Sat Apr 25, 2015 7:53 pm
by Johnny5C
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()

Re: Example Astro Pi code

Posted: Sun Apr 26, 2015 4:50 am
by W. H. Heydt
This kind of implies that AstroPi boards are available... Where?

Re: Example Astro Pi code

Posted: Sun Apr 26, 2015 12:53 pm
by DirkS
W. H. Heydt wrote:This kind of implies that AstroPi boards are available... Where?
Not yet. See viewtopic.php?f=104&t=108520

Re: Example Astro Pi code

Posted: Sun Apr 26, 2015 1:40 pm
by Johnny5C
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

Re: Example Astro Pi code

Posted: Sun Apr 26, 2015 4:50 pm
by W. H. Heydt
Okay... Thanks for putting the code out there. Pity no one can actually try out the code yet.

Re: Example Astro Pi code

Posted: Tue Apr 28, 2015 8:32 pm
by ThomasS
I know the board isn't yet available but how would I go about installing the Astro Pi library? Thanks!

Re: Example Astro Pi code

Posted: Tue Apr 28, 2015 8:35 pm
by ShiftPlusOne

Re: Example Astro Pi code

Posted: Wed Apr 29, 2015 7:00 pm
by ThomasS
Thanks. I'll give it ago.

Re: Example Astro Pi code

Posted: Mon May 11, 2015 9:15 pm
by Johnny5C
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')

Re: Example Astro Pi code

Posted: Sat May 16, 2015 1:20 pm
by Johnny5C
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!

Re: Example Astro Pi code

Posted: Mon May 25, 2015 9:31 pm
by Johnny5C
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

Re: Example Astro Pi code

Posted: Mon May 25, 2015 9:41 pm
by theirry
Thanks! I've been wanting to use this code!

Re: Example Astro Pi code

Posted: Thu Jun 25, 2015 1:09 pm
by Johnny5C
Bump - for you know who you are.