set up guide and programming examplesaiellom/pdf/4 grovepi.pdf · 2018-06-05 · grovepi+ set up...

25
GrovePi+ Set up guide and programming examples Ilche Georgievski [email protected] Room: IAAS 0.353 2017/2018 Spring

Upload: others

Post on 04-Jan-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

GrovePi+

Set up guide and programming examples

Ilche [email protected]

Room: IAAS 0.353

2017/2018Spring

Page 2: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Requisites

• Raspberry Pi set

• GrovePi+ board

• Grove LED

• Grove Temperature and Humidity sensor

• Basic python and Scala/Java skills

Page 3: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

GrovePi+ Board

Digital ports

Digital ports

Digital ports

GrovePi+ Serial

Raspberry Pi Serial

Analog ports

I2C ports

Page 4: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Install GrovePi software

1. Get the code from the Dexter Industries’ repository– mkdir /home/pi/DI && cd DI

– sudo git clone https://github.com/DexterInd/GrovePi

2. Make the installation script executable– cd GrovePi/Script

– sudo chmod +x install.sh

3. Install the software– sudo ./install.sh

– sudo reboot

Page 5: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Update GrovePi firmware

4. Go to the Firmware folder– cd /home/pi/DI/GrovePi/Firmware

5. Make the firmware update script executable– sudo chmod +x firmware_update.sh

6. Update the firmware– sudo ./firmware_update.sh

– Firmware update was successfully completed if the following is displayed avrdude: safemode: Fuses OK

Page 6: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Connect the GrovePi+ board

7. Connect the GrovePI board to the Pi

8. Check if the Pi recognizes the GrovePi– sudo i2cdetect –y 1

– You should see something like this

0 1 2 3 4 5 6 7 8 9 a b c d e f

00: -- 04 -- -- -- -- -- -- -- -- -- -- --

10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

70: -- -- -- -- -- -- -- --

Page 7: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED LIVE CODING IN PYTHON

Programming examples

Page 8: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED blinking

• Check the python API for GrovePi– Basic Arduino functions– Grove specific functions– Private functions for communication

• Connect the LED sensor to a digital port– A digital port can only take on/off and no other state– For example, D4– Make sure it is correctly connected

• Enter the python shell

• Get all functionality from GrovePi– import grovepi

• Turn the LED on– Write a value to a digital port, i.e., digitalWrite(pin,value)– digitalWrite(4,1)

• Turn the LED off– digitalWrite(4,0)

• Troubleshooting– if digitalWrite(pin,value) is not recognized, try using grovepi.digitalWrite(pin,value)instead

Page 9: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED AND DHT PROGRAMS IN PYTHON

Programming examples

Page 10: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED blinking program (1)

• Create a file– mkdir scripts && cd scripts

– nano led_blinking.py

• Write the code from before– Save (Ctrl+O)

– Exit (Ctrl+E)

• Run the program– sudo python led_blinking.py

Page 11: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED blinking program (2)

• Add a break between the on/off states

• Add some information about state changes

import grovepi

from time import sleep

digitalWrite(4,1)

sleep(1)

digitalWrite(4,0)

import grovepi

from time import sleep

digitalWrite(4,1)

print ("LED is on")

sleep(1)

digitalWrite(4,0)

print ("LED is off")

Page 12: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED blinking program (3)

• Make the blinking repetitive

import grovepi

from time import sleep

port=4

timeout=1

while True:

try:

digitalWrite(port,1)

print ("LED is on")

sleep(timeout)

digitalWrite(port,0)

print("LED is off")

sleep(timeout)

Page 13: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

LED blinking program

• Take care of exceptions

import grovepi

from time import sleep

port=4

timeout=1

while True:

try:

digitalWrite(port,1)

print ("LED is on")

sleep(timeout)

digitalWrite(port,0)

print("LED is off")

sleep(timeout)

except KeyboardInterrupt:

digitalWrite(port,0)

break

except IOError:

print ("Communication error")

Page 14: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (1)

• Connect the DHT sensor to a digital port– For example, D7

• Check the dht function– dht(pin,module_type), where module_type is• 0 for the blue sensor

• 1 for the white sensor

– Returns array of 2 floats [temperature,humidity]

