I decided to try one of the learn modules on the Raspberry Pi web site. This introduces me to
1 Python3
2 Using the GPIO pins
I wired up the 10 LEDs as shown on the R_PI project page. Then started Menu > Programming > Python 3. The first window is raw access to the Python interpreter, here you can type commands and see them happen. To create an app you need to open File > New File in a new window, here you can type your program. You must save it, so I suggest to go out to the File Manager and create a new folder under Documents > Python3 Projects. Then you can save you work there.
The program looks like this:
import requests from gpiozero import LED pins = [2, 3, 4, 14, 15, 17, 18, 27, 23, 22] leds = [LED(p) for p in pins] url = "http://api.open-notify.org/astros.json" r = requests.get(url) j = r.json() n = j['number'] for i, led in enumerate(leds): if n > i: led.on() else: led.off()I saved it as astros.py. Run it by Run > Run Module. And if you got it all wired up right and typed in right, at this moment there are 6 astronauts on the ISS so 6 LEDs will light up.
As I understand it the program works like this
- import the libraries you need "requests" and the "gpiozero" function "LED".
- define a list of the led pins you have wired up
- make another list from this of the leds you will access and their pin numbers
- define the url for the data and get the data from it into variable "r"
- read the json data into the variable "j"
- finally extract the 'number' of astronauts from the json "number" data
- enumerate the leds - simply number them 0...9. And step through them in a "for" loop. If the number of astronauts "n" is more than the current led "i" in the loop, turn it on. Else turn it off.
This will give 6 LEDs on for 6 astronauts.
No comments:
Post a Comment