{"id":1520,"date":"2019-04-11T14:24:54","date_gmt":"2019-04-11T13:24:54","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1520"},"modified":"2019-04-12T11:46:09","modified_gmt":"2019-04-12T10:46:09","slug":"remote-ultrasonic-sensor-for-ev3-with-microbit","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2019\/remote-ultrasonic-sensor-for-ev3-with-microbit","title":{"rendered":"Remote ultrasonic sensor for EV3 with micro:bit"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 1 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-and-microbit\" class=\"series-351\" title=\"EV3 and micro:bit\">EV3 and micro:bit<\/a><\/div>\n<p>Been testing the micro:bit BLE Uart service. It&#8217;s a service that uses Nordic chipset features to deliver a simple TX\/RX mechanism so we talk with the micro:bit without having to create our own BLE services.<\/p>\n\n\n\n<p>Someone offered me a ultrasonic sensor this week. It&#8217;s a <a href=\"https:\/\/www.sparkfun.com\/products\/13959\">HC-SR04<\/a>, very common with Arduino projects but can also be <a href=\"http:\/\/www.teachwithict.com\/hcsr045v.html\">used with the micro:bit<\/a>, there&#8217;s even a &#8220;sonar&#8221; extension available in the Makecode editor.<\/p>\n\n\n\n<p>So I added the BLE Uart service and started writing the distance values to the TX characteristic every second:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/raw.githubusercontent.com\/JorgePe\/microbit\/master\/makecode-ultrasonic-ble.png?w=840&#038;ssl=1\" alt=\"\"\/><\/figure>\n\n\n\n<p>Nordic nRF Connect App reads them fine but gatttool doesn&#8217;t&#8230; except when in Interactive mode. Strange!<\/p>\n\n\n\n<p>After a while, I found out <a href=\"https:\/\/github.com\/alcir\/microbit-ble\">a note<\/a> that explains that the way the TX characteristic works forces us to keep the connection or reset the micro:bit regularly. That&#8217;s why Interactive mode works (it keeps the connection). So I have to forget gatttool  for a while and use something better like python.<\/p>\n\n\n\n<p>On Ubuntu, my scripts worked fine. Latest version of pygatt (4.0.3) allow me to subscribe to the TX Characterist (in &#8220;Indication&#8221; mode only &#8211; almost the same as notifications but with an extra acknowledge step) and I was getting distance values on my laptop through BLE.<\/p>\n\n\n\n<p>But on the EV3 the scripts failed because &#8220;No characteristic found matching&#8221;. Why? I&#8217;m using same version of pygatt and don&#8217;t think that kernel version make much difference now that pybluez has settled.<\/p>\n\n\n\n<p>I was almost giving up when something worked: decided to use the python exception mechanism to retry several times the subscribing step. <\/p>\n\n\n\n<p>So instead of just<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>device.subscribe(\"6e400002-b5a3-f393-e0a9-e50e24dcca9e\", callback=handle_data, indication=True)<\/code><\/pre>\n\n\n\n<p>I included the subscribe command in a loop that only ends when the command completes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    try:\n        device.subscribe(\"6e400002-b5a3-f393-e0a9-e50e24dcca9e\", callback=handle_data, indication=True)\n        break\n    except pygatt.exceptions.BLEError:\n        print(\"unknown characteristic\")<\/code><\/pre>\n\n\n\n<p>And it works?! Usually just retrying once:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connecting\u2026<br> Connected<br> unknown characteristic<br> Dist = 1.19 m<br> Dist = 1.19 m<br> Dist = 1.18 m<\/pre>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Micro:bit Ultrasonic Sensor and EV3\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/gnZdKOMnr2E?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>My <a href=\"https:\/\/github.com\/JorgePe\/microbit\">micro:bit github page<\/a> now includes the python script used on the above video<\/p>\n\n\n\n<p>If you wonder about the format of the data, the micro:bit sends the distance value as a string of bytes, each byte being the ASCII code of the character, so 40 cm is sent as a &#8220;4&#8221; followed by a &#8220;0&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">34h 30h<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 1 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-and-microbit\" class=\"series-351\" title=\"EV3 and micro:bit\">EV3 and micro:bit<\/a><\/div><p>Been testing the micro:bit BLE Uart service. It&#8217;s a service that uses Nordic chipset features to deliver a simple TX\/RX mechanism so we talk with the micro:bit without having to create our own BLE services. Someone offered me a ultrasonic sensor this week. It&#8217;s a HC-SR04, very common with Arduino projects but can also be &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2019\/remote-ultrasonic-sensor-for-ev3-with-microbit\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Remote ultrasonic sensor for EV3 with micro:bit&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"series":[351],"class_list":["post-1520","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","series-ev3-and-microbit"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-ow","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1520"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1520\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1520"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}