Microcontrollers,  python,  raspberry pi

Getting a Response Back from the atMega to the Pi

In the last installment, we managed to get the Pi to talk over BLE to the atMega, and for the atMega to turn on an LED to show that this had happened. While interesting, this on its own is of no use. We want to get a response back, too.

Simply trying to use the readCharacteristic() function failed. Further investigation is needed.

After searching, it appears that the issue is that bluepy is not queueing th information coming back. In fact, it has a mechanism for such issues.

Python has objects, and bluesy uses them. We created lawn as one of these objects–but that’s not enough. We need to assign our own method/function to lawn to be able to process when they come in. bluepy handles this with subclasses called “delegates”.

We’ll define a lawnDelegate class as a derived class of DefaultDelegate:

class lawnDelegate(btle.DefaultDelegate):
def handleNotification(self,cHandle,data):
print "in handleNotification with "
print cHandle, data
theBits=data.split()
print theBits
print theBits[1]

All we’ve done with this class is define the single function that lets us handle the data (unless you want to count the gratuitous debugging statements being printed.)

We then add an assignment to the declaration of lawn to use this new class to handle its incoming methods:

 #connect to the device as "lawn"
#you need to use your own MAC address here, as shown in the article
lawn=btle.Peripheral("7c:01:0a:7c:d1:9e")
lawn.setDelegate(lawnDelegate())

With this declaration, whenever the Pi receives data over the BLE connection, setDelegate() gets called.

Trying this, we get

 pi@sprinklers:~ $ ./blemcp.190102a.py 
in handleNotification with
37 T 72.54 H 21.60
['T', '72.54', 'H', '21.60']
72.54
got: True
***splat***
pi@sprinklers:~ $

This is what we were after, and we put a notch in our belt. On demand, we are now taking samples from the atMega–next time, we’ll do something with them!

The complete code for the pi follows. Note that I have switched to my standard versioning of YYMMDD<letter> from the simple numbering in the past.

  #!/usr/bin/python

#blemcp.190102a.py

#monitor and control the garden ble devices


# needed services

#we need the btle package from bluesy
from bluepy import btle

#for sleep()
import time

#we need a delegate to catch notifications

class lawnDelegate(btle.DefaultDelegate):
def handleNotification(self,cHandle,data):
print "in handleNotification with "
print cHandle, data
theBits=data.split()
print theBits
print theBits[1]


#connect to the device as "lawn"
#you need to use your own MAC address here, as shown in the article
lawn=btle.Peripheral("7c:01:0a:7c:d1:9e")
lawn.setDelegate(lawnDelegate())


#eventually we'll loop forever and doze between passes

while True :

#now, tell it to tun on its LED
lawn.writeCharacteristic(37, 'y')


#give it a moment to be ready
time.sleep(2)

#ask for temperature and humidity
lawn.writeCharacteristic(37, 't')


lawnDataBack=lawn.waitForNotifications(3)
print "got: ", lawnDataBack
# we really don't want to loop forever. yet, so break out of this and exit
# (note that your led will return to blinking.
break


#manualy disconnect, just for good mesure
# first tell the atMega to kill it, and then kill it ourselves for cleanup

lawn.writeCharacteristic(37, 'k')
lawn.disconnect()

print "***splat***"


# the end of the main program


Leave a Reply