Bluetooth device control

    This article is a continuation of the previous article “The smartphone controls a toy car” and should help users who decide to repeat the project to control their device using Bluetooth using the BASIC programming environment! ..

    Voice control is an effective but not very reliable control tool, especially when control object more than 2 meters. The attenuation and reverberation of the sound and extraneous noise interferes, besides you need constant access to the Internet. More reliable management on bluetooth.

    For this, two programs are written.

    One program, I will call it "Server", works on the smartphone controlling the device. The server will listen to the communication channel, receive commands and execute them.

    The other, call it "Client", works on a smartphone that performs the role of a remote control. The client will initiate communication, form a command, transfer it to the server via Bluetooth.

    The command is a text message. For example, “right” or “r”, which should be interpreted as “right”, means that the rudder drive is turned on to the right and the main engine is 300 milliseconds forward.

    Before the start of the program between smartphones will need to organize access. To do this, open the setting, turn on the bluetooth, turn on the search for available devices and select smartphones.

    Before starting the server and the client, turn off the bluetooth in order for the OS to request permission to turn on the bluetooth, otherwise the connection may not be created.

    First, the Server program starts, after you make sure that it has started listening to the communication channel, start the Client and control the device.

    These programs demonstrate only the transmission of commands, their interpretation into light spots on the screen was shown in the last article. The shutdown of programs is made by the client. If you need to stop the server directly on your smartphone, press the return key three times.

    Server program listing

    FN.DEF speak(t$)
    TTS.INIT
    TTS.SPEAK t$
    TTS.STOP
    FN.END
    speak("начало работы сервера")
    ONERROR:
    newConnection:
    BT.OPEN
    speak ("Жду запрос на соединение ")
    DO % ++++++++
    BT.STATUS s
    IF s = 1
    !speak("Слушаю")
    ELSEIF s =2
    speak( "Соединяюсь")
    ELSEIF s = 3
    speak( "Соединение создано")
    ENDIF
    PAUSE 1000
    UNTIL s = 3 % ++++++
    BT.DEVICE.NAME device$
    DO %---------
    BT.STATUS s
    IF (s<> 3)
    speak( "Соединение разорвано")
    GOTO new_connection
    ENDIF
    DO % ======
    BT.READ.READY rr
    IF rr
    BT.READ.BYTES s$
    PRINT ":";s$
    s$ =mid$(s$,1,len(s$)-1)
    speak(s$)
    IF (s$="end") THENGOTO xEnd
    ENDIF
    UNTIL rr = 0 % ======
    UNTIL 0 % --------
    xEnd:
    speak("Сервер остановлен")
    BT.CLOSE
    END
    Листинг программы «Клиент»
    ARRAY.LOAD menucom$[], "Вперед", "Назад", "Вправо", "Влево", "Остановить клиент", "Остановить сервер"
    BT.OPEN
    BT.CONNECT
    n = 0DO %+++++++++++
    BT.STATUS s
    IF s = 1
    PRINT "Слушаю, секунды: ", n++
    ELSEIF s =2
    PRINT "Соединяюсь, секунды: ",n++
    ELSEIF s = 3
    PRINT "Есть соединение"
    ENDIF
    PAUSE 1000
    UNTIL s = 3 %+++++++++
    BT.DEVICE.NAME device$
    PRINT device$
    PAUSE 1000
    x = 0DO %#########
    SELECT menu, menuCom$[], "Выбери команду"IF menu = 1THEN BT.WRITE "forward"IF menu = 2THEN BT.WRITE "backward"IF menu = 3THEN BT.WRITE "right"IF menu = 4THEN BT.WRITE "left"IF menu = 5THEN x=1IF menu = 6THEN BT.WRITE "end"
    UNTIL x=1 %#########
    BT.CLOSE
    END

    Also popular now: