The Sense Hat is an add-on board with various sensors and LEDs. Sensors included in the Sense Hat are as follows.
- Gyroscope
- Accelerometer
- Magnetometer
- Temperature
- Barometric pressure
- Humidity
There is also a Python Library that gives easy access to everything on the board.
Python is a dynamic typed programming language used in various applications. It is widely used in various fields such as machine learning, statistical analysis, sensor control. Python has the following features.
- Easy to learn.
- Blocks are indicated by the number of indents.
- It is a dynamic typed language in which all integers and character strings are also as objects.
- Many libraries available for scientific computing and machine learning are prepared.
- The implementation of Python is available as an open source license that you can freely use, freely distribute, and can also use for commercial use. etc.
- Open Python Shell [Menu]→[Programming]→[Python3(IDLE)]
- Write code
print("Hello World!")
.
-
Things to prepare bread board, 50Ω Resistor, LED, Jumper wire
※Bread board is the substrate used for prototyping, experimentation, evaluation. An electric signal flows in the direction of the arrow line in the following figure.
This time connect to GPIO21 and GND.
-
Open Python3 IDLE [Menu]→[Programming]→[Python3(IDLE)]→[File]→[New File]
-
Write the following code.
1 import RPi.GPIO as GPIO #O isn't zero(0). 2 import time 3 4 pin = 21 5 GPIO.setmode(GPIO.BCM) 6 GPIO.setup(pin, GPIO.OUT) 7 8 try: 9 for i in range(10): 10 GPIO.output(pin, GPIO.HIGH) 11 time.sleep(1) 12 GPIO.output(pin, GPIO.LOW) 13 time.sleep(1) 14 GPIO.cleanup() 15 16 except KeyboardInterrupt: 17 GPIO.cleanup()
- [Run]→[Run Module] or push F5 key on keyboard.
-
- Open Raspberry Pi Configuration [Menu]→[Programming]→[Sense Hat Emulator]
- Open Sample Code. (about humidity.py) [File]→[Open example]→[Simple]→[humidity.py]
- Run humidity.py [Run]→[Run Module] or Press the F5 key.
- Result screen By changing the value by raising or lowering the Humidity bar, how the LEDs shine also changes.
※If you want to run with the real Sense Hat, change from sense_emu import SenseHat
on program to from sense_hat import SenseHat
.
As an example, let's get the value of the humidity sensor.
- Open Python3 IDLE [Menu]→[Programming]→[Python3(IDLE)]→[File]→[New File]
- Writing program. And run the program with [Run]→[Run Module].
1 from sense_hat import SenseHat
2
3 sense = SenseHat()
4 humidity = sense.get_humidity()
5 print("humidity: %s" % humidity)
- Humidity value is displayed on Shell.
※Refer to Sense Hat official document for how to get the values of other sensors, so please see.
-
Blinking one LED
from sense_hat import SenseHat import time sense = SenseHat() sense.clear() for i in range(100): sense.set_pixel( 0, 0, 255, 0, 0) time.sleep(1.0) sense.set_pixel( 0, 0, 0, 0, 0) time.sleep(1.0)
-
Blinking all LEDs
from sense_hat import SenseHat import time sense = SenseHat() sense.clear() for x in range(8) for y in range(8) sense.set_pixel( x, y, 255 - int(255 * x / 8), 255, int(255 * y /8)) time.sleep(0.3)
ThingSpeak is one of the IoT Platforms. As a similar platform there are Kibana and Milkcocoa. These platforms can accumulate, analyze, and visualize data.
- Open sign up page from ThingSpeak's homepage. https://thingspeak.com/users/sign_up
- Follow the instructions to create an account.
- Login to ThingSpeak.
- Click to Channels on Menu tab. And push [New Channel] button.
- Open [API Keys] tab. And confirm API key.
- Open Python3 IDLE on raspberry pi and open [File] tab → [New file].
- Write the following code. Insert the Write API Key confirmed above into the Write_API_KEY below.
1 from sense_hat import SenseHat
2 import urllib.parse
3 import urllib.request
4 import time
5
6 sleep = 15 #Send a value every 15 seconds
7 key = "Write_API_KEY" #This Write_API_KEY is your API Key
8 sense = SenseHat()
9
10 def send_data():
11 humidity = sense.get_humidity()
12 params = urllib.parse.urlencode({'api_key': key, 'field1': humidity})
13 url = 'https://api.thingspeak.com/update?' + params
14 try:
15 f = urllib.request.urlopen(url)
16 except:
17 print("connection failed")
18
19 if __name__ == "__main__": #two "_"
20 while True:
21 send_data()
22 time.sleep(sleep)
- Set and save the file name.
- [Run] → [Run Module] or push F5 key.
- Confirm Private view on ThingSpeak. You can see that the current humidity is plotted.
Let's try other sensor data obtained by Sense Hat.
https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/
https://docs.python.org/3.6/tutorial/index.html#the-python-tutorial
https://pythonhosted.org/sense-hat/api/#sense-hat-api-reference
https://jp.mathworks.com/help/thingspeak/update-channel-feed.html