Examples - Code Fragments

Introduction

In this last section we will show you some code fragments that you can use as a reference. This fragments will not work out of the box, you will need to make some changes to them in order to use them, but can be used as a reference to see how to do some things.

Wire sensor reading into file system

This example shows the use of the MultiSensor from iButtonLink.com. It logs temperature and humidity into the file system for later download.

0 REM start 1-wire
50 T = pioclr 6
51 T = uarton
52 T = baud 96
53 PRINTU "D";
54 $0[0] = 0
55 TIMEOUTU 2
56 INPUTU $0
0 REM if we have an 'E' (69) end routine
57 IF $0[0] <> 69 THEN 60;
58 PRINTS "EOD\n"
59 RETURN

0 REM timeout
60 IF $0[0] = 0 THEN 62
61 GOTO 65
62 PRINTS "timeout\n"
63 RETURN

0 REM see if we have a "26" (50,54) start
65 IF $0[0] = 50 THEN 69;
66 PRINTS "no "
67 REM PRINTS $0
68 GOTO 54
0 REM read next line from uart
69 IF $0[1] <> 54 THEN 54;
70 PRINTS $0
71 S = append "tmplog.txt"
72 Q = size
0 REM don't overrun file, stop
73 IF Q > 200 THEN 80
0 REM read clock and overwrite first 16
74 S = date $0
0 REM change the null to a space
75 $0[16] = 32
0 REM add newline to the end of the string
76 PRINTV "\n"
77 PRINTS $0
78 R = strlen $0
79 S = write R
80 S = close
81 GOTO 54

Send Sensor Data via OBEX

This example reads binary data from a gas sensor, converts it into a string and sends it via an OBEX vNote to another Bluetooth machine every 10 seconds.

0 REM gas sensor read function

50 T = 17
51 TIMEOUTU 2
52 PRINTU "\x04"
53 UART T
0 REM see if we get a 0xfd
54 IF $0[0] <> 253 THEN 69;
55 $1 = $0
56 REM PRINTS "found\n"
57 $0[0] = 0;
58 FOR I = 0 TO 16;
59 PRINTV $1[I];
60 PRINTV " "
61 NEXT I
62 REM PRINTS $0
64 REM C = message "0013105D4CAC"
65 C = message "000B0D04B695"
66 REM PRINTS "sent\n"
67 REM WAIT 5
68 REM GOTO 50
69 RETURN

@ALARM 400
400 D=status;
401 IF D = 0 THEN 410
402 D=pioget 2;
403 IF D = 0 THEN 407
404 D=pioclr 2
405 ALARM 1
406 RETURN
407 D=pioset 2
408 ALARM 2
409 RETURN
0 REM just blink short
410 D=pioset 2
411 D=pioclr 2
0 REM keep DTR on
412 D=pioclr 6
0 REM read sensor
413 GOSUB 50
0 REM do it again in 5 seconds
414 ALARM 5
415 RETURN

Send Sensor Data via FTP

This is a version of the program that reads the sensor and puts the data in a file, then FTPs the file over to another device.

0 REM sensor reading program
50 T = 17
51 TIMEOUTU 2
52 PRINTU "\x04"
53 UART T
0 REM see if we get a 0xfd
54 IF $0[0] <> 253 THEN 97;
55 $1 = $0
58 S = open "sensor.sns"
0 REM change binaries to string
60 $0[0] = 0
61 FOR I = 0 TO 16
62 PRINTV $1[I];
63 PRINTV " ";
64 NEXT I;
66 R = strlen $0
67 S = write R
68 REM PRINTS $0
69 GOTO 90
90 S = close
91 S = open "sensor.sns"
92 REM $0 = "0013105D4CAC"
93 $0 = "0800170FA7B7"
94 U = ftp "FromSEE.sns"
95 REM WAIT 5
96 REM GOTO 50
97 RETURN

Log System Boot

This initialization routine logs the date into a file when the system boots up.

@INIT 10
0 REM RS232_off set
11 A=pioout 5
12 A=pioset 5
0 REM RS232_on set
14 A=pioout 3
15 A=pioset 3
0 REM DTR output clear
17 A=pioout 6
18 A=pioclr 6
0 REM DSR input
20 A=pioin 4
21 REM set DSR to IRQ
22 A=pioirq "P000100"
0 REM LED output and on
24 A=pioout 2
25 A=pioset 2
0 REM alarm in 5 seconds

0 REM boot log
30 A = append "bootlog.txt"
31 A = size
32 IF A > 1000 THEN 52
33 A = date $0
0 REM add newline to the end of the string
34 PRINTV "\n"
35 A = strlen $0
36 B = write A
37 A = close
0 REM that's it
38 ALARM 5
39 RETURN

52 A = exist "bootlog.bak"
53 IF A = 0 THEN 45
54 A = delete "bootlog.bak"

55 A = rename "bootlog.bak"
56 A = close
0 REM create a new file
57 A = open "bootlog.txt"
58 GOTO 33

Break an Incoming Data Streaming Connection

This example shows how to break an incoming data streaming connection within a certain period of time.

@SLAVE 300
0 REM 5 seconds timeout for the next INPUT
300 PRINTS "type + for shell: "
301 TIMEOUTS 5
302 INPUTS $0
303 IF $0[0] = 43 THEN 312
304 PRINTS "connected for 30s\n"
305 C = date $0
306 PRINTS $0
307 PRINTS "\n"
308 ALARM 30
0 REM stop reading aio
309 C = nextsns 0
310 C = link 1
311 RETURN

312 PRINTS "shell started\n"
313 C = nextsns 10
314 RETURN

@ALARM 400
400 C = unlink 1
401 PRINTS "shell started\n"
402 C = date $0
403 PRINTS $0
404 PRINTS "\n"
405 C = nextsns 10
406 RETURN

Log Serial Data into File System

This example shows the capture capabilities to log UART serial data directly into the file system.

0 REM test program for CAPTURE
250 S = open "log.txt"
251 R = 1000
252 TIMEOUTU 60
252 CAPTURE R
255 PRINTS "done, size "
256 Q = size
257 PRINTS Q
258 PRINTS "\n"
259 S = close
260 END

FTP Test Routine

This test routine sends a file via FTP to another Bluetooth device, such as a PC, laptop or Palmpilot, discovering the other device via an inquiry process, and is named "PALM2".

@INQUIRY 100
0 TEST ftp transfer
100 $1 = $0;
0 REM substring assignment
101 $2 = $1[13];
102 C = cancel
103 C = strcmp "PALM2"
104 IF C = 0 THEN 107
105 PRINTS $2
106 RETURN
107 C = open "vCard.vcf"
108 $0 = $1;
109 C = ftp "fromair.vcf"
110 RETURN

vNote Message

When a (vNote)message is received, the BODY part of the vNote is printed as a message:

@MESSAGE 500
500 PRINTS "msg received "
501 PRINTS $0
502 RETURN
Read from the UART
Read only 10 bytes from the UART
0 REM test UART input
141 PRINTU "type 10"
142 PRINTU "\n"
143 S=10;
144 UART S
145 PRINTU "got "
146 PRINTU $0
147 PRINTU "\n"
148 END
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License