Skip to content

Latest commit

 

History

History
195 lines (164 loc) · 6.05 KB

document.md

File metadata and controls

195 lines (164 loc) · 6.05 KB

Sensing base and control programming

About Sense Hat

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.

About Python

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.

Python Tutorial

  1. Open Python Shell [Menu]→[Programming]→[Python3(IDLE)]
  2. Write code print("Hello World!") .

shell

Let's light the LED

  • Things to prepare bread board, 50Ω Resistor, LED, Jumper wire

    1. Connect as shown. pin_asign ↑Raspberry Pi3's pin assignment.

    ※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. LED

    1. Open Python3 IDLE [Menu]→[Programming]→[Python3(IDLE)]→[File]→[New File]

    2. 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()
    
    1. [Run]→[Run Module] or push F5 key on keyboard.

Running The Sense Hat Emulator

  1. Open Raspberry Pi Configuration [Menu]→[Programming]→[Sense Hat Emulator] SHE
  2. Open Sample Code. (about humidity.py) [File]→[Open example]→[Simple]→[humidity.py] humidity
  3. Run humidity.py [Run]→[Run Module] or Press the F5 key. Run_module
  4. Result screen SHE_result 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. real_SHE

Obtaining values of sensor data

As an example, let's get the value of the humidity sensor.

  1. Open Python3 IDLE [Menu]→[Programming]→[Python3(IDLE)]→[File]→[New File]
  2. 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)

humidity2

  1. Humidity value is displayed on Shell.

※Refer to Sense Hat official document for how to get the values of other sensors, so please see.

Let's Light the LED on Sense Hat

  • 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)
    
    

about ThingSpeak

ThingSpeak is one of the IoT Platforms. As a similar platform there are Kibana and Milkcocoa. These platforms can accumulate, analyze, and visualize data.

Sign up ThingSpeak

  1. Open sign up page from ThingSpeak's homepage. https://thingspeak.com/users/sign_up sign_up
  2. Follow the instructions to create an account.

Send sensor data to ThingSpeak.

  1. Login to ThingSpeak.
  2. Click to Channels on Menu tab. And push [New Channel] button. newchannel
  3. Open [API Keys] tab. And confirm API key. apikey
  4. Open Python3 IDLE on raspberry pi and open [File] tab → [New file].
  5. 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)
  1. Set and save the file name.
  2. [Run] → [Run Module] or push F5 key.
  3. Confirm Private view on ThingSpeak. You can see that the current humidity is plotted. result

Work

Let's try other sensor data obtained by Sense Hat.

Raspberry pi's pin assignment

https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/

Python tutorial

https://docs.python.org/3.6/tutorial/index.html#the-python-tutorial

Sense Hat official document

https://pythonhosted.org/sense-hat/api/#sense-hat-api-reference

ThingSpeak tutorial

https://jp.mathworks.com/help/thingspeak/update-channel-feed.html