Tagged: python

Hakology – How to grab openweathermap data using python.

I’ve been playing with the openweathermap api thought i’d share a little code, the following pulls the json and constructs a string containing the location, temp, tax temp, min temp and humidity. You will need to sign up for an API key and free user account 1st. The free account at the time of writing this allows 50k calls a day so if you’re writing for a small personal project 50k calls should be fine.

Copy the code below to a file
eg. displayweather.py

chmod the file if needed …
sudo chmod +x displayweather.py

run the file using …
python displayweather.py

import requests
r = requests.get('http://api.openweathermap.org/data/2.5/forecast/city?id=YOURCITYIDGOESHERE&APPID=YOURAPIKEYGOESHERE')
data = r.json()
mystring = ""
mystring = mystring + data['city']['name'] + " "
mystring = mystring + "Temp:" + str(int(data['list'][0]['main']['temp'] - 273.15)) + "C "
mystring = mystring + "Max:" + str(int(data['list'][0]['main']['temp_max'] - 273.15)) + "C "
mystring = mystring + "Min:" + str(int(data['list'][0]['main']['temp_min'] - 273.15)) + "C "
mystring = mystring + "Hum:" + str(data['list'][0]['main']['humidity']) + "% "
print mystring

Click here to download the file.

Hakology Project Notes : Python, RedditAPI, Arduino, RaspberryPi thing.

Over the last three weeks I’ve been playing with various breakout boards, microcontrollers and embedded linux. I’ve been trying to take time to learn how the technology works and how best to combine them to teach and produce something useful, different and entertaining. So this project initially started out when I was playing with the RTLSDR dongle, I thought it might be cool to display the messages as they got demodulated on a separate device that was dedicated to grabbing POCSAG messages, I slapped it all together, then when my son was over the other weekend we decided to modify the code to grab the top 25 articles from a given subreddit.

Eventually when I get time ill put together a video tutorial on setting this all up but in the meantime here are the notes …

You will need:

1 x Raspberry Pi model B.
1 x Wifi dongle or ethernet cable.
1 x Arduino deumilinove.
1 x USB lead (for serial communication with the arduino)
1 x USB lead (to power the RPi)
1 x USB Charger
1 x LED Matrix 8×8 x4 (I used the horizontal style layout)

So how does it work?
Its pretty simple really, the raspberry pi runs a small python script that grabs the top 25 articles from a predefined subreddit using the api (most of reddits api can be accessed just by post fixing .json to a lot of their URLS the data structures are also well documented and available on github.) The script strips out any weird characters as the JSON is returned in utf-8 format. A string is built containing the articles rank, total amount of upvotes and the article title. If the string is over 300 characters it gets trimmed and “…” appended to the end. The script creates a serial connection to the arduino (the device is usually addressable using /dev/ttyUSB0) The string is then sent to the arduino over serial. When the arduino receives a new string it displays it to the LED matrix after having scrolled any current messages.

What’s the python code?

Install and configure raspbian in the normal way. Before running the script make sure you have internet access otherwise the python script will fail when it trys to communicate with reddit. (todo: add better error handling.)
Python code – http://pastebin.com/shXyry5C
Copy the code in to a file called reddit.py to your home directory.
Edit the file so it points to the correct serial device.
nano reddit.py
If you’re unsure about the arduino serial port run
sudo ls /dev
Look for something like …
ttyUSB0
Edit the line in reddit.py where the serial communication is initialised ensure it matches the port for the arduino.
ser = serial.Serial('/dev/ttyUSB0','57600')
Save the file using …
ctrl+x
Make sure the file has execute permissions by issuing the following command …
chmod +x reddit.py
You can test the script by running it from the command line using the following command (*this needs to be run with superuser privs to access the serial port) …
sudo python reddit.py
Once you’re happy with the way the script is running you can make it autoboot with the pi by using the following command …
sudo crontab -e
If you are prompted which editor to use and are unsure use nano (option 2)
At the bottom of the file add the following line …
@reboot /local/bin/python /home/user/reddit.py
Save the file by pressing ctrl+x
Reboot and test your script is autoloading.

To display the messages on the matrix you’ll need to flash your arduino with a little code …

Wiring the arduino …
To connect the arduino to the matrix you will need to ensure you have the correct SPI pins as they differ between arduino boards the ones on the deumilinove are pins A4 and A5, A4 SDA and A5 SCL, you will also need to attach the CS (chip select) pin to pin number 13 on the arduino and also connect VCC to 5v and GND to GND on the arduino.

