We will add a new service to the already existing example from Gatt. This new service will publish two new characteristics to begin with: one for humidity and the other for temperature measurements. We will read the measurements the same way using the techniques we've discussed in Chapter 2, Server Management with Pi. To read these measurements, we will create two new files with content similar to the sense.py
file that we discussed Chapter 2, Server Management with Pi. Let's create two files under the home
directory, and name them humidity.py
and temperature.py
. The temperature.py
file has the following content:
#!/usr/bin/python import sys import Adafruit_DHT humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4) print str(temperature)
The humidity.py
file has similar content. The only difference is that it prints out the humidity part of the measurement instead of the temperature:
#!/usr/bin/python import sys import Adafruit_DHT humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4) print str(humidity)
We need to change the file access mode to executable as well using the following command:
chmod +x temperature.py humidity.py
Now, you can test sensor measurements using the following commands:
sudo ./temperature.py sudo ./humidity.py
The next step is to publish these readings via the Bluetooth channel. We will create a new service inside the existing Gatt server example. For this purpose, you can start editing the server.go source file
for the server example in the /home/pi/gopath/src/github.com/paypal/gatt/examples
path. You only need to add three lines of code in the function definition for onStateChanged
in between other service definitions. In the following content, note that the count service and battery service already exist. We only need to add the sensor service:
// A simple count service for demo. s1 := service.NewCountService() d.AddService(s1) // A sensor service for demo. sSensor := service.NewSensorService() d.AddService(sSensor) // A fake battery service for demo. s2 := service.NewBatteryService() d.AddService(s2)
Additionally, in the same file, change the line where new services are advertised to the following code in order to advertise the new service as well:
// Advertise device name and service's UUIDs. d.AdvertiseNameAndServices("Gopher", []gatt.UUID{s1.UUID(), sSensor.UUID(), s2.UUID()})
We need to add the definition for the new service also. The following code should be placed in a file, named sensor.go
, under the service
directory of the Gatt examples at the same level as other service definition files, such as count.go
and battery.go
:
package service import ( "fmt" "log" "os/exec" "strings" "github.com/paypal/gatt" ) func NewSensorService() *gatt.Service { s := gatt.NewService(gatt.MustParseUUID("19fc95c0-c111-11e3-9904- 0002a5d5c51b")) s.AddCharacteristic(gatt.MustParseUUID("21fac9e0-c111-11e3-9246- 0002a5d5c51b")).HandleReadFunc( func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { out, err := exec.Command("sh", "-c", "sudo /home/pi/temperature.py").Output() if err != nil { fmt.Fprintf(rsp, "error occured %s", err) log.Println("Wrote: error %s", err) } else { stringout := string(out) stringout = strings.TrimSpace(stringout) fmt.Fprintf(rsp, stringout) log.Println("Wrote:", stringout) } }) s.AddCharacteristic(gatt.MustParseUUID("31fac9e0-c111-11e3-9246- 0002a5d5c51b")).HandleReadFunc( func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { out, err := exec.Command("sh", "-c", "sudo /home/pi/humidity.py").Output() if err != nil { fmt.Fprintf(rsp, "error occured %s", err) log.Println("Wrote: error %s", err) } else { stringout := string(out) stringout = strings.TrimSpace(stringout) fmt.Fprintf(rsp, stringout) log.Println("Wrote:", stringout) } }) return s }
We need to build and rerun our server code using go
. The following commands that we used earlier will help us do this. Note that you should be in the /home/pi/gopath/src/github.com/paypal/gatt
directory:
go build examples/server.go sudo ./server
We can use the BLE Scanner app on Android again to connect to this new service and read the temperature and humidity sensor values. The following screenshot illustrates the Gopher services:
After connecting to the Gopher device, you should see the newly added service with the 19fc95c0-c111-11e3-9904-0002a5d5c51b
ID, and new characteristics for that service as shown in the following screenshot:
The following screenshot illustrates the characteristic details for temperature measurement after pressing the the Read button:
3.16.48.181