<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Issues using Bluetooth with SiPy]]></title><description><![CDATA[<p dir="auto">I have been trying to use the SiPy board as a Bluetooth hub to read several devices with the same name and send the data from a specific characteristic via Sigfox and WiFi.</p>
<p dir="auto">As I said, I am using a SiPy board with:</p>
<pre><code>(sysname='SiPy', nodename='SiPy', release='1.6.13.b1', version='v1.8.6-607-g9c8a0e9e on 2017-05-01', machine='SiPy with ESP32', sigfox='1.0.1')
</code></pre>
<p dir="auto">The <a href="http://boot.py" target="_blank" rel="noopener noreferrer nofollow">boot.py</a> file sets up the WiFi connection:</p>
<pre><code>from machine import UART
import os
uart = UART(0, 115200)
os.dupterm(uart)

import machine
from network import WLAN

wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
    if net.ssid == 'NetName':
        wlan.connect(net.ssid, auth=(net.sec, 'Password'), timeout=5000)
        while not wlan.isconnected():
            machine.idle()
        break
</code></pre>
<p dir="auto">The <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a> program is based in the examples in the documentation shown <a href="https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/bluetooth.html" target="_blank" rel="noopener noreferrer nofollow">here</a>, <a href="https://docs.pycom.io/pycom_esp32/library/network.Bluetooth.html" target="_blank" rel="noopener noreferrer nofollow">here</a> and <a href="https://forum.pycom.io/topic/954/send-data-requests-http-post-solved">this</a> forum topic.</p>
<pre><code>from network import Bluetooth
import binascii
import time
import urequests
import gc

def digit_to_char(digit):
    if digit &lt; 10: return chr(ord('0') + digit)
    else: return chr(ord('a') + digit - 10)

def str_base(number,base):
    if number &lt; 0:
        return '-' + str_base(-number,base)
    else:
        (d,m) = divmod(number,base)
        if d:
            return str_base(d,base) + digit_to_char(m)
        else:
            return digit_to_char(m)

def password_generate(deviceID):
    a=devID[0]^0x50
    b=devID[1]^0x50
    c=devID[2]^0x50
    d=devID[3]^0x50
    e=devID[4]^0x50
    f=devID[5]^0x50
    g=devID[6]^0x50
    h=devID[7]^0x50

    password=a
    password=(password&lt;&lt;8)+b
    password=(password&lt;&lt;8)+c
    password=(password&lt;&lt;8)+d
    password=(password&lt;&lt;8)+e
    password=(password&lt;&lt;8)+f
    password=(password&lt;&lt;8)+g
    password=(password&lt;&lt;8)+h

    PASSb16=str_base(password, 16)

    return PASSb16

while True:
    bluetooth = Bluetooth()
    devID=0
    bluetooth.start_scan(-1)
    adv = None

    while True:
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName': 
            try:
                conn=bluetooth.connect(adv.mac)
                services = conn.services()
                
                for service in services:
                    time.sleep(0.050)
                    service.uuid()
                    chars = service.characteristics()
                    for char in chars:
                        if (char.properties() &amp; Bluetooth.PROP_READ):
                            char.uuid()
                            if (char.uuid() == 10787):
                                devID=char.read()
                                print('DeviceID = {}'.format(binascii.hexlify(devID)))
                                pword=password_generate(devID)
                            if devID:
                                if (char.uuid() == 65521):
                                    char.write(binascii.unhexlify(pword))
                                    char1= binascii.hexlify(char.read())
                                    print('MagRead = '+str(char1))
                                    urequests.request(&quot;POST&quot;,&quot;http://requestb.in/1csszaw1&quot;,'{&quot;DeviceID&quot;:'+str(binascii.hexlify(devID))+',&quot;MagRead&quot;:'+str(char1)+'}').text
                break
            except:
                bluetooth.start_scan(-1)
                continue
            break
    bluetooth.stop_scan()
    conn.disconnect()
    gc.collect()
</code></pre>
<p dir="auto">Running the program I got constant Guru Meditation Errors or freezes, so I decided to change the program so that it could connect to any device; going from:</p>
<pre><code> while True:
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName': 
            try:
</code></pre>
<p dir="auto">to:</p>
<pre><code> while True:
        adv = bluetooth.get_adv()
        if adv: 
            try:
</code></pre>
<p dir="auto">This worked better but I still got the same problems as before.</p>
<p dir="auto">I then reduced the program to it basic functionality to find where exactly it was failing.</p>
<pre><code>from network import Bluetooth
import time
import gc

bluetooth = Bluetooth()
while True:
    bluetooth.start_scan(-1)
    adv = None
    while True:
        time.sleep(0.3)
        print('1')
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName':
            print('22')
            try:
                print('333')
                conn=bluetooth.connect(adv.mac)
                print('4444')
                services = conn.services()
                print('55555')
            except:
                print('AAAAAA')
                bluetooth.start_scan(-1)
                continue
            break
    conn.disconnect()
    bluetooth.stop_scan()
    gc.collect()
</code></pre>
<p dir="auto">I found that most of the time the program would fail when trying to connect to the device or read the services. It also got stuck several times in the <code>if</code> statement not finding a device even when there was one advertising. It would also connect to the device and freeze, and after a soft reboot it would stay connected to the device.</p>
<p dir="auto">Am I doing something incorrectly? Should I try to optimize it further? Or is it a firmware/hardware issue?</p>
]]></description><link>https://forum.pycom.io/topic/1245/issues-using-bluetooth-with-sipy</link><generator>RSS for Node</generator><lastBuildDate>Tue, 16 Jun 2026 03:38:10 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/1245.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 May 2017 20:49:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Fri, 19 May 2017 20:49:43 GMT]]></title><description><![CDATA[<p dir="auto">I have been trying to use the SiPy board as a Bluetooth hub to read several devices with the same name and send the data from a specific characteristic via Sigfox and WiFi.</p>
<p dir="auto">As I said, I am using a SiPy board with:</p>
<pre><code>(sysname='SiPy', nodename='SiPy', release='1.6.13.b1', version='v1.8.6-607-g9c8a0e9e on 2017-05-01', machine='SiPy with ESP32', sigfox='1.0.1')
</code></pre>
<p dir="auto">The <a href="http://boot.py" target="_blank" rel="noopener noreferrer nofollow">boot.py</a> file sets up the WiFi connection:</p>
<pre><code>from machine import UART
import os
uart = UART(0, 115200)
os.dupterm(uart)

import machine
from network import WLAN

wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
    if net.ssid == 'NetName':
        wlan.connect(net.ssid, auth=(net.sec, 'Password'), timeout=5000)
        while not wlan.isconnected():
            machine.idle()
        break
</code></pre>
<p dir="auto">The <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a> program is based in the examples in the documentation shown <a href="https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/bluetooth.html" target="_blank" rel="noopener noreferrer nofollow">here</a>, <a href="https://docs.pycom.io/pycom_esp32/library/network.Bluetooth.html" target="_blank" rel="noopener noreferrer nofollow">here</a> and <a href="https://forum.pycom.io/topic/954/send-data-requests-http-post-solved">this</a> forum topic.</p>
<pre><code>from network import Bluetooth
import binascii
import time
import urequests
import gc

def digit_to_char(digit):
    if digit &lt; 10: return chr(ord('0') + digit)
    else: return chr(ord('a') + digit - 10)

def str_base(number,base):
    if number &lt; 0:
        return '-' + str_base(-number,base)
    else:
        (d,m) = divmod(number,base)
        if d:
            return str_base(d,base) + digit_to_char(m)
        else:
            return digit_to_char(m)

def password_generate(deviceID):
    a=devID[0]^0x50
    b=devID[1]^0x50
    c=devID[2]^0x50
    d=devID[3]^0x50
    e=devID[4]^0x50
    f=devID[5]^0x50
    g=devID[6]^0x50
    h=devID[7]^0x50

    password=a
    password=(password&lt;&lt;8)+b
    password=(password&lt;&lt;8)+c
    password=(password&lt;&lt;8)+d
    password=(password&lt;&lt;8)+e
    password=(password&lt;&lt;8)+f
    password=(password&lt;&lt;8)+g
    password=(password&lt;&lt;8)+h

    PASSb16=str_base(password, 16)

    return PASSb16

while True:
    bluetooth = Bluetooth()
    devID=0
    bluetooth.start_scan(-1)
    adv = None

    while True:
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName': 
            try:
                conn=bluetooth.connect(adv.mac)
                services = conn.services()
                
                for service in services:
                    time.sleep(0.050)
                    service.uuid()
                    chars = service.characteristics()
                    for char in chars:
                        if (char.properties() &amp; Bluetooth.PROP_READ):
                            char.uuid()
                            if (char.uuid() == 10787):
                                devID=char.read()
                                print('DeviceID = {}'.format(binascii.hexlify(devID)))
                                pword=password_generate(devID)
                            if devID:
                                if (char.uuid() == 65521):
                                    char.write(binascii.unhexlify(pword))
                                    char1= binascii.hexlify(char.read())
                                    print('MagRead = '+str(char1))
                                    urequests.request(&quot;POST&quot;,&quot;http://requestb.in/1csszaw1&quot;,'{&quot;DeviceID&quot;:'+str(binascii.hexlify(devID))+',&quot;MagRead&quot;:'+str(char1)+'}').text
                break
            except:
                bluetooth.start_scan(-1)
                continue
            break
    bluetooth.stop_scan()
    conn.disconnect()
    gc.collect()
</code></pre>
<p dir="auto">Running the program I got constant Guru Meditation Errors or freezes, so I decided to change the program so that it could connect to any device; going from:</p>
<pre><code> while True:
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName': 
            try:
</code></pre>
<p dir="auto">to:</p>
<pre><code> while True:
        adv = bluetooth.get_adv()
        if adv: 
            try:
</code></pre>
<p dir="auto">This worked better but I still got the same problems as before.</p>
<p dir="auto">I then reduced the program to it basic functionality to find where exactly it was failing.</p>
<pre><code>from network import Bluetooth
import time
import gc

bluetooth = Bluetooth()
while True:
    bluetooth.start_scan(-1)
    adv = None
    while True:
        time.sleep(0.3)
        print('1')
        adv = bluetooth.get_adv()
        if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName':
            print('22')
            try:
                print('333')
                conn=bluetooth.connect(adv.mac)
                print('4444')
                services = conn.services()
                print('55555')
            except:
                print('AAAAAA')
                bluetooth.start_scan(-1)
                continue
            break
    conn.disconnect()
    bluetooth.stop_scan()
    gc.collect()
</code></pre>
<p dir="auto">I found that most of the time the program would fail when trying to connect to the device or read the services. It also got stuck several times in the <code>if</code> statement not finding a device even when there was one advertising. It would also connect to the device and freeze, and after a soft reboot it would stay connected to the device.</p>
<p dir="auto">Am I doing something incorrectly? Should I try to optimize it further? Or is it a firmware/hardware issue?</p>
]]></description><link>https://forum.pycom.io/post/7622</link><guid isPermaLink="true">https://forum.pycom.io/post/7622</guid><dc:creator><![CDATA[adg]]></dc:creator><pubDate>Fri, 19 May 2017 20:49:43 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Fri, 19 May 2017 23:30:41 GMT]]></title><description><![CDATA[<p dir="auto">hello,<br />
<a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1377">@adg</a><br />
have you tried to stop scan before to connect to a bluetooth device ?</p>
<pre><code>if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'DeviceName':
            print('22')
            try:
                bluetooth.stop_scan()
                print('333')