A4 to SDA
A5 to SCL
13 to CS
5v to VCC
GND to GND

(todo : will post a pic when i get time)

What’s the arduino code?
Arduino code – http://pastebin.com/pvkM2DUK
Copy and paste the arduino code in to your arduino IDE, you’ll also need to grab the md_max29xx library from git hub. (Download the zip file and unzip the files to your arduino IDE library folder usually /Documents/arduino-x.x.xx/libraries) This library is used to drive the LED matrix. Once you’ve installed the library you’ll have to specify how many matrices you are using in the arduino code. (How many LED matrix panels you will be displaying to my board had 4 8×8 LED panels.) Here’s the important bit though depending on what/where/who you bought your led matrix board from you might find you’ll have to edit way the arduino draws to the matrix otherwise you could be in for some very interesting results. This can be done in the md_max29xx lib folder. There are a few variables that define the matrix rotation and draw direction IE. right to left, left to right, top to bottom etc. You will need to play with these variables until you get the right combination for your matrix.

NB. When initially getting this running use the example grapicstest to make sure the leds are drawing as expected then flash the arduino reddit serial code once you have the library configured correctly.

Now all the hard work’s done
Once you’ve installed the library, edited the arduino code, updated the variables for the draw direction and successfully flashed the code, plug the arduino in to the raspberry pi and boot. There should be enough of a delay in the script to ensure the RPI has connected to your network before it makes its initial request.

If you have the arduino setup correctly you should see scrolling text on the matrix. “Waiting for serial data!” after a short while the rpi should connect and start displaying articles from reddit.

Concluding
Still yet to build an enclosure or decided how / where im going to use this yet but i’ve had fun making it. Initially getting the libraries setup and running correctly was by far the most frustrating part of this project writing / editing the code was pretty straight forward. There’s plenty of scope for improvement / modification too, if I get time I was thinking I’d get it to cycle multiple subreddits, but for the time being I’ve spent enough hours on this project.

NB. The original code running on the arduino was limited to 25 characters I’ve modified this to allow for messages up to 300 characters in length (the size of an article title from reddit) Since doing so some of the serial messages appear to glitch intermittently. I’ve been checking the code for errors but haven’t managed to pinpoint the problem yet. As far as I can tell the python code runs fine but there might be a problem when the arduino is receiving the serial messages. Possible solutions maybe change the baud rate and send the data slower or add a checksum. (*As suggested by advancednewbie) Example code an library for implementing a checksum. https://github.com/AdvancedNewbie/serialChecksum

IF YOU HAVE ANY QUESTIONS REGARDING THIS PROJECT PLEASE PUT THEM IN THE COMMENTS ON THE YOUTUBE VBLOG EPISODE AT THE TOP OF THIS POST.

Hakology Day 50 : Blog Entry

[1303]
Running on minimal sleep. Waiting for two deliveries at separate addresses on the same day … chaos! Coffee in hand, weather is still terrible. Starting to write up reddit python project notes.
[/1303]

Hakology Day 43 : Blog Entry

[1714]
Time to get busy NO MATTER WHAT. Had a great weekend spent with my son and family, we even did a little coding using the reddit API. Sunday was cool too, I managed to grab some bargains from a few second hand sales, nothing exciting or tech related, just a few books on survival/bushcraft/scouting. Today im going to attempt to record the SDR# video as the last time I forgot to hit record! (*not quite I forgot to hit record but that’s another story/rant), I’m also ready to go war driving so that might be a thing in a few hours. Had some more breakout / dev boards turn up in the post but not the one I’m waiting for, lol, its always the way.
[/1714]

[2318]
Nothing ever goes as planned, I went for a little war drive earlier after having installed kismet, gpsd-clients and gpsd. Recently I reinstalled debian with lxde and I couldn’t figure out, or seem to kill the gpsd process to set my gps as the default device (/dev/ttyUSB0), no matter what I did couldn’t get kismet to detect the dongle in gpsd or even find the configuration file. I decided to run kismet with the –use–nmea option BUT this was a lot less reliable in kismet than what gpsd usually is. Then my netbook went to sleep as I havent figured out the power saving settings in lxde yet. (I’ve been using lxde less than a week) I pulled over to restart my scan but accidentally chose the wrong wireless device when I restarted kismet, so all in all a complete mess. That said I’m working on a little script to load everything at once correctly so this won’t happen again. I really newb’d out tonight, If at first you don’t succeed try harder.
[/2318]

