📜 ⬆️ ⬇️

Bluetooth device control

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

Voice control is an effective, but not very reliable means of control, especially when the distance to the control object is 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 starts 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") THEN GOTO xEnd ENDIF UNTIL rr = 0 % ====== UNTIL 0 % -------- xEnd: speak(" ") BT.CLOSE END   «» ARRAY.LOAD menucom$[], "", "", "", "", " ", " " BT.OPEN BT.CONNECT n = 0 DO %+++++++++++ 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 = 0 DO %######### SELECT menu, menuCom$[], " " IF menu = 1 THEN BT.WRITE "forward" IF menu = 2 THEN BT.WRITE "backward" IF menu = 3 THEN BT.WRITE "right" IF menu = 4 THEN BT.WRITE "left" IF menu = 5 THEN x=1 IF menu = 6 THEN BT.WRITE "end" UNTIL x=1 %######### BT.CLOSE END 

Source: https://habr.com/ru/post/426249/


All Articles