</code></pre>
]]></description><link>https://forum.pycom.io/post/7625</link><guid isPermaLink="true">https://forum.pycom.io/post/7625</guid><dc:creator><![CDATA[Jurassic Pork]]></dc:creator><pubDate>Fri, 19 May 2017 23:30:41 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Sat, 20 May 2017 10:45:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1377">@adg</a><br />
Try calling bluetooth.stop_scan() before the connect().</p>
]]></description><link>https://forum.pycom.io/post/7641</link><guid isPermaLink="true">https://forum.pycom.io/post/7641</guid><dc:creator><![CDATA[jmarcelino]]></dc:creator><pubDate>Sat, 20 May 2017 10:45:22 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Sun, 21 May 2017 19:28:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/212">@jmarcelino</a> hi.  It sounds like you have something working.   Could you tell me the version of firmware you are using.</p>
<p dir="auto">Any chance you could share a snippet of working code?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.pycom.io/post/7689</link><guid isPermaLink="true">https://forum.pycom.io/post/7689</guid><dc:creator><![CDATA[thrag]]></dc:creator><pubDate>Sun, 21 May 2017 19:28:52 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Mon, 22 May 2017 15:09:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1310">@thrag</a><br />
I am using firmware version <strong>1.6.13.b1</strong>.</p>
]]></description><link>https://forum.pycom.io/post/7712</link><guid isPermaLink="true">https://forum.pycom.io/post/7712</guid><dc:creator><![CDATA[adg]]></dc:creator><pubDate>Mon, 22 May 2017 15:09:22 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Mon, 22 May 2017 16:57:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1310">@thrag</a></p>
<p dir="auto">The full program works like this:</p>
<pre><code>Running main.py
BTDM CONTROLLER VERSION: 010101
BTDM ROM VERSION 0101
BD_ADDR: 24:0A:C4:00:FA:CA
NVDS MAGIC FAILED
RF Init OK with coex
Enable Classic BT
Enable Low Energy
DeviceID = b'28dbafedff3ace00'
MagRead = b'2e'
</code></pre>
<p dir="auto">After the first successful connection it gets stuck and doesn't connect to anything.</p>
<p dir="auto">The simplified version of the program:</p>
<pre><code>Running main.py
BTDM CONTROLLER VERSION: 010101
BTDM ROM VERSION 0101
BD_ADDR: 24:0A:C4:00:FA:CA
NVDS MAGIC FAILED
RF Init OK with coex
Enable Classic BT
Enable Low Energy
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
22
333
4444
AAAAAA
1
1
1
1
1
1
1
22
333
4444
AAAAAA
1
22
333
4444
AAAAAA
1
1
1
22
333
4444
55555
1
1
1
1
1
1
1
1
1
1
1
1
1
ASSERT_PARAM(10 0), in lld_pdu.c at line 1260
</code></pre>
]]></description><link>https://forum.pycom.io/post/7719</link><guid isPermaLink="true">https://forum.pycom.io/post/7719</guid><dc:creator><![CDATA[adg]]></dc:creator><pubDate>Mon, 22 May 2017 16:57:08 GMT</pubDate></item><item><title><![CDATA[Reply to Issues using Bluetooth with SiPy on Mon, 22 May 2017 17:07:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1321">@Jurassic-Pork</a> <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/212">@jmarcelino</a><br />
I tried it, and as far as I can see the result is the same.</p>
]]></description><link>https://forum.pycom.io/post/7720</link><guid isPermaLink="true">https://forum.pycom.io/post/7720</guid><dc:creator><![CDATA[adg]]></dc:creator><pubDate>Mon, 22 May 2017 17:07:33 GMT</pubDate></item></channel></rss>