[0033]
sudo kismet -c wlan1 -use-nmea-gps /dev/ttyUSB0 -gps-reconnect true
DO NOT USE THIS FOR GPS IT SUCKS … I had the most sketchy log files using this syntax to call kismet. Might have been the way I had the dongle positioned but I think its the way netxmltokml process the file when its been generated using that switch.
[/0033]

[0515]

[/0515]

Hakology – Retropie steam controller setup guide

Notes to accompany the following video showing you how to setup and configure the steam controller for retropie. Tested and verified as working with python 2.7 on the rpi model B and rpi3 model B.

sudo apt-get install python-pip
sudo pip install libusb1
sudo pip install enum34
git clone https://github.com/ynsta/steamcontroller.git
cd steamcontroller
sudo python setup.py install
sudo nano /etc/udev/rules.d/99-steam-controller.rules
Add these lines :
SUBSYSTEM==”usb”, ATTRS{idVendor}==”28de”, GROUP=”games”, MODE=”0660″
KERNEL==”uinput”, MODE=”0660″, GROUP=”games”, OPTIONS+=”static_node=uinput”

sudo udevadm control –reload
sudo crontab -e
@reboot /usr/local/bin/sc-xbox.py start

Hakology – Ubercrypt – Python multi-layer AES encryption.

In this video we’ll be looking at a script I wrote a few months ago to facilitate multilayer aes encryption (cryptographic strengthening) using multiple keys and ivs combined into one monster key. Allows users to encrypt share and decrypt data using the command line. Demo is for linux but this may work if you can install the python crypto library on windows.

Full source code …
https://github.com/caffeinemonster/ubercrypt

sudo apt-get install git python python-crypto
git clone …
python crypto.py -g 4096
python crypto.py -e “Hack the planet”
python crypto.py -d ENC-23-32-54-232

enjoy
have fun hacking !

PyGame : Tutorial PT2 (Drawing objects to the screen)

So its that time of the week … I apologise if the tutorials are slow in being published as I’m working on a few other projects in my spare time and want these tutorials to be correctly written so I don’t get you in to any bad habits or poor coding situations … (I’ve been reading the documentation for pygame A LOT) so without further a do, here’s pygame tutorial part 2. (Click here for part 1)

In the last tutorial we covered setting up the basic game window and initialising pygame.

In this tutorial were going to be drawing some simple shapes and text to the screen and moving them around using the keyboard.

There are some minor modifications to last weeks code I will explain these as we go along.

normal = from original tutorial
bold = added or modified

import pygame
from pygame.locals import *

wWIDTH = 640 # game window width
wHEIGHT = 480 # game window height
loc = [0, 0] # location data

The above code is pretty much the same as last week apart from the loc[0,0] (loc[x,y]) variable this is going to be used to store the xy location of the player.
Here we set the variables both equal to zero. These numbers represent the players starting position so x=0 and y=0 would result in the player starting in the top left hand corner.

def main():

r = 1
while(1): # do for a while (game loop)

for event in pygame.event.get(): # handle events

if event.type == QUIT: # ctrl+c

r = 0 # return 0 = (game is over)

elif event.type == KEYDOWN: # down arrow

if event.key == K_ESCAPE:

r = 0 # return 0 = (game is over)

if event.key == K_q: # q key pressed

r = 0

if event.key == K_LEFT:

loc[0] = loc[0] – 1 # move left

if event.key == K_RIGHT:

loc[0] = loc[0] + 1 # move right

if event.key == K_UP:

loc[1] = loc[1] – 1 # move up

if event.key == K_DOWN:

loc[1] = loc[1] + 1 # move down

So whats changed above well again most of the code is exactly the same but this time were trapping keys to trigger more events. In the first tutorial we trapped the escape key to make the game exit. Here we are trapping the arrow keys to change the variables held in the (loc) location variable. If the key is pressed down then add or subtract to the loc[x,y] variable depending on which key is being pressed (UP, DOWN, LEFT or RIGHT).

We will use the loc variable later to draw the objects to the screen.

# keep location visible on the screen
# check loc is not out of bounds (screen/window size)

if loc[0] < 0:

loc[0] = wWIDTH

if loc[0] > wWIDTH:

loc[0] = 0

if loc[1] < 0:

loc[1] = wHEIGHT

if loc[1] > wHEIGHT:

loc[1] = 0

The above code checks that the numbers stored in the loc(x,y) variable are within the bounds of the screen. If the bounds are exceeded the code keeps them on the screen. So if the object goes off the left of the screen it will appear on the right and also if the object / player moves off the bottom they will appear at the top and vice versa. The loc positions are compared to the window bounds (the variables we defined at the top of the code. wWIDTH amd wHEIGHT)

