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

Post on 04-Jan-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

GrovePi+

Set up guide and programming examples

Ilche Georgievskiilche.georgievski@iaas.uni-stuttgart.de

Room: IAAS 0.353

2017/2018Spring

Requisites

• Raspberry Pi set

• GrovePi+ board

• Grove LED

• Grove Temperature and Humidity sensor

• Basic python and Scala/Java skills

GrovePi+ Board

Digital ports

Digital ports

Digital ports

GrovePi+ Serial

Raspberry Pi Serial

Analog ports

I2C ports

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

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

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: -- -- -- -- -- -- -- --

LED LIVE CODING IN PYTHON

Programming examples

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

LED AND DHT PROGRAMS IN PYTHON

Programming examples

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

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

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)

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

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]

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)

DHT PROGRAM IN SCALAProgramming examples

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

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)

Response

Command Number of bytes Note

1 1

2 -

3 3 First byte not used

4 -

40 9 First byte not used

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)

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)

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)

}

}

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}")

}

}

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

More languages/examples

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

– c++

– go

– java

– nodeJS

– python

– ruby

– scratch

top related