Menu

HM-11 Bluetooth modules

2023-08-29
2023-09-07
  • Steve Scott

    Steve Scott - 2023-08-29

    Hello All-
    I am new to this compiler and thanks to the amazing documentation and help, I am slowly learning it.
    I am wanting to add Bluetooth to a project I had made years ago with a different compiler and want to upgrade it to HM-11 so I can control it with my iPhone.
    I wanted the versatility of Master/Slave so I decided on the HM-11.
    Anyone have any experience with this module?
    -Regards

     
  • Anobium

    Anobium - 2023-08-30

    This thread look interesting for these devices. https://sourceforge.net/p/gcbasic/discussion/579125/thread/1539e130db/

    Send the author a private message to get his program ? This may help.

     
  • Steve Scott

    Steve Scott - 2023-08-30

    Thanks, will look further when back home.
    Regards!

     
  • Steve Scott

    Steve Scott - 2023-09-02

    I have looked into this for a few days and read AT command sets.
    With HM-11 I can easily see the modules and send AT commands and all seems to be fine.
    My read question is if you do NOT have an iPhone of a serial terminal hooked to a target board and ALL you have is to microcontrollers (PICs) and how can you ensure that each time you power down, these two boards will connect to each other?
    I get one as Master and other as slave, but it seems after the 1st transmit, they un-couple from each.
    Must be something real simple I am missing.......

     
  • Steve Scott

    Steve Scott - 2023-09-02

    Here is what I have thus far for trying to alk to a HM-11 module:

        Sub InitPPS
    
                'Module: EUSART1
                RX1PPS = 0x0011    'RC1 > RX1
                RC0PPS = 0x000C    'TX1 > RC0
                'Module: EUSART2
                RX2PPS = 0x0031    'RG1 > RX2
                RG0PPS = 0x000E    'TX2 > RG0
                'Module: EUSART3
                RB4PPS = 0x0010    'TX3 > RB4
                 'Module: EUSART5
                RX5PPS = 0x0032    'RG2 > RX5
                RG7PPS = 0x0014    'TX5 > RG7
    
        End Sub
    
        'Bluetooth Stuff
    Dir PORTG.7 Out                                                             'TX to Bluetooth module
    #define BT_TX PORTG.7                                                       'Define TX for BT module
    Dir PORTG.2 In                                                              'RX from Bluetooth module
    #define BT_RX PORTG.2                                                       'Define RX for BT module
    Dir PORTF.2 In                                                              'Connected status to proc
    #define BT_Connected PORTF.2                                                'Define the BT module is connected or not
    Dir PORTF.3 Out                                                             'Key or Enable: 1 for BT Command mode, 0 for Data mode
    #define BT_KEY PORTF.3                                                      'Define for the command/data mode
    Dir PORTF.7 Out                                                             '3.3v subsystem power enable pin for Bluetooth
    #define BT_PwrEn PORTF.7                                                   'Power ENABLE for the Bluetooth module
    BT_PwrEn = 1                                                               'DISABLES the power to the module
    BT_KEY = 0
    'USART settings for USART5 which is used for BT module
    '    SP5BRGH = 1                                                               'Initial baud rate for USART5
    '    SP5BRG = 0x28                                                             '9600 at 0.01% error
        'Set PORTG.2 RX5 to input
        Dir PORTG.2 In
        'Set PORTG.7 pin to output
        Dir PORTG.7 Out
        #define USART5_BAUD_RATE 9600
        #define USART5_BLOCKING
        #define USART5_DELAY 5 ms
    
        Dim SendBTpacket as Alloc
    
        ' Bluetooth Modele
          'The following sets the BT module in AT mode for setting up 
        wait 1 sec 
    '    SP5BRG = 0x09                                                            'AT mode uses higher speed - 38400
        BT_PwrEn = 0                                                              'Applies power to BT module
        wait 10 us 
    '      BT_KEY = 1                                                             'Puts BT module into AT mode - will see if this works
          'Sets up the BT Module
        SendBTpacket = "AT+NAMERemote"
        HserPrint ( SendBTpacket, 5 )                                             'BT Module is now 'Master'
    
        SendBTpacket = "AT+MODE2"                                                 'Both modules can send/receive data not include any AT command 
                                                                                  'and per package must less than 20 bytes 
        HserPrint ( SendBTpacket, 5 )                                             'Send Mode BT Modeule
    
    When I scan, I still see the original name and not 'Remote'.
    
     

    Last edit: Anobium 2023-09-03
  • Steve Scott

    Steve Scott - 2023-09-02

    And I added this:
    HserPrint ( SendBTpacket, 1 )

    right after the line:
    HserPrint ( SendBTpacket, 5 )

    Just to ensure the data is going out and it was sent to the built-in terminal -good feature!

     
  • Anobium

    Anobium - 2023-09-03

    No sure. So, I clean up the code to understand. Try this.

    A few points in the original code.

    • #option explicit was missing. But, I do not think this is the issue
    • ALLOC. Changed to a string. Alloc is essentially a lump of memory and using as a string may, or, may not work. The alloc would have created a reserved buffer ( I dont remember the default size of the alloc). Anyway, use a string.
    • HserPrint ( SendBTpacket, 1 ) would not output anything as USART1 is not setup, and there is not USART1 PPS.

    I would check with an analyser that the strings are being sent on USART5. This will prove the comms are working and what you think you are sending, is actaully being sent.

    #chip 18f67K40 
    #option explicit
    
    Sub InitPPS
    
                'Module: EUSART1
                RX1PPS = 0x0011    'RC1 > RX1
                RC0PPS = 0x000C    'TX1 > RC0
                'Module: EUSART2
                RX2PPS = 0x0031    'RG1 > RX2
                RG0PPS = 0x000E    'TX2 > RG0
                'Module: EUSART3
                RB4PPS = 0x0010    'TX3 > RB4
                 'Module: EUSART5
                RX5PPS = 0x0032    'RG2 > RX5
                RG7PPS = 0x0014    'TX5 > RG7
    
        End Sub
    
    // Setup Bluetooth intefface
        Dir PORTG.7 Out                                                             'TX to Bluetooth module
        #define BT_TX PORTG.7                                                       'Define TX for BT module
        Dir PORTG.2 In                                                              'RX from Bluetooth module
        #define BT_RX PORTG.2                                                       'Define RX for BT module
        Dir PORTF.2 In                                                              'Connected status to proc
        #define BT_Connected PORTF.2                                                'Define the BT module is connected or not
        Dir PORTF.3 Out                                                             'Key or Enable: 1 for BT Command mode, 0 for Data mode
        #define BT_KEY PORTF.3                                                      'Define for the command/data mode
        Dir PORTF.7 Out                                                             '3.3v subsystem power enable pin for Bluetooth
        #define BT_PWREN PORTF.7                                                   'Power ENABLE for the Bluetooth module
    
    // Initialise variables
        Dim SendBTpacket as String * 32
        BT_PWREN = 1                                                               'DISABLES THE POWER TO THE MODULE
        BT_KEY = 0
    
    // USART settings for USART5 which is used for BT module
        // Set PORTG.2 RX5 to input
        Dir PORTG.2 In
        // Set PORTG.7 pin to output
        Dir PORTG.7 Out
        #define USART5_BAUD_RATE 9600
        #define USART5_BLOCKING
        #define USART5_DELAY 5 ms
    
    
    
        // Bluetooth Modele
        // The following sets the BT module in AT mode for setting up 
        wait 1 sec 
        BT_PWREN = 0                                                              'APPLIES POWER TO BT MODULE
        wait 10 us 
    
        // Sets up the BT Module
        SendBTpacket = "AT+NAMERemote"
        HserPrint ( SendBTpacket, 5 )                                             'BT Module is now 'Master'
    
        SendBTpacket = "AT+MODE2"                                                 'Both modules can send/receive data not include any AT command                                                                           'and per package must less than 20 bytes 
        HserPrint ( SendBTpacket, 5 )      
    
     

    Last edit: Anobium 2023-09-03
  • Steve Scott

    Steve Scott - 2023-09-03

    Thanks Evan, I did not post all of the code as this is a learning code.
    I have set up COM1 and it displays on the compiler's serial terminal what is supposed to go to the BT module.
    I also changesd from ALLOC to STRING but no difference.
    I did hook in my analyzer (should have already done that) and there does NOT seem to be any data going to the modules.
    Will look closer into
    SendBTpacket = "AT+RENEW"
    HserPrint ( SendBTpacket, 5 ) 'RESETS BT Module
    HserPrint ( SendBTpacket, 1 )
    why HSerPrint works with com1 but not with com5.
    I believe I have saet up com5 correctly:

    startup InitPPS, 85

    define PPSToolPart 18lf67k40

    config MCLR=ON 'Uses the RESET pushbutton

    Sub InitPPS
    
            'Module: EUSART1
            RX1PPS = 0x0011    'RC1 > RX1
            RC0PPS = 0x000C    'TX1 > RC0
            'Module: EUSART2
            RX2PPS = 0x0031    'RG1 > RX2
            RG0PPS = 0x000E    'TX2 > RG0
            'Module: EUSART3
            RB4PPS = 0x0010    'TX3 > RB4
             'Module: EUSART5
            RX5PPS = 0x0032    'RG2 > RX5
            RG7PPS = 0x0014    'TX5 > RG7
    
    End Sub
    // Template comment at the end of the config file
    
    The direction has been checked for the BT module as well as the com5.
    
     
  • Steve Scott

    Steve Scott - 2023-09-05

    Hello All-
    I have tested my code with a wired connection between the two boards and they work just fine.
    Learned a few things along the way.
    I plugged in the HM-11 modules one at a time and looked on my iPhone to see that they were advertising and they were - I can them on my iPhone one at a time.
    Then, again one at a time, I pushed 1 of the 4 buttons on the board to see if my iPhone would see the number: button1 = 1, Button2 = 2, etc. and everytime I pushed a button I would see its number on my iPhone.
    I see that both BT modules transmit as they should to my iPhone one at a time.
    I then reset both boards and looked to see the indication LED on the modules was steady which means they are connected and they were. I also checked on my iPhone to ensure I did not see them advertising because they were 'linked' to each other - I did not see them on my iPhone.
    When I press a button on either board, the other board does not display on the LCD display as it did with just the wired connection.
    So, I think I am not understanding something in the setup for these modules to send and receive to each other however, the send and receive to my iPhone when connected one at a time.
    Here are my 4 commands I send to the modules:
    AT+RENEW - for both boards
    AT+NAMEMain - for the Main board
    AT+NAMERemote - for the Remote board
    AT+ROLE1 - for the Main board
    AT+ROLE0 - for the Remote board
    AT+WORK - for both boards
    Only difference between boards are the NAME and the ROLE.
    Any suggestions?
    -Stuck in Texas

     
  • Steve Scott

    Steve Scott - 2023-09-05

    Oh, and I did reverse RXdata and TXdata on both just to be certain. When I did, I could not communicate to them via iPhone.

     
  • Steve Scott

    Steve Scott - 2023-09-06

    He is no longer listed at that department.
    I have read the white paper and it did not provide much help.
    Most if not all are using these module to communicate to a cell phone or to a computer.
    My goal is to have a micro talk back and fort to another micro without using a computer or cellphone.
    Its easy to talk to a cellphone and a micro - not what I am looking to learn however.
    Regards and thanks for the interest!

     
    • Anobium

      Anobium - 2023-09-06

      Contact DS Tech. Companies are typically very helpful.

       
  • Steve Scott

    Steve Scott - 2023-09-06

    Thanks, will try that.

     
  • Steve Scott

    Steve Scott - 2023-09-07

    This has been solved - there is a resolvable issue which needs a new USART.h file.
    Evan helped with this.
    I am using a PIC18F67K40 and was using com5 for the bluetooth module.
    Thanks Evan!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.