#fill the screen with black
GSURF.fill((0, 0, 0))

#draw stuff here …
#circle(Surface, color, pos, radius, width=0) -> Rect
#rect(top,left,width,height)
pygame.draw.circle(GSURF, (0, 255, 0), (loc[0], loc[1]), 6, 2)
pygame.draw.circle(GSURF, (255, 0, 0), (loc[0], loc[1]), 4)

pygame.draw.rect(GSURF, (0, 0, 255), [loc[0]-10, loc[1]-10, 20, 20], 3)
pygame.draw.ellipse(GSURF, (0,255,0), [loc[0]-30, loc[1]-10, 60, 20], 1)
pygame.draw.arc(GSURF, (255,0,0), [loc[0]-40, loc[1]-40, 80, 80], 3.141, 2*3.141, 1)
pygame.draw.line(GSURF, (0,0,255), [loc[0]-50, loc[1]-50], [loc[0]+50, loc[1]+50] , 1)

Here’s the good stuff … first of all we fill the canvas / surface black. Its important to draw the background colour first, if we drew the bg colour last we would just end up with a black screen no matter what we’d drawn underneath / first.

pygame.draw.circle(GSURF, (0, 255, 0), (loc[0], loc[1]), 6, 2)
draws a green circle to the game surface at the location held in loc[x,y] make the radius 6 pixels and make the stroke / border 2 pixels wide
so this will not fill the circle

pygame.draw.circle(GSURF, (255, 0, 0), (loc[0], loc[1]), 4)
The above code is the same as the last snippet but will fill the circle in red (if the stroke isn’t provided the circle is filled.)

pygame.draw.rect(GSURF, (0, 0, 255), [loc[0]-10, loc[1]-10, 20, 20], 3)
Draws a red rectangle to the screen. Again the last parameter is optional if you omit this parameter the rectangle will be filled red.

pygame.draw.ellipse(GSURF, (0,255,0), [loc[0]-30, loc[1]-10, 60, 20], 1)
Draws an ellipse to the screen. Optional border parameter.

pygame.draw.arc(GSURF, (255,0,0), [loc[0]-40, loc[1]-40, 80, 80], 3.141, 2*3.141, 1)
Draws an arc to the screen. Last parameter represents line thickness.
NB. Arc start point and endpoints are measured in radiants.
These sound complicated but they are really easy.
Basically a circle contains 6.28 radiants. (2*pi).
A start point of 0 and endpoint of 3.141 would draw half a circle.
A start point of 0 and endpoint of 6.282 would draw a whole circle.
Degrees to radiants = 6.282 / 360 * Degrees

pygame.draw.line(GSURF, (0,0,255), [loc[0]-50, loc[1]-50], [loc[0]+50, loc[1]+50] , 1)
Draws a line from one point to another.

All the above shapes are drawn @ the location of the player.

myfont = pygame.font.SysFont(“monospace”, 15)
label = myfont.render(“Frame rate : ” + str(int(GCLOCK.get_fps())), 1, (255,255,0))
GSURF.blit(label, (loc[0], loc[1]))

# check the ‘pygame draw’ documentation for more information on other you can draw to the screen
# above are most of the basics.
pygame.display.flip() # update the screen
GCLOCK.tick() # update the clock

if r != 1:

break

return r

Above we define a font variable as myfont we use the default pygame fonts (which are all available in the documentation.) Next we make a label variable this variable is going to contain the frame rate as this variable isnt a string we have to use str() to convert the fps from a number to a string. (NB. I think I made a school boy error above. Fps is returned as an int, there is no need for the int() function u can remove this. Edit the code so it looks like this str(GCLOCK.get_fps()) ) Then we blit the label object to the screen at the specified location. display.flip() is used for double buffering (which is a whole other topic) ill cover it briefly here if we replaced the last frames image with the current one we were drawing in the loop bad things would happen basically double buffering displays the last drawn image until the next has been drawn and is ready to be displayed. Double buffering cuts out screen flicker.

tick() is then called to update the pygame clock and advance to the next frame. No numbers are passed in here. Before we were passing a number which will restrict or slow the pace of your game by a set amount depending on what number you use. This will also help us to determine accurately how many cycles the game is running at per second accurately.

if __name__ == “__main__”: # main function call

r = 1 # set variable for return value

pygame.init() # initialise python pygame

GCLOCK = pygame.time.Clock() # set game clock

GSURF = pygame.display.set_mode((wWIDTH, wHEIGHT)) # main game surface