Page 15: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (2)

• Read temperature and humidity

• Make the reading repetitive

import grovepi

port=7

sensor=0

[temperature,humidity] = grovepi.dht(port,sensor)

print ("Temperature = %.02f C, humidity = %.02f %%"%(temperature,humidity))

import grovepi

import time

port=7

sensor=0

timeout=1

while True:

[temperature,humidity] = grovepi.dht(port,sensor)

print (“[%] Temperature = %.02f C, humidity = %.02f %%“%(time.ctime(),temperature,humidity))

sleep(timeout)

Page 16: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT PROGRAM IN SCALAProgramming examples

Page 17: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

I2C

• GrovePi sensors use the I2C interface– GrovePi can use either I2C Bus 0 or I2C Bus 1

– I2C address of a sensor is needed (check thistable)

– For example, DHT uses the 0x4 I2C address

• Pi4J library– Java API for I/O access of Raspberry Pi

– Access to the I2C interface

Page 18: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Request

• GrovePi expects a command block consisting of 5 bytes: 1 (dummy), Command, Pin, Data, Data

Command Pin Data Data Description

1 Number * * Read digital input

2 Number Value * Write value on digital output

3 Number * * Read analog input

4 Number Value * Write value on analog output

40 Number TypeRead the temperature and humidity sensor. First 4 bytes are temperature, next 4 are humidity (float)

Type DHT

0 DHT11 (blue)

1 DHT22 (white)

Page 19: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Response

Command Number of bytes Note

1 1

2 -

3 3 First byte not used

4 -

40 9 First byte not used

Page 20: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (1)

• Get the DHT sensor

• Create the request command

• Send the request

private val device = I2CFactory.getInstance(i2cBus).getDevice(i2cAddress)

private val command = Array(40, pin, 0x0, 0x0).map(_.toByte)

device.write(command, 0, command.length)

Page 21: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (2)

• Read data from the device

• Parse the temperature and humidity values

val buffer = Array.fill[Byte](9)(0)

device.read(buffer, 0, buffer.length)

val temperature = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat(1)

val humidity = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat(5)

Page 22: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (3)

• Full implementation of the DHT sensor class

package sess

import java.nio.{ByteBuffer, ByteOrder}

import com.pi4j.io.i2c.I2CFactory

/** + created by Ilche + **/

class DHTSensor(i2cBus: Int, i2cAddress: Int, pin: Int) {

private val device = I2CFactory.getInstance(i2cBus).getDevice(i2cAddress)

private val command = Array(40, pin, 0x0, 0x0).map(_.toByte)

def read: (Float, Float) = {

device.write(command, 0, command.length)

val buffer = Array.fill[Byte](9)(0)

device.read(buffer, 0, buffer.length)

println("Buffer: " + buffer.mkString(" "))

val temperature = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat(1)

val humidity = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat(5)

(temperature, humidity)

}

}

Page 23: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

DHT readings program (4)

• Create the main program

package sess.app

import akka.actor.ActorSystem

import com.pi4j.io.i2c.I2CBus

import sess.DHTSensor

import scala.concurrent.duration._

/** + created by Ilche + **/

object Main extends App {

val sensor = new DHTSensor(I2CBus.BUS_1, 4, 4)

val system = ActorSystem("dhtSensorSystem")

import system.dispatcher

system.scheduler.schedule(5 seconds, 2 seconds) {

val reading = sensor.read

println(s"Temperature = ${reading._1}, humidity = ${reading._2}")

}

}

Page 24: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

Run it on the Pi

• Create a generic (universal) distribution package– sbt universal:packageBin

• Upload the practicals-0.0.1.zip file inside /target/universal/ onto the Pi

• Unzip the file– unzip –q –o practicals-0.0.1.zip

• Run the program– sudo ./practicals-0.0.1/bin/practicals

Page 25: Set up guide and programming examplesaiellom/pdf/4 GrovePi.pdf · 2018-06-05 · GrovePi+ Set up guide and programming examples Ilche Georgievski ilche.georgievski@iaas.uni-stuttgart.de

More languages/examples

• GitHub page of GrovePi contains code and examples for:– c#

– c++

– go

– java

– nodeJS

– python

– ruby

– scratch