pygame.key.set_repeat(1, 0) # repeat keys delay by 0

while r == 1: # quit on 0

r = main() # get return value from main loop (1 == OK … 0 == exit)

pygame.quit()

Here is the last bit of code (that initialises the whole game rather important) only one line changed here which is the keyboard repeat rate, this forces the keyboard to repeat key presses if they are held down for a specified period of time.

That concludes tutorial part two.

Here is a screen shot of what the code does …
Screenshot from 2013-07-09 01:58:41

Here is a link to the code …
GIST : https://gist.github.com/caffeinemonster/5953538
PASTE BIN : http://pastebin.com/sxchRZx7

PyGame : Tutorial PT1 (Setup & Initialisation)

I would consider this my first proper post hehe … I started this blog to make life easier when doing videos about code sometimes the screen capture and editing that goes in to a short clip can be painful and consume a lot of my spare time. Here ill be able to waffle on about code and linux to my hearts content (with the ability to access the articles and update things after they have been published).

So this hopefully will turn in to some kind of archive generated by hakology plus various odds and ends I find lying about the interwebz.

I digress … keeping on point this is a python pygame introduction to simple 2D game engines (with a view to move on to opengl and 3D graphics at some later point) for this tutorial you will need the following:

Python 2.7

Python- Pygame Librarys and Modules

IDE (It doesnt really matter what you use – at the moment I’m really enjoying ninja-ide)

This tutorial is for linux users, the development rig I’m using at the moment is Debian Jessie (Test/Dev Release)

Ok so down to business if your rocking debian jessie or similar you shouldnt have too many problems getting up and running.

First install python

sudo apt-get install python2.7

Now install pygame

sudo apt-get install python-pygame

Now install ninja-ide

sudo apt-get install ninja-ide

Hopefully that all went well 🙂 (if it didn’t please consult google.com/pygame/python/debian documentation)

Now in your home folder we need to create a folder to store everything so pop a terminal and make a directory

mkdir pygame
cd pygame
touch tutorial1.py
ninja-ide tutorial1.py

Now you should have created a folder and a file in the folder and opened that file with ninja-ide

This is where the fun begins.

Add the following to the first lines of your python file ….

import pygame
from pygame.locals import *

The lines above tell the python interpreter to import and load all the librarys and variables from pygame.

Next we need to set some variable to store how big we’d like to make our game window.

wWIDTH = 640
wHEIGHT = 480

Right now were ready to add the first function that is called when the file is executed

if __name__ == “__main__”: # main function call

r = 1 # set variable for return value
pygame.init() # initialise python pygame
GCLOCK = pygame.time.Clock() # set game clock
GSURF = pygame.display.set_mode((wWIDTH, wHEIGHT)) # main game surface
while r != 0: # quit if return not equal to 0 (0 = game exit, 1 = restart main loop)

r = main() # get return value from main loop (1 == OK … 0 == exit)

Ok so lets explain the code … the first line of the above block is defacto python code you will find this in (almost) every python script you encounter, the second line defines a variable called ‘r’ and sets it equal to 1 this value is used later to determine if the game should continue or quit. GCLOCK is a variable used to store pygames clock which is used later for frame rate calculations and game speed. GSURF is the game surface ie. the area or the screen we are going to be drawing to notice we pass in the pre defined screen sizes we set at the top of the script. The while loop and main game call will not quit until the return value is equal to 0. The line within the while loop is the main game loop call when we call this function we expect a return value of either 1 or 0.

Right now to add the main game loop …

When you add this code it needs to go below the variables we created earlier but above the first function call.

def main():

r = 1
while(1): # do for a while (game loop)

for event in pygame.event.get(): # handle events

if event.type == QUIT: # ctrl+c

r = 0 # return 0 = (game is over)

elif event.type == KEYDOWN: # down arrow

if event.key == K_ESCAPE:

r = 0 # return 0 = (game is over)

if event.key == K_q: # q key pressed

r = 0

GSURF.fill((0, 0, 0))

if r != 1: #check value of r

break #exit main game loop

return r

Ok so the above code basically runs and keeps the main game window open it loops forever until the escape key is detected if the escape key is detected or quit program event then the window will close.

You can now test the script by pressing play in the ninja-ide tool bar.

or entering the following in the command line …

cd ~
cd pygame
python tutorial1.py

Enjoy your nice blank window opening and closing.

Whoot well done you and stay tuned for part two coming very very soon 🙂

Pastebin link to raw code – http://pastebin.com/vzACDi3v